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

org.efaps.admin.user.Company Maven / Gradle / Ivy

/*
 * 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: 8229 $
 * Last Changed:    $Date: 2012-11-22 14:25:34 -0500 (Thu, 22 Nov 2012) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.admin.user;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Collections;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

import org.efaps.db.Context;
import org.efaps.db.transaction.ConnectionResource;
import org.efaps.db.wrapper.SQLSelect;
import org.efaps.util.EFapsException;
import org.efaps.util.cache.AbstractCache;
import org.efaps.util.cache.CacheReloadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 * @version $Id: Company.java 8229 2012-11-22 19:25:34Z [email protected] $
 */
public final class Company
    extends AbstractUserObject
{
    /**
     * Needed for serialization.
     */
    private static final long serialVersionUID = 1L;

    /**
     * Logging instance used in this class.
     */
    private static final Logger LOG = LoggerFactory.getLogger(Company.class);

    /**
     * This is the sql select statement to select all roles from the database.
     */
    private static final SQLSelect SQL_SELECT = new SQLSelect()
                                                        .column("ID")
                                                        .column("UUID")
                                                        .column("NAME")
                                                        .column("STATUS")
                                                        .from("V_USERCOMPANY");

    /**
     * The company belonging to this Consortiums.
     */
    private final Set consortiums = new HashSet();


    /**
     * Stores all instances of class {@link Company}.
     *
     * @see #getCache
     */
    private static final CompanyCache CACHE = new CompanyCache();

    /**
     * @param _id       id for this company
     * @param _uuid     uuid for this company
     * @param _name     name for this company
     * @param _status    status for this company
     */
    private Company(final long _id,
                    final String _uuid,
                    final String _name,
                    final boolean _status)
    {
        super(_id, _uuid, _name, _status);
    }

    /**
     * @param _consortium Consortium to add to this Company
     */
    protected void addConsortium(final Consortium  _consortium)
    {
        this.consortiums.add(_consortium);
    }

    /**
     * Get the related Consortium (unmodifiable).
     * @return the set of related Consortiums
     */
    public Set getConsortiums()
    {
        return Collections.unmodifiableSet(this.consortiums);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean hasChildPerson(final Person _person)
    {
        return _person.isAssigned(this);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean isAssigned()
    {
        boolean ret = false;
        try {
            if (Context.getThreadContext().getCompany() != null) {
                if (Context.getThreadContext().getCompany().getId() == getId()) {
                    ret = hasChildPerson(Context.getThreadContext().getPerson());
                }
            } else {
                ret = true;
            }
        } catch (final EFapsException e) {
            Company.LOG.error("could not read Company or Person from Context ", e);
        }
        return ret;
    }

    /**
     * Method to initialize the Cache of this CacheObjectInterface.
     */
    public static void initialize()
    {
        Company.CACHE.initialize(Company.class);
    }

   /**
    * Returns for given parameter _id the instance of class {@link Company}.
    *
    * @param _id    id to search in the cache
    * @return instance of class {@link Company}
    * @throws CacheReloadException on error
    * @see #getCache
    */
    public static Company get(final long _id)
        throws CacheReloadException
    {
        return Company.CACHE.get(_id);
    }

   /**
    * Returns for given parameter _name the instance of class
    * {@link Company}.
    *
    * @param _name   name to search in the cache
    * @return instance of class {@link Company}
    * @throws CacheReloadException on error
    * @see #getCache
    */
    public static Company get(final String _name)
        throws CacheReloadException
    {
        return Company.CACHE.get(_name);
    }

    /**
     * Returns for given parameter _uuid the instance of class
     * {@link Company}.
     * @param _uuid UUI to search for
     * @return instance of class {@link Company}
     * @throws CacheReloadException on error
     */
    public static Company get(final UUID _uuid)
        throws CacheReloadException
    {
        return Company.CACHE.get(_uuid);
    }

    /**
     * Method to get the Cache for Company.
     * @return Cache
     */
    public static AbstractCache getCache()
    {
        return Company.CACHE;
    }

    /**
     * Cache for Companies.
     */
    private static final class CompanyCache
        extends AbstractCache
    {
        /**
         * {@inheritDoc}
         */
        @Override
        protected void readCache(final Map _cache4Id,
                                 final Map _cache4Name,
                                 final Map _cache4UUID)
            throws CacheReloadException
        {
            ConnectionResource con = null;
            try {
                con = Context.getThreadContext().getConnectionResource();

                Statement stmt = null;
                try {

                    stmt = con.getConnection().createStatement();

                    final ResultSet resulset = stmt.executeQuery(Company.SQL_SELECT.getSQL());
                    while (resulset.next()) {
                        final long id = resulset.getLong(1);
                        final String uuid = resulset.getString(2);
                        final String name = resulset.getString(3).trim();
                        final boolean status = resulset.getBoolean(4);

                        Company.LOG.debug("read company '" + name + "' (id = " + id + ")");
                        final Company company = new Company(id, uuid, name, status);
                        _cache4Id.put(company.getId(), company);
                        _cache4Name.put(company.getName(), company);
                        _cache4UUID.put(company.getUUID(), company);
                    }
                    resulset.close();
                } finally {
                    if (stmt != null) {
                        stmt.close();
                    }
                }
                con.commit();
            } catch (final SQLException e) {
                throw new CacheReloadException("could not read roles", e);
            } catch (final EFapsException e) {
                throw new CacheReloadException("could not read roles", e);
            } finally {
                if ((con != null) && con.isOpened()) {
                    try {
                        con.abort();
                    } catch (final EFapsException e) {
                        throw new CacheReloadException("could not read roles", e);
                    }
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy