1 24 25 package org.ofbiz.entity; 26 27 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javolution.lang.Reusable; 33 import javolution.realtime.ObjectFactory; 34 import javolution.util.FastMap; 35 36 import org.ofbiz.base.util.Debug; 37 import org.ofbiz.base.util.UtilMisc; 38 import org.ofbiz.base.util.UtilValidate; 39 import org.ofbiz.entity.model.ModelEntity; 40 import org.ofbiz.entity.model.ModelKeyMap; 41 import org.ofbiz.entity.model.ModelRelation; 42 import org.ofbiz.entity.util.EntityUtil; 43 44 45 53 public class GenericValue extends GenericEntity implements Reusable { 54 55 public static final GenericValue NULL_VALUE = new NullGenericValue(); 56 57 protected static final ObjectFactory genericValueFactory = new ObjectFactory() { 58 protected Object create() { 59 return new GenericValue(); 60 } 61 }; 62 63 64 public transient Map relatedCache = null; 65 66 67 public transient Map relatedOneCache = null; 68 69 73 protected Map originalDbValues = null; 74 75 protected GenericValue() { } 76 77 78 public static GenericValue create(ModelEntity modelEntity) { 79 GenericValue newValue = (GenericValue) genericValueFactory.object(); 80 newValue.init(modelEntity); 81 return newValue; 82 } 83 84 85 public static GenericValue create(ModelEntity modelEntity, Map fields) { 86 GenericValue newValue = (GenericValue) genericValueFactory.object(); 87 newValue.init(modelEntity, fields); 88 return newValue; 89 } 90 91 92 public static GenericValue create(GenericValue value) { 93 GenericValue newValue = (GenericValue) genericValueFactory.object(); 94 newValue.init(value); 95 return newValue; 96 } 97 98 99 public static GenericValue create(GenericPK primaryKey) { 100 GenericValue newValue = (GenericValue) genericValueFactory.object(); 101 newValue.init(primaryKey); 102 return newValue; 103 } 104 105 public void reset() { 106 super.reset(); 108 109 this.relatedCache = null; 111 this.relatedOneCache = null; 112 this.originalDbValues = null; 113 } 114 115 public void synchronizedWithDatasource() { 116 super.synchronizedWithDatasource(); 117 this.copyOriginalDbValues(); 118 } 119 120 public GenericValue create() throws GenericEntityException { 121 return this.getDelegator().create(this); 122 } 123 124 public void store() throws GenericEntityException { 125 this.getDelegator().store(this); 126 } 127 128 public void remove() throws GenericEntityException { 129 this.getDelegator().removeValue(this); 130 } 131 132 public void refresh() throws GenericEntityException { 133 this.getDelegator().refresh(this); 134 } 135 136 public void refreshFromCache() throws GenericEntityException { 137 this.getDelegator().refreshFromCache(this); 138 } 139 140 public boolean originalDbValuesAvailable() { 141 return this.originalDbValues != null ? true : false; 142 } 143 144 public Object getOriginalDbValue(String name) { 145 if (getModelEntity().getField(name) == null) { 146 throw new IllegalArgumentException ("[GenericEntity.get] \"" + name + "\" is not a field of " + entityName); 147 } 148 if (originalDbValues == null) return null; 149 return originalDbValues.get(name); 150 } 151 152 156 public void copyOriginalDbValues() { 157 this.originalDbValues = FastMap.newInstance(); 158 this.originalDbValues.putAll(this.fields); 159 } 160 161 165 public List getRelated(String relationName) throws GenericEntityException { 166 return this.getDelegator().getRelated(relationName, this); 167 } 168 169 175 public List getRelated(String relationName, List orderBy) throws GenericEntityException { 176 return this.getDelegator().getRelated(relationName, null, orderBy, this); 177 } 178 179 186 public List getRelated(String relationName, Map byAndFields, List orderBy) throws GenericEntityException { 187 return this.getDelegator().getRelated(relationName, byAndFields, orderBy, this); 188 } 189 190 195 public List getRelatedCache(String relationName) throws GenericEntityException { 196 return this.getDelegator().getRelatedCache(relationName, this); 197 } 198 199 210 public List getRelatedMulti(String relationNameOne, String relationNameTwo, List orderBy) throws GenericEntityException { 211 return this.getDelegator().getMultiRelation(this, relationNameOne, relationNameTwo, orderBy); 212 } 213 214 223 public List getRelatedMulti(String relationNameOne, String relationNameTwo) throws GenericEntityException { 224 return this.getDelegator().getMultiRelation(this, relationNameOne, relationNameTwo, null); 225 } 226 227 235 public List getRelatedCache(String relationName, Map byAndFields, List orderBy) throws GenericEntityException { 236 List col = getRelatedCache(relationName); 237 238 if (byAndFields != null) col = EntityUtil.filterByAnd(col, byAndFields); 239 if (UtilValidate.isNotEmpty(orderBy)) col = EntityUtil.orderBy(col, orderBy); 240 return col; 241 } 242 243 250 public List getRelatedCache(String relationName, List orderBy) throws GenericEntityException { 251 return this.getRelatedCache(relationName, null, orderBy); 252 } 253 254 260 public List getRelatedEmbeddedCache(String relationName) throws GenericEntityException { 261 if (relatedCache == null) relatedCache = FastMap.newInstance(); 262 List col = (List ) relatedCache.get(relationName); 263 264 if (col == null) { 265 col = getRelated(relationName); 266 relatedCache.put(relationName, col); 267 } 268 return col; 269 } 270 271 280 public List getRelatedEmbeddedCache(String relationName, Map byAndFields, List orderBy) throws GenericEntityException { 281 List col = getRelatedEmbeddedCache(relationName); 282 283 if (byAndFields != null) col = EntityUtil.filterByAnd(col, byAndFields); 284 if (UtilValidate.isNotEmpty(orderBy)) col = EntityUtil.orderBy(col, orderBy); 285 return col; 286 } 287 288 public void removeRelatedEmbeddedCache(String relationName) { 289 if (relatedCache == null) return; 290 relatedCache.remove(relationName); 291 } 292 293 public void storeRelatedEmbeddedCache(String relationName, List col) { 294 if (relatedCache == null) relatedCache = FastMap.newInstance(); 295 relatedCache.put(relationName, col); 296 } 297 298 public void storeRelatedEmbeddedCache(String relationName, GenericValue value) { 299 if (relatedCache == null) relatedCache = FastMap.newInstance(); 300 relatedCache.put(relationName, UtilMisc.toList(value)); 301 } 302 303 public void clearEmbeddedCache() { 304 relatedCache.clear(); 305 } 306 307 311 public GenericValue getRelatedOne(String relationName) throws GenericEntityException { 312 return this.getDelegator().getRelatedOne(relationName, this); 313 } 314 315 320 public GenericValue getRelatedOneCache(String relationName) throws GenericEntityException { 321 return this.getDelegator().getRelatedOneCache(relationName, this); 322 } 323 324 330 public GenericValue getRelatedOneEmbeddedCache(String relationName) throws GenericEntityException { 331 if (relatedOneCache == null) relatedOneCache = FastMap.newInstance(); 332 GenericValue value = (GenericValue) relatedOneCache.get(relationName); 333 334 if (value == null) { 335 value = getRelatedOne(relationName); 336 if (value != null) relatedOneCache.put(relationName, value); 337 } 338 return value; 339 } 340 341 346 public List getRelatedByAnd(String relationName, Map fields) throws GenericEntityException { 347 return this.getDelegator().getRelatedByAnd(relationName, fields, this); 348 } 349 350 356 public List getRelatedByAndCache(String relationName, Map fields) throws GenericEntityException { 357 return EntityUtil.filterByAnd(this.getDelegator().getRelatedCache(relationName, this), fields); 358 } 359 360 367 public List getRelatedByAndEmbeddedCache(String relationName, Map fields) throws GenericEntityException { 368 return EntityUtil.filterByAnd(getRelatedEmbeddedCache(relationName), fields); 369 } 370 371 376 public List getRelatedOrderBy(String relationName, List orderBy) throws GenericEntityException { 377 return this.getDelegator().getRelatedOrderBy(relationName, orderBy, this); 378 } 379 380 386 public List getRelatedOrderByCache(String relationName, List orderBy) throws GenericEntityException { 387 return EntityUtil.orderBy(this.getDelegator().getRelatedCache(relationName, this), orderBy); 388 } 389 390 397 public List getRelatedOrderByEmbeddedCache(String relationName, List orderBy) throws GenericEntityException { 398 return EntityUtil.orderBy(getRelatedEmbeddedCache(relationName), orderBy); 399 } 400 401 404 public void removeRelated(String relationName) throws GenericEntityException { 405 this.getDelegator().removeRelated(relationName, this); 406 } 407 408 414 public GenericPK getRelatedDummyPK(String relationName) throws GenericEntityException { 415 return this.getDelegator().getRelatedDummyPK(relationName, null, this); 416 } 417 418 425 public GenericPK getRelatedDummyPK(String relationName, Map byAndFields) throws GenericEntityException { 426 return this.getDelegator().getRelatedDummyPK(relationName, byAndFields, this); 427 } 428 429 437 public boolean checkFks(boolean insertDummy) throws GenericEntityException { 438 ModelEntity model = this.getModelEntity(); 439 Iterator relItr = model.getRelationsIterator(); 440 while (relItr.hasNext()) { 441 ModelRelation relation = (ModelRelation) relItr.next(); 442 if ("one".equalsIgnoreCase(relation.getType())) { 443 Map fields = FastMap.newInstance(); 445 for (int i = 0; i < relation.getKeyMapsSize(); i++) { 446 ModelKeyMap keyMap = relation.getKeyMap(i); 447 fields.put(keyMap.getRelFieldName(), this.get(keyMap.getFieldName())); 448 } 449 long count = this.getDelegator().findCountByAnd(relation.getRelEntityName(), fields); 450 if (count == 0) { 451 if (insertDummy) { 452 GenericValue newValue = this.getDelegator().makeValue(relation.getRelEntityName(), null); 454 Iterator keyMapIter = relation.getKeyMapsIterator(); 455 boolean allFieldsSet = true; 456 while (keyMapIter.hasNext()) { 457 ModelKeyMap mkm = (ModelKeyMap) keyMapIter.next(); 458 if (this.get(mkm.getFieldName()) != null) { 459 newValue.set(mkm.getRelFieldName(), this.get(mkm.getFieldName())); 460 if (Debug.infoOn()) Debug.logInfo("Set [" + mkm.getRelFieldName() + "] to - " + this.get(mkm.getFieldName()), module); 461 } else { 462 allFieldsSet = false; 463 } 464 } 465 if (allFieldsSet) { 466 if (Debug.infoOn()) Debug.logInfo("Creating place holder value : " + newValue, module); 467 468 newValue.checkFks(true); 470 newValue.create(); 471 } 472 } else { 473 return false; 474 } 475 } 476 } 477 } 478 return true; 479 } 480 481 484 public Object clone() { 485 GenericValue newEntity = GenericValue.create(this); 486 newEntity.setDelegator(internalDelegator); 487 return newEntity; 488 } 489 490 protected static class NullGenericValue extends GenericValue implements NULL { }; 491 } 492 | Popular Tags |