当先锋百科网

首页 1 2 3 4 5 6 7

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.sql.DataSource;

import org.springframework.jdbc.support.incrementer.OracleSequenceMaxValueIncrementer;

import com.huawei.widget.commons.dao.DBException;
import com.huawei.widget.commons.dao.spi.SequenceGenerator;
import com.huawei.widget.commons.exception.ErrorCode;


public class OracleSequenceGeneratorSpringImpl implements SequenceGenerator
{
   
    private Map<String, OracleSequenceMaxValueIncrementer> incrementerMap = null;

   
    public OracleSequenceGeneratorSpringImpl()
    {
    }

   
    public void init(DataSource dataSource, List<String> sequenceNames)
    {
        incrementerMap = Collections
                .synchronizedMap(new HashMap<String, OracleSequenceMaxValueIncrementer>());

        if (sequenceNames != null)
        {
            for (String sequenceName : sequenceNames)
            {
                incrementerMap.put(sequenceName,
                        new OracleSequenceMaxValueIncrementer(dataSource,
                                sequenceName));
            }
        }
    }

   
    public int nextIntValue(String sequenceName) throws DBException
    {
        return getIncrementer(sequenceName).nextIntValue();
    }

   
    public long nextLongValue(String sequenceName) throws DBException
    {
        return getIncrementer(sequenceName).nextLongValue();
    }

   
    public String nextStringValue(String sequenceName) throws DBException
    {
        return getIncrementer(sequenceName).nextStringValue();
    }

    private OracleSequenceMaxValueIncrementer getIncrementer(String sequenceName)
            throws DBException
    {
        OracleSequenceMaxValueIncrementer incrementer = incrementerMap
                .get(sequenceName);
        if (null == incrementer)
        {
            throw new DBException(ErrorCode.COMMON_GET_SEQUENCE_UNIKEY_FAIL,
                    "The incrementer of sequence:" + sequenceName
                            + " is not initialized.");
        }

        return incrementer;
    }
}