001: /*
002: * $Id: ModelRelation.java,v 1.3 2004/01/19 19:26:52 jonesde Exp $
003: *
004: * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: */
024: package org.ofbiz.entity.model;
025:
026: import java.util.*;
027: import org.w3c.dom.*;
028:
029: import org.ofbiz.base.util.*;
030:
031: /**
032: * Generic Entity - Relation model class
033: *
034: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
035: * @version $Revision: 1.3 $
036: * @since 2.0
037: */
038: public class ModelRelation {
039:
040: /** the title, gives a name/description to the relation */
041: protected String title;
042:
043: /** the type: either "one" or "many" or "one-nofk" */
044: protected String type;
045:
046: /** the name of the related entity */
047: protected String relEntityName;
048:
049: /** the name to use for a database foreign key, if applies */
050: protected String fkName;
051:
052: /** keyMaps defining how to lookup the relatedTable using columns from this table */
053: protected List keyMaps = new ArrayList();
054:
055: /** the main entity of this relation */
056: protected ModelEntity mainEntity = null;
057:
058: /** Default Constructor */
059: public ModelRelation() {
060: title = "";
061: type = "";
062: relEntityName = "";
063: fkName = "";
064: }
065:
066: /** Default Constructor */
067: public ModelRelation(String type, String title,
068: String relEntityName, String fkName, List keyMaps) {
069: this .title = title;
070: if (title == null)
071: title = "";
072: this .type = type;
073: this .relEntityName = relEntityName;
074: this .fkName = fkName;
075: this .keyMaps.addAll(keyMaps);
076: }
077:
078: /** XML Constructor */
079: public ModelRelation(ModelEntity mainEntity, Element relationElement) {
080: this .mainEntity = mainEntity;
081:
082: this .type = UtilXml.checkEmpty(relationElement
083: .getAttribute("type"));
084: this .title = UtilXml.checkEmpty(relationElement
085: .getAttribute("title"));
086: this .relEntityName = UtilXml.checkEmpty(relationElement
087: .getAttribute("rel-entity-name"));
088: this .fkName = UtilXml.checkEmpty(relationElement
089: .getAttribute("fk-name"));
090:
091: NodeList keyMapList = relationElement
092: .getElementsByTagName("key-map");
093: for (int i = 0; i < keyMapList.getLength(); i++) {
094: Element keyMapElement = (Element) keyMapList.item(i);
095:
096: if (keyMapElement.getParentNode() == relationElement) {
097: ModelKeyMap keyMap = new ModelKeyMap(keyMapElement);
098:
099: if (keyMap != null) {
100: this .keyMaps.add(keyMap);
101: }
102: }
103: }
104: }
105:
106: /** the title, gives a name/description to the relation */
107: public String getTitle() {
108: if (this .title == null) {
109: this .title = "";
110: }
111: return this .title;
112: }
113:
114: public void setTitle(String title) {
115: if (title == null) {
116: this .title = "";
117: } else {
118: this .title = title;
119: }
120: }
121:
122: /** the type: either "one" or "many" or "one-nofk" */
123: public String getType() {
124: return this .type;
125: }
126:
127: public void setType(String type) {
128: this .type = type;
129: }
130:
131: /** the name of the related entity */
132: public String getRelEntityName() {
133: return this .relEntityName;
134: }
135:
136: public void setRelEntityName(String relEntityName) {
137: this .relEntityName = relEntityName;
138: }
139:
140: public String getFkName() {
141: return this .fkName;
142: }
143:
144: public void setFkName(String fkName) {
145: this .fkName = fkName;
146: }
147:
148: /** the main entity of this relation */
149: public ModelEntity getMainEntity() {
150: return this .mainEntity;
151: }
152:
153: public void setMainEntity(ModelEntity mainEntity) {
154: this .mainEntity = mainEntity;
155: }
156:
157: /** keyMaps defining how to lookup the relatedTable using columns from this table */
158: public Iterator getKeyMapsIterator() {
159: return this .keyMaps.iterator();
160: }
161:
162: public int getKeyMapsSize() {
163: return this .keyMaps.size();
164: }
165:
166: public ModelKeyMap getKeyMap(int index) {
167: return (ModelKeyMap) this .keyMaps.get(index);
168: }
169:
170: public void addKeyMap(ModelKeyMap keyMap) {
171: this .keyMaps.add(keyMap);
172: }
173:
174: public ModelKeyMap removeKeyMap(int index) {
175: return (ModelKeyMap) this .keyMaps.remove(index);
176: }
177:
178: /** Find a KeyMap with the specified fieldName */
179: public ModelKeyMap findKeyMap(String fieldName) {
180: for (int i = 0; i < keyMaps.size(); i++) {
181: ModelKeyMap keyMap = (ModelKeyMap) keyMaps.get(i);
182:
183: if (keyMap.fieldName.equals(fieldName))
184: return keyMap;
185: }
186: return null;
187: }
188:
189: /** Find a KeyMap with the specified relFieldName */
190: public ModelKeyMap findKeyMapByRelated(String relFieldName) {
191: for (int i = 0; i < keyMaps.size(); i++) {
192: ModelKeyMap keyMap = (ModelKeyMap) keyMaps.get(i);
193:
194: if (keyMap.relFieldName.equals(relFieldName))
195: return keyMap;
196: }
197: return null;
198: }
199:
200: public String keyMapString(String separator, String afterLast) {
201: String returnString = "";
202:
203: if (keyMaps.size() < 1) {
204: return "";
205: }
206:
207: int i = 0;
208:
209: for (; i < keyMaps.size() - 1; i++) {
210: returnString = returnString
211: + ((ModelKeyMap) keyMaps.get(i)).fieldName
212: + separator;
213: }
214: returnString = returnString
215: + ((ModelKeyMap) keyMaps.get(i)).fieldName + afterLast;
216: return returnString;
217: }
218:
219: public String keyMapUpperString(String separator, String afterLast) {
220: if (keyMaps.size() < 1)
221: return "";
222:
223: StringBuffer returnString = new StringBuffer(
224: keyMaps.size() * 10);
225: int i = 0;
226: while (true) {
227: ModelKeyMap kmap = (ModelKeyMap) keyMaps.get(i);
228: returnString.append(ModelUtil
229: .upperFirstChar(kmap.fieldName));
230:
231: i++;
232: if (i >= keyMaps.size()) {
233: returnString.append(afterLast);
234: break;
235: }
236:
237: returnString.append(separator);
238: }
239:
240: return returnString.toString();
241: }
242:
243: public String keyMapRelatedUpperString(String separator,
244: String afterLast) {
245: if (keyMaps.size() < 1)
246: return "";
247:
248: StringBuffer returnString = new StringBuffer(
249: keyMaps.size() * 10);
250: int i = 0;
251: while (true) {
252: ModelKeyMap kmap = (ModelKeyMap) keyMaps.get(i);
253: returnString.append(ModelUtil
254: .upperFirstChar(kmap.relFieldName));
255:
256: i++;
257: if (i >= keyMaps.size()) {
258: returnString.append(afterLast);
259: break;
260: }
261:
262: returnString.append(separator);
263: }
264:
265: return returnString.toString();
266: }
267: }
|