KickJava   Java API By Example, From Geeks To Geeks.

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


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56
57 package org.objectstyle.cayenne.map;
58
59 import java.util.Iterator JavaDoc;
60
61 import org.apache.commons.collections.IteratorUtils;
62 import org.apache.commons.lang.StringUtils;
63 import org.objectstyle.cayenne.CayenneException;
64 import org.objectstyle.cayenne.util.Util;
65 import org.objectstyle.cayenne.util.XMLEncoder;
66
67 /**
68  * An ObjAttribute is a mapping descriptor of a Java class property.
69  *
70  * @author Misha Shengaout
71  * @author Andrei Adamchik
72  */

73 public class ObjAttribute extends Attribute {
74     protected String JavaDoc type;
75     protected boolean usedForLocking;
76     private String JavaDoc dbAttributePath;
77
78     public ObjAttribute() {
79     }
80
81     public ObjAttribute(String JavaDoc name) {
82         super(name);
83     }
84
85     public ObjAttribute(String JavaDoc name, String JavaDoc type, ObjEntity entity) {
86         setName(name);
87         setType(type);
88         setEntity(entity);
89     }
90
91     /**
92      * Prints itself as XML to the provided XMLEncoder.
93      *
94      * @since 1.1
95      */

96     public void encodeAsXML(XMLEncoder encoder) {
97         encoder.print("<obj-attribute name=\"" + getName() + '\"');
98
99         if (getType() != null) {
100             encoder.print(" type=\"");
101             encoder.print(getType());
102             encoder.print('\"');
103         }
104         
105         if(isUsedForLocking()) {
106             encoder.print(" lock=\"true\"");
107         }
108
109         // If this obj attribute is mapped to db attribute
110
if (getDbAttribute() != null) {
111             encoder.print(" db-attribute-path=\"");
112             encoder.print(Util.encodeXmlAttribute(getDbAttributePath()));
113             encoder.print('\"');
114         }
115
116         encoder.println("/>");
117     }
118
119     /**
120      * Returns fully qualified Java class name of the data object property represented
121      * by this attribute.
122      */

123     public String JavaDoc getType() {
124         return type;
125     }
126
127     /**
128      * Sets the type of the data object property.
129      */

130     public void setType(String JavaDoc type) {
131         this.type = type;
132     }
133
134     /**
135      * Returns whether this attribute should be used for locking.
136      *
137      * @since 1.1
138      */

139     public boolean isUsedForLocking() {
140         return usedForLocking;
141     }
142
143     /**
144      * Sets whether this attribute should be used for locking.
145      *
146      * @since 1.1
147      */

148     public void setUsedForLocking(boolean usedForLocking) {
149         this.usedForLocking = usedForLocking;
150     }
151
152     /**
153      * Returns a DbAttribute mapped by this ObjAttribute.
154      */

155     public DbAttribute getDbAttribute() {
156         Iterator JavaDoc pathIterator = getDbPathIterator();
157         Object JavaDoc o = null;
158         while (pathIterator.hasNext()) {
159             o = pathIterator.next();
160         }
161         return (DbAttribute) o;
162     }
163
164     public Iterator JavaDoc getDbPathIterator() {
165         if (dbAttributePath == null) {
166             return IteratorUtils.EMPTY_ITERATOR;
167         }
168
169         ObjEntity ent = (ObjEntity) getEntity();
170         if (ent == null) {
171             return IteratorUtils.EMPTY_ITERATOR;
172         }
173
174         DbEntity dbEnt = ent.getDbEntity();
175         if (dbEnt == null) {
176             return IteratorUtils.EMPTY_ITERATOR;
177         }
178
179         int lastPartStart = dbAttributePath.lastIndexOf('.');
180         if (lastPartStart < 0) {
181             Attribute attribute = dbEnt.getAttribute(dbAttributePath);
182             if (attribute == null) {
183                 return IteratorUtils.EMPTY_ITERATOR;
184             }
185             return IteratorUtils.singletonIterator(attribute);
186         }
187
188         return dbEnt.resolvePathComponents(dbAttributePath);
189     }
190
191     /**
192      * Set mapped DbAttribute.
193      */

194     public void setDbAttribute(DbAttribute dbAttribute) {
195         if (dbAttribute == null) {
196             this.setDbAttributePath(null);
197         }
198         else {
199             this.setDbAttributePath(dbAttribute.getName());
200         }
201     }
202
203     /**
204      * Returns the dbAttributeName.
205      * @return String
206      */

207     public String JavaDoc getDbAttributeName() {
208         if (dbAttributePath == null)
209             return null;
210         int lastPartStart = dbAttributePath.lastIndexOf('.');
211         String JavaDoc lastPart =
212             StringUtils.substring(
213                 dbAttributePath,
214                 lastPartStart + 1,
215                 dbAttributePath.length());
216         return lastPart;
217     }
218
219     /**
220      * Sets the dbAttributeName.
221      * @param dbAttributeName The dbAttributeName to set
222      */

223     public void setDbAttributeName(String JavaDoc dbAttributeName) {
224         if (dbAttributePath == null || dbAttributeName == null) {
225             dbAttributePath = dbAttributeName;
226             return;
227         }
228         int lastPartStart = dbAttributePath.lastIndexOf('.');
229         String JavaDoc newPath =
230             (lastPartStart > 0 ? StringUtils.chomp(dbAttributePath, ".") : "");
231         newPath += (newPath.length() > 0 ? "." : "") + dbAttributeName;
232         this.dbAttributePath = newPath;
233     }
234
235     public void setDbAttributePath(String JavaDoc dbAttributePath) {
236         this.dbAttributePath = dbAttributePath;
237     }
238
239     public String JavaDoc getDbAttributePath() {
240         return dbAttributePath;
241     }
242
243     public boolean isCompound() {
244         return (dbAttributePath != null && dbAttributePath.indexOf('.') >= 0);
245     }
246
247     public boolean mapsToDependentDbEntity() {
248         Iterator JavaDoc i = getDbPathIterator();
249         if (!i.hasNext())
250             return false;
251         Object JavaDoc o = i.next();
252         if (!i.hasNext())
253             return false;
254         Object JavaDoc o1 = i.next();
255         if (!(o1 instanceof DbAttribute))
256             return false;
257         DbRelationship toDependent = (DbRelationship) o;
258         return toDependent.isToDependentPK();
259     }
260
261     public void validate() throws CayenneException {
262         String JavaDoc head = "ObjAttribute: " + getName() + " ";
263         ObjEntity ent = (ObjEntity) getEntity();
264         if (ent == null) {
265             throw new CayenneException(head + "Parent ObjEntity not defined.");
266         }
267         head += "ObjEntity: " + ent.getName() + " ";
268
269         if (getName() == null)
270             throw new CayenneException(head + "ObjAttribute's name not defined.");
271
272         if (getDbAttributePath() == null)
273             throw new CayenneException(head + "dbAttributePath not defined.");
274
275         try {
276             Iterator JavaDoc i = getDbPathIterator();
277             boolean dbAttributeFound = false;
278             while (i.hasNext()) {
279                 Object JavaDoc pathPart = i.next();
280                 if (pathPart instanceof DbRelationship) {
281                     DbRelationship r = (DbRelationship) pathPart;
282                     if (r.isToMany())
283                         throw new CayenneException(
284                             head + "DbRelationship: " + r.getName() + " is to-many.");
285                 }
286                 else if (pathPart instanceof DbAttribute) {
287                     dbAttributeFound = true;
288                 }
289             }
290             if (!dbAttributeFound)
291                 throw new CayenneException(head + "DbAttribute not found.");
292         }
293         catch (CayenneException ex) {
294             throw ex;
295         }
296         catch (Exception JavaDoc ex) {
297             throw new CayenneException(head + ex.getMessage(), ex);
298         }
299     }
300 }
301
Popular Tags