01: /**
02: * Objective Database Abstraction Layer (ODAL)
03: * Copyright (c) 2004, The ODAL Development Group
04: * All rights reserved.
05: * For definition of the ODAL Development Group please refer to LICENCE.txt file
06: *
07: * Distributable under LGPL license.
08: * See terms of license at gnu.org.
09: */package com.completex.objective.components.persistency.key.impl;
10:
11: import com.completex.objective.components.persistency.MetaTable;
12: import com.completex.objective.components.persistency.PersistentObject;
13: import com.completex.objective.components.persistency.Record;
14:
15: /**
16: * @author Gennady Krizhevsky
17: *
18: * DefaultNaturalKey works only for unflattened objects and based on promary key if there is one
19: * or on object references otherwise
20: */
21: public class SimpleNaturalKeyImpl extends AbstractSimpleNaturalKey {
22: public SimpleNaturalKeyImpl() {
23: }
24:
25: /**
26: * This constructor can be used with flattened objects
27: *
28: * @param className
29: * @param values
30: */
31: public SimpleNaturalKeyImpl(String className, Object[] values) {
32: super (className, values);
33: }
34:
35: public SimpleNaturalKeyImpl(PersistentObject persistent) {
36: this (persistent, null);
37: }
38:
39: public SimpleNaturalKeyImpl(PersistentObject persistent,
40: int[] keyIndeces) {
41: super (persistent, keyIndeces);
42: }
43:
44: protected void doParse(PersistentObject persistent, int[] keyIndeces) {
45: setClassName(persistent.getClass().getName());
46: if (keyIndeces == null) {
47: MetaTable table = persistent.record().getTable();
48: keyIndeces = table.toPrimaryKeyIndeces();
49: }
50: Object[] values;
51: if (keyIndeces != null) {
52: values = new Object[keyIndeces.length];
53: for (int i = 0; i < keyIndeces.length; i++) {
54: int keyIndex = keyIndeces[i];
55: Record record = persistent.record();
56: values[i] = record.getObject(keyIndex);
57: }
58: } else { // This object does not have PK - let's give it some id
59: // to be able to identify it in memory
60: values = new Object[] { "@"
61: + Integer.toHexString(hashCode()) };
62: }
63: setValues(values);
64: }
65:
66: }
|