KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > map > EmbeddedAttribute


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 package org.apache.cayenne.map;
20
21 import java.util.ArrayList JavaDoc;
22 import java.util.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.Map JavaDoc;
26 import java.util.SortedMap JavaDoc;
27 import java.util.TreeMap JavaDoc;
28
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.util.Util;
31 import org.apache.cayenne.util.XMLEncoder;
32
33 /**
34  * An attribute of the ObjEntity that maps to an embeddable class.
35  *
36  * @since 3.0
37  * @author Andrus Adamchik
38  */

39 public class EmbeddedAttribute extends Attribute {
40
41     protected String JavaDoc type;
42     protected SortedMap JavaDoc attributeOverrides;
43
44     public EmbeddedAttribute() {
45         attributeOverrides = new TreeMap JavaDoc();
46     }
47
48     public EmbeddedAttribute(String JavaDoc name) {
49         this();
50         setName(name);
51     }
52
53     public EmbeddedAttribute(String JavaDoc name, String JavaDoc type, ObjEntity entity) {
54         this();
55         setName(name);
56         setType(type);
57         setEntity(entity);
58     }
59
60     public void encodeAsXML(XMLEncoder encoder) {
61         encoder.print("<embedded-attribute name=\"" + getName() + '\"');
62         if (getType() != null) {
63             encoder.print(" type=\"");
64             encoder.print(getType());
65             encoder.print('\"');
66         }
67
68         if (attributeOverrides.isEmpty()) {
69             encoder.println("/>");
70             return;
71         }
72
73         encoder.println('>');
74
75         encoder.indent(1);
76         
77         
78         
79         Iterator JavaDoc it = attributeOverrides.entrySet().iterator();
80         while (it.hasNext()) {
81             Map.Entry JavaDoc e = (Map.Entry JavaDoc) it.next();
82             encoder.print("<embeddable-attribute-override name=\"");
83             encoder.print(e.getKey().toString());
84             encoder.print("\" db-attribute-path=\"");
85             encoder.print(e.getValue().toString());
86             encoder.println("\"/>");
87         }
88
89         encoder.indent(-1);
90         encoder.println("</embedded-attribute>");
91     }
92
93     public Map JavaDoc getAttributeOverrides() {
94         return Collections.unmodifiableMap(attributeOverrides);
95     }
96
97     public Embeddable getEmbeddable() {
98         if (type == null) {
99             return null;
100         }
101
102         return getNonNullNamespace().getEmbeddable(type);
103     }
104
105     private ObjAttribute makeObjAttribute(EmbeddableAttribute embeddableAttribute) {
106         String JavaDoc dbPath = (String JavaDoc) attributeOverrides.get(embeddableAttribute.getName());
107         if (dbPath == null) {
108             dbPath = embeddableAttribute.getDbAttributeName();
109         }
110
111         return makeObjAttribute(embeddableAttribute, dbPath);
112     }
113
114     private ObjAttribute makeObjAttribute(
115             EmbeddableAttribute embeddableAttribute,
116             String JavaDoc dbPath) {
117         String JavaDoc fullName = getName() + "." + embeddableAttribute.getName();
118
119         ObjAttribute oa = new ObjAttribute(
120                 fullName,
121                 embeddableAttribute.getType(),
122                 (ObjEntity) getEntity());
123         oa.setDbAttributeName(dbPath);
124         return oa;
125     }
126
127     /**
128      * Returns an ObjAttribute that maps to a given {@link DbAttribute}, or returns null
129      * if no such attribute exists.
130      */

131     public ObjAttribute getAttributeForDbPath(String JavaDoc dbPath) {
132
133         Embeddable e = getEmbeddable();
134         if (e == null) {
135             return null;
136         }
137
138         EmbeddableAttribute ea = null;
139
140         Iterator JavaDoc overrides = attributeOverrides.entrySet().iterator();
141         while (overrides.hasNext()) {
142             Map.Entry JavaDoc override = (Map.Entry JavaDoc) overrides.next();
143             if (dbPath.equals(override.getValue())) {
144                 ea = e.getAttribute(override.getKey().toString());
145                 break;
146             }
147         }
148
149         if (ea == null) {
150             ea = e.getAttributeForDbPath(dbPath);
151         }
152
153         if (ea != null) {
154             return makeObjAttribute(ea, dbPath);
155         }
156
157         return null;
158     }
159
160     /**
161      * Returns an ObjAttribute for a given name, taking into account column name
162      * overrides.
163      */

164     public ObjAttribute getAttribute(String JavaDoc name) {
165         Embeddable e = getEmbeddable();
166         if (e == null) {
167             return null;
168         }
169
170         EmbeddableAttribute ea = e.getAttribute(name);
171         if (ea == null) {
172             return null;
173         }
174
175         return makeObjAttribute(ea);
176     }
177
178     /**
179      * Returns a Collection of ObjAttributes of an embedded object taking into account
180      * column name overrides.
181      */

182     public Collection JavaDoc getAttributes() {
183         Embeddable e = getEmbeddable();
184         if (e == null) {
185             return Collections.EMPTY_LIST;
186         }
187
188         Collection JavaDoc embeddableAttributes = e.getAttributes();
189         Collection JavaDoc objectAttributes = new ArrayList JavaDoc(embeddableAttributes.size());
190         Iterator JavaDoc it = embeddableAttributes.iterator();
191         while (it.hasNext()) {
192             EmbeddableAttribute ea = (EmbeddableAttribute) it.next();
193             objectAttributes.add(makeObjAttribute(ea));
194         }
195
196         return objectAttributes;
197     }
198
199     public void addAttributeOverride(String JavaDoc name, String JavaDoc dbAttributeName) {
200         attributeOverrides.put(name, dbAttributeName);
201     }
202
203     public void removeAttributeOverride(String JavaDoc name) {
204         attributeOverrides.remove(name);
205     }
206
207     /**
208      * Returns a type of this attribute that must be an {@link Embeddable} object.
209      */

210     public String JavaDoc getType() {
211         return type;
212     }
213
214     /**
215      * Returns Java class of an object property described by this attribute. Wraps any
216      * thrown exceptions into CayenneRuntimeException.
217      */

218     public Class JavaDoc getJavaClass() {
219         if (this.getType() == null) {
220             return null;
221         }
222
223         try {
224             return Util.getJavaClass(getType());
225         }
226         catch (ClassNotFoundException JavaDoc e) {
227             throw new CayenneRuntimeException("Failed to load class for name '"
228                     + this.getType()
229                     + "': "
230                     + e.getMessage(), e);
231         }
232     }
233
234     /**
235      * Sets a type of this attribute that must be an {@link Embeddable} object.
236      */

237     public void setType(String JavaDoc type) {
238         this.type = type;
239     }
240
241     /**
242      * Returns guaranteed non-null MappingNamespace of this relationship. If it happens to
243      * be null, and exception is thrown. This method is intended for internal use by
244      * Relationship class.
245      */

246     final MappingNamespace getNonNullNamespace() {
247
248         if (entity == null) {
249             throw new CayenneRuntimeException("Embedded attribute '"
250                     + getName()
251                     + "' has no parent Entity.");
252         }
253
254         return entity.getNonNullNamespace();
255     }
256 }
257
Popular Tags