PageRenderTime 38ms CodeModel.GetById 23ms app.highlight 12ms RepoModel.GetById 1ms app.codeStats 0ms

/projects/compiere-330/base/src/org/compiere/model/MMediaServer.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus
Java | 310 lines | 213 code | 23 blank | 74 comment | 18 complexity | 25db7e77cd36e1f8b49cf17d585b666d MD5 | raw file
  1/******************************************************************************
  2 * Product: Compiere ERP & CRM Smart Business Solution                        *
  3 * Copyright (C) 1999-2007 ComPiere, Inc. All Rights Reserved.                *
  4 * This program is free software, you can redistribute it and/or modify it    *
  5 * under the terms version 2 of the GNU General Public License as published   *
  6 * by the Free Software Foundation. This program is distributed in the hope   *
  7 * that it will be useful, but WITHOUT ANY WARRANTY, without even the implied *
  8 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
  9 * See the GNU General Public License for more details.                       *
 10 * You should have received a copy of the GNU General Public License along    *
 11 * with this program, if not, write to the Free Software Foundation, Inc.,    *
 12 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
 13 * For the text or an alternative of this public license, you may reach us    *
 14 * ComPiere, Inc., 3600 Bridge Parkway #102, Redwood City, CA 94065, USA      *
 15 * or via info@compiere.org or http://www.compiere.org/license.html           *
 16 *****************************************************************************/
 17package org.compiere.model;
 18
 19import java.io.*;
 20import java.sql.*;
 21import java.util.*;
 22import java.util.logging.*;
 23
 24import org.apache.commons.net.ftp.*;
 25import org.compiere.util.*;
 26
 27/**
 28 * 	Media Server Model
 29 *	
 30 *  @author Yves Sandfort
 31 */
 32public class MMediaServer extends X_CM_Media_Server
 33{
 34	/**
 35	 * 
 36	 */
 37	private static final long serialVersionUID = 1L;
 38
 39
 40	/**
 41	 * 	Get Media Server
 42	 *	@param project
 43	 *	@return server list
 44	 */
 45	public static MMediaServer[] getMediaServer (MWebProject project)
 46	{
 47		ArrayList<MMediaServer> list = new ArrayList<MMediaServer>();
 48		PreparedStatement pstmt = null;
 49		String sql = "SELECT * FROM CM_Media_Server WHERE CM_WebProject_ID=? ORDER BY CM_Media_Server_ID";
 50		try
 51		{
 52			pstmt = DB.prepareStatement (sql, project.get_Trx());
 53			pstmt.setInt (1, project.getCM_WebProject_ID());
 54			ResultSet rs = pstmt.executeQuery ();
 55			while (rs.next ())
 56			{
 57				list.add (new MMediaServer (project.getCtx(), rs, project.get_Trx()));
 58			}
 59			rs.close ();
 60			pstmt.close ();
 61			pstmt = null;
 62		}
 63		catch (Exception e)
 64		{
 65			s_log.log (Level.SEVERE, sql, e);
 66		}
 67		try
 68		{
 69			if (pstmt != null)
 70				pstmt.close ();
 71			pstmt = null;
 72		}
 73		catch (Exception e)
 74		{
 75			pstmt = null;
 76		}
 77		MMediaServer[] retValue = new MMediaServer[list.size ()];
 78		list.toArray (retValue);
 79		return retValue;
 80	}	//	getMediaServer
 81	
 82	/**	Logger	*/
 83	private static CLogger s_log = CLogger.getCLogger (MMediaServer.class);
 84	
 85	
 86	/**************************************************************************
 87	 * 	Standard Constructor
 88	 *	@param ctx context
 89	 *	@param CM_Media_Server_ID id
 90	 *	@param trx transaction
 91	 */
 92	public MMediaServer (Ctx ctx, int CM_Media_Server_ID, Trx trx)
 93	{
 94		super (ctx, CM_Media_Server_ID, trx);
 95	}	//	MMediaServer
 96
 97	/**
 98	 * 	Load Constructor
 99	 *	@param ctx context
100	 *	@param rs request 
101	 *	@param trx transaction
102	 */
103	public MMediaServer (Ctx ctx, ResultSet rs, Trx trx)
104	{
105		super (ctx, rs, trx);
106	}	//	MMediaServer
107	
108	/**  MWebProject WebProject (parent) */
109	public MWebProject m_project = null;
110	
111	/**
112	 * 	getWebProject
113	 *	@return Webproject (Parent)
114	 */
115	public MWebProject getWebProject()
116	{
117		if (m_project==null) 
118			m_project = new MWebProject(getCtx(), getCM_WebProject_ID (), get_Trx ());
119		return m_project;
120	}
121	
122	/**
123	 * 	reDeployAll set all media items to redeploy
124	 */
125	public void reDeployAll () 
126	{
127		MMedia[] media = MMedia.getMedia (getWebProject());
128		if (media!=null && media.length>0)
129		{
130			for (MMedia element : media) {
131				MMediaDeploy thisDeploy = MMediaDeploy.getByMedia (getCtx(), element.get_ID (), get_ID(), true, null);
132				if (thisDeploy.isDeployed ())
133				{
134					log.log (Level.FINE, "Reset Deployed Flag on MediaItem" + element.get_ID());
135					thisDeploy.setIsDeployed (false);
136					thisDeploy.save ();
137				}
138			}
139		}
140	}
141	
142	/**
143	 * 	(Re-)Deploy all media
144	 * 	@return true if deployed
145	 */
146	public boolean deploy ()
147	{
148		MMedia[] media = MMedia.getMediaToDeploy(getCtx(), this.get_ID (), get_Trx());
149
150		// Check whether the host is our example localhost, we will not deploy locally, but this is no error
151		if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
152			log.warning("You have not defined your own server, we will not really deploy to localhost!");
153			return true;
154		}
155
156		FTPClient ftp = new FTPClient();
157		try
158		{
159			ftp.connect (getIP_Address());
160			if (ftp.login (getUserName(), getPassword()))
161				log.info("Connected to " + getIP_Address() + " as " + getUserName());
162			else
163			{
164				log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
165				return false;
166			}
167		}
168		catch (Exception e)
169		{
170			log.log(Level.WARNING, "Could NOT connect to " + getIP_Address() 
171				+ " as " + getUserName(), e);
172			return false;
173		}
174
175		boolean success = true;
176		String cmd = null;
177		//	List the files in the directory
178		try
179		{
180			cmd = "cwd";
181			ftp.changeWorkingDirectory (getFolder());
182			//
183			cmd = "list";
184			String[] fileNames = ftp.listNames();
185			log.log(Level.FINE, "Number of files in " + getFolder() + ": " + fileNames.length);
186			
187			/*
188			FTPFile[] files = ftp.listFiles();
189			log.config("Number of files in " + getFolder() + ": " + files.length);
190			for (int i = 0; i < files.length; i++)
191				log.fine(files[i].getTimestamp() + " \t" + files[i].getName());*/
192			//
193			cmd = "bin";
194			ftp.setFileType(FTP.BINARY_FILE_TYPE);
195			//
196			for (int i = 0; i < media.length; i++)
197			{
198				MMediaDeploy thisDeployment = MMediaDeploy.getByMedia (getCtx(), media[i].get_ID (), this.get_ID (), false, get_Trx());
199				if (!media[i].isSummary() && media[i].getMediaType ()!=null) {
200					log.log(Level.INFO, " Deploying Media Item: " + media[i].toString());
201					MImage thisImage = media[i].getImage();
202		
203					// Open the file and output streams
204					byte[] buffer = thisImage.getData();
205					ByteArrayInputStream is = new ByteArrayInputStream(buffer);
206
207					String fileName = media[i].get_ID() + media[i].getExtension();
208					cmd = "put " + fileName;
209					ftp.storeFile(fileName, is);
210					is.close();
211					thisDeployment.setIsDeployed (true);
212					thisDeployment.save ();
213				}
214			}
215		}
216		catch (Exception e)
217		{
218			log.log(Level.WARNING, cmd, e);
219			success = false;
220		}
221		//	Logout from the FTP Server and disconnect
222		try
223		{
224			cmd = "logout";
225			ftp.logout();
226			log.log(Level.FINE, " FTP logged out");
227			cmd = "disconnect";
228			ftp.disconnect();
229		}
230		catch (Exception e)
231		{
232			log.log(Level.WARNING, cmd, e);
233		}
234		ftp = null;
235		return success;
236	}	//	deploy
237	
238	/**
239	 * 	(Re-)Deploy all media
240	 * 	@return true if deployed
241	 */
242	public boolean deleteMediaItem (MMedia t_media)
243	{
244		// Check whether the host is our example localhost, we will not deploy locally, but this is no error
245		if (this.getIP_Address().equals("127.0.0.1") || this.getName().equals("localhost")) {
246			log.warning("You have not defined your own server, we will not really deploy to localhost!");
247			return true;
248		}
249
250		FTPClient ftp = new FTPClient();
251		try
252		{
253			ftp.connect (getIP_Address());
254			if (ftp.login (getUserName(), getPassword()))
255				log.info("Connected to " + getIP_Address() + " as " + getUserName());
256			else
257			{
258				log.warning("Could NOT connect to " + getIP_Address() + " as " + getUserName());
259				return false;
260			}
261		}
262		catch (Exception e)
263		{
264			log.log(Level.WARNING, "Could NOT connect to " + getIP_Address() 
265				+ " as " + getUserName(), e);
266			return false;
267		}
268
269		boolean success = true;
270		String cmd = null;
271		//	List the files in the directory
272		try
273		{
274			cmd = "cwd";
275			ftp.changeWorkingDirectory (getFolder());
276			//
277			if (!t_media.isSummary()) {
278				log.log(Level.INFO, " Deleting Media Item:" + t_media.get_ID() + t_media.getExtension());
279				ftp.dele (t_media.get_ID() + t_media.getExtension());
280			}
281		}
282		catch (Exception e)
283		{
284			log.log(Level.WARNING, cmd, e);
285			success = false;
286		}
287		//	Logout from the FTP Server and disconnect
288		try
289		{
290			cmd = "logout";
291			ftp.logout();
292			cmd = "disconnect";
293			ftp.disconnect();
294		}
295		catch (Exception e)
296		{
297			log.log(Level.WARNING, cmd, e);
298		}
299		ftp = null;
300		return success;
301	}	//	deploy
302	
303	@Override
304	public String toString() 
305	{
306		StringBuffer sb = new StringBuffer ("MMediaServer[")
307		.append (get_ID()).append ("-").append (getName()).append ("]");
308		return sb.toString ();
309	}
310}	//	MMediaServer