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

org.efaps.admin.access.AccessSet Maven / Gradle / Ivy

/*
 * Copyright 2003 - 2013 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: 8823 $
 * Last Changed:    $Date: 2013-02-18 14:09:52 -0500 (Mon, 18 Feb 2013) $
 * Last Changed By: $Author: [email protected] $
 */

package org.efaps.admin.access;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

import org.efaps.admin.AbstractAdminObject;
import org.efaps.admin.datamodel.Status;
import org.efaps.admin.datamodel.Type;
import org.efaps.db.Context;
import org.efaps.db.transaction.ConnectionResource;
import org.efaps.db.wrapper.SQLPart;
import org.efaps.db.wrapper.SQLSelect;
import org.efaps.util.EFapsException;
import org.efaps.util.cache.CacheLogListener;
import org.efaps.util.cache.CacheReloadException;
import org.efaps.util.cache.InfinispanCache;
import org.infinispan.Cache;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author The eFaps Team
 * @version $Id: AccessSet.java 8823 2013-02-18 19:09:52Z [email protected] $
 */
public final class AccessSet
    extends AbstractAdminObject
{

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

    /**
     * This is the SQL select statement to select a role from the database by
     * ID.
     */
    private static final String SQL_ID = new SQLSelect()
                    .column("ID")
                    .column("UUID")
                    .column("NAME")
                    .from("T_ACCESSSET", 0)
                    .addPart(SQLPart.WHERE).addColumnPart(0, "ID").addPart(SQLPart.EQUAL).addValuePart("?").toString();

    /**
     * This is the SQL select statement to select a role from the database by
     * Name.
     */
    private static final String SQL_NAME = new SQLSelect()
                    .column("ID")
                    .column("UUID")
                    .column("NAME")
                    .from("T_ACCESSSET", 0)
                    .addPart(SQLPart.WHERE).addColumnPart(0, "NAME").addPart(SQLPart.EQUAL).addValuePart("?")
                    .toString();

    /**
     * This is the SQL select statement to select a role from the database by
     * UUID.
     */
    private static final String SQL_UUID = new SQLSelect()
                    .column("ID")
                    .column("UUID")
                    .column("NAME")
                    .from("T_ACCESSSET", 0)
                    .addPart(SQLPart.WHERE).addColumnPart(0, "UUID").addPart(SQLPart.EQUAL).addValuePart("?")
                    .toString();

    /**
     * Name of the Cache by UUID.
     */
    private static final String UUIDCACHE = "AccessSet4UUID";

    /**
     * Name of the Cache by ID.
     */
    private static final String IDCACHE = "AccessSet4ID";

    /**
     * Name of the Cache by Name.
     */
    private static final String NAMECACHE = "AccessSet4Name";

    /**
     * This is the sql select statement to select the links from all access sets
     * to all access types in the database.
     *
     * @see #init4ReadLinks2AccessTypes
     */
    private static final String SQL_SET2TYPE = new SQLSelect()
                    .column("ACCESSTYPE")
                    .from("T_ACCESSSET2TYPE", 0)
                    .addPart(SQLPart.WHERE).addColumnPart(0, "ACCESSSET").addPart(SQLPart.EQUAL).addValuePart("?")
                    .toString();
    /**
     * This is the sql select statement to select the links from all access sets
     * to all data model types in the database.
     *
     * @see #init4ReadLinks2DMTypes
     */
    private static final String SQL_SET2DMTYPE = new SQLSelect()
                    .column("DMTYPE")
                    .from("T_ACCESSSET2DMTYPE", 0)
                    .addPart(SQLPart.WHERE).addColumnPart(0, "ACCESSSET").addPart(SQLPart.EQUAL).addValuePart("?")
                    .toString();

    /**
     * This is the sql select statement to select the links from all access sets
     * to all stati in the database.
     *
     * @see #init4ReadLinks2DMTypes
     */
    private static final String SQL_SET2STATUS = new SQLSelect()
                    .column("ACCESSSTATUS")
                    .from("T_ACCESSSET2STATUS", 0)
                    .addPart(SQLPart.WHERE).addColumnPart(0, "ACCESSSET").addPart(SQLPart.EQUAL).addValuePart("?")
                    .toString();

    /**
     * All related access types of this access set are referenced in this
     * instance variable.
     *
     * @see #getAccessTypes
     */
    private final Set accessTypes = new HashSet();

    /**
     * All related data models types of this access set are referenced in this
     * instance variable.
     *
     * @see #getDataModelTypes
     */
    private final Set dataModelTypes = new HashSet();

    /**
     * All related Status of this access set are referenced in this instance
     * variable.
     */
    private final Set stati = new HashSet();

    /**
     * This is the constructor.
     *
     * @param _id id of this access type
     * @param _uuid universal unique identifier of this access type
     * @param _name name of this access type
     */
    private AccessSet(final long _id,
                      final String _uuid,
                      final String _name)
    {
        super(_id, _uuid, _name);
    }

    /**
     * This is the getter method for instance variable {@link #accessTypes}.
     *
     * @return the value of the instance variable {@link #accessTypes}.
     * @see #accessTypes
     */
    public Set getAccessTypes()
    {
        return this.accessTypes;
    }

    /**
     * This is the getter method for instance variable {@link #dataModelTypes}.
     *
     * @return the value of the instance variable {@link #dataModelTypes}.
     * @see #dataModelTypes
     */
    public Set getDataModelTypes()
    {
        return this.dataModelTypes;
    }

    /**
     * Getter method for instance variable {@link #stati}.
     *
     * @return value of instance variable {@link #stati}
     */
    public Set getStati()
    {
        return this.stati;
    }

    /**
     * Read the related {@link org.efaps.admin.access.AccessTypes}.
     * @throws CacheReloadException on error
     */
    private void readLinks2AccessTypes()
        throws CacheReloadException
    {
        ConnectionResource con = null;
        try {
            final List values = new ArrayList();
            con = Context.getThreadContext().getConnectionResource();
            PreparedStatement stmt = null;
            try {
                stmt = con.getConnection().prepareStatement(AccessSet.SQL_SET2TYPE);
                stmt.setObject(1, getId());
                final ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    values.add(rs.getLong(1));
                }
                rs.close();
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
            con.commit();
            for (final Long accessTypeId : values) {
                final AccessType accessType = AccessType.getAccessType(accessTypeId);
                if (accessType == null) {
                    AccessSet.LOG.error("could not found access type with id " + "'" + accessTypeId + "'");
                } else {
                    AccessSet.LOG.debug(
                            "read link from AccessSet '{}' (id = {}, uuid = {}) to AccessType '{}' (id = {} uuid = {})",
                                    getName(), getId(), getUUID(), accessType.getName(), accessType.getId(),
                                    accessType.getUUID());
                    getAccessTypes().add(accessType);
                }
            }
        } 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);
                }
            }
        }
    }

    /**
     * Read the related {@link org.efaps.admin.datamodel.Type}.
     * @throws CacheReloadException on error
     */
    private void readLinks2DMTypes()
        throws CacheReloadException
    {
        ConnectionResource con = null;
        try {
            final List values = new ArrayList();
            con = Context.getThreadContext().getConnectionResource();
            PreparedStatement stmt = null;
            try {
                stmt = con.getConnection().prepareStatement(AccessSet.SQL_SET2DMTYPE);
                stmt.setObject(1, getId());
                final ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    values.add(rs.getLong(1));
                }
                rs.close();
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
            con.commit();
            for (final Long dataModelTypeId : values) {
                final Type dataModelType = Type.get(dataModelTypeId);
                if (dataModelType == null) {
                    AccessSet.LOG.error("could not found data model type with id " + "'" + dataModelTypeId + "'");
                } else {
                    AccessSet.LOG.debug(
                         "read link from AccessSet '{}' (id = {}, uuid = {}) to DataModelType '{}' (id = {} uuid = {})",
                                    getName(), getId(), getUUID(), dataModelType.getName(), dataModelType.getId(),
                                    dataModelType.getUUID());
                    getDataModelTypes().add(dataModelType);
                    dataModelType.addAccessSet(this);
                }
            }
        } 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);
                }
            }
        }
    }

    /**
     * Read the related {@link org.efaps.admin.datamodel.Status}.
     * @throws CacheReloadException on error
     */
    private void readLinks2Status()
        throws CacheReloadException
    {
        ConnectionResource con = null;
        try {
            final List values = new ArrayList();
            con = Context.getThreadContext().getConnectionResource();
            PreparedStatement stmt = null;
            try {
                stmt = con.getConnection().prepareStatement(AccessSet.SQL_SET2STATUS);
                stmt.setObject(1, getId());
                final ResultSet rs = stmt.executeQuery();
                while (rs.next()) {
                    values.add(rs.getLong(1));
                }
                rs.close();
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
            con.commit();
            for (final Long statusId : values) {
                final Status status = Status.get(statusId);
                if (status == null) {
                    AccessSet.LOG.error("could not found status with id " + "'" + statusId + "'");
                } else {
                    AccessSet.LOG.debug(
                                "read link from AccessSet '{}' (id = {}, uuid = {}) to status '{}' (id = {})",
                                    getName(), getId(), getUUID(), status.getKey(), status.getId());
                    getStati().add(status);
                }
            }
        } 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);
                }
            }
        }
    }

    /**
     * Method to initialize the Cache of this CacheObjectInterface.
     */
    public static void initialize()
    {
        if (InfinispanCache.get().exists(AccessSet.UUIDCACHE)) {
            InfinispanCache.get().getCache(AccessSet.UUIDCACHE).clear();
        } else {
            InfinispanCache.get().getCache(AccessSet.UUIDCACHE)
                            .addListener(new CacheLogListener(AccessSet.LOG));
        }
        if (InfinispanCache.get().exists(AccessSet.IDCACHE)) {
            InfinispanCache.get().getCache(AccessSet.IDCACHE).clear();
        } else {
            InfinispanCache.get().getCache(AccessSet.IDCACHE)
                            .addListener(new CacheLogListener(AccessSet.LOG));
        }
        if (InfinispanCache.get().exists(AccessSet.NAMECACHE)) {
            InfinispanCache.get().getCache(AccessSet.NAMECACHE).clear();
        } else {
            InfinispanCache.get().getCache(AccessSet.NAMECACHE)
                            .addListener(new CacheLogListener(AccessSet.LOG));
        }
    }

    /**
     * Returns for given identifier in _id the cached instance of class
     * AccessSet.
     *
     * @param _id id the AccessSet is wanted for
     * @return instance of class AccessSet
     * @throws CacheReloadException on error
     */
    public static AccessSet get(final long _id)
        throws CacheReloadException
    {
        final Cache cache = InfinispanCache.get().getCache(AccessSet.IDCACHE);
        if (!cache.containsKey(_id)) {
            AccessSet.getAccessSetFromDB(AccessSet.SQL_ID, _id);
        }
        return cache.get(_id);
    }

    /**
     * Returns for given name in _name the cached instance of class
     * AccessSet.
     *
     * @param _name name the AccessSet is wanted for
     * @return instance of class AccessSet
     * @throws CacheReloadException on error
     */
    public static AccessSet get(final String _name)
        throws CacheReloadException
    {
        final Cache cache = InfinispanCache.get().getCache(AccessSet.NAMECACHE);
        if (!cache.containsKey(_name)) {
            AccessSet.getAccessSetFromDB(AccessSet.SQL_NAME, _name);
        }
        return cache.get(_name);
    }

    /**
     * Returns for given universal unique identifier in _uuid the cached
     * instance of class AccessSet.
     *
     * @param _uuid UUID the AccessSet is wanted for
     * @return instance of class AccessSet
     * @throws CacheReloadException on error
     */
    public static AccessSet get(final UUID _uuid)
        throws CacheReloadException
    {
        final Cache cache = InfinispanCache.get().getCache(AccessSet.UUIDCACHE);
        if (!cache.containsKey(_uuid)) {
            AccessSet.getAccessSetFromDB(AccessSet.SQL_UUID, String.valueOf(_uuid));
        }
        return cache.get(_uuid);
    }

    /**
     * @param _role Role to be cached
     */
    private static void cacheAccessSet(final AccessSet _role)
    {
        final Cache cache4UUID = InfinispanCache.get().getCache(AccessSet.UUIDCACHE);
        if (!cache4UUID.containsKey(_role.getUUID())) {
            cache4UUID.put(_role.getUUID(), _role);
        }

        final Cache nameCache = InfinispanCache.get().getCache(
                        AccessSet.NAMECACHE);
        if (!nameCache.containsKey(_role.getName())) {
            nameCache.put(_role.getName(), _role);
        }
        final Cache idCache = InfinispanCache.get().getCache(AccessSet.IDCACHE);
        if (!idCache.containsKey(_role.getId())) {
            idCache.put(_role.getId(), _role);
        }
    }

    /**
     * Read the AccessSet from the DataBase.
     * @param _sql  SQL Statement to be executed
     * @param _criteria filter criteria
     * @return true if founr
     * @throws CacheReloadException on error
     */
    private static boolean getAccessSetFromDB(final String _sql,
                                              final Object _criteria)
        throws CacheReloadException
    {
        boolean ret = false;
        ConnectionResource con = null;
        try {
            AccessSet accessSet = null;
            con = Context.getThreadContext().getConnectionResource();
            PreparedStatement stmt = null;
            try {
                stmt = con.getConnection().prepareStatement(_sql);
                stmt.setObject(1, _criteria);
                final ResultSet rs = stmt.executeQuery();

                if (rs.next()) {
                    final long id = rs.getLong(1);
                    final String uuid = rs.getString(2);
                    final String name = rs.getString(3);
                    AccessSet.LOG.debug("read AccessSet '{}' (id = {}, uuid ={})", name, id, uuid);
                    accessSet = new AccessSet(id, uuid, name);
                    AccessSet.cacheAccessSet(accessSet);
                }
                ret = true;
                rs.close();
            } finally {
                if (stmt != null) {
                    stmt.close();
                }
            }
            con.commit();
            if (accessSet != null) {
                accessSet.readLinks2AccessTypes();
                accessSet.readLinks2DMTypes();
                accessSet.readLinks2Status();
            }
        } 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);
                }
            }
        }
        return ret;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy