egovframework.rte.fdl.idgnr.impl.AbstractIdGnrService.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.idgnr.impl.AbstractIdGnrService.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * 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.
 */
package egovframework.rte.fdl.idgnr.impl;

import java.math.BigDecimal;
import java.util.Locale;

import org.apache.commons.logging.Log;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.MessageSource;

import egovframework.rte.fdl.cmmn.exception.FdlException;
import egovframework.rte.fdl.idgnr.EgovIdGnrService;
import egovframework.rte.fdl.idgnr.EgovIdGnrStrategy;

/**
 * ID Generation   Abstract Service
 * @author   
 * @since 2009.02.01
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.02.01             ?
 * 
 * </pre>
 */
public abstract class AbstractIdGnrService implements EgovIdGnrService, ApplicationContextAware, BeanFactoryAware {

    /**
     * BeanFactory
     */
    private BeanFactory beanFactory;

    /**
     * BIG_DECIMAL_MAX_LONG ?
     */
    private static final BigDecimal BIG_DECIMAL_MAX_LONG = new BigDecimal(Long.MAX_VALUE);

    /**
     *  synchronization?  
     */
    private final Object mSemaphore = new Object();

    /**
     *  ?
     */
    private EgovIdGnrStrategy strategy = new EgovIdGnrStrategy() {
        public String makeId(String originalId) {
            return originalId;
        }
    };

    /**
     * BigDecimal  
     */
    protected boolean useBigDecimals = false;

    /**
     * MessageSource
     */
    protected MessageSource messageSource;

    /**
     *  ??
     */
    public AbstractIdGnrService() {
    }

    /**
     *   
     * @return Log IdGenerationService logger
     */
    protected Log getLogger() {
        return EgovIdGnrService.LOGGER;
    }

    /**
     * BigDecimal ? ? ? 
     * @return BigDecimal ? ? ID
     * @throws FdlException
     *         if an Id could not be allocated for any
     *         reason.
     */
    protected abstract BigDecimal getNextBigDecimalIdInner() throws FdlException;

    /**
     * long ? ? ? 
     * @return long ? ? ID
     * @throws FdlException
     *         ?? ? ? ??  
     */
    protected abstract long getNextLongIdInner() throws FdlException;

    /**
     * BigDecimal  
     * @param useBigDecimals
     *        BigDecimal 
     */
    public final void setUseBigDecimals(boolean useBigDecimals) {
        this.useBigDecimals = useBigDecimals;
    }

    /**
     * BigDecimal   
     * @return boolean check using BigDecimal
     */
    protected final boolean isUsingBigDecimals() {
        return useBigDecimals;
    }

    /**
     *    ? Long ? ? ID
     * @param maxId
     *        
     * @return long value to be less than the specified
     *         maxId
     * @throws FdlException
     *         ? ID ? MaxId ?
     */
    protected final long getNextLongIdChecked(long maxId) throws FdlException {
        long nextId;
        if (useBigDecimals) {
            // Use BigDecimal data type
            BigDecimal bd;
            synchronized (mSemaphore) {
                bd = getNextBigDecimalIdInner();
            }

            if (bd.compareTo(BIG_DECIMAL_MAX_LONG) > 0) {
                getLogger().error(messageSource.getMessage("error.idgnr.greater.maxid", new String[] { "Long" },
                        Locale.getDefault()));
                throw new FdlException(messageSource, "error.idgnr.greater.maxid");
            }
            nextId = bd.longValue();
        } else {
            // Use long data type
            synchronized (mSemaphore) {
                nextId = getNextLongIdInner();
            }
        }

        // Make sure that the id is valid for the
        // requested data type.
        if (nextId > maxId) {
            getLogger().error(messageSource.getMessage("error.idgnr.greater.maxid", new String[] { "Long" },
                    Locale.getDefault()));
            throw new FdlException(messageSource, "error.idgnr.greater.maxid");
        }

        return nextId;
    }

    /**
     * Returns BigDecimal ? ? ID 
     * @return BigDecimal the next Id.
     * @throws FdlException
     *         ? ?  BigDecimal?  
     */
    public final BigDecimal getNextBigDecimalId() throws FdlException {
        BigDecimal bd;
        if (useBigDecimals) {
            // Use BigDecimal data type
            synchronized (mSemaphore) {
                bd = getNextBigDecimalIdInner();
            }
        } else {
            // Use long data type
            synchronized (mSemaphore) {
                bd = new BigDecimal(getNextLongIdInner());
            }
        }

        return bd;
    }

    /**
     * Returns long ? ? ID 
     * @return the next Id.
     * @throws FdlException
     *         ? ?  long?  
     */
    public final long getNextLongId() throws FdlException {
        return getNextLongIdChecked(Long.MAX_VALUE);
    }

    /**
     * Returns int ? ? ID 
     * @return the next Id.
     * @throws FdlException
     *         ? ?  integer?  
     */
    public final int getNextIntegerId() throws FdlException {
        return (int) getNextLongIdChecked(Integer.MAX_VALUE);
    }

    /**
     * Returns Short ? ? ID 
     * @return the next Id.
     * @throws FdlException
     *         ? ?  Short?  
     */
    public final short getNextShortId() throws FdlException {
        return (short) getNextLongIdChecked(Short.MAX_VALUE);
    }

    /**
     * Returns Byte ? ? ID 
     * @return the next Id.
     * @throws FdlException
     *         ? ?  Byte  
     */
    public final byte getNextByteId() throws FdlException {
        return (byte) getNextLongIdChecked(Byte.MAX_VALUE);
    }

    /**
     * String ? Id ? ? ? ? 
     * @return the next Id.
     * @throws FdlException
     *         ? ?  byte?  
     */
    public final String getNextStringId() throws FdlException {
        return strategy.makeId(getNextBigDecimalId().toString());
    }

    /**
     *  ?  String ? Id 
     * @param strategy
     *        ??  ?
     * @return the next Id.
     * @throws FdlException
     *         ? ?  byte?  
     */
    public String getNextStringId(EgovIdGnrStrategy strategy) throws FdlException {
        this.strategy = strategy;
        return getNextStringId();
    }

    /**
     *  String  String ? Id 
     * @param strategyId
     *         ?
     * @return the next Id.
     * @throws FdlException
     *         ? ?  byte?  
     */
    public String getNextStringId(String strategyId) throws FdlException {
        this.strategy = (EgovIdGnrStrategy) this.beanFactory.getBean(strategyId);
        return getNextStringId();
    }

    /**
     *  
     * @return IdGenerationStrategy
     */
    public EgovIdGnrStrategy getStrategy() {
        return strategy;
    }

    /**
     *  
     * @param strategy
     *        to be set by Spring Framework
     */
    public void setStrategy(EgovIdGnrStrategy strategy) {
        this.strategy = strategy;
    }

    /**
     * set BeanFactory
     * @param beanFactory
     *        to be set by Spring Framework
     */
    public void setBeanFactory(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    /**
     * Message Source Injection
     */
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.messageSource = (MessageSource) applicationContext.getBean("messageSource");

    }
}