|
Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory
URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation%24Factory?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/META-INF/services/org.ofbiz.minilang.method.MethodOperation$Factory Tue Jun 19 21:36:11 2012 @@ -49,15 +49,12 @@ org.ofbiz.minilang.method.entityops.GetR org.ofbiz.minilang.method.entityops.GetRelatedOne$GetRelatedOneFactory org.ofbiz.minilang.method.entityops.MakeNextSeqId$MakeNextSeqIdFactory org.ofbiz.minilang.method.entityops.MakeValue$MakeValueFactory -org.ofbiz.minilang.method.entityops.NowDateToEnv$NowDateToEnvFactory -org.ofbiz.minilang.method.entityops.NowTimestampToEnv$NowTimestampFactory org.ofbiz.minilang.method.entityops.OrderValueList$OrderValueListFactory org.ofbiz.minilang.method.entityops.RefreshValue$RefreshValueFactory org.ofbiz.minilang.method.entityops.RemoveByAnd$RemoveByAndFactory org.ofbiz.minilang.method.entityops.RemoveList$RemoveListFactory org.ofbiz.minilang.method.entityops.RemoveRelated$RemoveRelatedFactory org.ofbiz.minilang.method.entityops.RemoveValue$RemoveValueFactory -org.ofbiz.minilang.method.entityops.SequencedIdToEnv$SequencedIdToEnvFactory org.ofbiz.minilang.method.entityops.SequencedIdToEnv$SequencedIdFactory org.ofbiz.minilang.method.entityops.SetCurrentUserLogin$SetCurrentUserLoginFactory org.ofbiz.minilang.method.entityops.SetNonpkFields$SetNonpkFieldsFactory @@ -83,6 +80,8 @@ org.ofbiz.minilang.method.envops.ListToL org.ofbiz.minilang.method.envops.Loop$LoopFactory org.ofbiz.minilang.method.envops.MapToMap$MapToMapFactory org.ofbiz.minilang.method.envops.Now$NowFactory +org.ofbiz.minilang.method.envops.Now$NowDateToEnvFactory +org.ofbiz.minilang.method.envops.Now$NowTimestampFactory org.ofbiz.minilang.method.envops.OrderMapList$OrderMapListFactory org.ofbiz.minilang.method.envops.SetCalendar$SetCalendarFactory org.ofbiz.minilang.method.envops.SetOperation$SetOperationFactory Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/MiniLangElement.java Tue Jun 19 21:36:11 2012 @@ -24,11 +24,12 @@ import org.ofbiz.minilang.method.MethodC import org.w3c.dom.Element; /** - * A single Mini-language element. This class is the superclass for all <code>Element</code> models. + * A single Mini-language element. This class is the superclass for all XML element models. */ public class MiniLangElement { - public static final String module = MiniLangElement.class.getName(); + // This must be private so subclasses cannot reference it. + private static final String module = MiniLangElement.class.getName(); private final Object lineNumber; protected final SimpleMethod simpleMethod; Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMapProcessor.java Tue Jun 19 21:36:11 2012 @@ -38,8 +38,8 @@ import org.w3c.dom.Element; */ public class SimpleMapProcessor { - protected static UtilCache<String, Map<String, MapProcessor>> simpleMapProcessorsResourceCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsResource", 0, 0); - protected static UtilCache<URL, Map<String, MapProcessor>> simpleMapProcessorsURLCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsURL", 0, 0); + private static final UtilCache<String, Map<String, MapProcessor>> simpleMapProcessorsResourceCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsResource", 0, 0); + private static final UtilCache<URL, Map<String, MapProcessor>> simpleMapProcessorsURLCache = UtilCache.createUtilCache("minilang.SimpleMapProcessorsURL", 0, 0); protected static Map<String, MapProcessor> getAllProcessors(URL xmlURL) throws MiniLangException { Map<String, MapProcessor> mapProcessors = FastMap.newInstance(); @@ -68,22 +68,16 @@ public class SimpleMapProcessor { protected static Map<String, MapProcessor> getProcessors(String xmlResource, String name, ClassLoader loader) throws MiniLangException { Map<String, MapProcessor> simpleMapProcessors = simpleMapProcessorsResourceCache.get(xmlResource); if (simpleMapProcessors == null) { - synchronized (SimpleMapProcessor.class) { - simpleMapProcessors = simpleMapProcessorsResourceCache.get(xmlResource); - if (simpleMapProcessors == null) { - URL xmlURL = null; - try { - xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader); - } catch (MalformedURLException e) { - throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource + "; error was: " + e.toString(), e); - } - if (xmlURL == null) { - throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource); - } - simpleMapProcessors = getAllProcessors(xmlURL); - simpleMapProcessorsResourceCache.put(xmlResource, simpleMapProcessors); - } + URL xmlURL = null; + try { + xmlURL = FlexibleLocation.resolveLocation(xmlResource, loader); + } catch (MalformedURLException e) { + throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource + "; error was: " + e.toString(), e); } + if (xmlURL == null) { + throw new MiniLangException("Could not find SimpleMapProcessor XML document in resource: " + xmlResource); + } + simpleMapProcessors = simpleMapProcessorsResourceCache.putIfAbsentAndGet(xmlResource, getAllProcessors(xmlURL)); } return simpleMapProcessors; } @@ -91,13 +85,7 @@ public class SimpleMapProcessor { protected static Map<String, MapProcessor> getProcessors(URL xmlURL, String name) throws MiniLangException { Map<String, MapProcessor> simpleMapProcessors = simpleMapProcessorsURLCache.get(xmlURL); if (simpleMapProcessors == null) { - synchronized (SimpleMapProcessor.class) { - simpleMapProcessors = simpleMapProcessorsURLCache.get(xmlURL); - if (simpleMapProcessors == null) { - simpleMapProcessors = getAllProcessors(xmlURL); - simpleMapProcessorsURLCache.put(xmlURL, simpleMapProcessors); - } - } + simpleMapProcessors = simpleMapProcessorsURLCache.putIfAbsentAndGet(xmlURL, getAllProcessors(xmlURL)); } return simpleMapProcessors; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/SimpleMethod.java Tue Jun 19 21:36:11 2012 @@ -59,7 +59,24 @@ import org.w3c.dom.Document; import org.w3c.dom.Element; /** - * Contains a block of Mini-language code. + * Implements the <simple-method> element. + * <p> + * The Mini-language script engine follows the + * <a href="http://en.wikipedia.org/wiki/Flyweight_pattern">flyweight</a> + * design pattern. Mini-language XML files are parsed twice - first into a W3C DOM + * tree, then the DOM tree is parsed into element model objects. Each XML element + * has a model class, and each model class has its own factory. + * </p> + * <p> + * Mini-language can be extended by:<br /> + * <ul> + * <li>Creating model classes that extend {@link org.ofbiz.minilang.method.MethodOperation}</li> + * <li>Creating factories for the model classes that implement {@link org.ofbiz.minilang.method.MethodOperation.Factory}</li> + * <li>Create a service provider information file + * (see <a href="http://docs.oracle.com/javase/6/docs/api/java/util/ServiceLoader.html" target="_blank">ServiceLoader</a>) + * for the factory classes</li> + * </ul> + * </p> */ public final class SimpleMethod extends MiniLangElement { @@ -137,8 +154,7 @@ public final class SimpleMethod extends Map<String, SimpleMethod> simpleMethods = simpleMethodsDirectCache.get(name); if (simpleMethods == null) { simpleMethods = getAllDirectSimpleMethods(name, content, fromLocation); - simpleMethodsDirectCache.putIfAbsent(name, simpleMethods); - simpleMethods = simpleMethodsDirectCache.get(name); + simpleMethods = simpleMethodsDirectCache.putIfAbsentAndGet(name, simpleMethods); } return simpleMethods; } @@ -175,8 +191,7 @@ public final class SimpleMethod extends Map<String, SimpleMethod> simpleMethods = simpleMethodsResourceCache.get(cacheKey); if (simpleMethods == null) { simpleMethods = getAllSimpleMethods(xmlURL); - simpleMethodsResourceCache.putIfAbsent(cacheKey, simpleMethods); - simpleMethods = simpleMethodsResourceCache.get(cacheKey); + simpleMethods = simpleMethodsResourceCache.putIfAbsentAndGet(cacheKey, simpleMethods); } return simpleMethods; } @@ -400,8 +415,6 @@ public final class SimpleMethod extends if (userLogin != null) { methodContext.putEnv(getUserLoginEnvName(), userLogin); } - // always put the null field object in as "null" - methodContext.putEnv("null", GenericEntity.NULL_FIELD); methodContext.putEnv("nullField", GenericEntity.NULL_FIELD); methodContext.putEnv(getDelegatorEnvName(), methodContext.getDelegator()); methodContext.putEnv(getSecurityEnvName(), methodContext.getSecurity()); Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/MethodOperation.java Tue Jun 19 21:36:11 2012 @@ -29,7 +29,7 @@ import org.ofbiz.minilang.SimpleMethod; import org.w3c.dom.Element; /** - * A single operation, does the specified operation on the given field + * An abstract class for Mini-language element models. */ public abstract class MethodOperation extends MiniLangElement { @@ -38,14 +38,22 @@ public abstract class MethodOperation ex super(element, simpleMethod); } - /** Execute the operation. Returns false if no further operations should be executed. + /** + * Executes the operation. + * Returns <code>true</code> if script execution should continue, or + * <code>false</code> if script execution should stop. + * * @throws MiniLangException */ public abstract boolean exec(MethodContext methodContext) throws MiniLangException; - /** Create an expanded string representation of the operation, is for the current context */ + /** Create a string representation of the operation, using the current context. + * <p><strong>Deprecated:</strong> No replacement.</p> + */ public abstract String expandedString(MethodContext methodContext); - /** Create a raw string representation of the operation, would be similar to original XML */ + /** Create a string representation of the operation - similar to the original XML. + * <p><strong>Deprecated:</strong> Use {@link #toString()}.</p> + */ public abstract String rawString(); @Retention(RetentionPolicy.RUNTIME) Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityCount.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityCount.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityCount.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityCount.java Tue Jun 19 21:36:11 2012 @@ -18,9 +18,6 @@ *******************************************************************************/ package org.ofbiz.minilang.method.entityops; -import java.util.Map; - -import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; import org.ofbiz.base.util.UtilValidate; @@ -36,39 +33,47 @@ import org.ofbiz.entity.finder.EntityFin import org.ofbiz.entity.finder.EntityFinderUtil.ConditionObject; import org.ofbiz.entity.model.ModelEntity; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; +import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find entity values by a condition + * Implements the <entity-count> element. */ -public class EntityCount extends MethodOperation { +public final class EntityCount extends MethodOperation { public static final String module = EntityCount.class.getName(); - protected FlexibleMapAccessor<Long> countAcsr; - protected FlexibleStringExpander delegatorNameExdr; - protected FlexibleStringExpander entityNameExdr; - protected Condition havingCondition; - protected Condition whereCondition; + private final FlexibleMapAccessor<Long> countFma; + private final FlexibleStringExpander delegatorNameFse; + private final FlexibleStringExpander entityNameFse; + private final Condition havingCondition; + private final Condition whereCondition; public EntityCount(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - this.entityNameExdr = FlexibleStringExpander.getInstance(element.getAttribute("entity-name")); - this.delegatorNameExdr = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name")); - if (UtilValidate.isNotEmpty(element.getAttribute("count-field"))) { - this.countAcsr = FlexibleMapAccessor.getInstance(element.getAttribute("count-field")); - } else { - this.countAcsr = FlexibleMapAccessor.getInstance(element.getAttribute("count-name")); - } - // process condition-expr | condition-list + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "count-field", "delegator-name"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "count-field"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "count-field"); + MiniLangValidate.childElements(simpleMethod, element, "condition-expr", "condition-list", "condition-object", "having-condition-list"); + MiniLangValidate.requireAnyChildElement(simpleMethod, element, "condition-expr", "condition-list", "condition-object"); + } + this.entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name")); + this.delegatorNameFse = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name")); + this.countFma = FlexibleMapAccessor.getInstance(element.getAttribute("count-field")); + int conditionElementCount = 0; Element conditionExprElement = UtilXml.firstChildElement(element, "condition-expr"); + conditionElementCount = conditionExprElement == null ? conditionElementCount : conditionElementCount++; Element conditionListElement = UtilXml.firstChildElement(element, "condition-list"); + conditionElementCount = conditionListElement == null ? conditionElementCount : conditionElementCount++; Element conditionObjectElement = UtilXml.firstChildElement(element, "condition-object"); - if (conditionExprElement != null && conditionListElement != null) { - throw new IllegalArgumentException("In entity find by condition element, cannot have condition-expr and condition-list sub-elements"); + conditionElementCount = conditionObjectElement == null ? conditionElementCount : conditionElementCount++; + if (conditionElementCount > 1) { + MiniLangValidate.handleError("Element must include only one condition child element", simpleMethod, conditionObjectElement); } if (conditionExprElement != null) { this.whereCondition = new ConditionExpr(conditionExprElement); @@ -76,47 +81,41 @@ public class EntityCount extends MethodO this.whereCondition = new ConditionList(conditionListElement); } else if (conditionObjectElement != null) { this.whereCondition = new ConditionObject(conditionObjectElement); + } else { + this.whereCondition = null; } - // process having-condition-list Element havingConditionListElement = UtilXml.firstChildElement(element, "having-condition-list"); if (havingConditionListElement != null) { this.havingCondition = new ConditionList(havingConditionListElement); + } else { + this.havingCondition = null; } } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { try { - Map<String, Object> context = methodContext.getEnvMap(); + String delegatorName = this.delegatorNameFse.expandString(methodContext.getEnvMap()); Delegator delegator = methodContext.getDelegator(); - String entityName = this.entityNameExdr.expandString(context); - String delegatorName = this.delegatorNameExdr.expandString(context); if (UtilValidate.isNotEmpty(delegatorName)) { delegator = DelegatorFactory.getDelegator(delegatorName); } + String entityName = this.entityNameFse.expandString(methodContext.getEnvMap()); ModelEntity modelEntity = delegator.getModelEntity(entityName); - // create whereEntityCondition from whereCondition EntityCondition whereEntityCondition = null; if (this.whereCondition != null) { - whereEntityCondition = this.whereCondition.createCondition(context, modelEntity, delegator.getModelFieldTypeReader(modelEntity)); + whereEntityCondition = this.whereCondition.createCondition(methodContext.getEnvMap(), modelEntity, delegator.getModelFieldTypeReader(modelEntity)); } - // create havingEntityCondition from havingCondition EntityCondition havingEntityCondition = null; if (this.havingCondition != null) { - havingEntityCondition = this.havingCondition.createCondition(context, modelEntity, delegator.getModelFieldTypeReader(modelEntity)); + havingEntityCondition = this.havingCondition.createCondition(methodContext.getEnvMap(), modelEntity, delegator.getModelFieldTypeReader(modelEntity)); } long count = delegator.findCountByCondition(entityName, whereEntityCondition, havingEntityCondition, null); - this.countAcsr.put(context, count); + this.countFma.put(methodContext.getEnvMap(), count); } catch (GeneralException e) { - Debug.logError(e, module); - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process: " + e.getMessage(); - if (methodContext.getMethodType() == MethodContext.EVENT) { - methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg); - methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg); - methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); - } + String errMsg = "Exception thrown while performing entity count: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; @@ -124,26 +123,41 @@ public class EntityCount extends MethodO @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public void gatherArtifactInfo(ArtifactInfoContext aic) { - aic.addEntityName(entityNameExdr.toString()); + aic.addEntityName(entityNameFse.toString()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<entity-count/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<entity-count "); + sb.append("entity-name=\"").append(this.entityNameFse).append("\" "); + sb.append("count-field=\"").append(this.countFma).append("\" "); + if (!this.delegatorNameFse.isEmpty()) { + sb.append("delegator-name=\"").append(this.delegatorNameFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <entity-count> element. + */ public static final class EntityCountFactory implements Factory<EntityCount> { + @Override public EntityCount createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new EntityCount(element, simpleMethod); } + @Override public String getName() { return "entity-count"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java Tue Jun 19 21:36:11 2012 @@ -27,52 +27,65 @@ import javolution.util.FastList; import org.ofbiz.base.location.FlexibleLocation; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.DelegatorFactory; import org.ofbiz.entity.util.EntityDataAssert; import org.ofbiz.entity.util.EntitySaxReader; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find entity values by a primary key + * Implements the <entity-data> element. */ -public class EntityData extends MethodOperation { +public final class EntityData extends MethodOperation { public static final String module = EntityData.class.getName(); - protected FlexibleStringExpander delegatorNameExdr; - protected ContextAccessor<List<Object>> errorListAcsr; - protected FlexibleStringExpander locationExdr; - protected String mode; - protected FlexibleStringExpander timeoutExdr; + private final FlexibleStringExpander delegatorNameFse; + private final FlexibleMapAccessor<List<Object>> errorListFma; + private final FlexibleStringExpander locationFse; + private final String mode; + private final int timeout; public EntityData(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - locationExdr = FlexibleStringExpander.getInstance(element.getAttribute("location")); - delegatorNameExdr = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name")); - timeoutExdr = FlexibleStringExpander.getInstance(element.getAttribute("timeout")); - errorListAcsr = new ContextAccessor<List<Object>>(element.getAttribute("error-list-name"), "error_list"); - mode = element.getAttribute("mode"); - if (UtilValidate.isEmpty(mode)) { - mode = "load"; + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "location", "timeout", "delegator-name", "error-list-name", "mode"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "location"); + MiniLangValidate.constantAttributes(simpleMethod, element, "timeout", "mode"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + locationFse = FlexibleStringExpander.getInstance(element.getAttribute("location")); + delegatorNameFse = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name")); + String timeoutAttribute = element.getAttribute("timeout"); + int timeout = -1; + if (!timeoutAttribute.isEmpty()) { + try { + timeout = Integer.parseInt(timeoutAttribute); + } catch (NumberFormatException e) { + MiniLangValidate.handleError("Exception thrown while parsing timeout attribute: " + e.getMessage(), simpleMethod, element); + } } + this.timeout = timeout; + errorListFma = FlexibleMapAccessor.getInstance(MiniLangValidate.checkAttribute(element.getAttribute("error-list-name"), "error_list")); + mode = MiniLangValidate.checkAttribute(element.getAttribute("mode"), "load"); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - List<Object> messages = errorListAcsr.get(methodContext); + List<Object> messages = errorListFma.get(methodContext.getEnvMap()); if (messages == null) { messages = FastList.newInstance(); - errorListAcsr.put(methodContext, messages); + errorListFma.put(methodContext.getEnvMap(), messages); } - String location = this.locationExdr.expandString(methodContext.getEnvMap()); - String delegatorName = this.delegatorNameExdr.expandString(methodContext.getEnvMap()); + String location = this.locationFse.expandString(methodContext.getEnvMap()); + String delegatorName = this.delegatorNameFse.expandString(methodContext.getEnvMap()); Delegator delegator = methodContext.getDelegator(); if (UtilValidate.isNotEmpty(delegatorName)) { delegator = DelegatorFactory.getDelegator(delegatorName); @@ -86,31 +99,19 @@ public class EntityData extends MethodOp if (dataUrl == null) { messages.add("Could not find Entity Data document in resource: " + location); } - String timeout = this.timeoutExdr.expandString(methodContext.getEnvMap()); - int txTimeout = -1; - if (UtilValidate.isNotEmpty(timeout)) { - try { - txTimeout = Integer.parseInt(timeout); - } catch (NumberFormatException e) { - Debug.logWarning("Timeout not formatted properly in entity-data operation, defaulting to container default", module); - } - } if ("assert".equals(mode)) { - // load the XML file, read in one element at a time and check it against the - // database try { EntityDataAssert.assertData(dataUrl, delegator, messages); } catch (Exception e) { String xmlError = "Error checking/asserting XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage(); - // Debug.logError(e, xmlError, module); messages.add(xmlError); + Debug.logWarning(e, xmlError, module); } } else { - // again, default to load try { EntitySaxReader reader = null; - if (txTimeout > 0) { - reader = new EntitySaxReader(delegator, txTimeout); + if (timeout > 0) { + reader = new EntitySaxReader(delegator, timeout); } else { reader = new EntitySaxReader(delegator); } @@ -118,7 +119,7 @@ public class EntityData extends MethodOp } catch (Exception e) { String xmlError = "Error loading XML Resource \"" + dataUrl.toExternalForm() + "\"; Error was: " + e.getMessage(); messages.add(xmlError); - Debug.logError(e, xmlError, module); + Debug.logWarning(e, xmlError, module); } } return true; @@ -126,23 +127,39 @@ public class EntityData extends MethodOp @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<entity-data/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<entity-data "); + sb.append("location=\"").append(this.locationFse).append("\" "); + sb.append("mode=\"").append(this.mode).append("\" "); + sb.append("timeout=\"").append(this.timeout).append("\" "); + if (!this.delegatorNameFse.isEmpty()) { + sb.append("delegator-name=\"").append(this.delegatorNameFse).append("\" "); + } + sb.append("error-list-name=\"").append(this.errorListFma).append("\" "); + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <entity-data> element. + */ public static final class EntityDataFactory implements Factory<EntityData> { + @Override public EntityData createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new EntityData(element, simpleMethod); } + @Override public String getName() { - // FIXME: not in SimpleMethod return "entity-data"; } } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java Tue Jun 19 21:36:11 2012 @@ -21,25 +21,33 @@ package org.ofbiz.minilang.method.entity import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.finder.PrimaryKeyFinder; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find entity values by a primary key + * Implements the <entity-one> element. */ -public class EntityOne extends MethodOperation { +public final class EntityOne extends MethodOperation { public static final String module = EntityOne.class.getName(); - protected PrimaryKeyFinder finder; + private final PrimaryKeyFinder finder; public EntityOne(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "auto-field-map", "value-field"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "value-field"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field"); + MiniLangValidate.childElements(simpleMethod, element, "field-map", "select-field"); + } this.finder = new PrimaryKeyFinder(element); } @@ -49,15 +57,9 @@ public class EntityOne extends MethodOpe Delegator delegator = methodContext.getDelegator(); this.finder.runFind(methodContext.getEnvMap(), delegator); } catch (GeneralException e) { - Debug.logError(e, module); - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process: " + e.getMessage(); - if (methodContext.getMethodType() == MethodContext.EVENT) { - methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg); - methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg); - methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); - } + String errMsg = "Exception thrown while performing entity find: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; @@ -65,8 +67,7 @@ public class EntityOne extends MethodOpe @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override @@ -76,15 +77,26 @@ public class EntityOne extends MethodOpe @Override public String rawString() { - // TODO: something more than the empty tag - return "<entity-one/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<entity-one "); + sb.append("entity-name=\"").append(this.finder.getEntityName()).append("\" />"); + return sb.toString(); } + /** + * A factory for the <entity-one> element. + */ public static final class EntityOneFactory implements Factory<EntityOne> { + @Override public EntityOne createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new EntityOne(element, simpleMethod); } + @Override public String getName() { return "entity-one"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java Tue Jun 19 21:36:11 2012 @@ -21,61 +21,81 @@ package org.ofbiz.minilang.method.entity import java.util.List; import java.util.Map; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.GenericEntity; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find entity values by anding the map fields + * Implements the <filter-list-by-and> element. */ -public class FilterListByAnd extends MethodOperation { +public final class FilterListByAnd extends MethodOperation { - ContextAccessor<List<GenericEntity>> listAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - ContextAccessor<List<GenericEntity>> toListAcsr; + private final FlexibleMapAccessor<List<GenericEntity>> listFma; + private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma; + private final FlexibleMapAccessor<List<GenericEntity>> toListFma; public FilterListByAnd(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - listAcsr = new ContextAccessor<List<GenericEntity>>(element.getAttribute("list"), element.getAttribute("list-name")); - toListAcsr = new ContextAccessor<List<GenericEntity>>(element.getAttribute("to-list"), element.getAttribute("to-list-name")); - if (toListAcsr.isEmpty()) { - toListAcsr = listAcsr; + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "list", "map", "to-list"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "list", "map"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "map", "to-list"); + MiniLangValidate.noChildElements(simpleMethod, element); } - mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map"), element.getAttribute("map-name")); + listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list")); + String toListAttribute = element.getAttribute("to-list"); + if (toListAttribute.isEmpty()) { + toListFma = listFma; + } else { + toListFma = FlexibleMapAccessor.getInstance(toListAttribute); + } + mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - Map<String, ? extends Object> theMap = null; - if (!mapAcsr.isEmpty()) { - theMap = mapAcsr.get(methodContext); - } - toListAcsr.put(methodContext, EntityUtil.filterByAnd(listAcsr.get(methodContext), theMap)); + Map<String, ? extends Object> theMap = mapFma.get(methodContext.getEnvMap()); + toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByAnd(listFma.get(methodContext.getEnvMap()), theMap)); return true; } @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<filter-list-by-and/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<filter-list-by-and "); + sb.append("list=\"").append(this.listFma).append("\" "); + sb.append("map=\"").append(this.mapFma).append("\" "); + sb.append("to-list=\"").append(this.toListFma).append("\" "); + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <filter-list-by-and> element. + */ public static final class FilterListByAndFactory implements Factory<FilterListByAnd> { + @Override public FilterListByAnd createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new FilterListByAnd(element, simpleMethod); } + @Override public String getName() { return "filter-list-by-and"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java Tue Jun 19 21:36:11 2012 @@ -22,72 +22,91 @@ import java.sql.Timestamp; import java.util.List; import org.ofbiz.base.util.UtilDateTime; -import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.GenericEntity; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find entity values by anding the map fields + * Implements the <filter-list-by-date> element. */ -public class FilterListByDate extends MethodOperation { +public final class FilterListByDate extends MethodOperation { - String allSameStr; - String fromFieldName; - ContextAccessor<List<GenericEntity>> listAcsr; - String thruFieldName; - ContextAccessor<List<GenericEntity>> toListAcsr; - ContextAccessor<Timestamp> validDateAcsr; + private final FlexibleMapAccessor<List<GenericEntity>> listFma; + private final FlexibleMapAccessor<List<GenericEntity>> toListFma; + private final FlexibleMapAccessor<Timestamp> validDateFma; + private final String fromFieldName; + private final String thruFieldName; public FilterListByDate(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - listAcsr = new ContextAccessor<List<GenericEntity>>(element.getAttribute("list"), element.getAttribute("list-name")); - toListAcsr = new ContextAccessor<List<GenericEntity>>(element.getAttribute("to-list"), element.getAttribute("to-list-name")); - if (toListAcsr.isEmpty()) { - toListAcsr = listAcsr; + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "list", "to-list", "valid-date", "fromDate", "thruDate"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "list"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "to-list", "valid-date"); + MiniLangValidate.constantAttributes(simpleMethod, element, "fromDate", "thruDate"); + MiniLangValidate.noChildElements(simpleMethod, element); } - validDateAcsr = new ContextAccessor<Timestamp>(element.getAttribute("valid-date"), element.getAttribute("valid-date-name")); - fromFieldName = element.getAttribute("from-field-name"); - if (UtilValidate.isEmpty(fromFieldName)) - fromFieldName = "fromDate"; - thruFieldName = element.getAttribute("thru-field-name"); - if (UtilValidate.isEmpty(thruFieldName)) - thruFieldName = "thruDate"; - allSameStr = element.getAttribute("all-same"); + listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list")); + String toListAttribute = element.getAttribute("to-list"); + if (toListAttribute.isEmpty()) { + toListFma = listFma; + } else { + toListFma = FlexibleMapAccessor.getInstance(toListAttribute); + } + validDateFma = FlexibleMapAccessor.getInstance(element.getAttribute("valid-date")); + fromFieldName = MiniLangValidate.checkAttribute(element.getAttribute("from-field-name"), "fromDate"); + thruFieldName = MiniLangValidate.checkAttribute(element.getAttribute("thru-field-name"), "thruDate"); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - if (!validDateAcsr.isEmpty()) { - toListAcsr.put(methodContext, EntityUtil.filterByDate(listAcsr.get(methodContext), validDateAcsr.get(methodContext), fromFieldName, thruFieldName, true)); + if (!validDateFma.isEmpty()) { + toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByDate(listFma.get(methodContext.getEnvMap()), validDateFma.get(methodContext.getEnvMap()), fromFieldName, thruFieldName, true)); } else { - toListAcsr.put(methodContext, EntityUtil.filterByDate(listAcsr.get(methodContext), UtilDateTime.nowTimestamp(), fromFieldName, thruFieldName, true)); + toListFma.put(methodContext.getEnvMap(), EntityUtil.filterByDate(listFma.get(methodContext.getEnvMap()), UtilDateTime.nowTimestamp(), fromFieldName, thruFieldName, true)); } return true; } @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<filter-list-by-date/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<filter-list-by-date "); + sb.append("list=\"").append(this.listFma).append("\" "); + sb.append("to-list=\"").append(this.toListFma).append("\" "); + sb.append("valid-date=\"").append(this.validDateFma).append("\" "); + sb.append("from-field-name=\"").append(this.fromFieldName).append("\" "); + sb.append("thru-field-name=\"").append(this.thruFieldName).append("\" "); + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <filter-list-by-date> element. + */ public static final class FilterListByDateFactory implements Factory<FilterListByDate> { + @Override public FilterListByDate createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new FilterListByDate(element, simpleMethod); } + @Override public String getName() { return "filter-list-by-date"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java Tue Jun 19 21:36:11 2012 @@ -21,82 +21,78 @@ package org.ofbiz.minilang.method.entity import java.util.List; import java.util.Map; -import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.DelegatorFactory; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.condition.EntityCondition; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find entity values by anding the map fields + * Implements the <find-by-and> element. */ -public class FindByAnd extends MethodOperation { +public final class FindByAnd extends MethodOperation { public static final String module = FindByAnd.class.getName(); - String delegatorName; + private final FlexibleStringExpander delegatorNameFse; + private final FlexibleStringExpander entityNameFse; + private final FlexibleMapAccessor<Object> listFma; + private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma; + private final FlexibleMapAccessor<List<String>> orderByListFma; + private final FlexibleStringExpander useCacheFse; + private final FlexibleStringExpander useIteratorFse; - String entityName; - ContextAccessor<Object> listAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - ContextAccessor<List<String>> orderByListAcsr; - String useCacheStr; - String useIteratorStr; public FindByAnd(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - listAcsr = new ContextAccessor<Object>(element.getAttribute("list"), element.getAttribute("list-name")); - entityName = element.getAttribute("entity-name"); - mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map"), element.getAttribute("map-name")); - orderByListAcsr = new ContextAccessor<List<String>>(element.getAttribute("order-by-list"), element.getAttribute("order-by-list-name")); - delegatorName = element.getAttribute("delegator-name"); - useCacheStr = element.getAttribute("use-cache"); - useIteratorStr = element.getAttribute("use-iterator"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "use-iterator", "list", "map", "order-by-list", "delegator-name"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "list", "map"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "map", "order-by-list"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name")); + listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list")); + mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map")); + orderByListFma = FlexibleMapAccessor.getInstance(element.getAttribute("order-by-list")); + useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache")); + useIteratorFse = FlexibleStringExpander.getInstance(element.getAttribute("use-iterator")); + delegatorNameFse = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String entityName = methodContext.expandString(this.entityName); - String delegatorName = methodContext.expandString(this.delegatorName); - String useCacheStr = methodContext.expandString(this.useCacheStr); - String useIteratorStr = methodContext.expandString(this.useIteratorStr); - boolean useCache = "true".equals(useCacheStr); - boolean useIterator = "true".equals(useIteratorStr); - List<String> orderByNames = null; - if (!orderByListAcsr.isEmpty()) { - orderByNames = orderByListAcsr.get(methodContext); - } + String entityName = entityNameFse.expandString(methodContext.getEnvMap()); + String delegatorName = delegatorNameFse.expandString(methodContext.getEnvMap()); + boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap())); + boolean useIterator = "true".equals(useIteratorFse.expandString(methodContext.getEnvMap())); + List<String> orderByNames = orderByListFma.get(methodContext.getEnvMap()); Delegator delegator = methodContext.getDelegator(); - if (UtilValidate.isNotEmpty(delegatorName)) { + if (!delegatorName.isEmpty()) { delegator = DelegatorFactory.getDelegator(delegatorName); } try { if (useIterator) { EntityCondition whereCond = null; - if (!mapAcsr.isEmpty()) { - whereCond = EntityCondition.makeCondition(mapAcsr.get(methodContext)); + if (!mapFma.isEmpty()) { + whereCond = EntityCondition.makeCondition(mapFma.get(methodContext.getEnvMap())); } - listAcsr.put(methodContext, delegator.find(entityName, whereCond, null, null, orderByNames, null)); + listFma.put(methodContext.getEnvMap(), delegator.find(entityName, whereCond, null, null, orderByNames, null)); } else { - listAcsr.put(methodContext, delegator.findByAnd(entityName, mapAcsr.get(methodContext), orderByNames, useCache)); + listFma.put(methodContext.getEnvMap(), delegator.findByAnd(entityName, mapFma.get(methodContext.getEnvMap()), orderByNames, useCache)); } } catch (GenericEntityException e) { - Debug.logError(e, module); - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem finding the " + entityName + " entity: " + e.getMessage() + "]"; - if (methodContext.getMethodType() == MethodContext.EVENT) { - methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg); - methodContext.putEnv(simpleMethod.getEventResponseCodeName(), simpleMethod.getDefaultErrorCode()); - } else if (methodContext.getMethodType() == MethodContext.SERVICE) { - methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg); - methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), simpleMethod.getDefaultErrorCode()); - } + String errMsg = "Exception thrown while performing entity find: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; @@ -104,26 +100,51 @@ public class FindByAnd extends MethodOpe @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public void gatherArtifactInfo(ArtifactInfoContext aic) { - aic.addEntityName(entityName); + aic.addEntityName(entityNameFse.toString()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<find-by-and/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<find-by-and "); + sb.append("entity-name=\"").append(this.entityNameFse).append("\" "); + sb.append("list=\"").append(this.listFma).append("\" "); + sb.append("map=\"").append(this.mapFma).append("\" "); + if (!orderByListFma.isEmpty()) { + sb.append("order-by-list=\"").append(this.orderByListFma).append("\" "); + } + if (!useCacheFse.isEmpty()) { + sb.append("use-cache=\"").append(this.useCacheFse).append("\" "); + } + if (!useIteratorFse.isEmpty()) { + sb.append("use-iterator=\"").append(this.useIteratorFse).append("\" "); + } + if (!delegatorNameFse.isEmpty()) { + sb.append("delegator-name=\"").append(this.delegatorNameFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <find-by-and> element. + */ public static final class FindByAndFactory implements Factory<FindByAnd> { + @Override public FindByAnd createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new FindByAnd(element, simpleMethod); } + @Override public String getName() { return "find-by-and"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java Tue Jun 19 21:36:11 2012 @@ -21,75 +21,85 @@ package org.ofbiz.minilang.method.entity import java.util.Collection; import java.util.Map; -import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilMisc; -import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.DelegatorFactory; import org.ofbiz.entity.GenericEntity; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangRuntimeException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to find an entity value by its primary key + * Implements the <find-by-primary-key> element. */ -public class FindByPrimaryKey extends MethodOperation { +public final class FindByPrimaryKey extends MethodOperation { public static final String module = FindByPrimaryKey.class.getName(); - String delegatorName; - String entityName; - ContextAccessor<Collection<String>> fieldsToSelectListAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - String useCacheStr; - ContextAccessor<GenericValue> valueAcsr; + private final FlexibleStringExpander delegatorNameFse; + private final FlexibleStringExpander entityNameFse; + private final FlexibleMapAccessor<Collection<String>> fieldsToSelectListFma; + private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma; + private final FlexibleStringExpander useCacheFse; + private final FlexibleMapAccessor<GenericValue> valueFma; public FindByPrimaryKey(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - valueAcsr = new ContextAccessor<GenericValue>(element.getAttribute("value-field"), element.getAttribute("value-name")); - entityName = element.getAttribute("entity-name"); - mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map"), element.getAttribute("map-name")); - fieldsToSelectListAcsr = new ContextAccessor<Collection<String>>(element.getAttribute("fields-to-select-list")); - delegatorName = element.getAttribute("delegator-name"); - useCacheStr = element.getAttribute("use-cache"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "entity-name", "use-cache", "fields-to-select-list", "map", "value-field", "delegator-name"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "map"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "map", "fields-to-select-list"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field")); + entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name")); + mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map")); + fieldsToSelectListFma = FlexibleMapAccessor.getInstance(element.getAttribute("fields-to-select-list")); + delegatorNameFse = FlexibleStringExpander.getInstance(element.getAttribute("delegator-name")); + useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String entityName = methodContext.expandString(this.entityName); - String delegatorName = methodContext.expandString(this.delegatorName); - String useCacheStr = methodContext.expandString(this.useCacheStr); - boolean useCache = "true".equals(useCacheStr); + String entityName = entityNameFse.expandString(methodContext.getEnvMap()); + boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap())); Delegator delegator = methodContext.getDelegator(); - if (UtilValidate.isNotEmpty(delegatorName)) { + String delegatorName = delegatorNameFse.expandString(methodContext.getEnvMap()); + if (!delegatorName.isEmpty()) { delegator = DelegatorFactory.getDelegator(delegatorName); } - Map<String, ? extends Object> inMap = mapAcsr.get(methodContext); - if (UtilValidate.isEmpty(entityName) && inMap instanceof GenericEntity) { + Map<String, ? extends Object> inMap = mapFma.get(methodContext.getEnvMap()); + if (inMap == null) { + throw new MiniLangRuntimeException("Primary key map \"" + mapFma + "\" not found", this); + } + if (entityName.isEmpty() && inMap instanceof GenericEntity) { GenericEntity inEntity = (GenericEntity) inMap; entityName = inEntity.getEntityName(); } - Collection<String> fieldsToSelectList = null; - if (!fieldsToSelectListAcsr.isEmpty()) { - fieldsToSelectList = fieldsToSelectListAcsr.get(methodContext); + if (entityName.isEmpty()) { + throw new MiniLangRuntimeException("Entity name not found", this); } + Collection<String> fieldsToSelectList = fieldsToSelectListFma.get(methodContext.getEnvMap()); try { if (fieldsToSelectList != null) { - valueAcsr.put(methodContext, delegator.findByPrimaryKeyPartial(delegator.makePK(entityName, inMap), UtilMisc.makeSetWritable(fieldsToSelectList))); + valueFma.put(methodContext.getEnvMap(), delegator.findByPrimaryKeyPartial(delegator.makePK(entityName, inMap), UtilMisc.makeSetWritable(fieldsToSelectList))); } else { - valueAcsr.put(methodContext, delegator.findOne(entityName, inMap, useCache)); + valueFma.put(methodContext.getEnvMap(), delegator.findOne(entityName, inMap, useCache)); } } catch (GenericEntityException e) { - Debug.logError(e, module); - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem finding the " + entityName + " entity: " + e.getMessage() + "]"; - methodContext.setErrorReturn(errMsg, simpleMethod); + String errMsg = "Exception thrown while performing entity find: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; @@ -97,26 +107,48 @@ public class FindByPrimaryKey extends Me @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public void gatherArtifactInfo(ArtifactInfoContext aic) { - aic.addEntityName(entityName); + aic.addEntityName(entityNameFse.toString()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<find-by-primary-key/>"; + return toString(); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<find-by-primary-key "); + sb.append("entity-name=\"").append(this.entityNameFse).append("\" "); + sb.append("value-field=\"").append(this.valueFma).append("\" "); + sb.append("map=\"").append(this.mapFma).append("\" "); + if (!fieldsToSelectListFma.isEmpty()) { + sb.append("fields-to-select-list=\"").append(this.fieldsToSelectListFma).append("\" "); + } + if (!useCacheFse.isEmpty()) { + sb.append("use-cache=\"").append(this.useCacheFse).append("\" "); + } + if (!delegatorNameFse.isEmpty()) { + sb.append("delegator-name=\"").append(this.delegatorNameFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); + } + + /** + * A factory for the <find-by-primary-key> element. + */ public static final class FindByPrimaryKeyFactory implements Factory<FindByPrimaryKey> { + @Override public FindByPrimaryKey createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new FindByPrimaryKey(element, simpleMethod); } + @Override public String getName() { return "find-by-primary-key"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java Tue Jun 19 21:36:11 2012 @@ -21,69 +21,66 @@ package org.ofbiz.minilang.method.entity import java.util.List; import java.util.Map; -import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangRuntimeException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Gets a list of related entity instance according to the specified relation-name + * Implements the <get-related> element. */ -public class GetRelated extends MethodOperation { +public final class GetRelated extends MethodOperation { public static final String module = GetRelated.class.getName(); - ContextAccessor<List<GenericValue>> listAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - ContextAccessor<List<String>> orderByListAcsr; - String relationName; - String useCacheStr; - ContextAccessor<GenericValue> valueAcsr; + private final FlexibleMapAccessor<Object> listFma; + private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma; + private final FlexibleMapAccessor<List<String>> orderByListFma; + private final FlexibleStringExpander relationNameFse; + private final FlexibleStringExpander useCacheFse; + private final FlexibleMapAccessor<GenericValue> valueFma; public GetRelated(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - valueAcsr = new ContextAccessor<GenericValue>(element.getAttribute("value-field"), element.getAttribute("value-name")); - relationName = element.getAttribute("relation-name"); - listAcsr = new ContextAccessor<List<GenericValue>>(element.getAttribute("list"), element.getAttribute("list-name")); - mapAcsr = new ContextAccessor<Map<String, ? extends Object>>(element.getAttribute("map"), element.getAttribute("map-name")); - orderByListAcsr = new ContextAccessor<List<String>>(element.getAttribute("order-by-list"), element.getAttribute("order-by-list-name")); - useCacheStr = element.getAttribute("use-cache"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "relation-name", "list", "map", "order-by-list", "use-cache"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "relation-name", "list"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "list", "map", "order-by-list"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field")); + relationNameFse = FlexibleStringExpander.getInstance(element.getAttribute("relation-name")); + listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list")); + mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map")); + orderByListFma = FlexibleMapAccessor.getInstance(element.getAttribute("order-by-list")); + useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String relationName = methodContext.expandString(this.relationName); - String useCacheStr = methodContext.expandString(this.useCacheStr); - boolean useCache = "true".equals(useCacheStr); - List<String> orderByNames = null; - if (!orderByListAcsr.isEmpty()) { - orderByNames = orderByListAcsr.get(methodContext); - } - Map<String, ? extends Object> constraintMap = null; - if (!mapAcsr.isEmpty()) { - constraintMap = mapAcsr.get(methodContext); - } - GenericValue value = valueAcsr.get(methodContext); + GenericValue value = valueFma.get(methodContext.getEnvMap()); if (value == null) { - Debug.logWarning("Value not found with name: " + valueAcsr + ", not getting related...", module); - return true; + throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this); } + String relationName = relationNameFse.expandString(methodContext.getEnvMap()); + boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap())); + List<String> orderByNames = orderByListFma.get(methodContext.getEnvMap()); + Map<String, ? extends Object> constraintMap = mapFma.get(methodContext.getEnvMap()); try { - if (useCache) { - listAcsr.put(methodContext, value.getRelatedCache(relationName, constraintMap, orderByNames)); - } else { - listAcsr.put(methodContext, value.getRelated(relationName, constraintMap, orderByNames)); - } + listFma.put(methodContext.getEnvMap(), value.getRelated(relationName, constraintMap, orderByNames, useCache)); } catch (GenericEntityException e) { - 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() + "]"; - Debug.logError(e, errMsg, module); - methodContext.setErrorReturn(errMsg, simpleMethod); + String errMsg = "Exception thrown while finding related values: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; @@ -91,26 +88,48 @@ public class GetRelated extends MethodOp @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public void gatherArtifactInfo(ArtifactInfoContext aic) { - aic.addEntityName(relationName); + aic.addEntityName(relationNameFse.toString()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<get-related/>"; + return toString(); } + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<get-related "); + sb.append("value-field=\"").append(this.valueFma).append("\" "); + sb.append("relation-name=\"").append(this.relationNameFse).append("\" "); + sb.append("list=\"").append(this.listFma).append("\" "); + if (!mapFma.isEmpty()) { + sb.append("map=\"").append(this.mapFma).append("\" "); + } + if (!orderByListFma.isEmpty()) { + sb.append("order-by-list=\"").append(this.orderByListFma).append("\" "); + } + if (!useCacheFse.isEmpty()) { + sb.append("use-cache=\"").append(this.useCacheFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); + } + + /** + * A factory for the <get-related> element. + */ public static final class GetRelatedFactory implements Factory<GetRelated> { + @Override public GetRelated createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new GetRelated(element, simpleMethod); } + @Override public String getName() { return "get-related"; } Modified: ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java URL: http://svn.apache.org/viewvc/ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java?rev=1351866&r1=1351865&r2=1351866&view=diff ============================================================================== --- ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java (original) +++ ofbiz/branches/20120329_portletWidget/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java Tue Jun 19 21:36:11 2012 @@ -18,64 +18,60 @@ *******************************************************************************/ package org.ofbiz.minilang.method.entityops; -import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.base.util.Debug; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.minilang.MiniLangException; +import org.ofbiz.minilang.MiniLangRuntimeException; +import org.ofbiz.minilang.MiniLangValidate; import org.ofbiz.minilang.SimpleMethod; -import org.ofbiz.minilang.method.ContextAccessor; +import org.ofbiz.minilang.artifact.ArtifactInfoContext; import org.ofbiz.minilang.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Gets a list of related entity instance according to the specified relation-name + * Implements the <get-related-one> element. */ -public class GetRelatedOne extends MethodOperation { +public final class GetRelatedOne extends MethodOperation { public static final String module = GetRelatedOne.class.getName(); - String relationName; - ContextAccessor<GenericValue> toValueAcsr; - String useCacheStr; - ContextAccessor<Object> valueAcsr; + private final FlexibleStringExpander relationNameFse; + private final FlexibleMapAccessor<GenericValue> toValueFma; + private final FlexibleStringExpander useCacheFse; + private final FlexibleMapAccessor<GenericValue> valueFma; public GetRelatedOne(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - valueAcsr = new ContextAccessor<Object>(element.getAttribute("value-field"), element.getAttribute("value-name")); - toValueAcsr = new ContextAccessor<GenericValue>(element.getAttribute("to-value-field"), element.getAttribute("to-value-name")); - relationName = element.getAttribute("relation-name"); - useCacheStr = element.getAttribute("use-cache"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "relation-name", "to-value-field", "use-cache"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "relation-name", "to-value-field"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "to-value-field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field")); + relationNameFse = FlexibleStringExpander.getInstance(element.getAttribute("relation-name")); + toValueFma = FlexibleMapAccessor.getInstance(element.getAttribute("to-value-field")); + useCacheFse = FlexibleStringExpander.getInstance(element.getAttribute("use-cache")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String relationName = methodContext.expandString(this.relationName); - String useCacheStr = methodContext.expandString(this.useCacheStr); - boolean useCache = "true".equals(useCacheStr); - Object valueObject = valueAcsr.get(methodContext); - if (!(valueObject instanceof GenericValue)) { - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [env variable for value-name " + valueAcsr.toString() + " is not a GenericValue object; for the relation-name: " + relationName + "]"; - Debug.logError(errMsg, module); - methodContext.setErrorReturn(errMsg, simpleMethod); - return false; - } - GenericValue value = (GenericValue) valueObject; + GenericValue value = valueFma.get(methodContext.getEnvMap()); if (value == null) { - Debug.logWarning("Value not found with name: " + valueAcsr + ", not getting related...", module); - return true; + throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this); } + String relationName = relationNameFse.expandString(methodContext.getEnvMap()); + boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap())); try { - if (useCache) { - toValueAcsr.put(methodContext, value.getRelatedOneCache(relationName)); - } else { - toValueAcsr.put(methodContext, value.getRelatedOne(relationName)); - } + toValueFma.put(methodContext.getEnvMap(), value.getRelatedOne(relationName, useCache)); } catch (GenericEntityException e) { - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem getting related one from entity with name " + value.getEntityName() + " for the relation-name: " + relationName + ": " + e.getMessage() + "]"; - Debug.logError(e, errMsg, module); - methodContext.setErrorReturn(errMsg, simpleMethod); + String errMsg = "Exception thrown while finding related value: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; @@ -83,26 +79,42 @@ public class GetRelatedOne extends Metho @Override public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); + return FlexibleStringExpander.expandString(toString(), methodContext.getEnvMap()); } @Override public void gatherArtifactInfo(ArtifactInfoContext aic) { - aic.addEntityName(relationName); + aic.addEntityName(relationNameFse.toString()); } @Override public String rawString() { - // TODO: something more than the empty tag - return "<get-related-one/>"; + return toString(); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder("<get-related-one "); + sb.append("value-field=\"").append(this.valueFma).append("\" "); + sb.append("relation-name=\"").append(this.relationNameFse).append("\" "); + sb.append("to-value-field=\"").append(this.toValueFma).append("\" "); + if (!useCacheFse.isEmpty()) { + sb.append("use-cache=\"").append(this.useCacheFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <get-related-one> element. + */ public static final class GetRelatedOneFactory implements Factory<GetRelatedOne> { + @Override public GetRelatedOne createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new GetRelatedOne(element, simpleMethod); } + @Override public String getName() { return "get-related-one"; } |
| Free forum by Nabble | Edit this page |
