DownloadFiles.java :  » J2EE » Expresso » com » jcorporate » expresso » ext » dbobj » Java Open Source

Java Open Source » J2EE » Expresso 
Expresso » com » jcorporate » expresso » ext » dbobj » DownloadFiles.java
/* ====================================================================
 * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
 *
 * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. The end-user documentation included with the redistribution,
 *    if any, must include the following acknowledgment:
 *       "This product includes software developed by Jcorporate Ltd.
 *        (http://www.jcorporate.com/)."
 *    Alternately, this acknowledgment may appear in the software itself,
 *    if and wherever such third-party acknowledgments normally appear.
 *
 * 4. "Jcorporate" and product names such as "Expresso" must
 *    not be used to endorse or promote products derived from this
 *    software without prior written permission. For written permission,
 *    please contact info@jcorporate.com.
 *
 * 5. Products derived from this software may not be called "Expresso",
 *    or other Jcorporate product names; nor may "Expresso" or other
 *    Jcorporate product names appear in their name, without prior
 *    written permission of Jcorporate Ltd.
 *
 * 6. No product derived from this software may compete in the same
 *    market space, i.e. framework, without prior written permission
 *    of Jcorporate Ltd. For written permission, please contact
 *    partners@jcorporate.com.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED.  IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 * ====================================================================
 *
 * This software consists of voluntary contributions made by many
 * individuals on behalf of the Jcorporate Ltd. Contributions back
 * to the project(s) are encouraged when you make modifications.
 * Please send them to support@jcorporate.com. For more information
 * on Jcorporate Ltd. and its products, please see
 * <http://www.jcorporate.com/>.
 *
 * Portions of this software are based upon other open source
 * products and are subject to their respective licenses.
 */

package com.jcorporate.expresso.ext.dbobj;

import com.jcorporate.expresso.core.controller.ControllerRequest;
import com.jcorporate.expresso.core.db.DBException;
import com.jcorporate.expresso.core.dbobj.SecuredDBObject;
import com.jcorporate.expresso.core.misc.DateTime;
import com.jcorporate.expresso.core.misc.FileUtil;
import com.jcorporate.expresso.services.dbobj.MimeTypes;

import java.io.File;


/**
 * This DBobject is used by the Download Controller to manage information about
 * files available for download, security groups allowed to download which files,
 * Download notes, and Mime Types settings.
 *
 * @author Michael Nash
 * @see com.jcorporate.expresso.ext.controller.Download
 */
public class DownloadFiles
        extends SecuredDBObject {


    public static final String FLD_DESCRIP = "Descrip";
    public static final String FLD_DISPLAY_NAME = "DisplayName";
    public static final String FLD_FILE_PATH_NAME = "FilePathName";
//    public static final String FLD_FILE_TYPE = "FileType";
    public static final String FLD_IS_ACTIVE = "IsActive";
    public static final String FLD_IS_RESTRICTED = "IsRestricted";
    public static final String FLD_MIME_TYPE = "MimeNumber";
    public static final String FLD_GROUP_NAME = "GroupName";
    public static final String FLD_PROJECT = "Project";
    public static final String FLD_FILE_NUMBER = "FileNumber";
    public static final String FLD_FILE_URL = "FileURL";
    public static final String FLD_LAST_UPDATED = "LastUpdated";
    public static final String FLD_DL_NOTES = "DLNotes";

    /**
     * Constructor
     */
    public DownloadFiles()
            throws DBException {
        super();
    } /* DownloadFiles() */


    /**
     * Use over (String) constructor.  Initializes the object in the context
     * of the user who's uid belongs to the parameter.
     *
     * @param uid the Uid of the user context
     * @throws DBException if there's an initialization problem
     */
    public DownloadFiles(int uid)
            throws DBException {
        super(uid);
    }

    /**
     * For using DBObjects within Controllers.  Initializes based upon the current
     * user and the requested db. [Of course this can be modified later]
     *
     * @param request - The controller request handed to you by the framework.
     * @throws DBException upon initialization error
     */
    public DownloadFiles(ControllerRequest request)
            throws DBException {
        super(request);
    }

    /**
     * Extends the usual add method to fetch a next number field
     *
     * @throws DBException If the next number could not be allocated
     *                     or the add fails
     */
    public void add()
            throws DBException {
        setField(FLD_LAST_UPDATED, DateTime.getDateTimeForDB(this.getDataContext()));
        determineMimeType();
        if (this.getDataField("IsRestricted").isNull()) {
            this.setField("IsRestricted", false);
        }
        super.add();
    } /* add() */


    public void update()
            throws DBException {
        determineMimeType();
        setField("LastUpdated", DateTime.getDateTimeForDB(this.getDataContext()));
        super.update();
    }

    /**
     * Overrided to check to make sure that the entered file pathname points to
     * a real file.  If not, throws an exception.
     *
     * @param fieldName  the name of the field to check
     * @param fieldValue the value to check
     * @throws DBException if there is a validation error
     */
    public synchronized void checkField(String fieldName, String fieldValue)
            throws DBException {
        super.checkField(fieldName, fieldValue);

        //Check if the filePathName actually exists. Only do this if the download is active.
        if (getField("IsActive").equals("Y")) {
            if (fieldName.equals("FilePathName")) {
                File f = new File(fieldValue);

                if (!f.isFile() && !f.isHidden()) {
                    String msg = DownloadFiles.class +
                            ".checkField(" + fieldName + "," +
                            fieldValue +
                            ") File Does not exist";
                    this.addFieldError(fieldName, msg);
                    throw new DBException(msg);
                }
            }
        }
    }

    /**
     * Function that examines the file path name and automatically determines the
     * MIME type.  If it cannot figure things out, it sets it to Application/Unknown
     *
     * @return true if successful, false if we couldn't determine the mime type.
     * @throws DBException if a database communication occurs, OR if we can't
     *                     find the application/unknown mimetype.
     */
    private boolean determineMimeType() throws DBException {
        String baseName = FileUtil.getBase(this.getField(FLD_FILE_PATH_NAME)) + "."
                + FileUtil.getExtension(this.getField(FLD_FILE_PATH_NAME));
        if (baseName == null || baseName.length() == 0) {
            setMimeAsUnknown();
            return false;
        }

        MimeTypes mt = MimeTypes.getMimeType(baseName, this.getDataContext());
        if (mt == null) {
            setMimeAsUnknown();
            return false;
        }
        this.set(FLD_MIME_TYPE,
                mt.getDataField(MimeTypes.FLD_MIMENUMBER).asString());

        return true;
    }

    /**
     * For Mimetypes that we can't auto-detect, we set it to unknown
     *
     * @throws DBException upon error.
     */
    private void setMimeAsUnknown() throws DBException {
        MimeTypes mt = new MimeTypes(SecuredDBObject.SYSTEM_ACCOUNT);
        mt.set(MimeTypes.FLD_MIMETYPE, "application/unknown");
        if (!mt.find()) {
            throw new DBException("Unable to determine mime type of: " +
                    this.getField(FLD_FILE_PATH_NAME) +
                    "and cannot find the 'application/unknown' MIME type");
        }

        this.set(FLD_MIME_TYPE,
                mt.getDataField(MimeTypes.FLD_MIMENUMBER).asString());
    }

    /**
     * Define the table and fields for this object
     */
    public void setupFields()
            throws DBException {
        setTargetTable("DOWNLOADFILES");
        setDescription("DBdownloadFiles");
        setCharset("ISO-8859-1");
        addField(FLD_FILE_NUMBER, "auto-inc", 0, false, "fileNumber");
        addField(FLD_FILE_PATH_NAME, "text", 0, false, "filePath");
        addField(FLD_DISPLAY_NAME, "varchar", 128, true, "displayName");
        addField(FLD_FILE_URL, "text", 0, true, "urlForRedirect");
        addField(FLD_DESCRIP, "varchar", 80, false, "descriptionOfFile");

        //Commented out... the extension and filetype really take care of it
        //addField("FileType", "varchar", 30, true, "typeOfFile");
        addField(FLD_PROJECT, "char", 10, true, "project");
        addField(FLD_GROUP_NAME, "char", 10, false, "groupAllowedToDL");
        addField(FLD_LAST_UPDATED, "datetime", 0, true, "lastUpdated");
        setReadOnly(FLD_LAST_UPDATED);

        //We allow null because it's updated from Add and Update
        addField(FLD_DL_NOTES, "text", 0, true, "notes"); //Changed to allow notes to be null
        addField(FLD_MIME_TYPE, "int", 0, false, "contentType");
        setReadOnly(FLD_MIME_TYPE);
        setMultiValued(FLD_MIME_TYPE);
        setLookupObject(FLD_MIME_TYPE,
                com.jcorporate.expresso.services.dbobj.MimeTypes
                .class.getName());

        addField(FLD_IS_ACTIVE, "char", 1, false, "downloadActive");
        setDefaultValue(FLD_IS_ACTIVE, "Y");

        addField(FLD_IS_RESTRICTED, "boolean", 0, false, "Restricted Download?");
        setStringFilter(FLD_FILE_PATH_NAME, "stripFilter");

        //Descip default filter is fine.
        setStringFilter(FLD_PROJECT, "stripFilter");
        setStringFilter(FLD_GROUP_NAME, "stripFilter");
        setMultiValued(FLD_GROUP_NAME);
        setLookupObject(FLD_GROUP_NAME,
                com.jcorporate.expresso.services.dbobj.UserGroup.class.getName());
        addKey(FLD_FILE_NUMBER);
        setReadOnly(FLD_FILE_NUMBER);
        this.getMetaData().setAttribute(FLD_IS_ACTIVE, "checkbox", "Y");
        this.getMetaData().setAttribute(FLD_IS_RESTRICTED, "checkbox", "Y");
    } /* setupFields() */


}

/* DownloadFiles */
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.