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.rule.impl;
010:
011: import com.completex.objective.components.persistency.PersistentEntry;
012: import com.completex.objective.components.persistency.PersistentObject;
013: import com.completex.objective.components.persistency.Record;
014: import com.completex.objective.components.persistency.Rule;
015: import com.completex.objective.components.persistency.rule.FieldConvertor;
016: import com.completex.objective.components.persistency.rule.RecordConvertor;
017:
018: /**
019: * @author Gennady Krizhevsky
020: */
021: public class RecordConvertorImpl implements Rule, FieldConvertor,
022: RecordConvertor {
023:
024: private int size;
025:
026: private final FieldConvertor[][] fieldConvertors;
027:
028: // public RecordConvertorImpl() {
029: // this(0);
030: // }
031:
032: public RecordConvertorImpl(PersistentObject persistentObject) {
033: // assert size >= 0;
034: this .size = persistentObject.record().size();
035: fieldConvertors = new FieldConvertor[size][];
036: }
037:
038: public FieldConvertor[][] getFieldConvertors() {
039: return fieldConvertors;
040: }
041:
042: public RecordConvertor addFieldConvertor(int fieldIndex,
043: FieldConvertor fieldConvertor) {
044: assertIndex(fieldIndex);
045: //
046: // See if validators exists, if not - create,
047: // if yes - add to FieldValidator[]
048: FieldConvertor[] old = fieldConvertors[fieldIndex];
049: if (old == null) {
050: old = new FieldConvertor[0];
051: }
052:
053: FieldConvertor[] collection = new FieldConvertor[old.length + 1];
054: System.arraycopy(old, 0, collection, 0, old.length);
055: collection[old.length] = fieldConvertor;
056: fieldConvertors[fieldIndex] = collection;
057: return this ;
058: }
059:
060: private void assertIndex(int fieldIndex) {
061: if (fieldIndex >= size || fieldIndex < 0) {
062: throw new IllegalArgumentException(
063: "fieldIndex >= size || fieldIndex < 0 : "
064: + fieldIndex);
065: }
066: }
067:
068: public void setFieldConvertors(int fieldIndex,
069: FieldConvertor[] convertors) {
070: fieldConvertors[fieldIndex] = convertors;
071: }
072:
073: protected Record convert(Record record) throws Exception {
074: if (record == null) {
075: return null;
076: }
077:
078: for (int k = 0; k < size; k++) {
079: PersistentEntry entry = record.getEntry(k);
080: convertField(k, entry);
081: }
082: return record;
083: }
084:
085: private void convertField(int k, PersistentEntry entry)
086: throws Exception {
087: FieldConvertor[] convertors = fieldConvertors[k];
088: if (convertors != null) {
089: for (int i = 0; i < convertors.length; i++) {
090: convertors[i].convert(entry);
091: }
092: }
093: }
094:
095: public Object executeRule(PersistentObject persistentObject)
096: throws Exception {
097: convert(persistentObject.record());
098: persistentObject.toBeanFields();
099: return persistentObject;
100: }
101:
102: public void convert(PersistentEntry entry) throws Exception {
103: int k = entry.getColumn().getColumnIndex();
104: FieldConvertor[] convertors = fieldConvertors[k];
105: if (convertors != null) {
106: for (int i = 0; i < convertors.length; i++) {
107: convertors[i].convert(entry);
108: }
109: }
110: }
111:
112: public void executeFieldRule(PersistentEntry entry)
113: throws Exception {
114: convert(entry);
115: }
116:
117: public Object clone() throws CloneNotSupportedException {
118: return super.clone();
119: }
120:
121: }
|