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

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.idgnr.impl.EgovTableIdGnrService.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 javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;

import egovframework.rte.fdl.cmmn.exception.FdlException;

/**
 * ID Generation   Table  ?
 * <p>
 * <b>NOTE</b>:  ?? ?, ? ??    Max 
 *  Table ? ?  ?  .
 * 
 * <pre>
 *        ? ? ?
 *   CREATE TABLE ids (
 *       table_name varchar(16) NOT NULL,
 *       next_id INTEGER NOT NULL,
 *       PRIMARY KEY (table_name)
 *   );
 * </pre>
 * @author   
 * @since 2009.02.01
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.02.01       ?
 *   2013.03.25       ? , JdbcTemplate ? ,  id  ?(?? insert ),  
 * 
 * </pre>
 */
public class EgovTableIdGnrService extends AbstractDataBlockIdGnrService {

    /**
     * ID??  ?  ? ids.
     */
    private String table = "ids";

    /**
     * ? ? ?? ?  ?  ? ?? ?? 
     */
    private String tableName = "id";

    /**
     * ?()?  ?  
     */
    private String tableNameFieldName = "table_name";

    /**
     * Next Id    
     */
    private String nextIdFieldName = "next_id";

    /**
     * Jdbc template
     */
    private JdbcTemplate jdbcTemplate;

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

    @Override
    public void setDataSource(DataSource dataSource) {
        super.setDataSource(dataSource);

        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }

    /**
     * tableName?   ?    id  ? (blockSize )
     * 
     * @param useBigDecimals
     * @param initId
     */
    private Object insertInitId(boolean useBigDecimals, int blockSize) {

        if (getLogger().isDebugEnabled()) {
            getLogger().debug(messageSource.getMessage("debug.idgnr.init.idblock", new Object[] { tableName },
                    Locale.getDefault()));
        }

        Object initId = null;

        String insertQuery = "INSERT INTO " + table + "(" + tableNameFieldName + ", " + nextIdFieldName + ") "
                + "values('" + tableName + "', ?)";

        if (getLogger().isDebugEnabled()) {
            getLogger().debug("Insert Query : " + insertQuery);
        }

        if (useBigDecimals) {
            initId = new BigDecimal(blockSize);

        } else {
            initId = new Long(blockSize);
        }

        jdbcTemplate.update(insertQuery, initId);

        return initId;
    }

    /**
     * blockSize  ID 
     * 
     * @param blockSize ? blockSize
     * @param useBigDecimals BigDecimal  
     * @return BigDecimal?  BigDecimal  long 
     * @throws FdlException ID??   ? 
     */
    private Object allocateIdBlock(int blockSize, boolean useBigDecimals) throws FdlException {

        if (getLogger().isDebugEnabled()) {
            getLogger().debug(messageSource.getMessage("debug.idgnr.allocate.idblock",
                    new Object[] { new Integer(blockSize), tableName }, Locale.getDefault()));
        }

        Object nextId;
        Object newNextId;

        try {

            String selectQuery = "SELECT " + nextIdFieldName + " FROM " + table + " WHERE " + tableNameFieldName
                    + " = ?";

            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Select Query : " + selectQuery);
            }

            if (useBigDecimals) {
                try {
                    nextId = jdbcTemplate.queryForObject(selectQuery, new Object[] { tableName }, BigDecimal.class);
                } catch (EmptyResultDataAccessException erdae) {
                    nextId = null;
                }

                if (nextId == null) { // no row
                    insertInitId(useBigDecimals, blockSize);

                    return new BigDecimal(0);
                }
            } else {
                try {
                    nextId = jdbcTemplate.queryForLong(selectQuery, tableName);
                } catch (EmptyResultDataAccessException erdae) {
                    nextId = -1L;
                }

                if ((Long) nextId == -1L) { // no row
                    insertInitId(useBigDecimals, blockSize);

                    return new Long(0);
                }
            }
        } catch (DataAccessException dae) {
            dae.printStackTrace();
            if (getLogger().isDebugEnabled()) {
                getLogger().debug("", dae);
            }
            throw new FdlException(messageSource, "error.idgnr.select.idblock", new String[] { tableName }, null);
        }

        try {
            String updateQuery = "UPDATE " + table + " SET " + nextIdFieldName + " = ?" + " WHERE "
                    + tableNameFieldName + " = ?";

            if (getLogger().isDebugEnabled()) {
                getLogger().debug("Update Query : " + updateQuery);
            }

            if (useBigDecimals) {
                newNextId = ((BigDecimal) nextId).add(new BigDecimal(blockSize));

            } else {
                newNextId = new Long(((Long) nextId).longValue() + blockSize);
            }

            jdbcTemplate.update(updateQuery, newNextId, tableName);

            return nextId;
        } catch (DataAccessException dae) {
            throw new FdlException(messageSource, "error.idgnr.update.idblockk", new String[] { tableName }, null);
        }

    }

    /**
     * blockSize  ID (BigDecimal)
     * 
     * @param blockSize ? blockSize
     * @return ? ?  ?
     * @throws FdlException ID??   ? 
     */
    protected BigDecimal allocateBigDecimalIdBlock(int blockSize) throws FdlException {
        return (BigDecimal) allocateIdBlock(blockSize, true);
    }

    /**
     * blockSize  ID (long)
     * 
     * @param blockSize ? blockSize
     * @return ? ?  ?
     * @throws FdlException ID??   ? 
     */
    protected long allocateLongIdBlock(int blockSize) throws FdlException {
        Long id = (Long) allocateIdBlock(blockSize, false);

        return id.longValue();
    }

    /**
     * ID??  ?  Injection
     * 
     * @param table config ? 
     */
    public void setTable(String table) {
        this.table = table;
    }

    /**
     * ID ??  ??  ( ? ? ??  )
     * 
     * @param tableName config ? 
     */
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /**
     *  ?()?  ?   
     * 
     * @param tableNameFieldName
     */
    public void setTableNameFieldName(String tableNameFieldName) {
        this.tableNameFieldName = tableNameFieldName;
    }

    /**
     * Next Id     
     * 
     * @param nextIdFieldName
     */
    public void setNextIdFieldName(String nextIdFieldName) {
        this.nextIdFieldName = nextIdFieldName;
    }
}