1 64 65 package com.jcorporate.expresso.ext.dbobj; 66 67 import com.jcorporate.expresso.core.controller.ControllerRequest; 68 import com.jcorporate.expresso.core.db.DBException; 69 import com.jcorporate.expresso.core.dbobj.SecuredDBObject; 70 import com.jcorporate.expresso.core.misc.DateTime; 71 import com.jcorporate.expresso.core.misc.FileUtil; 72 import com.jcorporate.expresso.services.dbobj.MimeTypes; 73 74 import java.io.File ; 75 76 77 85 public class DownloadFiles 86 extends SecuredDBObject { 87 88 89 public static final String FLD_DESCRIP = "Descrip"; 90 public static final String FLD_DISPLAY_NAME = "DisplayName"; 91 public static final String FLD_FILE_PATH_NAME = "FilePathName"; 92 public static final String FLD_IS_ACTIVE = "IsActive"; 94 public static final String FLD_IS_RESTRICTED = "IsRestricted"; 95 public static final String FLD_MIME_TYPE = "MimeNumber"; 96 public static final String FLD_GROUP_NAME = "GroupName"; 97 public static final String FLD_PROJECT = "Project"; 98 public static final String FLD_FILE_NUMBER = "FileNumber"; 99 public static final String FLD_FILE_URL = "FileURL"; 100 public static final String FLD_LAST_UPDATED = "LastUpdated"; 101 public static final String FLD_DL_NOTES = "DLNotes"; 102 103 106 public DownloadFiles() 107 throws DBException { 108 super(); 109 } 110 111 112 119 public DownloadFiles(int uid) 120 throws DBException { 121 super(uid); 122 } 123 124 131 public DownloadFiles(ControllerRequest request) 132 throws DBException { 133 super(request); 134 } 135 136 142 public void add() 143 throws DBException { 144 setField(FLD_LAST_UPDATED, DateTime.getDateTimeForDB(this.getDataContext())); 145 determineMimeType(); 146 if (this.getDataField("IsRestricted").isNull()) { 147 this.setField("IsRestricted", false); 148 } 149 super.add(); 150 } 151 152 153 public void update() 154 throws DBException { 155 determineMimeType(); 156 setField("LastUpdated", DateTime.getDateTimeForDB(this.getDataContext())); 157 super.update(); 158 } 159 160 168 public synchronized void checkField(String fieldName, String fieldValue) 169 throws DBException { 170 super.checkField(fieldName, fieldValue); 171 172 if (getField("IsActive").equals("Y")) { 174 if (fieldName.equals("FilePathName")) { 175 File f = new File (fieldValue); 176 177 if (!f.isFile() && !f.isHidden()) { 178 String msg = DownloadFiles.class + 179 ".checkField(" + fieldName + "," + 180 fieldValue + 181 ") File Does not exist"; 182 this.addFieldError(fieldName, msg); 183 throw new DBException(msg); 184 } 185 } 186 } 187 } 188 189 197 private boolean determineMimeType() throws DBException { 198 String baseName = FileUtil.getBase(this.getField(FLD_FILE_PATH_NAME)) + "." 199 + FileUtil.getExtension(this.getField(FLD_FILE_PATH_NAME)); 200 if (baseName == null || baseName.length() == 0) { 201 setMimeAsUnknown(); 202 return false; 203 } 204 205 MimeTypes mt = MimeTypes.getMimeType(baseName, this.getDataContext()); 206 if (mt == null) { 207 setMimeAsUnknown(); 208 return false; 209 } 210 this.set(FLD_MIME_TYPE, 211 mt.getDataField(MimeTypes.FLD_MIMENUMBER).asString()); 212 213 return true; 214 } 215 216 221 private void setMimeAsUnknown() throws DBException { 222 MimeTypes mt = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT); 223 mt.set(MimeTypes.FLD_MIMETYPE, "application/unknown"); 224 if (!mt.find()) { 225 throw new DBException("Unable to determine mime type of: " + 226 this.getField(FLD_FILE_PATH_NAME) + 227 "and cannot find the 'application/unknown' MIME type"); 228 } 229 230 this.set(FLD_MIME_TYPE, 231 mt.getDataField(MimeTypes.FLD_MIMENUMBER).asString()); 232 } 233 234 237 public void setupFields() 238 throws DBException { 239 setTargetTable("DOWNLOADFILES"); 240 setDescription("DBdownloadFiles"); 241 setCharset("ISO-8859-1"); 242 addField(FLD_FILE_NUMBER, "auto-inc", 0, false, "fileNumber"); 243 addField(FLD_FILE_PATH_NAME, "text", 0, false, "filePath"); 244 addField(FLD_DISPLAY_NAME, "varchar", 128, true, "displayName"); 245 addField(FLD_FILE_URL, "text", 0, true, "urlForRedirect"); 246 addField(FLD_DESCRIP, "varchar", 80, false, "descriptionOfFile"); 247 248 addField(FLD_PROJECT, "char", 10, true, "project"); 251 addField(FLD_GROUP_NAME, "char", 10, false, "groupAllowedToDL"); 252 addField(FLD_LAST_UPDATED, "datetime", 0, true, "lastUpdated"); 253 setReadOnly(FLD_LAST_UPDATED); 254 255 addField(FLD_DL_NOTES, "text", 0, true, "notes"); addField(FLD_MIME_TYPE, "int", 0, false, "contentType"); 258 setReadOnly(FLD_MIME_TYPE); 259 setMultiValued(FLD_MIME_TYPE); 260 setLookupObject(FLD_MIME_TYPE, 261 com.jcorporate.expresso.services.dbobj.MimeTypes 262 .class.getName()); 263 264 addField(FLD_IS_ACTIVE, "char", 1, false, "downloadActive"); 265 setDefaultValue(FLD_IS_ACTIVE, "Y"); 266 267 addField(FLD_IS_RESTRICTED, "boolean", 0, false, "Restricted Download?"); 268 setStringFilter(FLD_FILE_PATH_NAME, "stripFilter"); 269 270 setStringFilter(FLD_PROJECT, "stripFilter"); 272 setStringFilter(FLD_GROUP_NAME, "stripFilter"); 273 setMultiValued(FLD_GROUP_NAME); 274 setLookupObject(FLD_GROUP_NAME, 275 com.jcorporate.expresso.services.dbobj.UserGroup.class.getName()); 276 addKey(FLD_FILE_NUMBER); 277 setReadOnly(FLD_FILE_NUMBER); 278 this.getMetaData().setAttribute(FLD_IS_ACTIVE, "checkbox", "Y"); 279 this.getMetaData().setAttribute(FLD_IS_RESTRICTED, "checkbox", "Y"); 280 } 281 282 283 } 284 285 | Popular Tags |