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.AbstractPersistentObject;
012: import com.completex.objective.components.persistency.CompoundPersistentObject;
013: import com.completex.objective.components.persistency.MetaTable;
014: import com.completex.objective.components.persistency.OdalRuntimePersistencyException;
015: import com.completex.objective.components.persistency.PersistentObject;
016: import com.completex.objective.components.persistency.Record;
017: import com.completex.objective.components.persistency.key.SimpleNaturalKeyFactory;
018: import com.completex.objective.components.persistency.key.SimpleNaturalKey;
019:
020: /**
021: * Default Natural Key Factory, used in AbstractPersistentObject and its descendants to create keys
022: *
023: * @author Gennady Krizhevsky
024: */
025: public class DefaultNaturalKeyFactory {
026:
027: private static final SimpleNaturalKeyFactory DEFAULT_SIMPLE_NATURAL_KEY_FACTORY = new SimpleNaturalKeyFactoryImpl();
028:
029: private DefaultNaturalKeyFactory() {
030: }
031:
032: /**
033: * @param abstractPersistent
034: * @param className
035: * @param values
036: * @return new natural key
037: */
038: public static Object naturalKey(
039: AbstractPersistentObject abstractPersistent,
040: String className, Object[] values) {
041: return naturalKey(abstractPersistent,
042: DEFAULT_SIMPLE_NATURAL_KEY_FACTORY, className, values);
043: }
044:
045: /**
046: * @param abstractPersistent
047: * @return new natural key
048: */
049: public static Object naturalKey(
050: AbstractPersistentObject abstractPersistent) {
051: return naturalKey(abstractPersistent, null);
052: }
053:
054: /**
055: * @param abstractPersistent
056: * @param keyIndeces
057: * @return new natural key
058: */
059: public static Object naturalKey(
060: AbstractPersistentObject abstractPersistent,
061: int[] keyIndeces) {
062: return naturalKey(abstractPersistent,
063: DEFAULT_SIMPLE_NATURAL_KEY_FACTORY, keyIndeces);
064: }
065:
066: /**
067: * @param abstractPersistent
068: * @param simpleNaturalKeyFactory
069: * @param keyIndeces
070: * @return new natural key
071: */
072: public static Object naturalKey(
073: AbstractPersistentObject abstractPersistent,
074: SimpleNaturalKeyFactory simpleNaturalKeyFactory,
075: int[] keyIndeces) {
076: Object naturalKey;
077: if (abstractPersistent.compound()) {
078: naturalKey = compoundNaturalKey(abstractPersistent,
079: simpleNaturalKeyFactory, keyIndeces);
080: } else if (abstractPersistent instanceof PersistentObject) {
081: naturalKey = simpleNaturalKey(simpleNaturalKeyFactory,
082: abstractPersistent, keyIndeces);
083: } else {
084: throw new OdalRuntimePersistencyException(
085: "Unsupported natural key type "
086: + abstractPersistent);
087: }
088: return naturalKey;
089: }
090:
091: private static Object compoundNaturalKey(
092: AbstractPersistentObject abstractPersistent,
093: SimpleNaturalKeyFactory simpleNaturalKeyFactory,
094: int[] keyIndeces) {
095: Object naturalKey;
096: SimpleNaturalKey firstKey = null;
097: if (abstractPersistent instanceof PersistentObject) {
098: firstKey = simpleNaturalKey(simpleNaturalKeyFactory,
099: abstractPersistent, keyIndeces);
100: }
101: naturalKey = new CompoundNaturalKeyImpl(abstractPersistent
102: .getClass(), firstKey, abstractPersistent
103: .compoundEntries());
104: return naturalKey;
105: }
106:
107: private static Object compoundNaturalKey(
108: AbstractPersistentObject abstractPersistent,
109: String className,
110: SimpleNaturalKeyFactory simpleNaturalKeyFactory,
111: Object[] values) {
112: Object naturalKey;
113: SimpleNaturalKey firstKey = null;
114: if (abstractPersistent instanceof PersistentObject) {
115: firstKey = simpleNaturalKeyFactory.newSimpleNaturalKey(
116: className, values);
117: }
118: naturalKey = new CompoundNaturalKeyImpl(abstractPersistent
119: .getClass(), firstKey, abstractPersistent
120: .compoundEntries());
121: return naturalKey;
122: }
123:
124: public static SimpleNaturalKey simpleNaturalKey(
125: SimpleNaturalKeyFactory simpleNaturalKeyFactory,
126: AbstractPersistentObject abstractPersistent,
127: int[] keyIndeces) {
128: return simpleNaturalKeyFactory.newSimpleNaturalKey(
129: (PersistentObject) abstractPersistent, keyIndeces);
130: }
131:
132: /**
133: * @param abstractPersistent
134: * @param simpleNaturalKeyFactory
135: * @param className
136: * @param values
137: * @return new natural key
138: */
139: public static Object naturalKey(
140: AbstractPersistentObject abstractPersistent,
141: SimpleNaturalKeyFactory simpleNaturalKeyFactory,
142: String className, Object[] values) {
143: Object naturalKey;
144: if (abstractPersistent.compound()) {
145: naturalKey = compoundNaturalKey(abstractPersistent,
146: className, simpleNaturalKeyFactory, values);
147: } else if (abstractPersistent instanceof PersistentObject) {
148: naturalKey = simpleNaturalKeyFactory.newSimpleNaturalKey(
149: className, values);
150: } else {
151: throw new OdalRuntimePersistencyException(
152: "Unsupported natural key type "
153: + abstractPersistent);
154: }
155: return naturalKey;
156: }
157:
158: public static void main(String[] args) {
159: CompoundPersistentObject compound = new CompoundPersistentObject(
160: new PersistentObject[0]);
161: PersistentObject persistent = new PersistentObject(new Record(
162: new MetaTable("test", "test")));
163:
164: Object naturalKey;
165: naturalKey = DefaultNaturalKeyFactory.naturalKey(compound);
166: System.out.println("naturalKey = " + naturalKey);
167: naturalKey = DefaultNaturalKeyFactory.naturalKey(persistent);
168: System.out.println("naturalKey = " + naturalKey);
169: }
170: }
|