AbstractService.java 3.35 KB
Newer Older
Kim Gyeongeun committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
package jp.agentec.sinaburocast.service;

import jp.agentec.sinaburocast.common.util.SinaburoUtil;
import jp.agentec.sinaburocast.entity.AbstractEntity;

import org.apache.commons.lang.IllegalClassException;
import org.seasar.extension.jdbc.DbmsDialect;
import org.seasar.extension.jdbc.manager.JdbcManagerImplementor;
import org.seasar.extension.jdbc.service.S2AbstractService;

/**
 * AbstractServiceクラス
 *
 * @author tsukada
 *
 * @param <ENTITY>
 */
public abstract class AbstractService<ENTITY extends AbstractEntity> extends S2AbstractService<ENTITY> {
//	private final Log logger= LogFactory.getLog(getClass());

	/**
	 * PostgreSQLのシーケンスキーを指定し、次に採番されるシーケンス番号を取得します。
	 * @param sequenceName シーケンスキー
	 * @return Integer
	 */
	public Integer getSeqNextVal(String sequenceName) {
        DbmsDialect dialect = ((JdbcManagerImplementor)this.jdbcManager).getDialect();

        String nextValSql = dialect.getSequenceNextValString(sequenceName, 0);

        return this.jdbcManager.selectBySql(Integer.class, nextValSql).getSingleResult();
	}

	/**
	 * PostgreSQLのシーケンスキーを指定し、次に採番されるシーケンス番号を取得します。
	 * @param sequenceName シーケンスキー
	 * @return Long
	 */
	public Long getSeqNextLongVal(String sequenceName) {
        DbmsDialect dialect = ((JdbcManagerImplementor)this.jdbcManager).getDialect();

        String nextValSql = dialect.getSequenceNextValString(sequenceName, 0);

        return this.jdbcManager.selectBySql(Long.class, nextValSql).getSingleResult();
	}

	/**
	 * PostgreSQLのシーケンスキーを指定し、次に採番されるシーケンス番号を取得します。
	 * @param <T>
	 * @param valueClass
	 * @param sequenceName
	 * @return
	 */
	public <T> T getSeqNextVal(Class<T> valueClass, String sequenceName) {

        if (!(this.jdbcManager instanceof JdbcManagerImplementor)) {
            throw new IllegalClassException(
                JdbcManagerImplementor.class, this.jdbcManager);
        }

        DbmsDialect dialect = ((JdbcManagerImplementor)this.jdbcManager).getDialect();

        String nextValSql = dialect.getSequenceNextValString(sequenceName, 0);

        return this.jdbcManager.selectBySql(valueClass, nextValSql).getSingleResult();
    }

	/**
	 * DB共通項目を設定し、Insertします
	 * @param entity
	 * @param insIdStr ユーザーID
	 * @return
	 */
	public int insert(ENTITY entity, String insIdStr){
		setInsertMetaInfo(entity, insIdStr);
		return super.insert(entity);
	}

	/**
	 * DB共通項目を設定し、Updateします
	 * @param entity
	 * @param insId ユーザーID
	 * @return
	 */
	public int update(ENTITY entity, String updIdStr){
		setUpdateMetaInfo(entity, updIdStr);
		return super.update(entity);
	}

	/**
	 * 登録者・登録日・バージョンを設定する
	 * @param entity
	 * @param insId ユーザーID
	 */
	protected void setInsertMetaInfo(ENTITY entity, String insId){
		entity.insId = insId;
		entity.insertDate = SinaburoUtil.getTimestamp();
		entity.version = 0;
		setUpdateMetaInfo(entity,insId);
	}

	/**
	 * 更新者・更新日を設定する
	 * @param entity
	 * @param insId ユーザーID
	 */
	protected void setUpdateMetaInfo(ENTITY entity, String updateId) {
		entity.upId = updateId;
		entity.updateDate = SinaburoUtil.getTimestamp();
	}

}