KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > ext > dbobj > DownloadFiles


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

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 JavaDoc;
75
76
77 /**
78  * This DBobject is used by the Download Controller to manage information about
79  * files available for download, security groups allowed to download which files,
80  * Download notes, and Mime Types settings.
81  *
82  * @author Michael Nash
83  * @see com.jcorporate.expresso.ext.controller.Download
84  */

85 public class DownloadFiles
86         extends SecuredDBObject {
87
88
89     public static final String JavaDoc FLD_DESCRIP = "Descrip";
90     public static final String JavaDoc FLD_DISPLAY_NAME = "DisplayName";
91     public static final String JavaDoc FLD_FILE_PATH_NAME = "FilePathName";
92 // public static final String FLD_FILE_TYPE = "FileType";
93
public static final String JavaDoc FLD_IS_ACTIVE = "IsActive";
94     public static final String JavaDoc FLD_IS_RESTRICTED = "IsRestricted";
95     public static final String JavaDoc FLD_MIME_TYPE = "MimeNumber";
96     public static final String JavaDoc FLD_GROUP_NAME = "GroupName";
97     public static final String JavaDoc FLD_PROJECT = "Project";
98     public static final String JavaDoc FLD_FILE_NUMBER = "FileNumber";
99     public static final String JavaDoc FLD_FILE_URL = "FileURL";
100     public static final String JavaDoc FLD_LAST_UPDATED = "LastUpdated";
101     public static final String JavaDoc FLD_DL_NOTES = "DLNotes";
102
103     /**
104      * Constructor
105      */

106     public DownloadFiles()
107             throws DBException {
108         super();
109     } /* DownloadFiles() */
110
111
112     /**
113      * Use over (String) constructor. Initializes the object in the context
114      * of the user who's uid belongs to the parameter.
115      *
116      * @param uid the Uid of the user context
117      * @throws DBException if there's an initialization problem
118      */

119     public DownloadFiles(int uid)
120             throws DBException {
121         super(uid);
122     }
123
124     /**
125      * For using DBObjects within Controllers. Initializes based upon the current
126      * user and the requested db. [Of course this can be modified later]
127      *
128      * @param request - The controller request handed to you by the framework.
129      * @throws DBException upon initialization error
130      */

131     public DownloadFiles(ControllerRequest request)
132             throws DBException {
133         super(request);
134     }
135
136     /**
137      * Extends the usual add method to fetch a next number field
138      *
139      * @throws DBException If the next number could not be allocated
140      * or the add fails
141      */

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     } /* add() */
151
152
153     public void update()
154             throws DBException {
155         determineMimeType();
156         setField("LastUpdated", DateTime.getDateTimeForDB(this.getDataContext()));
157         super.update();
158     }
159
160     /**
161      * Overrided to check to make sure that the entered file pathname points to
162      * a real file. If not, throws an exception.
163      *
164      * @param fieldName the name of the field to check
165      * @param fieldValue the value to check
166      * @throws DBException if there is a validation error
167      */

168     public synchronized void checkField(String JavaDoc fieldName, String JavaDoc fieldValue)
169             throws DBException {
170         super.checkField(fieldName, fieldValue);
171
172         //Check if the filePathName actually exists. Only do this if the download is active.
173
if (getField("IsActive").equals("Y")) {
174             if (fieldName.equals("FilePathName")) {
175                 File JavaDoc f = new File JavaDoc(fieldValue);
176
177                 if (!f.isFile() && !f.isHidden()) {
178                     String JavaDoc 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     /**
190      * Function that examines the file path name and automatically determines the
191      * MIME type. If it cannot figure things out, it sets it to Application/Unknown
192      *
193      * @return true if successful, false if we couldn't determine the mime type.
194      * @throws DBException if a database communication occurs, OR if we can't
195      * find the application/unknown mimetype.
196      */

197     private boolean determineMimeType() throws DBException {
198         String JavaDoc 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     /**
217      * For Mimetypes that we can't auto-detect, we set it to unknown
218      *
219      * @throws DBException upon error.
220      */

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     /**
235      * Define the table and fields for this object
236      */

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         //Commented out... the extension and filetype really take care of it
249
//addField("FileType", "varchar", 30, true, "typeOfFile");
250
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         //We allow null because it's updated from Add and Update
256
addField(FLD_DL_NOTES, "text", 0, true, "notes"); //Changed to allow notes to be null
257
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         //Descip default filter is fine.
271
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     } /* setupFields() */
281
282
283 }
284
285 /* DownloadFiles */
Popular Tags