1 24 package org.ofbiz.entity.finder; 25 26 import java.io.Serializable ; 27 import java.util.HashMap ; 28 import java.util.List ; 29 import java.util.Map ; 30 import java.util.Set ; 31 32 import org.ofbiz.base.util.Debug; 33 import org.ofbiz.base.util.UtilValidate; 34 import org.ofbiz.base.util.GeneralException; 35 import org.ofbiz.base.util.collections.FlexibleMapAccessor; 36 import org.ofbiz.base.util.string.FlexibleStringExpander; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.ofbiz.entity.GenericPK; 40 import org.ofbiz.entity.GenericValue; 41 import org.ofbiz.entity.model.ModelEntity; 42 import org.w3c.dom.Element ; 43 44 51 public class PrimaryKeyFinder implements Serializable { 52 public static final String module = PrimaryKeyFinder.class.getName(); 53 54 protected FlexibleStringExpander entityNameExdr; 55 protected FlexibleMapAccessor valueNameAcsr; 56 protected FlexibleStringExpander useCacheExdr; 57 protected FlexibleStringExpander autoFieldMapExdr; 58 protected Map fieldMap; 59 protected List selectFieldExpanderList; 60 61 public PrimaryKeyFinder(Element entityOneElement) { 62 this.entityNameExdr = new FlexibleStringExpander(entityOneElement.getAttribute("entity-name")); 63 if (UtilValidate.isNotEmpty(entityOneElement.getAttribute("value-name"))) 64 this.valueNameAcsr = new FlexibleMapAccessor(entityOneElement.getAttribute("value-name")); 65 this.useCacheExdr = new FlexibleStringExpander(entityOneElement.getAttribute("use-cache")); 66 this.autoFieldMapExdr = new FlexibleStringExpander(entityOneElement.getAttribute("auto-field-map")); 67 68 this.fieldMap = EntityFinderUtil.makeFieldMap(entityOneElement); 70 71 selectFieldExpanderList = EntityFinderUtil.makeSelectFieldExpanderList(entityOneElement); 73 } 74 75 public void runFind(Map context, GenericDelegator delegator) throws GeneralException { 76 String entityName = this.entityNameExdr.expandString(context); 77 ModelEntity modelEntity = delegator.getModelEntity(entityName); 78 79 String useCacheString = this.useCacheExdr.expandString(context); 80 boolean useCacheBool = "true".equals(useCacheString); 82 83 String autoFieldMapString = this.autoFieldMapExdr.expandString(context); 84 boolean autoFieldMapBool = !"false".equals(autoFieldMapString); 86 87 Map entityContext = new HashMap (); 89 if (autoFieldMapBool) { 90 GenericValue tempVal = delegator.makeValue(entityName, null); 91 92 Object parametersObj = context.get("parameters"); 94 if (parametersObj != null && parametersObj instanceof Map ) { 95 tempVal.setAllFields((Map ) parametersObj, true, null, Boolean.TRUE); 96 } 97 98 tempVal.setAllFields(context, true, null, Boolean.TRUE); 100 101 entityContext.putAll(tempVal); 102 } 103 EntityFinderUtil.expandFieldMapToContext(this.fieldMap, context, entityContext); 104 modelEntity.convertFieldMapInPlace(entityContext, delegator); 107 108 Set fieldsToSelect = EntityFinderUtil.makeFieldsToSelect(selectFieldExpanderList, context); 110 111 if (fieldsToSelect != null && useCacheBool) { 113 throw new IllegalArgumentException ("Error in entity-one definition, cannot specify select-field elements when use-cache is set to true"); 114 } 115 116 try { 117 GenericValue valueOut = null; 118 GenericPK entityPK = delegator.makePK(entityName, entityContext); 119 120 if (entityPK.containsPrimaryKey(true)) { 122 if (useCacheBool) { 123 valueOut = delegator.findByPrimaryKeyCache(entityPK); 124 } else { 125 if (fieldsToSelect != null) { 126 valueOut = delegator.findByPrimaryKeyPartial(entityPK, fieldsToSelect); 127 } else { 128 valueOut = delegator.findByPrimaryKey(entityPK); 129 } 130 } 131 } else { 132 if (Debug.infoOn()) Debug.logInfo("Returning null because found incomplete primary key in find: " + entityPK, module); 133 } 134 135 if (valueNameAcsr != null) { 138 this.valueNameAcsr.put(context, valueOut); 139 } else { 140 if (valueOut != null) { 141 context.putAll(valueOut); 142 } 143 } 144 } catch (GenericEntityException e) { 145 String errMsg = "Error finding entity value by primary key with entity-one: " + e.toString(); 146 Debug.logError(e, errMsg, module); 147 throw new IllegalArgumentException (errMsg); 148 } 149 } 150 } 151 152 | Popular Tags |