1 14 package org.compiere.model; 15 16 import java.awt.*; 17 import java.util.*; 18 import java.sql.*; 19 import java.io.*; 20 import javax.swing.*; 21 22 import org.compiere.util.*; 23 24 31 public class MImage extends PO 32 { 33 38 public MImage(Properties ctx, int AD_Image_ID) 39 { 40 super (ctx, AD_Image_ID); 41 if (AD_Image_ID < 1) 42 setName("-/-"); 43 } 45 46 private ImageIcon m_image = null; 47 48 private File m_file = null; 49 50 55 protected POInfo initPO (Properties ctx) 56 { 57 int AD_Table_ID = 461; 58 return POInfo.getPOInfo (ctx, AD_Table_ID); 59 } 61 65 public String getName() 66 { 67 return (String )getValue("Name"); 68 } 70 74 public void setName (String newName) 75 { 76 String fileName = newName; 77 if (fileName.length() > 60) 78 fileName = "..." + fileName.substring(fileName.length()-60+3); 79 setValue("Name", fileName); 80 } 82 86 public void setImageURL (String newURL) 87 { 88 String url = newURL; 89 if (url.length() > 256) 90 url = "..." + url.substring(url.length()-256+3); 91 setValue("ImageURL", url); 92 } 94 98 public ImageIcon getImage() 99 { 100 return m_image; 101 } 103 107 public String toString() 108 { 109 return "MImage[ID=" + getID() + ",Name=" + getName() + "]"; 110 } 112 120 protected Object loadSpecial (ResultSet rs, int index) throws SQLException 121 { 122 Log.trace(Log.l4_Data, "MImage.loadSpecial", p_info.getColumnName(index)); 123 124 int length = 0; 126 try 127 { 128 Blob blob = rs.getBlob(index); 129 length = (int)blob.length(); 130 if (length > 0) 131 { 132 byte[] data = blob.getBytes(1, length); 133 m_image = new ImageIcon(data); 135 if (m_image == null) 136 Log.error("MImage.loadSpecial - Image not created - BLOB length = " + length); 137 else 138 Log.trace(Log.l6_Database, "Image loaded", "BLOB length = " + length 139 + ", h=" + m_image.getIconHeight() + ", w=" + m_image.getIconWidth()); 140 } 141 else 142 Log.trace(Log.l6_Database, "BLOB not Loaded - length=" + length); 143 } 144 catch (SQLException e) 145 { 146 Log.error("MImage.loadSpecial - BLOB length = " + length, e); 147 } 148 catch (Exception e1) 149 { 150 Log.error("MImage.loadSpecial - BLOB length = " + length, e1); 151 } 152 return m_image; 153 } 155 161 protected String saveNewSpecial (Object value, int index) 162 { 163 Log.trace(Log.l5_DData, "MImage.saveNewSpecial", p_info.getColumnName(index)); 164 return "Empty_BLOB()"; 165 } 167 171 public void setImageFile (File imageFile) 172 { 173 if (imageFile != null && (imageFile.isDirectory() || !imageFile.exists())) 174 throw new IllegalArgumentException ("MImage.setImageFile - Invalid File " + imageFile.toString()); 175 m_file = imageFile; 176 } 178 179 183 public boolean save() 184 { 185 if (m_file != null) 186 return super.save(); 187 return true; 188 } 190 197 protected boolean saveComplete (boolean newRecord, boolean success) 198 { 199 if (!success || m_file == null) 201 return success; 202 Log.trace(Log.l4_Data, "MImage.saveComplete - Save Image to BLOB", "Success=" + success); 203 204 String sql = "SELECT BinaryData FROM AD_Image WHERE AD_Image_ID=? FOR UPDATE"; 205 try 206 { 207 Connection con = DB.createConnection(false, Connection.TRANSACTION_SERIALIZABLE); 208 PreparedStatement pstmt = con.prepareStatement(sql, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE); 209 pstmt.setInt(1, getID()); 210 ResultSet rs = pstmt.executeQuery(); 211 if (rs.next()) 212 { 213 Blob blob = rs.getBlob(1); 214 long blobLength = blob.length(); 215 Log.trace(Log.l5_DData, "MImage.saveComplete blob - " 216 + m_file.getAbsolutePath() + ", Size=" + m_file.length() 217 + ", BLOB length=" + blobLength); 218 try 219 { 220 FileInputStream fis = new FileInputStream (m_file); 221 byte[] buffer = new byte[1024]; int length = -1; 223 long pos = 1l; 224 while ((length = fis.read(buffer)) != -1) 225 { 226 int written = blob.setBytes(pos, buffer); 227 pos += written; 228 } 229 fis.close(); 230 Log.trace(Log.l6_Database, "New length=" + blob.length()); 231 } 232 catch (IOException ioe) 233 { 234 Log.error("MImage.saveComplete (blob-io)", ioe); 235 success = false; 236 } 237 } 238 else 239 { 240 Log.error("MImage.saveComplete - No Record for BLOB"); 241 success = false; 242 } 243 rs.close(); 244 pstmt.close(); 245 con.close(); 246 } 247 catch (SQLException e) 248 { 249 Log.error("MImage.saveComplete", e); 250 success = false; 251 } 252 253 return success; 254 } 256 } | Popular Tags |