1 64 65 package com.jcorporate.expresso.services.dbobj; 66 67 import com.jcorporate.expresso.core.dataobjects.DataField; 68 import com.jcorporate.expresso.core.dataobjects.jdbc.LobField; 69 import com.jcorporate.expresso.core.db.DBConnection; 70 import com.jcorporate.expresso.core.db.DBException; 71 import com.jcorporate.expresso.core.dbobj.RequestContext; 72 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 73 import com.jcorporate.expresso.core.misc.ConfigJdbc; 74 import com.jcorporate.expresso.core.misc.ConfigManager; 75 import com.jcorporate.expresso.core.misc.ConfigurationException; 76 import org.apache.log4j.Logger; 77 78 import java.io.File ; 79 import java.io.FileInputStream ; 80 import java.io.FileNotFoundException ; 81 import java.io.IOException ; 82 import java.io.InputStream ; 83 import java.sql.SQLException ; 84 85 116 public class MediaDBObject extends SecuredDBObject { 117 118 121 private LobField lf = null; 122 123 126 private static final Logger log = Logger.getLogger(MediaDBObject.class); 127 128 134 public static final String FLD_FILE_SUFFIX = "_fileName"; 135 136 143 public static final String FLD_MIME_SUFFIX = "_mimeType"; 144 145 152 public static final String FLD_SIZE_SUFFIX = "_fileSize"; 153 154 155 160 public MediaDBObject() throws DBException { 161 super(); 162 } 163 164 165 171 public MediaDBObject(DBConnection newConnection) 172 throws DBException { 173 super(newConnection); 174 } 175 176 193 public MediaDBObject(DBConnection newConnection, String setupTablesContext) 194 throws DBException { 195 super(newConnection, setupTablesContext); 196 } 197 198 208 public MediaDBObject(int newUid) 209 throws DBException { 210 super(newUid); 211 } 212 213 220 public MediaDBObject(RequestContext request) 221 throws DBException { 222 super(request); 223 } 224 225 231 public String getBlobFilename(String fieldName) throws DBException { 232 return getField(fieldName + FLD_FILE_SUFFIX); 233 } 234 235 241 public String getBlobMimeType(String fieldName) throws DBException { 242 return getField(fieldName + FLD_MIME_SUFFIX); 243 } 244 245 251 public String getBlobFileSize(String fieldName) throws DBException { 252 return getField(fieldName + FLD_SIZE_SUFFIX); 253 } 254 255 262 public int getBlobFileSizeInt(String fieldName) throws DBException { 263 return getFieldInt(fieldName + FLD_SIZE_SUFFIX); 264 } 265 266 287 protected void addBlobField(String fieldName, String fieldDescription) throws DBException { 288 this.addField(fieldName, "blob", 0, true, fieldDescription); 289 this.addField(fieldName + FLD_MIME_SUFFIX, "int", 0, true, fieldDescription + " Mime Type"); 290 this.addField(fieldName + FLD_FILE_SUFFIX, "varchar", 128, true, fieldDescription + " File Name"); 291 this.setLookupObject(fieldName + FLD_MIME_SUFFIX, 292 com.jcorporate.expresso.services.dbobj.MimeTypes.class.getName()); 293 294 this.addField(fieldName + FLD_SIZE_SUFFIX, "long", 0, true, 295 fieldDescription + " File Size"); 296 297 this.setReadOnly(fieldName + FLD_FILE_SUFFIX); 298 this.setReadOnly(fieldName + FLD_MIME_SUFFIX); 299 this.setReadOnly(fieldName + FLD_SIZE_SUFFIX); 300 } 301 302 321 public void saveBlob(String fieldName) throws DBException { 322 if (fieldName == null) { 323 throw new IllegalArgumentException ("Argument fieldName must not be null"); 324 } 325 326 DataField myField = this.getDataField(fieldName); 327 if (myField == null) { 328 throw new IllegalArgumentException ("Field " + fieldName + " is not defined"); 329 } 330 331 String origFileName = (String ) myField.getAttribute("origFileName"); 332 String localFileName = (String ) myField.getAttribute("fileName"); 333 334 if (origFileName == null || localFileName == null) { 335 log.error("Unable to retrieve attributes: origFileName = " + 336 origFileName + " localFileName = " + localFileName); 337 338 throw new IllegalStateException ("Unable to DataObject attribtues"); 339 } 340 341 File f = new File (localFileName); 342 if (f == null) { 343 log.error("File: " + localFileName + " doesn't appear to exist."); 344 throw new IllegalStateException ("Unable to retrieve uploaded file!"); 345 } 346 347 int fileSize = (int) f.length(); 348 InputStream is; 349 try { 350 is = new FileInputStream (f); 351 } catch (FileNotFoundException ex) { 352 log.error("File: " + localFileName + " doesn't appear to exist.", ex); 353 throw new IllegalStateException ("Unable to retrieve uploaded file!"); 354 } 355 356 this.saveBlob(fieldName, is, origFileName, fileSize); 357 358 try { 359 is.close(); 360 f.delete(); 361 } catch (IOException ex) { 362 log.error("Error closing and deleting file " + localFileName, ex); 363 } 364 } 365 366 367 375 public void saveBlob(String fieldName, java.io.InputStream value, 376 String fileName, int fileSize) throws DBException { 377 if (lf == null) { 378 lf = new LobField(); 379 } 380 381 lf.setCriteria(this); 382 try { 383 lf.saveBlob(fieldName, value, fileSize); 384 385 } finally { 386 lf.close(); 387 } 388 389 this.setField(fieldName + "_fileName", fileName); 390 391 this.setField(fieldName + "_mimeType", 392 MimeTypes.getMimeType(fileName, 393 this.getDataContext()).getField(MimeTypes.FLD_MIMENUMBER)); 394 this.setField(fieldName + "_fileSize", fileSize); 395 this.update(); 396 } 397 398 406 public void saveBlob(String fieldName, java.io.InputStream value, int fileSize) throws DBException { 407 if (lf == null) { 408 lf = new LobField(); 409 } 410 411 lf.setCriteria(this); 412 lf.saveBlob(fieldName, value, fileSize); 413 try { 414 lf.saveBlob(fieldName, value, fileSize); 415 } finally { 416 lf.close(); 417 } 418 this.setField(fieldName + "_fileName", "unknown"); 419 this.setField(fieldName + "_mimeType", "application/x-unknown"); 420 this.setField(fieldName + "_fileSize", fileSize); 421 this.update(); 422 } 423 424 435 public java.io.InputStream retrieveBlob(String fieldName) throws DBException { 436 if (lf == null) { 437 lf = new LobField(); 438 } 439 440 lf.setCriteria(this); 441 442 try { 443 ConfigJdbc jdbcConfig = ConfigManager.getContext(this.getDataContext()).getJdbc(); 444 boolean nativeBlob = jdbcConfig.isNativeBlob(); 445 if (!nativeBlob) { 446 return lf.getBlobStream(fieldName); 447 } else { 448 try { 449 java.sql.Blob blob = lf.getBlob(fieldName); 450 if (blob == null) { 451 throw new DBException("Error getting BLOB object from field " + fieldName); 452 } 453 return blob.getBinaryStream(); 454 } catch (SQLException ex) { 455 throw new DBException("Error getting binary stream from Blob object", ex); 456 } 457 } 458 } catch (ConfigurationException ex) { 459 throw new DBException("Unable to get 'nativeBlob' setting.", ex); 460 } 461 } 462 463 467 public void release() { 468 if (lf != null) { 469 lf.close(); 470 } 471 } 472 473 474 } 475 | Popular Tags |