KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > access > trans > DataObjectMatchTranslator


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20
21 package org.apache.cayenne.access.trans;
22
23 import java.util.Collections JavaDoc;
24 import java.util.HashMap JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27
28 import org.apache.cayenne.CayenneRuntimeException;
29 import org.apache.cayenne.ObjectId;
30 import org.apache.cayenne.Persistent;
31 import org.apache.cayenne.exp.Expression;
32 import org.apache.cayenne.map.DbAttribute;
33 import org.apache.cayenne.map.DbEntity;
34 import org.apache.cayenne.map.DbJoin;
35 import org.apache.cayenne.map.DbRelationship;
36
37 /**
38  * @author Andrus Adamchik
39  */

40 public class DataObjectMatchTranslator {
41     protected Map JavaDoc attributes;
42     protected Map JavaDoc values;
43     protected String JavaDoc operation;
44     protected Expression expression;
45     protected DbRelationship relationship;
46
47     public Expression getExpression() {
48         return expression;
49     }
50
51     public void setExpression(Expression expression) {
52         this.expression = expression;
53     }
54
55     public void reset() {
56         attributes = null;
57         values = null;
58         operation = null;
59         expression = null;
60         relationship = null;
61     }
62
63     /**
64      * Initializes itself to do translation of the match ending
65      * with a DbRelationship.
66      */

67     public void setRelationship(DbRelationship rel) {
68         this.relationship = rel;
69         attributes = new HashMap JavaDoc(rel.getJoins().size() * 2);
70
71         if (rel.isToMany() || !rel.isToPK()) {
72
73             // match on target PK
74
DbEntity ent = (DbEntity) rel.getTargetEntity();
75             Iterator JavaDoc pk = ent.getPrimaryKey().iterator();
76
77             // index by name
78
while (pk.hasNext()) {
79                 DbAttribute pkAttr = (DbAttribute) pk.next();
80                 attributes.put(pkAttr.getName(), pkAttr);
81             }
82         } else {
83
84             // match on this FK
85
Iterator JavaDoc joins = rel.getJoins().iterator();
86             while (joins.hasNext()) {
87                 DbJoin join = (DbJoin) joins.next();
88
89                 // index by target name
90
attributes.put(join.getTargetName(), join.getSource());
91             }
92         }
93     }
94
95     public void setDataObject(Persistent obj) {
96         if (obj == null) {
97             values = Collections.EMPTY_MAP;
98             return;
99         }
100         
101         setObjectId(obj.getObjectId());
102     }
103     
104     /**
105      * @since 1.2
106      */

107     public void setObjectId(ObjectId id) {
108         if (id == null) {
109             throw new CayenneRuntimeException(
110                     "Null ObjectId, probably an attempt to use TRANSIENT object as a query parameter.");
111         }
112         else if (id.isTemporary()) {
113             throw new CayenneRuntimeException(
114                     "Temporary id, probably an attempt to use NEW object as a query parameter.");
115         }
116         else {
117             values = id.getIdSnapshot();
118         }
119     }
120
121     public Iterator JavaDoc keys() {
122         if (attributes == null) {
123             throw new IllegalStateException JavaDoc(
124                 "An attempt to use uninitialized DataObjectMatchTranslator: "
125                     + "[attributes: null, values: "
126                     + values
127                     + "]");
128         }
129
130         return attributes.keySet().iterator();
131     }
132     
133     public DbRelationship getRelationship() {
134         return relationship;
135     }
136
137     public DbAttribute getAttribute(String JavaDoc key) {
138         return (DbAttribute) attributes.get(key);
139     }
140
141     public Object JavaDoc getValue(String JavaDoc key) {
142         return values.get(key);
143     }
144
145     public void setOperation(String JavaDoc operation) {
146         this.operation = operation;
147     }
148
149     public String JavaDoc getOperation() {
150         return operation;
151     }
152 }
153
Popular Tags