All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.efaps.db.Checkin Maven / Gradle / Ivy

Go to download

eFaps is a framework used to map objects with or without attached files to a relational database and optional file systems (only for attaches files). Configurable access control can be provided down to object and attribute level depending on implementation and use case. Depending on requirements, events (like triggers) allow to implement business logic and to separate business logic from user interface. The framework includes integrations (e.g. webdav, full text search) and a web application as 'simple' configurable user interface. Some best practises, example web application modules (e.g. team work module) support administrators and implementers using this framework.

There is a newer version: 3.2.0
Show newest version
/*
 * Copyright 2003 - 2011 The eFaps Team
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Author:          tmo
 * Revision:        $Rev: 6210 $
 * Last Changed:    $Date: 2011-02-18 16:02:56 -0500 (Fri, 18 Feb 2011) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.db;
import java.io.File;
import java.io.InputStream;

import org.efaps.admin.access.AccessTypeEnums;
import org.efaps.admin.datamodel.Type;
import org.efaps.admin.event.EventType;
import org.efaps.db.store.Resource;
import org.efaps.util.EFapsException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static org.efaps.db.store.Store.PROPERTY_ATTR_FILE_LENGTH;
import static org.efaps.db.store.Store.PROPERTY_ATTR_FILE_NAME;

/**
 * The class is used to check in a file to a given attribute of an object.
 *
 * @author The eFaps Team
 * @version $Id: Checkin.java 6210 2011-02-18 21:02:56Z [email protected] $
 */
public class Checkin
    extends AbstractAction
{
    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(Checkin.class);

    /**
     * Constructor with a string as object id.
     *
     * @param _oid      oid of object on which the check in is made
     */
    public Checkin(final String _oid)
    {
        this(Instance.get(_oid));
    }

    /**
     * Constructor with an instance object as object id.
     *
     * @param _instance     instance on which the check in is made
     */
    public Checkin(final Instance _instance)
    {
        super.setInstance(_instance);
    }

    /**
     * @param _fileName     file name to check in (could include also the path)
     * @param _in           input stream with the binary data
     * @param _size         size of file in stream to check in (negative size
     *                      means that all from the stream must be written)
     * @see #executeWithoutAccessCheck(String, InputStream, int)
     *@throws EFapsException on error
     */
    public void execute(final String _fileName,
                        final InputStream _in,
                        final int _size)
        throws EFapsException
    {
        final boolean hasAccess = super.getInstance().getType().hasAccess(super.getInstance(),
                                                                          AccessTypeEnums.CHECKIN.getAccessType());
        if (!hasAccess) {
            throw new EFapsException(getClass(), "execute.NoAccess");
        }
        executeWithoutAccessCheck(_fileName, _in, _size);
    }

    /**
     * Executes the check in without checking the access rights (but with
     * triggers).
     * 
    *
  1. executes the pre check in trigger (if exists)
  2. *
  3. executes the check in trigger (if exists)
  4. *
  5. executes if no check in trigger exists or the check in trigger is * not executed the update ({@see #executeWithoutTrigger})
  6. *
  7. executes the post check in trigger (if exists)
  8. *
* * @param _fileName file name to check in (could include also the path) * @param _in input stream with the binary data * @param _size size of file in stream to check in (negative size * means that all from the stream must be written) * @throws EFapsException if checkout action fails * TODO: history entries */ public void executeWithoutAccessCheck(final String _fileName, final InputStream _in, final int _size) throws EFapsException { executeEvents(EventType.CHECKIN_PRE); if (!executeEvents(EventType.CHECKIN_OVERRIDE)) { executeWithoutTrigger(_fileName, _in, _size); } executeEvents(EventType.CHECKIN_POST); } /** * The check in is done without calling triggers and check of access * rights. Executes the check in: *
    *
  • the file is checked in
  • *
  • the file name and file length is stored in with * {@link org.efaps.db.Update} (complete filename without path)
  • *
* * @param _fileName file name to check in (could include also the path) * @param _in input stream with the binary data * @param _size size of file in stream to check in (negative size means * that all from the stream must be written) * @throws EFapsException if checkout action fails */ public void executeWithoutTrigger(final String _fileName, final InputStream _in, final int _size) throws EFapsException { final Context context = Context.getThreadContext(); Resource storeRsrc = null; boolean ok = false; try { final Type type = getInstance().getType(); final String attrFileName = type.getProperty(PROPERTY_ATTR_FILE_NAME); final String attrFileLength = type.getProperty(PROPERTY_ATTR_FILE_LENGTH); storeRsrc = context.getStoreResource(getInstance()); final int size = storeRsrc.write(_in, _size); storeRsrc.commit(); storeRsrc = null; final File file = new File(_fileName); String fileName = file.getName(); // remove the path from the filename final int lastSeperatorPosX = fileName.lastIndexOf("/"); final int lastSeperatorPosWin = fileName.lastIndexOf("\\"); final int lastSeperatorPosMac = fileName.lastIndexOf(":"); int lastSeperatorPos = lastSeperatorPosX; if (lastSeperatorPos < lastSeperatorPosWin) { lastSeperatorPos = lastSeperatorPosWin; } if (lastSeperatorPos < lastSeperatorPosMac) { lastSeperatorPos = lastSeperatorPosMac; } if (lastSeperatorPos > -1 && lastSeperatorPos < fileName.length() - 1) { fileName = fileName.substring(lastSeperatorPos + 1); } // set file name and length in the eFaps object final Update update = new Update(super.getInstance()); update.add(attrFileName, fileName); update.add(attrFileLength, size); update.executeWithoutAccessCheck(); ok = true; } catch (final EFapsException e) { Checkin.LOG.error("could not checkin " + super.getInstance(), e); throw e; } catch (final Throwable e) { Checkin.LOG.error("could not checkin " + super.getInstance(), e); throw new EFapsException(Checkin.class, "executeWithoutAccessCheck.Throwable", e); } finally { if (!ok) { context.abort(); } if ((storeRsrc != null) && (storeRsrc.isOpened())) { storeRsrc.abort(); } } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy