1 24 package org.ofbiz.minilang.method.entityops; 25 26 import java.util.List ; 27 import java.util.Map ; 28 29 import org.ofbiz.base.util.Debug; 30 import org.ofbiz.entity.GenericEntityException; 31 import org.ofbiz.entity.GenericValue; 32 import org.ofbiz.minilang.SimpleMethod; 33 import org.ofbiz.minilang.method.ContextAccessor; 34 import org.ofbiz.minilang.method.MethodContext; 35 import org.ofbiz.minilang.method.MethodOperation; 36 import org.w3c.dom.Element ; 37 38 45 public class GetRelated extends MethodOperation { 46 47 public static final String module = GetRelated.class.getName(); 48 49 ContextAccessor valueAcsr; 50 ContextAccessor mapAcsr; 51 ContextAccessor orderByListAcsr; 52 String relationName; 53 String useCacheStr; 54 ContextAccessor listAcsr; 55 56 public GetRelated(Element element, SimpleMethod simpleMethod) { 57 super(element, simpleMethod); 58 valueAcsr = new ContextAccessor(element.getAttribute("value-name")); 59 relationName = element.getAttribute("relation-name"); 60 listAcsr = new ContextAccessor(element.getAttribute("list-name")); 61 mapAcsr = new ContextAccessor(element.getAttribute("map-name")); 62 orderByListAcsr = new ContextAccessor(element.getAttribute("order-by-list-name")); 63 64 useCacheStr = element.getAttribute("use-cache"); 65 } 66 67 public boolean exec(MethodContext methodContext) { 68 String relationName = methodContext.expandString(this.relationName); 69 String useCacheStr = methodContext.expandString(this.useCacheStr); 70 boolean useCache = "true".equals(useCacheStr); 71 72 List orderByNames = null; 73 if (!orderByListAcsr.isEmpty()) { 74 orderByNames = (List ) orderByListAcsr.get(methodContext); 75 } 76 Map constraintMap = null; 77 if (!mapAcsr.isEmpty()) { 78 constraintMap = (Map ) mapAcsr.get(methodContext); 79 } 80 81 GenericValue value = (GenericValue) valueAcsr.get(methodContext); 82 if (value == null) { 83 Debug.logWarning("Value not found with name: " + valueAcsr + ", not getting related...", module); 84 return true; 85 } 86 try { 87 if (useCache) { 88 listAcsr.put(methodContext, value.getRelatedCache(relationName, constraintMap, orderByNames)); 89 } else { 90 listAcsr.put(methodContext, value.getRelated(relationName, constraintMap, orderByNames)); 91 } 92 } catch (GenericEntityException e) { 93 String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem getting related from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage() + "]"; 94 Debug.logError(e, errMsg, module); 95 methodContext.setErrorReturn(errMsg, simpleMethod); 96 return false; 97 } 98 return true; 99 } 100 101 public String rawString() { 102 return "<get-related/>"; 104 } 105 public String expandedString(MethodContext methodContext) { 106 return this.rawString(); 108 } 109 } 110 | Popular Tags |