1 24 package org.ofbiz.minilang.method.entityops; 25 26 import java.util.HashSet ; 27 import java.util.List ; 28 import java.util.Map ; 29 30 import org.ofbiz.base.util.Debug; 31 import org.ofbiz.base.util.UtilValidate; 32 import org.ofbiz.entity.GenericDelegator; 33 import org.ofbiz.entity.GenericEntity; 34 import org.ofbiz.entity.GenericEntityException; 35 import org.ofbiz.minilang.SimpleMethod; 36 import org.ofbiz.minilang.method.ContextAccessor; 37 import org.ofbiz.minilang.method.MethodContext; 38 import org.ofbiz.minilang.method.MethodOperation; 39 import org.w3c.dom.Element ; 40 41 48 public class FindByPrimaryKey extends MethodOperation { 49 50 public static final String module = FindByPrimaryKey.class.getName(); 51 52 ContextAccessor valueAcsr; 53 String entityName; 54 ContextAccessor mapAcsr; 55 String delegatorName; 56 String useCacheStr; 57 ContextAccessor fieldsToSelectListAcsr; 58 59 public FindByPrimaryKey(Element element, SimpleMethod simpleMethod) { 60 super(element, simpleMethod); 61 valueAcsr = new ContextAccessor(element.getAttribute("value-name")); 62 entityName = element.getAttribute("entity-name"); 63 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 64 fieldsToSelectListAcsr = new ContextAccessor(element.getAttribute("fields-to-select-list")); 65 delegatorName = element.getAttribute("delegator-name"); 66 useCacheStr = element.getAttribute("use-cache"); 67 } 68 69 public boolean exec(MethodContext methodContext) { 70 String entityName = methodContext.expandString(this.entityName); 71 String delegatorName = methodContext.expandString(this.delegatorName); 72 String useCacheStr = methodContext.expandString(this.useCacheStr); 73 74 boolean useCache = "true".equals(useCacheStr); 75 76 GenericDelegator delegator = methodContext.getDelegator(); 77 if (delegatorName != null && delegatorName.length() > 0) { 78 delegator = GenericDelegator.getGenericDelegator(delegatorName); 79 } 80 81 Map inMap = (Map ) mapAcsr.get(methodContext); 82 if (UtilValidate.isEmpty(entityName) && inMap instanceof GenericEntity) { 83 GenericEntity inEntity = (GenericEntity) inMap; 84 entityName = inEntity.getEntityName(); 85 } 86 87 List fieldsToSelectList = null; 88 if (!fieldsToSelectListAcsr.isEmpty()) { 89 fieldsToSelectList = (List ) fieldsToSelectListAcsr.get(methodContext); 90 } 91 92 try { 93 if (fieldsToSelectList != null) { 94 valueAcsr.put(methodContext, delegator.findByPrimaryKeyPartial(delegator.makePK("Product", inMap), new HashSet (fieldsToSelectList))); 95 } else { 96 if (useCache) { 97 valueAcsr.put(methodContext, delegator.findByPrimaryKeyCache(entityName, inMap)); 98 } else { 99 valueAcsr.put(methodContext, delegator.findByPrimaryKey(entityName, inMap)); 100 } 101 } 102 } catch (GenericEntityException e) { 103 Debug.logError(e, module); 104 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem finding the " + entityName + " entity: " + e.getMessage() + "]"; 105 methodContext.setErrorReturn(errMsg, simpleMethod); 106 return false; 107 } 108 return true; 109 } 110 111 public String rawString() { 112 return "<find-by-primary-key/>"; 114 } 115 public String expandedString(MethodContext methodContext) { 116 return this.rawString(); 118 } 119 } 120 | Popular Tags |