www.pudn.com > code_source_compiere_erp_crm_logiciel_java.zip > MImage.java


/****************************************************************************** 
 * The contents of this file are subject to the   Compiere License  Version 1.1 
 * ("License"); You may not use this file except in compliance with the License 
 * You may obtain a copy of the License at http://www.compiere.org/license.html 
 * Software distributed under the License is distributed on an  "AS IS"  basis, 
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for 
 * the specific language governing rights and limitations under the License. 
 * The Original Code is                  Compiere  ERP & CRM  Business Solution 
 * The Initial Developer of the Original Code is Jorg Janke  and ComPiere, Inc. 
 * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts 
 * created by ComPiere are Copyright (C) ComPiere, Inc.;   All Rights Reserved. 
 * Contributor(s): ______________________________________. 
 *****************************************************************************/ 
package org.compiere.model; 
 
import java.awt.*; 
import java.util.*; 
import java.sql.*; 
import java.io.*; 
import javax.swing.*; 
 
import org.compiere.util.*; 
 
/** 
 *  Image Model 
 *  (DisplayType = 32) 
 * 
 *  @author Jorg Janke 
 *  @version $Id: MImage.java,v 1.7 2002/08/14 02:26:10 jjanke Exp $ 
 */ 
public class MImage extends PO 
{ 
	/** 
	 *  Constructor 
	 *  @param ctx context 
	 *  @param AD_Image_ID image 
	 */ 
	public MImage(Properties ctx, int AD_Image_ID) 
	{ 
		super (ctx, AD_Image_ID); 
		if (AD_Image_ID < 1) 
			setName("-/-"); 
	}   //  MImage 
 
	/** The Image                   */ 
	private ImageIcon       m_image = null; 
	/** The Image File              */ 
	private File            m_file = null; 
 
	/** 
	 *  Load 
	 *  @param ctx context 
	 *  @return POInfo PO Info 
	 */ 
	protected POInfo initPO (Properties ctx) 
	{ 
		int AD_Table_ID = 461; 
		return POInfo.getPOInfo (ctx, AD_Table_ID); 
	}   //  MImage 
 
	/** 
	 *  Get Name 
	 *  @return Name 
	 */ 
	public String getName() 
	{ 
		return (String)getValue("Name"); 
	}   //  getName 
 
	/** 
	 *  Set Name 
	 *  @param newName name 
	 */ 
	public void setName (String newName) 
	{ 
		String fileName = newName; 
		if (fileName.length() > 60) 
			fileName = "..." + fileName.substring(fileName.length()-60+3); 
		setValue("Name", fileName); 
	}   //  setName 
 
	/** 
	 *  Set ImageURL 
	 *  @param newURL url 
	 */ 
	public void setImageURL (String newURL) 
	{ 
		String url = newURL; 
		if (url.length() > 256) 
			url = "..." + url.substring(url.length()-256+3); 
		setValue("ImageURL", url); 
	}   //  setImageURL 
 
	/** 
	 *  Get Image 
	 *  @return Image 
	 */ 
	public ImageIcon getImage() 
	{ 
		return m_image; 
	}   //  getImage 
 
	/** 
	 *  String Representation 
	 *  @return String 
	 */ 
	public String toString() 
	{ 
		return "MImage[ID=" + getID() + ",Name=" + getName() + "]"; 
	}   //  toString 
 
	/** 
	 *  Load Special data (images, ..). 
	 *  To be extended by sub-classes 
	 *  @param rs result set 
	 *  @param index zero based index 
	 *  @return value 
	 *  @throws SQLException 
	 */ 
	protected Object loadSpecial (ResultSet rs, int index) throws SQLException 
	{ 
		Log.trace(Log.l4_Data, "MImage.loadSpecial", p_info.getColumnName(index)); 
 
		//  BLOB 
		int length = 0; 
		try 
		{ 
			Blob blob = rs.getBlob(index); 
			length = (int)blob.length(); 
			if (length > 0) 
			{ 
				byte[] data = blob.getBytes(1, length); 
			//	Image image = Toolkit.getDefaultToolkit().createImage(data); 
				m_image = new ImageIcon(data); 
				if (m_image == null) 
					Log.error("MImage.loadSpecial - Image not created - BLOB length = " + length); 
				else 
					Log.trace(Log.l6_Database, "Image loaded", "BLOB length = " + length 
						+ ", h=" + m_image.getIconHeight() + ", w=" + m_image.getIconWidth()); 
			} 
			else 
				Log.trace(Log.l6_Database, "BLOB not Loaded - length=" + length); 
		} 
		catch (SQLException e) 
		{ 
			Log.error("MImage.loadSpecial - BLOB length = " + length, e); 
		} 
		catch (Exception e1) 
		{ 
			Log.error("MImage.loadSpecial - BLOB length = " + length, e1); 
		} 
		return m_image; 
	}   //  loadSpecial 
 
	/** 
	 *  Save Special Data 
	 *  @param value value 
	 *  @param index index 
	 *  @return SQL code for INSERT VALUES clause 
	 */ 
	protected String saveNewSpecial (Object value, int index) 
	{ 
		Log.trace(Log.l5_DData, "MImage.saveNewSpecial", p_info.getColumnName(index)); 
		return "Empty_BLOB()"; 
	}   //  saveNewSpecial 
 
	/** 
	 *  Set Image file to load; Set before saving 
	 *  @param imageFile file 
	 */ 
	public void setImageFile (File imageFile) 
	{ 
		if (imageFile != null && (imageFile.isDirectory() || !imageFile.exists())) 
			throw new IllegalArgumentException ("MImage.setImageFile - Invalid File " + imageFile.toString()); 
		m_file = imageFile; 
	}   //  setImageFile 
 
 
	/** 
	 *  Save only when a imageFile is set 
	 *  @return true if saved 
	 */ 
	public boolean save() 
	{ 
		if (m_file != null) 
			return super.save(); 
		return true; 
	}   //  save 
 
	/** 
	 *  Save new or update complete. 
	 *  Save Image from file. 
	 *  @param newRecord new record 
	 *  @param success success 
	 *  @return true if saved 
	 */ 
	protected boolean saveComplete (boolean newRecord, boolean success) 
	{ 
		//  do nothing if previous command failed or no image file there 
		if (!success || m_file == null) 
			return success; 
		Log.trace(Log.l4_Data, "MImage.saveComplete - Save Image to BLOB", "Success=" + success); 
 
		String sql = "SELECT BinaryData FROM AD_Image WHERE AD_Image_ID=? FOR UPDATE"; 
		try 
		{ 
			Connection con = DB.createConnection(false, Connection.TRANSACTION_SERIALIZABLE); 
			PreparedStatement pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 
			pstmt.setInt(1, getID()); 
			ResultSet rs = pstmt.executeQuery(); 
			if (rs.next()) 
			{ 
				Blob blob = rs.getBlob(1); 
				long blobLength = blob.length(); 
				Log.trace(Log.l5_DData, "MImage.saveComplete blob - " 
					+ m_file.getAbsolutePath() + ", Size=" + m_file.length() 
					+ ", BLOB length=" + blobLength); 
				try 
				{ 
					FileInputStream fis = new FileInputStream (m_file); 
					byte[] buffer = new byte[1024];         //  1k Buffer 
					int length = -1; 
					long pos = 1l; 
					while ((length = fis.read(buffer)) != -1) 
					{ 
						int written = blob.setBytes(pos, buffer); 
						pos += written; 
					} 
					fis.close(); 
					Log.trace(Log.l6_Database, "New length=" + blob.length()); 
				} 
				catch (IOException ioe) 
				{ 
					Log.error("MImage.saveComplete (blob-io)", ioe); 
					success = false; 
				} 
			} 
			else 
			{ 
				Log.error("MImage.saveComplete - No Record for BLOB"); 
				success = false; 
			} 
			rs.close(); 
			pstmt.close(); 
			con.close(); 
		} 
		catch (SQLException e) 
		{ 
			Log.error("MImage.saveComplete", e); 
			success = false; 
		} 
 
		return success; 
	}   //  saveComplete 
 
}   //  MImage