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

org.efaps.admin.ui.AbstractMenu 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 - 2012 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.
 *
 * Revision:        $Rev: 7483 $
 * Last Changed:    $Date: 2012-05-11 11:57:38 -0500 (Fri, 11 May 2012) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.admin.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.builder.ToStringBuilder;
import org.efaps.ci.CIAdminUserInterface;
import org.efaps.db.Instance;
import org.efaps.db.MultiPrintQuery;
import org.efaps.db.QueryBuilder;
import org.efaps.jaas.AppAccessHandler;
import org.efaps.util.EFapsException;
import org.efaps.util.cache.CacheReloadException;

/**
 * @author The eFaps Team
 * @version $Id: AbstractMenu.java 7483 2012-05-11 16:57:38Z [email protected] $
 */
public abstract class AbstractMenu
    extends AbstractCommand
{

    /**
     * All sub commands or menus are store in the tree map. The tree map is used
     * to sort the commands / menus belonging to their id.
     *
     * @see #getCommands
     * @add
     */
    private final Map commands = new TreeMap();

    /**
     * Constructor to set the id,uuid and name of the menu object.
     *
     * @param _id id of the command to set
     * @param _uuid UUID of the command to set
     * @param _name name of the command to set
     */
    protected AbstractMenu(final long _id,
                           final String _uuid,
                           final String _name)
    {
        super(_id, _uuid, _name);
    }

    /**
     * Adds a command or menu to this menu instance. The method must be specific
     * implemented by all menu implementations.
     *
     * @param _sortId id used to sort
     * @param _id id of the sub command / menu to add
     */
    protected abstract void add(final long _sortId,
                                final long _id);

    /**
     * Add a command to the menu structure.
     *
     * @param _sortId id used to sort
     * @param _command command to add
     */
    public void add(final long _sortId,
                    final AbstractCommand _command)
    {
        this.commands.put(_sortId, _command);
    }

    /**
     * Add all sub commands and menus of the given menu to this menu structure.
     *
     * @param _menu menu with sub structure
     */
    public void addAll(final AbstractMenu _menu)
    {
        this.commands.putAll(_menu.commands);
    }

    /**
     * Check, if the user of the context has access to this user interface
     * object. First, the instance method checks, if some access configuration
     * exists for this menu instance object. If the user has access for this
     * menu, it is test, if the context user has access to minimum one sub
     * command command / menu. If yes, the user is allowed to access this menu
     * instance, other the user is not allowed to access this menu.
     *
     * @param _targetMode TargetMode of the Command
     * @param _instance the field will represent, e.g. on edit mode
     * @return trueif context user has access, otherwise false is
     *         returned
     * @throws EFapsException on error
     */
    @Override
    public boolean hasAccess(final TargetMode _targetMode,
                             final Instance _instance)
        throws EFapsException
    {
        boolean ret = super.hasAccess(_targetMode, _instance);

        if (ret && getCommands().size() > 0 && !AppAccessHandler.excludeMode()) {
            ret = false;
            for (final AbstractCommand cmd : getCommands()) {
                if (cmd.hasAccess(_targetMode, _instance)) {
                    ret = true;
                    break;
                }
            }
        }
        return ret;
    }

    /**
     * Returns all information from the menu as string.
     *
     * @return String representation of this AbstractMenu
     */
    @Override
    public String toString()
    {
        final ToStringBuilder buf = new ToStringBuilder(this).appendSuper(super.toString());

        for (final AbstractCommand cmd : getCommands()) {
            buf.append(" ").append(cmd);
        }
        return buf.toString();
    }

    /**
     * The method takes values of the {@link #commands} and returnes them as
     * {@link java.util.ArrayList}.
     *
     * @return the values of the {@link #commands} map instance as array list
     * @see #commands
     * @see #add(Command)
     * @see #add(Menu)
     */
    public List getCommands()
    {
        return new ArrayList(this.commands.values());
    }

    /**
     * The instance method reads all needed information for this user interface
     * object. The method extends the original method, because the sub menus and
     * commands must be read.
     *
     * @see #readFromDB4Childs
     * @throws CacheReloadException on error during load
     */
    @Override
    protected void readFromDB()
        throws CacheReloadException
    {
        super.readFromDB();
        readFromDB4Childs();
    }

    /**
     * The instance method gets all sub menus and commands and adds them to this
     * menu instance via method {@link #add(long)}.
     *
     * @see #readFromDB
     * @see #add(long)
     * @throws CacheReloadException on error during load
     */
    private void readFromDB4Childs()
        throws CacheReloadException
    {
        try {
            final QueryBuilder queryBldr = new QueryBuilder(CIAdminUserInterface.Menu2Command);
            queryBldr.addWhereAttrEqValue(CIAdminUserInterface.Menu2Command.FromMenu, getId());
            final MultiPrintQuery multi = queryBldr.getPrint();
            multi.addAttribute(CIAdminUserInterface.Menu2Command.ToCommand);
            multi.executeWithoutAccessCheck();

            while (multi.next()) {
                final long commandId = multi. getAttribute(CIAdminUserInterface.Menu2Command.ToCommand);
                add(multi.getCurrentInstance().getId(), commandId);
            }
        } catch (final EFapsException e) {
            throw new CacheReloadException("could not read childs for menu '" + getName() + "'", e);
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy