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.ColumnType;
013: import com.completex.objective.components.persistency.OdalPersistencyException;
014: import com.completex.objective.components.persistency.Persistency;
015: import com.completex.objective.components.persistency.PersistentEntry;
016: import com.completex.objective.components.persistency.Record;
017: import com.completex.objective.components.persistency.core.DatabasePolicy;
018: import com.completex.objective.components.persistency.key.ComplexSequenceKeyGenerator;
019: import com.completex.objective.components.persistency.transact.Transaction;
020: import com.completex.objective.util.PropertyMap;
021:
022: import java.util.Map;
023:
024: /**
025: * Sequence based version generator - decorator for ComplexSequenceKeyGenerator
026: * <p/>
027: * Example of key generator section:
028: * <p/>
029: * <br>
030: * <PRE>
031: * keyGenerator = {
032: * class = com.completex.objective.components.persistency.key.impl.VersionSequenceKeyGeneratorImpl
033: * staticAttributes = {
034: * name = CONTACT_SEQ
035: * alwaysGenerate = TRUE # or FALSE
036: * }
037: * }
038: * </PRE>
039: *
040: * @author Gennady Krizhevsky
041: */
042: public class VersionSequenceKeyGeneratorImpl implements
043: ComplexSequenceKeyGenerator {
044: public static final String TAG_ALWAYS_GENERATE = "alwaysGenerate";
045:
046: protected boolean alwaysGenerate;
047:
048: protected ComplexSequenceKeyGenerator delegateKeyGenerator;
049:
050: /**
051: * @param delegateKeyGenerator
052: * @param databasePolicy
053: * @param seqName
054: * @param logger
055: */
056: public VersionSequenceKeyGeneratorImpl(
057: ComplexSequenceKeyGenerator delegateKeyGenerator,
058: DatabasePolicy databasePolicy, String seqName, Log logger) {
059: this .delegateKeyGenerator = delegateKeyGenerator;
060: this .delegateKeyGenerator.setDatabasePolicy(databasePolicy);
061: this .delegateKeyGenerator.setSeqName(seqName);
062: this .delegateKeyGenerator.setLogger(logger);
063: }
064:
065: public VersionSequenceKeyGeneratorImpl() {
066: }
067:
068: /**
069: * @see ComplexSequenceKeyGenerator#getSeqName()
070: */
071: public String getSeqName() {
072: return delegateKeyGenerator.getSeqName();
073: }
074:
075: /**
076: * @see ComplexSequenceKeyGenerator#getNextKeyPlain(Transaction transaction, Persistency persistency, String objectName)
077: */
078: public Object getNextKeyPlain(Transaction transaction,
079: Persistency persistency, String objectName)
080: throws OdalPersistencyException {
081: return delegateKeyGenerator.getNextKeyPlain(transaction,
082: persistency, objectName);
083: }
084:
085: /**
086: * Populates value if (persistentEntry.getValue() == null || ((Number) persistentEntry.getValue()).longValue() <= 0)
087: *
088: * @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)
089: */
090: public void insertValue(Transaction transaction,
091: Persistency persistency, PersistentEntry persistentEntry)
092: throws OdalPersistencyException {
093: if (isInsertConditionSatisfied0(persistentEntry)) {
094: Object nextKey = getNextKey(transaction, persistency,
095: persistentEntry.getRecord());
096: persistentEntry.setValue(AbstractKeyGenerator
097: .toConventional(persistentEntry, nextKey));
098: }
099: }
100:
101: protected boolean isInsertConditionSatisfied0(
102: PersistentEntry persistentEntry) {
103: return AbstractSequenceKeyGenerator
104: .isInsertConditionSatisfied(persistentEntry);
105: }
106:
107: /**
108: * Populates value when the condition (persistentEntry.getRecord().isDirty() || isAlwaysGenerate()) is true
109: * and the condition (persistentEntry.getValue() == null or not persistentEntry.isDirty()) is true.
110: *
111: * @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)
112: */
113: public void updateValue(Transaction transaction,
114: Persistency persistency, PersistentEntry persistentEntry,
115: boolean complexDirty) throws OdalPersistencyException {
116: if (persistentEntry.getRecord() != null
117: && (persistentEntry.getRecord().isDirty() || alwaysGenerate)) {
118: if (persistentEntry.getValue() == null
119: || !persistentEntry.isDirty()
120: || (ColumnType.isNumeric(persistentEntry
121: .getColumn().getType()) && ((Number) persistentEntry
122: .getValue()).longValue() <= 0)) {
123: persistentEntry.setValue(getNextKey(transaction,
124: persistency, persistentEntry.getRecord()));
125: }
126: }
127: }
128:
129: //
130: // Delegate:
131: //
132:
133: /**
134: * @see com.completex.objective.components.persistency.key.KeyGenerator#getNextKey(Transaction transaction, Persistency persistency, Record record)
135: */
136: public Object getNextKey(Transaction transaction,
137: Persistency persistency, Record record)
138: throws OdalPersistencyException {
139: return delegateKeyGenerator.getNextKey(transaction,
140: persistency, record);
141: }
142:
143: /**
144: * @see com.completex.objective.components.persistency.key.KeyGenerator#getNextKey(Transaction transaction, Persistency persistency, Record record, Object dynamicParameters)
145: */
146: public Object getNextKey(Transaction transaction,
147: Persistency persistency, Record record,
148: Object dynamicParameters) throws OdalPersistencyException {
149: return delegateKeyGenerator.getNextKey(transaction,
150: persistency, record, dynamicParameters);
151: }
152:
153: /**
154: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#setStaticParameters(Object staticParameters)
155: */
156: public void setStaticParameters(Object staticParameters) {
157: delegateKeyGenerator.setStaticParameters(staticParameters);
158: if (staticParameters != null) {
159: Map parameters = (Map) staticParameters;
160: PropertyMap propertyMap = PropertyMap
161: .toPropertyMap(parameters);
162: alwaysGenerate = propertyMap
163: .getBoolean(TAG_ALWAYS_GENERATE);
164: }
165: }
166:
167: /**
168: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#getStaticParameters()
169: */
170: public Object getStaticParameters() {
171: return delegateKeyGenerator.getStaticParameters();
172: }
173:
174: /**
175: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#setDatabasePolicy(com.completex.objective.components.persistency.core.DatabasePolicy)
176: */
177: public void setDatabasePolicy(DatabasePolicy databasePolicy) {
178: delegateKeyGenerator.setDatabasePolicy(databasePolicy);
179: }
180:
181: /**
182: * @see com.completex.objective.components.persistency.key.AutoKeyGenerator#setLogger(com.completex.objective.components.log.Log)
183: */
184: public void setLogger(Log log) {
185: delegateKeyGenerator.setLogger(log);
186: }
187:
188: /**
189: * @see ComplexSequenceKeyGenerator#setSeqName(String)
190: */
191: public void setSeqName(String seqName) {
192: delegateKeyGenerator.setSeqName(seqName);
193: }
194:
195: /**
196: * Returns true if the version value has to be populated even if Record is not dirty
197: *
198: * @return true if the version value has to be populated even if Record is not dirty
199: */
200: public boolean isAlwaysGenerate() {
201: return alwaysGenerate;
202: }
203:
204: /**
205: * Set true if the version value has to be populated even if Record is not dirty
206: *
207: * @param alwaysGenerate true if the version value has to be populated even if Record is not dirty
208: */
209: public void setAlwaysGenerate(boolean alwaysGenerate) {
210: this.alwaysGenerate = alwaysGenerate;
211: }
212: }
|