001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.entity.model;
019:
020: import java.util.*;
021: import org.w3c.dom.*;
022:
023: import org.ofbiz.base.util.*;
024:
025: /**
026: * Generic Entity - Relation model class
027: *
028: */
029: public class ModelRelation extends ModelChild {
030:
031: /** the title, gives a name/description to the relation */
032: protected String title;
033:
034: /** the type: either "one" or "many" or "one-nofk" */
035: protected String type;
036:
037: /** the name of the related entity */
038: protected String relEntityName;
039:
040: /** the name to use for a database foreign key, if applies */
041: protected String fkName;
042:
043: /** keyMaps defining how to lookup the relatedTable using columns from this table */
044: protected List keyMaps = new ArrayList();
045:
046: /** the main entity of this relation */
047: protected ModelEntity mainEntity = null;
048:
049: protected boolean isAutoRelation = false;
050:
051: /** Default Constructor */
052: public ModelRelation() {
053: title = "";
054: type = "";
055: relEntityName = "";
056: fkName = "";
057: }
058:
059: /** Default Constructor */
060: public ModelRelation(String type, String title,
061: String relEntityName, String fkName, List keyMaps) {
062: this .title = title;
063: if (title == null)
064: title = "";
065: this .type = type;
066: this .relEntityName = relEntityName;
067: this .fkName = fkName;
068: this .keyMaps.addAll(keyMaps);
069: }
070:
071: /** XML Constructor */
072: public ModelRelation(ModelEntity mainEntity, Element relationElement) {
073: this .mainEntity = mainEntity;
074:
075: this .type = UtilXml.checkEmpty(relationElement
076: .getAttribute("type"));
077: this .title = UtilXml.checkEmpty(relationElement
078: .getAttribute("title"));
079: this .relEntityName = UtilXml.checkEmpty(relationElement
080: .getAttribute("rel-entity-name"));
081: this .fkName = UtilXml.checkEmpty(relationElement
082: .getAttribute("fk-name"));
083:
084: NodeList keyMapList = relationElement
085: .getElementsByTagName("key-map");
086: for (int i = 0; i < keyMapList.getLength(); i++) {
087: Element keyMapElement = (Element) keyMapList.item(i);
088:
089: if (keyMapElement.getParentNode() == relationElement) {
090: ModelKeyMap keyMap = new ModelKeyMap(keyMapElement);
091:
092: if (keyMap != null) {
093: this .keyMaps.add(keyMap);
094: }
095: }
096: }
097: }
098:
099: /** the title, gives a name/description to the relation */
100: public String getTitle() {
101: if (this .title == null) {
102: this .title = "";
103: }
104: return this .title;
105: }
106:
107: public void setTitle(String title) {
108: if (title == null) {
109: this .title = "";
110: } else {
111: this .title = title;
112: }
113: }
114:
115: /** the type: either "one" or "many" or "one-nofk" */
116: public String getType() {
117: return this .type;
118: }
119:
120: public void setType(String type) {
121: this .type = type;
122: }
123:
124: /** the name of the related entity */
125: public String getRelEntityName() {
126: return this .relEntityName;
127: }
128:
129: public void setRelEntityName(String relEntityName) {
130: this .relEntityName = relEntityName;
131: }
132:
133: public String getFkName() {
134: return this .fkName;
135: }
136:
137: public void setFkName(String fkName) {
138: this .fkName = fkName;
139: }
140:
141: /** @deprecated
142: * the main entity of this relation */
143: public ModelEntity getMainEntity() {
144: return getModelEntity();
145: }
146:
147: /** @deprecated */
148: public void setMainEntity(ModelEntity mainEntity) {
149: setModelEntity(mainEntity);
150: }
151:
152: /** keyMaps defining how to lookup the relatedTable using columns from this table */
153: public Iterator getKeyMapsIterator() {
154: return this .keyMaps.iterator();
155: }
156:
157: public int getKeyMapsSize() {
158: return this .keyMaps.size();
159: }
160:
161: public ModelKeyMap getKeyMap(int index) {
162: return (ModelKeyMap) this .keyMaps.get(index);
163: }
164:
165: public void addKeyMap(ModelKeyMap keyMap) {
166: this .keyMaps.add(keyMap);
167: }
168:
169: public ModelKeyMap removeKeyMap(int index) {
170: return (ModelKeyMap) this .keyMaps.remove(index);
171: }
172:
173: /** Find a KeyMap with the specified fieldName */
174: public ModelKeyMap findKeyMap(String fieldName) {
175: for (int i = 0; i < keyMaps.size(); i++) {
176: ModelKeyMap keyMap = (ModelKeyMap) keyMaps.get(i);
177:
178: if (keyMap.fieldName.equals(fieldName))
179: return keyMap;
180: }
181: return null;
182: }
183:
184: /** Find a KeyMap with the specified relFieldName */
185: public ModelKeyMap findKeyMapByRelated(String relFieldName) {
186: for (int i = 0; i < keyMaps.size(); i++) {
187: ModelKeyMap keyMap = (ModelKeyMap) keyMaps.get(i);
188:
189: if (keyMap.relFieldName.equals(relFieldName))
190: return keyMap;
191: }
192: return null;
193: }
194:
195: public String keyMapString(String separator, String afterLast) {
196: String returnString = "";
197:
198: if (keyMaps.size() < 1) {
199: return "";
200: }
201:
202: int i = 0;
203:
204: for (; i < keyMaps.size() - 1; i++) {
205: returnString = returnString
206: + ((ModelKeyMap) keyMaps.get(i)).fieldName
207: + separator;
208: }
209: returnString = returnString
210: + ((ModelKeyMap) keyMaps.get(i)).fieldName + afterLast;
211: return returnString;
212: }
213:
214: public String keyMapUpperString(String separator, String afterLast) {
215: if (keyMaps.size() < 1)
216: return "";
217:
218: StringBuffer returnString = new StringBuffer(
219: keyMaps.size() * 10);
220: int i = 0;
221: while (true) {
222: ModelKeyMap kmap = (ModelKeyMap) keyMaps.get(i);
223: returnString.append(ModelUtil
224: .upperFirstChar(kmap.fieldName));
225:
226: i++;
227: if (i >= keyMaps.size()) {
228: returnString.append(afterLast);
229: break;
230: }
231:
232: returnString.append(separator);
233: }
234:
235: return returnString.toString();
236: }
237:
238: public String keyMapRelatedUpperString(String separator,
239: String afterLast) {
240: if (keyMaps.size() < 1)
241: return "";
242:
243: StringBuffer returnString = new StringBuffer(
244: keyMaps.size() * 10);
245: int i = 0;
246: while (true) {
247: ModelKeyMap kmap = (ModelKeyMap) keyMaps.get(i);
248: returnString.append(ModelUtil
249: .upperFirstChar(kmap.relFieldName));
250:
251: i++;
252: if (i >= keyMaps.size()) {
253: returnString.append(afterLast);
254: break;
255: }
256:
257: returnString.append(separator);
258: }
259:
260: return returnString.toString();
261: }
262:
263: /**
264: * @return Returns the isAutoRelation.
265: */
266: public boolean isAutoRelation() {
267: return isAutoRelation;
268: }
269:
270: /**
271: * @param isAutoRelation The isAutoRelation to set.
272: */
273: public void setAutoRelation(boolean isAutoRelation) {
274: this .isAutoRelation = isAutoRelation;
275: }
276:
277: public boolean equals(Object other) {
278: ModelRelation otherRel = (ModelRelation) other;
279:
280: if (!otherRel.type.equals(this .type))
281: return false;
282: if (!otherRel.title.equals(this .title))
283: return false;
284: if (!otherRel.relEntityName.equals(this .relEntityName))
285: return false;
286:
287: Set this KeyNames = new HashSet(this .keyMaps);
288: Set otherKeyNames = new HashSet(otherRel.keyMaps);
289: if (!this KeyNames.containsAll(otherKeyNames))
290: return false;
291: if (!otherKeyNames.containsAll(this KeyNames))
292: return false;
293:
294: return true;
295: }
296:
297: public Element toXmlElement(Document document) {
298: Element root = document.createElement("relation");
299: root.setAttribute("type", this .getType());
300: if (UtilValidate.isNotEmpty(this .getTitle())) {
301: root.setAttribute("title", this .getTitle());
302: }
303: root.setAttribute("rel-entity-name", this .getRelEntityName());
304:
305: if (UtilValidate.isNotEmpty(this .getFkName())) {
306: root.setAttribute("fk-name", this .getFkName());
307: }
308:
309: Iterator kmIter = this .getKeyMapsIterator();
310: while (kmIter != null && kmIter.hasNext()) {
311: ModelKeyMap km = (ModelKeyMap) kmIter.next();
312: root.appendChild(km.toXmlElement(document));
313: }
314:
315: return root;
316: }
317: }
|