1 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 38 public class ModelRelation extends ModelChild { 39 40 41 protected String title; 42 43 44 protected String type; 45 46 47 protected String relEntityName; 48 49 50 protected String fkName; 51 52 53 protected List keyMaps = new ArrayList(); 54 55 56 protected ModelEntity mainEntity = null; 57 58 protected boolean isAutoRelation = false; 59 60 61 public ModelRelation() { 62 title = ""; 63 type = ""; 64 relEntityName = ""; 65 fkName = ""; 66 } 67 68 69 public ModelRelation(String type, String title, String relEntityName, String 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 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 102 public String getTitle() { 103 if (this.title == null) { 104 this.title = ""; 105 } 106 return this.title; 107 } 108 109 public void setTitle(String title) { 110 if (title == null) { 111 this.title = ""; 112 } else { 113 this.title = title; 114 } 115 } 116 117 118 public String getType() { 119 return this.type; 120 } 121 122 public void setType(String type) { 123 this.type = type; 124 } 125 126 127 public String getRelEntityName() { 128 return this.relEntityName; 129 } 130 131 public void setRelEntityName(String relEntityName) { 132 this.relEntityName = relEntityName; 133 } 134 135 public String getFkName() { 136 return this.fkName; 137 } 138 139 public void setFkName(String fkName) { 140 this.fkName = fkName; 141 } 142 143 145 public ModelEntity getMainEntity() { 146 return getModelEntity(); 147 } 148 149 150 public void setMainEntity(ModelEntity mainEntity) { 151 setModelEntity(mainEntity); 152 } 153 154 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 176 public ModelKeyMap findKeyMap(String 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 186 public ModelKeyMap findKeyMapByRelated(String 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 keyMapString(String separator, String afterLast) { 197 String 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 keyMapUpperString(String separator, String afterLast) { 213 if (keyMaps.size() < 1) 214 return ""; 215 216 StringBuffer returnString = new StringBuffer ( 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 keyMapRelatedUpperString(String separator, String afterLast) { 235 if (keyMaps.size() < 1) 236 return ""; 237 238 StringBuffer returnString = new StringBuffer ( 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 258 public boolean isAutoRelation() { 259 return isAutoRelation; 260 } 261 264 public void setAutoRelation(boolean isAutoRelation) { 265 this.isAutoRelation = isAutoRelation; 266 } 267 268 public boolean equals(Object 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 |