KickJava   Java API By Example, From Geeks To Geeks.

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


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 package org.apache.cayenne.map;
21
22 import java.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.util.Util;
26 import org.apache.cayenne.util.XMLEncoder;
27 import org.apache.commons.collections.IteratorUtils;
28 import org.apache.commons.lang.StringUtils;
29
30 /**
31  * An ObjAttribute is a mapping descriptor of a Java class property.
32  *
33  * @author Misha Shengaout
34  * @author Andrus Adamchik
35  */

36 public class ObjAttribute extends Attribute {
37
38     protected String JavaDoc type;
39     protected boolean usedForLocking;
40     protected String JavaDoc dbAttributePath;
41
42     public ObjAttribute() {
43     }
44
45     public ObjAttribute(String JavaDoc name) {
46         super(name);
47     }
48
49     public ObjAttribute(String JavaDoc name, String JavaDoc type, ObjEntity entity) {
50         setName(name);
51         setType(type);
52         setEntity(entity);
53     }
54
55     /**
56      * Returns Java class of an object property described by this attribute. Wraps any
57      * thrown exceptions into CayenneRuntimeException.
58      */

59     public Class JavaDoc getJavaClass() {
60         if (this.getType() == null) {
61             return null;
62         }
63
64         try {
65             return Util.getJavaClass(getType());
66         }
67         catch (ClassNotFoundException JavaDoc e) {
68             throw new CayenneRuntimeException("Failed to load class for name '"
69                     + this.getType()
70                     + "': "
71                     + e.getMessage(), e);
72         }
73     }
74
75     /**
76      * Prints itself as XML to the provided XMLEncoder.
77      *
78      * @since 1.1
79      */

80     public void encodeAsXML(XMLEncoder encoder) {
81         encoder.print("<obj-attribute name=\"" + getName() + '\"');
82
83         if (getType() != null) {
84             encoder.print(" type=\"");
85             encoder.print(getType());
86             encoder.print('\"');
87         }
88
89         if (isUsedForLocking()) {
90             encoder.print(" lock=\"true\"");
91         }
92
93         // If this obj attribute is mapped to db attribute
94
if (getDbAttribute() != null) {
95             encoder.print(" db-attribute-path=\"");
96             encoder.print(Util.encodeXmlAttribute(getDbAttributePath()));
97             encoder.print('\"');
98         }
99
100         encoder.println("/>");
101     }
102
103     /**
104      * Returns fully qualified Java class name of the object property represented by this
105      * attribute.
106      */

107     public String JavaDoc getType() {
108         return type;
109     }
110
111     /**
112      * Sets the type of the data object property. Type is expected to be a fully qualified
113      * Java class name.
114      */

115     public void setType(String JavaDoc type) {
116         this.type = type;
117     }
118
119     /**
120      * Returns whether this attribute should be used for locking.
121      *
122      * @since 1.1
123      */

124     public boolean isUsedForLocking() {
125         return usedForLocking;
126     }
127
128     /**
129      * Sets whether this attribute should be used for locking.
130      *
131      * @since 1.1
132      */

133     public void setUsedForLocking(boolean usedForLocking) {
134         this.usedForLocking = usedForLocking;
135     }
136
137     /**
138      * Returns a DbAttribute mapped by this ObjAttribute.
139      */

140     public DbAttribute getDbAttribute() {
141         Iterator JavaDoc pathIterator = getDbPathIterator();
142         Object JavaDoc o = null;
143         while (pathIterator.hasNext()) {
144             o = pathIterator.next();
145         }
146         return (DbAttribute) o;
147     }
148
149     public Iterator JavaDoc getDbPathIterator() {
150         if (dbAttributePath == null) {
151             return IteratorUtils.EMPTY_ITERATOR;
152         }
153
154         ObjEntity ent = (ObjEntity) getEntity();
155         if (ent == null) {
156             return IteratorUtils.EMPTY_ITERATOR;
157         }
158
159         DbEntity dbEnt = ent.getDbEntity();
160         if (dbEnt == null) {
161             return IteratorUtils.EMPTY_ITERATOR;
162         }
163
164         int lastPartStart = dbAttributePath.lastIndexOf('.');
165         if (lastPartStart < 0) {
166             Attribute attribute = dbEnt.getAttribute(dbAttributePath);
167             if (attribute == null) {
168                 return IteratorUtils.EMPTY_ITERATOR;
169             }
170             return IteratorUtils.singletonIterator(attribute);
171         }
172
173         return dbEnt.resolvePathComponents(dbAttributePath);
174     }
175
176     /**
177      * Set mapped DbAttribute.
178      */

179     public void setDbAttribute(DbAttribute dbAttribute) {
180         if (dbAttribute == null) {
181             this.setDbAttributePath(null);
182         }
183         else {
184             this.setDbAttributePath(dbAttribute.getName());
185         }
186     }
187
188     /**
189      * Returns the dbAttributeName.
190      *
191      * @return String
192      */

193     public String JavaDoc getDbAttributeName() {
194         if (dbAttributePath == null)
195             return null;
196         int lastPartStart = dbAttributePath.lastIndexOf('.');
197         String JavaDoc lastPart = StringUtils.substring(
198                 dbAttributePath,
199                 lastPartStart + 1,
200                 dbAttributePath.length());
201         return lastPart;
202     }
203
204     /**
205      * Sets the dbAttributeName.
206      *
207      * @param dbAttributeName The dbAttributeName to set
208      */

209     public void setDbAttributeName(String JavaDoc dbAttributeName) {
210         if (dbAttributePath == null || dbAttributeName == null) {
211             dbAttributePath = dbAttributeName;
212             return;
213         }
214         int lastPartStart = dbAttributePath.lastIndexOf('.');
215         String JavaDoc newPath = (lastPartStart > 0
216                 ? StringUtils.chomp(dbAttributePath, ".")
217                 : "");
218         newPath += (newPath.length() > 0 ? "." : "") + dbAttributeName;
219         this.dbAttributePath = newPath;
220     }
221
222     public void setDbAttributePath(String JavaDoc dbAttributePath) {
223         this.dbAttributePath = dbAttributePath;
224     }
225
226     public String JavaDoc getDbAttributePath() {
227         return dbAttributePath;
228     }
229
230     public boolean isCompound() {
231         return (dbAttributePath != null && dbAttributePath.indexOf('.') >= 0);
232     }
233
234     /**
235      * Returns an ObjAttribute stripped of any server-side information, such as
236      * DbAttribute mapping.
237      *
238      * @since 1.2
239      */

240     public ObjAttribute getClientAttribute() {
241         ClientObjAttribute attribute = new ClientObjAttribute(getName());
242         attribute.setType(getType());
243         
244         DbAttribute dbAttribute = getDbAttribute();
245         if (dbAttribute != null) {
246             attribute.setMandatory(dbAttribute.isMandatory());
247             attribute.setMaxLength(dbAttribute.getMaxLength());
248         }
249
250         // TODO: will likely need "userForLocking" property as well.
251

252         return attribute;
253     }
254 }
255
Popular Tags