summaryrefslogtreecommitdiffstats
path: root/tags/java-M1-20060522/java/das/rdb/src/main/java/org/apache/tuscany/das/rdb/impl/DatabaseObject.java
blob: eaf56a6c2cfcbf95f22fc9f7ec696d23d82386c4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/**
 *
 *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.apache.tuscany.das.rdb.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

import org.apache.tuscany.das.rdb.config.Column;
import org.apache.tuscany.das.rdb.config.Config;
import org.apache.tuscany.das.rdb.config.KeyPair;
import org.apache.tuscany.das.rdb.config.Relationship;
import org.apache.tuscany.das.rdb.config.Table;
import org.apache.tuscany.das.rdb.config.wrapper.MappingWrapper;
import org.apache.tuscany.das.rdb.util.DebugUtil;

import commonj.sdo.DataObject;
import commonj.sdo.Property;

/**
 * DatabaseObject wraps DataObject. If a field is an FK field, it will return
 * the value from the parent.
 * 
 * 
 */
public class DatabaseObject {

    private final MappingWrapper mappingWrapper;

    private final DataObject dataObject;

    private Property parentReference;

    private static final boolean debug = false;

    private HashMap keyMappings = new HashMap();

    public DatabaseObject(Config model, DataObject changedObject) {
        this.mappingWrapper = new MappingWrapper(model);
        this.dataObject = changedObject;
        initialize();
    }

    // Initialize Key Mappings
    private void initialize() {
        if (mappingWrapper.getConfig() != null) {
            List relationships = mappingWrapper.getConfig().getRelationship();
            Iterator i = relationships.iterator();
            while (i.hasNext()) {
                Relationship r = (Relationship) i.next();
                DebugUtil.debugln(getClass(), debug, "Initializing relationship: " + r.getName());
                if (r.getForeignKeyTable().equals(getTypeName())) {
                    List pairs = r.getKeyPair();
                    Iterator iter = pairs.iterator();
                    while (iter.hasNext()) {
                        KeyPair pair = (KeyPair) iter.next();
                        keyMappings.put(pair.getForeignKeyColumn(), r);
                    }
                }
            }
        }
    }

    public Object get(String parameter) {

        if (isPartOfPrimaryKey(parameter))
            return dataObject.get(parameter);

        Relationship r = (Relationship) keyMappings.get(parameter);
        if (r == null)
            return dataObject.get(parameter);

        Property parentRef = getParentReference(r.getPrimaryKeyTable());
        DataObject parent = dataObject.getDataObject(parentRef);
        if (parent == null)
            return null;
        String parentKey = getParentKey(r, parameter);
        return parent.get(parentKey);

    }

    private String getParentKey(Relationship r, String parameter) {
        List keyPairs = r.getKeyPair();
        Iterator i = keyPairs.iterator();
        while (i.hasNext()) {
            KeyPair pair = (KeyPair) i.next();
            if (pair.getForeignKeyColumn().equals(parameter))
                return pair.getPrimaryKeyColumn();
        }
        return null;
    }

    public Property getParentReference(String parentName) {
        if (this.parentReference == null) {

            Iterator i = dataObject.getType().getProperties().iterator();
            while (i.hasNext()) {
                Property ref = (Property) i.next();
                if ((!ref.getType().isDataType()) && (ref.getType().getName().equals(parentName))) {
                    this.parentReference = ref;
                }
            }
        }
        return this.parentReference;
    }

    public String getTableName() {
        if (mappingWrapper.getConfig() != null)
            return mappingWrapper.getTableByPropertyName(getTypeName()).getName();
        else
            return null;
    }

    public String getTypeName() {
        return dataObject.getType().getName();
    }

    public void setPropagatedID(String propagatedID, int id) {
        dataObject.setInt(propagatedID, id);
    }

    private boolean isPartOfPrimaryKey(String parameter) {
        if (mappingWrapper.getConfig() == null)
            return false;
        Table t = mappingWrapper.getTable(getTableName());
        if (t == null)
            return false;
        Column c = mappingWrapper.getColumnByPropertyName(t, parameter);
        if (c == null)
            return false;
        return c.isPrimaryKey();
    }

}