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

org.efaps.admin.common.NumberGenerator 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.common;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Formatter;
import java.util.Locale;
import java.util.Map;
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.CacheObjectInterface;
import org.efaps.util.cache.CacheReloadException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * TODO comment!
 *
 * @author The eFaps Team
 * @version $Id: NumberGenerator.java 7483 2012-05-11 16:57:38Z [email protected] $
 */
public final class NumberGenerator
    implements CacheObjectInterface
{

    /**
     * Select statement that will be executed against the database
     * on reading the cache.
     */
    private static SQLSelect SQL_SELECT = new SQLSelect()
                                                .column(0, "ID")
                                                .column(1, "NAME")
                                                .column(1, "UUID")
                                                .column(0, "FORMAT")
                                                .from("T_CMNUMGEN", 0)
                                                .leftJoin("T_CMABSTRACT", 1, "ID", 0, "ID");

    /**
     * Stores all instances of type.
     *
     * @see #get
     */
    private static NumberGeneratorCache CACHE = new NumberGeneratorCache();

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

    /**
     * Prefix for the name used in the database.
     */
    private static final String NAME_PREFIX = "numgen_";

    /**
     * Suffix for the name used in the database.
     */
    private static final String NAME_SUFFIX = "_seq";

    /**
     * Id of this NumberGenerator.
     */
    private final long id;

    /**
     * Name of this NumberGenerator.
     */
    private final String name;

    /**
     * UUID of this NumberGenerator.
     */
    private final UUID uuid;

    /**
     * Format for  this NumberGenerator.
     */
    private final String format;

    /**
     * @param _id       Id of this NumberGenerator.
     * @param _name     Name of this NumberGenerator.
     * @param _uuid     UUID of this NumberGenerator.
     * @param _format   format of this NumberGenerator.
     */
    private NumberGenerator(final long _id,
                            final String _name,
                            final String _uuid,
                            final String _format)
    {
        this.id = _id;
        this.name = _name;
        this.uuid = UUID.fromString(_uuid);
        this.format = _format;
    }


    /**
     * Get the name used for this sequence in the database.
     * @return name of this sequence in the database;
     */
    public String getDBName()
    {
        return NumberGenerator.NAME_PREFIX + this.id + NumberGenerator.NAME_SUFFIX;
    }

    /**
     * Method to get the next value for this sequence.
     * To get the long value use {@link #getNextValAsLong()}
     * @return next value for this sequence
     * @throws EFapsException on error
     */
    public String getNextVal()
        throws EFapsException
    {
        return getNextVal(new Object[0]);
    }

    /**
     * Method to get the next value for this sequence.
     * Including additional format information.
     * To get the long value use {@link #getNextValAsLong()}
     * @param _args  arguments for the formatter
     * @return next value for this sequence
     * @throws EFapsException on error
     */
    public String getNextVal(final Object... _args)
        throws EFapsException
    {
        final Object[] args = new Object[_args.length + 1];
        try {
            final long val = Context.getDbType().nextSequence(Context.getThreadContext().getConnection(), getDBName());
            args[0] = val;
        } catch (final SQLException e) {
            throw new EFapsException(NumberGenerator.class, " getNextVal()", e);
        }
        for (int i = 0; i < _args.length; i++) {
            args[i + 1] = _args[i];
        }
        final Locale local = Context.getThreadContext().getLocale();
        final Formatter formatter = new Formatter(local);
        formatter.format(this.format, args);
        return formatter.toString();
    }

    /**
     * Method to get the next long value for this sequence.
     * To get the formated value use {@link #getNextVal()}
     * @return next value for this sequence
     * @throws EFapsException on error
     */
    public Long getNextValAsLong()
        throws EFapsException
    {
        long ret = 0;
        try {
            ret = Context.getDbType().nextSequence(Context.getThreadContext().getConnection(), getDBName());
        } catch (final SQLException e) {
            throw new EFapsException(NumberGenerator.class, " getNextValAsLong()", e);
        }
        return ret;
    }

    /**
     * Set the value for the numberGenerator. The next call of
     * {@link #getNextVal()} or {@link #getNextVal()} normally will
     * return the value + 1.
     * @param _value value for the sequence
     * @throws EFapsException on error
     */
    public void setVal(final String _value)
        throws EFapsException
    {
        try {
            Context.getDbType().setSequence(Context.getThreadContext().getConnection(), getDBName(),
                                            Long.valueOf(_value));
        } catch (final SQLException e) {
            throw new EFapsException(NumberGenerator.class, "setVal()", e);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public long getId()
    {
        return this.id;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public String getName()
    {
        return this.name;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public UUID getUUID()
    {
        return this.uuid;
    }

    /**
     * Method to initialize the Cache of this CacheObjectInterface.
     *
     * @throws CacheReloadException on error
     */
    public static void initialize()
        throws CacheReloadException
    {
        NumberGenerator.initialize(NumberGenerator.class);
    }

    /**
     * Method to initialize the Cache of this CacheObjectInterface.
     *
     * @param _class class that called the method
     * @throws CacheReloadException on error
     */
    public static void initialize(final Class _class)
        throws CacheReloadException
    {
        NumberGenerator.CACHE.initialize(_class);
    }

    /**
     * Returns for given parameter _id the instance of class Type.
     * .
     *
     * @param _id id of the type to get
     * @return instance of class {@link Type}
     * @throws CacheReloadException
     */
    public static NumberGenerator get(final long _id)
    {
        return NumberGenerator.CACHE.get(_id);
    }

    /**
     * Returns for given parameter _name the instance of class
     * {@link Type}.
     *
     * @param _name name of the type to get
     * @return instance of class {@link Type}
     * @throws CacheReloadException
     */
    public static NumberGenerator get(final String _name)
    {
        return NumberGenerator.CACHE.get(_name);
    }

    /**
     * Returns for given parameter _uuid the instance of class
     * {@link Type}.
     *
     * @param _uuid uuid of the type to get
     * @return instance of class {@link Type}
     * @throws CacheReloadException
     */
    public static NumberGenerator get(final UUID _uuid)
    {
        return NumberGenerator.CACHE.get(_uuid);
    }

    /**
     * Static getter method for the type hashtable {@link #CACHE}.
     *
     * @return value of static variable {@link #CACHE}
     */
    public static AbstractCache getTypeCache()
    {
        return NumberGenerator.CACHE;
    }

    /**
     * Cache for Types.
     */
    private static class NumberGeneratorCache 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 rs = stmt.executeQuery(NumberGenerator.SQL_SELECT.getSQL());
                    while (rs.next()) {
                        final long id = rs.getLong(1);
                        final String name = rs.getString(2).trim();
                        final String uuid = rs.getString(3).trim();
                        final String format = rs.getString(4).trim();

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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy