001: /**
002: * Objective Database Abstraction Layer (ODAL)
003: * Copyright (c) 2004, The ODAL Development Group
004: * All rights reserved.
005: * For definition of the ODAL Development Group please refer to LICENCE.txt file
006: *
007: * Distributable under LGPL license.
008: * See terms of license at gnu.org.
009: */package com.completex.objective.components.persistency.key.impl;
010:
011: import com.completex.objective.components.persistency.ColumnType;
012: import com.completex.objective.components.persistency.OdalPersistencyException;
013: import com.completex.objective.components.persistency.Persistency;
014: import com.completex.objective.components.persistency.PersistentEntry;
015: import com.completex.objective.components.persistency.core.DatabasePolicy;
016: import com.completex.objective.components.persistency.key.ComplexSequenceKeyGenerator;
017: import com.completex.objective.components.persistency.transact.Transaction;
018:
019: import java.sql.PreparedStatement;
020: import java.sql.ResultSet;
021: import java.sql.SQLException;
022:
023: /**
024: * @author Gennady Krizhevsky
025: */
026: public abstract class AbstractSequenceKeyGenerator extends
027: AbstractKeyGenerator implements ComplexSequenceKeyGenerator {
028:
029: /**
030: * @see AbstractKeyGenerator#getNextKeyPlain(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, String)
031: */
032: public Object getNextKeyPlain(Transaction transaction,
033: Persistency persistency, String objectName)
034: throws OdalPersistencyException {
035: ResultSet rs = null;
036: Long id = null;
037: PreparedStatement pstmt = null;
038: String sql = null;
039: try {
040: DatabasePolicy databasePolicy = persistency
041: .getDatabasePolicy();
042: sql = databasePolicy.nextSequenceSql(objectName);
043: pstmt = prepareStatement(transaction, sql);
044: rs = pstmt.executeQuery();
045: rs.next();
046: id = new Long(rs.getLong(1));
047: if (getLogger() != null && getLogger().isTraceEnabled()) {
048: getLogger().trace(
049: "getNextKey: objectName=" + objectName);
050: }
051: } catch (OdalPersistencyException e) {
052: logSqlException(sql, e);
053: throw e;
054: } catch (SQLException e) {
055: logSqlException(sql, e);
056: throw new OdalPersistencyException(e);
057: } finally {
058: closeAll(transaction, rs, pstmt);
059: }
060: return id;
061: }
062:
063: private void logSqlException(String sql, SQLException e) {
064: String sqlMsg = sql == null ? "" : ": " + sql;
065: getLogger().error("Cannot getNextKeyPlain" + sqlMsg, e);
066: }
067:
068: /**
069: * Populates value if (persistentEntry.getValue() == null || ((Number) persistentEntry.getValue()).longValue() <= 0)
070: *
071: * @see AbstractKeyGenerator#insertValue(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, com.completex.objective.components.persistency.PersistentEntry)
072: */
073: public void insertValue(Transaction transaction,
074: Persistency persistency, PersistentEntry persistentEntry)
075: throws OdalPersistencyException {
076: if (isInsertConditionSatisfied0(persistentEntry)) {
077: Object nextKey = getNextKey(transaction, persistency,
078: persistentEntry.getRecord());
079: persistentEntry.setValue(toConventional(persistentEntry,
080: nextKey));
081: }
082: }
083:
084: protected boolean isInsertConditionSatisfied0(
085: PersistentEntry persistentEntry) {
086: return AbstractSequenceKeyGenerator
087: .isInsertConditionSatisfied(persistentEntry);
088: }
089:
090: public static boolean isInsertConditionSatisfied(
091: PersistentEntry persistentEntry) {
092: return persistentEntry.getValue() == null
093: || (ColumnType.isNumeric(persistentEntry.getColumn()
094: .getType()) && ((Number) persistentEntry
095: .getValue()).longValue() <= 0);
096: }
097:
098: /**
099: * @see AbstractKeyGenerator#updateValue(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, com.completex.objective.components.persistency.PersistentEntry, boolean)
100: */
101: public void updateValue(Transaction transaction,
102: Persistency persistency, PersistentEntry persistentEntry,
103: boolean complexDirty) throws OdalPersistencyException {
104: }
105: }
|