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

org.efaps.db.MultiPrintQuery 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.
 *
 * 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.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.efaps.admin.access.AccessTypeEnums;
import org.efaps.admin.datamodel.Type;
import org.efaps.db.print.OneSelect;
import org.efaps.util.EFapsException;

/**
 *
 *
 * @author The eFaps Team
 * @version $Id: MultiPrintQuery.java 6210 2011-02-18 21:02:56Z [email protected] $
 */
public class MultiPrintQuery extends AbstractPrintQuery
{

    /**
     * Instances this PrintQuery is based on.
     */
    private final List instances;

    /**
     * Main type of this Query.
     */
    private final Type mainType;

    /**
     * Iterator used to iterate over the given instances.
     */
    private Iterator iterator;

    /**
     * Current instance.
     */
    private Instance current;

    /**
     * @param _instances instance to be updated.
     * @throws EFapsException on error
     */
    public MultiPrintQuery(final List _instances)
        throws EFapsException
    {
        this.instances = _instances;
        if (this.instances.size() > 0) {
            final Set types = new HashSet();
            for (final Instance instance : _instances) {
                if (!types.contains(instance.getType())) {
                    types.add(instance.getType());
                }
            }
            // if only one type is given the main type is this type
            // if more than one type is given they must have the same parent
            // type, if not it will not work. The next common parent is used.
            if (types.size() == 1) {
                this.mainType = _instances.get(0).getType();
            } else {
                final List> typeLists = new ArrayList>();
                for (final Type type : types) {
                    final List parents = new ArrayList();
                    Type currentType = type;
                    parents.add(currentType);
                    while (currentType.getParentType() != null) {
                        currentType = currentType.getParentType();
                        parents.add(currentType);
                    }
                    typeLists.add(parents);
                }

                Type tempType = null;
                final List compList = typeLists.get(0);
                typeLists.remove(0);
                for (final Type comp : compList) {
                    boolean found = true;
                    for (final List typeList : typeLists) {
                        if (!typeList.contains(comp)) {
                            found = false;
                            break;
                        }
                    }
                    if (found) {
                        tempType = comp;
                        break;
                    }
                }
                this.mainType = tempType;
            }
        } else {
            this.mainType = null;
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Type getMainType()
    {
        return this.mainType;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Instance getCurrentInstance()
    {
        return this.current;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public List getInstanceList()
    {
        return this.instances;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean execute() throws EFapsException
    {
        final Map> types = new HashMap>();
        for (final Instance instance : this.instances) {
            List list;
            if (!types.containsKey(instance.getType())) {
                list = new ArrayList();
                types.put(instance.getType(), list);
            } else {
                list = types.get(instance.getType());
            }
            list.add(instance);
        }
        //check the access for the given instances
        final Map accessmap = new HashMap();
        for (final Entry> entry : types.entrySet()) {
            accessmap.putAll(entry.getKey().checkAccess(entry.getValue(), AccessTypeEnums.SHOW.getAccessType()));
        }

        final Iterator tempIter = this.instances.iterator();
        while (tempIter.hasNext()) {
            final Instance instance = tempIter.next();
            if (accessmap.size() > 0) {
                if (!accessmap.containsKey(instance) || !accessmap.get(instance)) {
                    tempIter.remove();
                }
            }
        }
        return executeWithoutAccessCheck();
    }

    /**
     * Method to move the iterator to the next value.
     * @return true if the iterator was moved successfully to the next value
     */
    public boolean next()
    {
        boolean ret = false;
        if (this.iterator == null) {
            this.iterator = this.instances.iterator();
        }

        if (this.iterator.hasNext()) {
            this.current = this.iterator.next();
            ret = true;
        }
        if (ret) {
            for (final OneSelect oneSelect : getAllSelects()) {
                ret = oneSelect.next();
                if (!ret) {
                    break;
                }
            }
        }
        return ret;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy