001: /* ====================================================================
002: * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
003: *
004: * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
005: *
006: * Redistribution and use in source and binary forms, with or without
007: * modification, are permitted provided that the following conditions
008: * are met:
009: *
010: * 1. Redistributions of source code must retain the above copyright
011: * notice, this list of conditions and the following disclaimer.
012: *
013: * 2. Redistributions in binary form must reproduce the above copyright
014: * notice, this list of conditions and the following disclaimer in
015: * the documentation and/or other materials provided with the
016: * distribution.
017: *
018: * 3. The end-user documentation included with the redistribution,
019: * if any, must include the following acknowledgment:
020: * "This product includes software developed by Jcorporate Ltd.
021: * (http://www.jcorporate.com/)."
022: * Alternately, this acknowledgment may appear in the software itself,
023: * if and wherever such third-party acknowledgments normally appear.
024: *
025: * 4. "Jcorporate" and product names such as "Expresso" must
026: * not be used to endorse or promote products derived from this
027: * software without prior written permission. For written permission,
028: * please contact info@jcorporate.com.
029: *
030: * 5. Products derived from this software may not be called "Expresso",
031: * or other Jcorporate product names; nor may "Expresso" or other
032: * Jcorporate product names appear in their name, without prior
033: * written permission of Jcorporate Ltd.
034: *
035: * 6. No product derived from this software may compete in the same
036: * market space, i.e. framework, without prior written permission
037: * of Jcorporate Ltd. For written permission, please contact
038: * partners@jcorporate.com.
039: *
040: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
041: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
042: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
043: * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
044: * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
045: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
046: * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
047: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
048: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
049: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
050: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
051: * SUCH DAMAGE.
052: * ====================================================================
053: *
054: * This software consists of voluntary contributions made by many
055: * individuals on behalf of the Jcorporate Ltd. Contributions back
056: * to the project(s) are encouraged when you make modifications.
057: * Please send them to support@jcorporate.com. For more information
058: * on Jcorporate Ltd. and its products, please see
059: * <http://www.jcorporate.com/>.
060: *
061: * Portions of this software are based upon other open source
062: * products and are subject to their respective licenses.
063: */
064:
065: package com.jcorporate.expresso.ext.dbobj;
066:
067: import com.jcorporate.expresso.core.controller.ControllerRequest;
068: import com.jcorporate.expresso.core.db.DBException;
069: import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
070: import com.jcorporate.expresso.core.misc.DateTime;
071: import com.jcorporate.expresso.core.misc.FileUtil;
072: import com.jcorporate.expresso.services.dbobj.MimeTypes;
073:
074: import java.io.File;
075:
076: /**
077: * This DBobject is used by the Download Controller to manage information about
078: * files available for download, security groups allowed to download which files,
079: * Download notes, and Mime Types settings.
080: *
081: * @author Michael Nash
082: * @see com.jcorporate.expresso.ext.controller.Download
083: */
084: public class DownloadFiles extends SecuredDBObject {
085:
086: public static final String FLD_DESCRIP = "Descrip";
087: public static final String FLD_DISPLAY_NAME = "DisplayName";
088: public static final String FLD_FILE_PATH_NAME = "FilePathName";
089: // public static final String FLD_FILE_TYPE = "FileType";
090: public static final String FLD_IS_ACTIVE = "IsActive";
091: public static final String FLD_IS_RESTRICTED = "IsRestricted";
092: public static final String FLD_MIME_TYPE = "MimeNumber";
093: public static final String FLD_GROUP_NAME = "GroupName";
094: public static final String FLD_PROJECT = "Project";
095: public static final String FLD_FILE_NUMBER = "FileNumber";
096: public static final String FLD_FILE_URL = "FileURL";
097: public static final String FLD_LAST_UPDATED = "LastUpdated";
098: public static final String FLD_DL_NOTES = "DLNotes";
099:
100: /**
101: * Constructor
102: */
103: public DownloadFiles() throws DBException {
104: super ();
105: } /* DownloadFiles() */
106:
107: /**
108: * Use over (String) constructor. Initializes the object in the context
109: * of the user who's uid belongs to the parameter.
110: *
111: * @param uid the Uid of the user context
112: * @throws DBException if there's an initialization problem
113: */
114: public DownloadFiles(int uid) throws DBException {
115: super (uid);
116: }
117:
118: /**
119: * For using DBObjects within Controllers. Initializes based upon the current
120: * user and the requested db. [Of course this can be modified later]
121: *
122: * @param request - The controller request handed to you by the framework.
123: * @throws DBException upon initialization error
124: */
125: public DownloadFiles(ControllerRequest request) throws DBException {
126: super (request);
127: }
128:
129: /**
130: * Extends the usual add method to fetch a next number field
131: *
132: * @throws DBException If the next number could not be allocated
133: * or the add fails
134: */
135: public void add() throws DBException {
136: setField(FLD_LAST_UPDATED, DateTime.getDateTimeForDB(this
137: .getDataContext()));
138: determineMimeType();
139: if (this .getDataField("IsRestricted").isNull()) {
140: this .setField("IsRestricted", false);
141: }
142: super .add();
143: } /* add() */
144:
145: public void update() throws DBException {
146: determineMimeType();
147: setField("LastUpdated", DateTime.getDateTimeForDB(this
148: .getDataContext()));
149: super .update();
150: }
151:
152: /**
153: * Overrided to check to make sure that the entered file pathname points to
154: * a real file. If not, throws an exception.
155: *
156: * @param fieldName the name of the field to check
157: * @param fieldValue the value to check
158: * @throws DBException if there is a validation error
159: */
160: public synchronized void checkField(String fieldName,
161: String fieldValue) throws DBException {
162: super .checkField(fieldName, fieldValue);
163:
164: //Check if the filePathName actually exists. Only do this if the download is active.
165: if (getField("IsActive").equals("Y")) {
166: if (fieldName.equals("FilePathName")) {
167: File f = new File(fieldValue);
168:
169: if (!f.isFile() && !f.isHidden()) {
170: String msg = DownloadFiles.class + ".checkField("
171: + fieldName + "," + fieldValue
172: + ") File Does not exist";
173: this .addFieldError(fieldName, msg);
174: throw new DBException(msg);
175: }
176: }
177: }
178: }
179:
180: /**
181: * Function that examines the file path name and automatically determines the
182: * MIME type. If it cannot figure things out, it sets it to Application/Unknown
183: *
184: * @return true if successful, false if we couldn't determine the mime type.
185: * @throws DBException if a database communication occurs, OR if we can't
186: * find the application/unknown mimetype.
187: */
188: private boolean determineMimeType() throws DBException {
189: String baseName = FileUtil.getBase(this
190: .getField(FLD_FILE_PATH_NAME))
191: + "."
192: + FileUtil.getExtension(this
193: .getField(FLD_FILE_PATH_NAME));
194: if (baseName == null || baseName.length() == 0) {
195: setMimeAsUnknown();
196: return false;
197: }
198:
199: MimeTypes mt = MimeTypes.getMimeType(baseName, this
200: .getDataContext());
201: if (mt == null) {
202: setMimeAsUnknown();
203: return false;
204: }
205: this .set(FLD_MIME_TYPE, mt.getDataField(
206: MimeTypes.FLD_MIMENUMBER).asString());
207:
208: return true;
209: }
210:
211: /**
212: * For Mimetypes that we can't auto-detect, we set it to unknown
213: *
214: * @throws DBException upon error.
215: */
216: private void setMimeAsUnknown() throws DBException {
217: MimeTypes mt = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT);
218: mt.set(MimeTypes.FLD_MIMETYPE, "application/unknown");
219: if (!mt.find()) {
220: throw new DBException(
221: "Unable to determine mime type of: "
222: + this .getField(FLD_FILE_PATH_NAME)
223: + "and cannot find the 'application/unknown' MIME type");
224: }
225:
226: this .set(FLD_MIME_TYPE, mt.getDataField(
227: MimeTypes.FLD_MIMENUMBER).asString());
228: }
229:
230: /**
231: * Define the table and fields for this object
232: */
233: public void setupFields() throws DBException {
234: setTargetTable("DOWNLOADFILES");
235: setDescription("DBdownloadFiles");
236: setCharset("ISO-8859-1");
237: addField(FLD_FILE_NUMBER, "auto-inc", 0, false, "fileNumber");
238: addField(FLD_FILE_PATH_NAME, "text", 0, false, "filePath");
239: addField(FLD_DISPLAY_NAME, "varchar", 128, true, "displayName");
240: addField(FLD_FILE_URL, "text", 0, true, "urlForRedirect");
241: addField(FLD_DESCRIP, "varchar", 80, false, "descriptionOfFile");
242:
243: //Commented out... the extension and filetype really take care of it
244: //addField("FileType", "varchar", 30, true, "typeOfFile");
245: addField(FLD_PROJECT, "char", 10, true, "project");
246: addField(FLD_GROUP_NAME, "char", 10, false, "groupAllowedToDL");
247: addField(FLD_LAST_UPDATED, "datetime", 0, true, "lastUpdated");
248: setReadOnly(FLD_LAST_UPDATED);
249:
250: //We allow null because it's updated from Add and Update
251: addField(FLD_DL_NOTES, "text", 0, true, "notes"); //Changed to allow notes to be null
252: addField(FLD_MIME_TYPE, "int", 0, false, "contentType");
253: setReadOnly(FLD_MIME_TYPE);
254: setMultiValued(FLD_MIME_TYPE);
255: setLookupObject(FLD_MIME_TYPE,
256: com.jcorporate.expresso.services.dbobj.MimeTypes.class
257: .getName());
258:
259: addField(FLD_IS_ACTIVE, "char", 1, false, "downloadActive");
260: setDefaultValue(FLD_IS_ACTIVE, "Y");
261:
262: addField(FLD_IS_RESTRICTED, "boolean", 0, false,
263: "Restricted Download?");
264: setStringFilter(FLD_FILE_PATH_NAME, "stripFilter");
265:
266: //Descip default filter is fine.
267: setStringFilter(FLD_PROJECT, "stripFilter");
268: setStringFilter(FLD_GROUP_NAME, "stripFilter");
269: setMultiValued(FLD_GROUP_NAME);
270: setLookupObject(FLD_GROUP_NAME,
271: com.jcorporate.expresso.services.dbobj.UserGroup.class
272: .getName());
273: addKey(FLD_FILE_NUMBER);
274: setReadOnly(FLD_FILE_NUMBER);
275: this .getMetaData().setAttribute(FLD_IS_ACTIVE, "checkbox", "Y");
276: this .getMetaData().setAttribute(FLD_IS_RESTRICTED, "checkbox",
277: "Y");
278: } /* setupFields() */
279:
280: }
281:
282: /* DownloadFiles */
|