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;
010:
011: import com.completex.objective.util.PropertyMap;
012:
013: import java.util.ArrayList;
014: import java.util.HashMap;
015: import java.util.List;
016: import java.util.Map;
017:
018: /**
019: * Meta-data class describing object natural key.
020: * Natural keys are used in AbstractPersistentObject.toKey() methods.
021: *
022: * @see AbstractPersistentObject#toKey()
023: * @author Gennady Krizhevsky
024: */
025: public class MetaNaturalKey implements Mappable {
026:
027: public static final MetaNaturalKey NULL_NATURAL_KEY = new MetaNaturalKey();
028: static {
029: NULL_NATURAL_KEY
030: .setNaturalKeyFactory(MetaNaturalKeyFactory.NULL_NATURAL_KEY_FACTORY);
031: }
032:
033: /**
034: * naturalKey = {
035: * simpleKeyFactory = NULL
036: * columnNames = []
037: * }
038: */
039:
040: public static final String TAG_NATURAL_KEY_FACTORY = "naturalKeyFactory";
041: public static final String TAG_COLUMN_NAMES = "columnNames";
042:
043: private MetaNaturalKeyFactory naturalKeyFactory = MetaNaturalKeyFactory.NULL_NATURAL_KEY_FACTORY;
044: private List columnNames = new ArrayList();
045:
046: public MetaNaturalKey() {
047: }
048:
049: public MetaNaturalKey(Map map) {
050: fromMap(map);
051: }
052:
053: public void addColumnName(String columnName) {
054: if (!columnNames.contains(columnName)) {
055: columnNames.add(columnName);
056: }
057: }
058:
059: public String getColumnName(int index) {
060: return (String) columnNames.get(index);
061: }
062:
063: public MetaNaturalKeyFactory getNaturalKeyFactory() {
064: return naturalKeyFactory;
065: }
066:
067: public void setNaturalKeyFactory(
068: MetaNaturalKeyFactory naturalKeyFactory) {
069: this .naturalKeyFactory = naturalKeyFactory;
070: }
071:
072: public int size() {
073: return columnNames.size();
074: }
075:
076: public boolean isEmpty() {
077: return size() == 0;
078: }
079:
080: public List getColumnNames() {
081: return columnNames;
082: }
083:
084: public void setColumnNames(List columnNames) {
085: this .columnNames = columnNames;
086: }
087:
088: /**
089: * @see Mappable
090: */
091: public Map toMap() {
092: HashMap map = new HashMap();
093: map.put(TAG_NATURAL_KEY_FACTORY,
094: naturalKeyFactory == null ? null : naturalKeyFactory
095: .toMap());
096: map.put(TAG_COLUMN_NAMES, columnNames);
097: return map;
098: }
099:
100: /**
101: * @see Mappable
102: */
103: public void fromMap(Map map) {
104: PropertyMap propertyMap = PropertyMap.toPropertyMap(map, true);
105: Map keyFactoryMap = propertyMap
106: .getPropertyMap(TAG_NATURAL_KEY_FACTORY);
107: naturalKeyFactory = keyFactoryMap == null ? null
108: : new MetaNaturalKeyFactory(keyFactoryMap);
109: columnNames = propertyMap.getList(TAG_COLUMN_NAMES);
110: }
111:
112: }
|