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.log.Log;
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.Record;
016: import com.completex.objective.components.persistency.ColumnType;
017: import com.completex.objective.components.persistency.core.DatabasePolicy;
018: import com.completex.objective.components.persistency.key.ComplexKeyGenerator;
019: import com.completex.objective.components.persistency.transact.Transaction;
020:
021: import java.sql.PreparedStatement;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024:
025: /**
026: * @author Gennady Krizhevsky
027: */
028: public abstract class AbstractKeyGenerator implements
029: ComplexKeyGenerator {
030: //
031: // Keys for default sequence generators
032: //
033: public static final String SEQ_KEY = "name";
034: public static final String SEQ_TABLE_KEY = "table";
035: public static final String SEQ_SAME_TRANSACTION_KEY = "sameTransaction";
036:
037: private Object staticParameters;
038: private DatabasePolicy databasePolicy;
039: private Log log = Log.NULL_LOGGER;
040: protected final Object lock = new Object();
041: private String seqName;
042:
043: public Object getStaticParameters() {
044: return staticParameters;
045: }
046:
047: /**
048: * Set staticParameters once
049: *
050: * @param staticParameters
051: */
052: public void setStaticParameters(Object staticParameters) {
053: if (this .staticParameters == null && staticParameters != null) {
054: this .staticParameters = staticParameters;
055: }
056: }
057:
058: public Log getLogger() {
059: return log;
060: }
061:
062: public void setLogger(Log log) {
063: if (this .log == null && log != null) {
064: this .log = log;
065: }
066: }
067:
068: public DatabasePolicy getDatabasePolicy() {
069: return databasePolicy;
070: }
071:
072: public void setDatabasePolicy(DatabasePolicy databasePolicy) {
073: if (this .databasePolicy == null && databasePolicy != null) {
074: this .databasePolicy = databasePolicy;
075: }
076: }
077:
078: /**
079: * Closes result set if not null, releases statement
080: *
081: * @param transaction
082: * @param resultSet
083: * @param stmt
084: * @throws OdalPersistencyException
085: */
086: protected void closeAll(Transaction transaction,
087: ResultSet resultSet, PreparedStatement stmt)
088: throws OdalPersistencyException {
089: if (resultSet != null) {
090: try {
091: resultSet.close();
092: } catch (SQLException e) {
093: log.error("Cannot close result set.", e);
094: }
095: }
096: if (stmt != null) {
097: try {
098: releaseStatement(transaction, stmt);
099: } catch (SQLException e) {
100: throw new OdalPersistencyException(e);
101: }
102: }
103: }
104:
105: /**
106: * Prepares statement in transaction
107: * @see Transaction#prepareStatement(String)
108: *
109: * @param transaction
110: * @param sql
111: * @return PreparedStatement
112: * @throws SQLException
113: */
114: protected PreparedStatement prepareStatement(
115: Transaction transaction, String sql) throws SQLException {
116: validate(transaction);
117: return transaction.prepareStatement(sql);
118: }
119:
120: /**
121: * Releases statement from the transaction
122: * @see Transaction#releaseStatement(java.sql.PreparedStatement)
123: *
124: * @param transaction
125: * @param statement
126: * @throws SQLException
127: */
128: protected void releaseStatement(Transaction transaction,
129: PreparedStatement statement) throws SQLException {
130: validate(transaction);
131: transaction.releaseStatement(statement);
132: }
133:
134: protected void validate(Transaction transaction) {
135:
136: }
137:
138: /**
139: * @see com.completex.objective.components.persistency.key.KeyGenerator#getNextKey(Transaction transaction, Persistency persistency, Record record)
140: */
141: public abstract Object getNextKey(Transaction transaction,
142: Persistency persistency, Record record)
143: throws OdalPersistencyException;
144:
145: /**
146: * @see com.completex.objective.components.persistency.key.KeyGenerator#getNextKey(Transaction transaction, Persistency persistency, Record record, Object dynamicParameters)
147: */
148: public abstract Object getNextKey(Transaction transaction,
149: Persistency persistency, Record record,
150: Object dynamicParameters) throws OdalPersistencyException;
151:
152: /**
153: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#insertValue(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, com.completex.objective.components.persistency.PersistentEntry)
154: */
155: public abstract void insertValue(Transaction transaction,
156: Persistency persistency, PersistentEntry persistentEntry)
157: throws OdalPersistencyException;
158:
159: /**
160: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#updateValue(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, com.completex.objective.components.persistency.PersistentEntry, boolean)
161: */
162: public abstract void updateValue(Transaction transaction,
163: Persistency persistency, PersistentEntry persistentEntry,
164: boolean complexDirty) throws OdalPersistencyException;
165:
166: /**
167: * @see com.completex.objective.components.persistency.key.ComplexSequenceKeyGenerator#getNextKeyPlain(com.completex.objective.components.persistency.transact.Transaction, com.completex.objective.components.persistency.Persistency, String)
168: */
169: public abstract Object getNextKeyPlain(Transaction transaction,
170: Persistency persistency, String objectName)
171: throws OdalPersistencyException;
172:
173: public void setSeqName(String seqName) {
174: this .seqName = seqName;
175: }
176:
177: public String getSeqName() {
178: return seqName;
179: }
180:
181: public static Object toConventional(
182: PersistentEntry persistentEntry, Object value) {
183: if (value != null
184: && ColumnType.isString(persistentEntry.getColumn()
185: .getType()) && !(value instanceof String)) {
186: value = value.toString();
187: }
188: return value;
189: }
190: }
|