KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > model > ModelRelation


1 /*
2  * $Id: ModelRelation.java 6327 2005-12-14 18:56:54Z jaz $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.entity.model;
25
26 import java.util.*;
27 import org.w3c.dom.*;
28
29 import org.ofbiz.base.util.*;
30
31 /**
32  * Generic Entity - Relation model class
33  *
34  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
35  * @version $Rev: 6327 $
36  * @since 2.0
37  */

38 public class ModelRelation extends ModelChild {
39
40     /** the title, gives a name/description to the relation */
41     protected String JavaDoc title;
42
43     /** the type: either "one" or "many" or "one-nofk" */
44     protected String JavaDoc type;
45
46     /** the name of the related entity */
47     protected String JavaDoc relEntityName;
48
49     /** the name to use for a database foreign key, if applies */
50     protected String JavaDoc fkName;
51
52     /** keyMaps defining how to lookup the relatedTable using columns from this table */
53     protected List keyMaps = new ArrayList();
54
55     /** the main entity of this relation */
56     protected ModelEntity mainEntity = null;
57     
58     protected boolean isAutoRelation = false;
59
60     /** Default Constructor */
61     public ModelRelation() {
62         title = "";
63         type = "";
64         relEntityName = "";
65         fkName = "";
66     }
67
68     /** Default Constructor */
69     public ModelRelation(String JavaDoc type, String JavaDoc title, String JavaDoc relEntityName, String JavaDoc fkName, List keyMaps) {
70         this.title = title;
71         if (title == null) title = "";
72         this.type = type;
73         this.relEntityName = relEntityName;
74         this.fkName = fkName;
75         this.keyMaps.addAll(keyMaps);
76     }
77
78     /** XML Constructor */
79     public ModelRelation(ModelEntity mainEntity, Element relationElement) {
80         this.mainEntity = mainEntity;
81
82         this.type = UtilXml.checkEmpty(relationElement.getAttribute("type"));
83         this.title = UtilXml.checkEmpty(relationElement.getAttribute("title"));
84         this.relEntityName = UtilXml.checkEmpty(relationElement.getAttribute("rel-entity-name"));
85         this.fkName = UtilXml.checkEmpty(relationElement.getAttribute("fk-name"));
86
87         NodeList keyMapList = relationElement.getElementsByTagName("key-map");
88         for (int i = 0; i < keyMapList.getLength(); i++) {
89             Element keyMapElement = (Element) keyMapList.item(i);
90
91             if (keyMapElement.getParentNode() == relationElement) {
92                 ModelKeyMap keyMap = new ModelKeyMap(keyMapElement);
93
94                 if (keyMap != null) {
95                     this.keyMaps.add(keyMap);
96                 }
97             }
98         }
99     }
100
101     /** the title, gives a name/description to the relation */
102     public String JavaDoc getTitle() {
103         if (this.title == null) {
104             this.title = "";
105         }
106         return this.title;
107     }
108
109     public void setTitle(String JavaDoc title) {
110         if (title == null) {
111             this.title = "";
112         } else {
113             this.title = title;
114         }
115     }
116
117     /** the type: either "one" or "many" or "one-nofk" */
118     public String JavaDoc getType() {
119         return this.type;
120     }
121
122     public void setType(String JavaDoc type) {
123         this.type = type;
124     }
125
126     /** the name of the related entity */
127     public String JavaDoc getRelEntityName() {
128         return this.relEntityName;
129     }
130
131     public void setRelEntityName(String JavaDoc relEntityName) {
132         this.relEntityName = relEntityName;
133     }
134
135     public String JavaDoc getFkName() {
136         return this.fkName;
137     }
138
139     public void setFkName(String JavaDoc fkName) {
140         this.fkName = fkName;
141     }
142
143     /** @deprecated
144       * the main entity of this relation */

145     public ModelEntity getMainEntity() {
146         return getModelEntity();
147     }
148
149     /** @deprecated */
150     public void setMainEntity(ModelEntity mainEntity) {
151         setModelEntity(mainEntity);
152     }
153
154     /** keyMaps defining how to lookup the relatedTable using columns from this table */
155     public Iterator getKeyMapsIterator() {
156         return this.keyMaps.iterator();
157     }
158
159     public int getKeyMapsSize() {
160         return this.keyMaps.size();
161     }
162
163     public ModelKeyMap getKeyMap(int index) {
164         return (ModelKeyMap) this.keyMaps.get(index);
165     }
166
167     public void addKeyMap(ModelKeyMap keyMap) {
168         this.keyMaps.add(keyMap);
169     }
170
171     public ModelKeyMap removeKeyMap(int index) {
172         return (ModelKeyMap) this.keyMaps.remove(index);
173     }
174
175     /** Find a KeyMap with the specified fieldName */
176     public ModelKeyMap findKeyMap(String JavaDoc fieldName) {
177         for (int i = 0; i < keyMaps.size(); i++) {
178             ModelKeyMap keyMap = (ModelKeyMap) keyMaps.get(i);
179
180             if (keyMap.fieldName.equals(fieldName)) return keyMap;
181         }
182         return null;
183     }
184
185     /** Find a KeyMap with the specified relFieldName */
186     public ModelKeyMap findKeyMapByRelated(String JavaDoc relFieldName) {
187         for (int i = 0; i < keyMaps.size(); i++) {
188             ModelKeyMap keyMap = (ModelKeyMap) keyMaps.get(i);
189
190             if (keyMap.relFieldName.equals(relFieldName))
191                 return keyMap;
192         }
193         return null;
194     }
195
196     public String JavaDoc keyMapString(String JavaDoc separator, String JavaDoc afterLast) {
197         String JavaDoc returnString = "";
198
199         if (keyMaps.size() < 1) {
200             return "";
201         }
202
203         int i = 0;
204
205         for (; i < keyMaps.size() - 1; i++) {
206             returnString = returnString + ((ModelKeyMap) keyMaps.get(i)).fieldName + separator;
207         }
208         returnString = returnString + ((ModelKeyMap) keyMaps.get(i)).fieldName + afterLast;
209         return returnString;
210     }
211
212     public String JavaDoc keyMapUpperString(String JavaDoc separator, String JavaDoc afterLast) {
213         if (keyMaps.size() < 1)
214             return "";
215
216         StringBuffer JavaDoc returnString = new StringBuffer JavaDoc( keyMaps.size() * 10 );
217         int i=0;
218         while (true) {
219             ModelKeyMap kmap = (ModelKeyMap) keyMaps.get(i);
220             returnString.append( ModelUtil.upperFirstChar( kmap.fieldName));
221
222             i++;
223             if (i >= keyMaps.size()) {
224                 returnString.append( afterLast );
225                 break;
226             }
227
228             returnString.append( separator );
229         }
230
231         return returnString.toString();
232     }
233
234     public String JavaDoc keyMapRelatedUpperString(String JavaDoc separator, String JavaDoc afterLast) {
235         if (keyMaps.size() < 1)
236             return "";
237
238         StringBuffer JavaDoc returnString = new StringBuffer JavaDoc( keyMaps.size() * 10 );
239         int i=0;
240         while (true) {
241             ModelKeyMap kmap = (ModelKeyMap) keyMaps.get(i);
242             returnString.append( ModelUtil.upperFirstChar( kmap.relFieldName ));
243
244             i++;
245             if (i >= keyMaps.size()) {
246                 returnString.append( afterLast );
247                 break;
248             }
249
250             returnString.append( separator );
251         }
252
253         return returnString.toString();
254     }
255     /**
256      * @return Returns the isAutoRelation.
257      */

258     public boolean isAutoRelation() {
259         return isAutoRelation;
260     }
261     /**
262      * @param isAutoRelation The isAutoRelation to set.
263      */

264     public void setAutoRelation(boolean isAutoRelation) {
265         this.isAutoRelation = isAutoRelation;
266     }
267     
268     public boolean equals(Object JavaDoc other) {
269         ModelRelation otherRel = (ModelRelation) other;
270         
271         if (!otherRel.type.equals(this.type)) return false;
272         if (!otherRel.title.equals(this.title)) return false;
273         if (!otherRel.relEntityName.equals(this.relEntityName)) return false;
274         
275         Set thisKeyNames = new HashSet(this.keyMaps);
276         Set otherKeyNames = new HashSet(otherRel.keyMaps);
277         if (!thisKeyNames.containsAll(otherKeyNames)) return false;
278         if (!otherKeyNames.containsAll(thisKeyNames)) return false;
279         
280         return true;
281     }
282
283     public Element toXmlElement(Document document) {
284         Element root = document.createElement("relation");
285         root.setAttribute("type", this.getType());
286         if (UtilValidate.isNotEmpty(this.getTitle())) {
287             root.setAttribute("title", this.getTitle());
288         }
289         root.setAttribute("rel-entity-name", this.getRelEntityName());
290
291         if (UtilValidate.isNotEmpty(this.getFkName())) {
292             root.setAttribute("fk-name", this.getFkName());
293         }
294
295         Iterator kmIter = this.getKeyMapsIterator();
296         while (kmIter != null && kmIter.hasNext()) {
297             ModelKeyMap km = (ModelKeyMap) kmIter.next();
298             root.appendChild(km.toXmlElement(document));
299         }
300
301         return root;
302     }
303 }
304
Popular Tags