|
Modified: ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java
URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityData.java Tue Jul 10 12:05:55 2012 @@ -27,52 +27,70 @@ 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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Centitydata%3E}}">Mini-language Reference</a> */ -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")); + mode = MiniLangValidate.checkAttribute(element.getAttribute("mode"), "load"); + String timeoutAttribute = element.getAttribute("timeout"); + if (!"load".equals(mode) && !timeoutAttribute.isEmpty()) { + MiniLangValidate.handleError("timeout attribute is valid only when mode=\"load\".", simpleMethod, element); } + 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")); } @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 +104,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,31 +124,37 @@ 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; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - @Override - public String rawString() { - // TODO: something more than the empty tag - return "<entity-data/>"; + 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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/EntityOne.java Tue Jul 10 12:05:55 2012 @@ -23,22 +23,32 @@ import org.ofbiz.base.util.GeneralExcept 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.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 primary key + * Implements the <entity-one> element. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Centityone%3E}}">Mini-language Reference</a> */ -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); } @@ -48,41 +58,36 @@ 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; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - public String getEntityName() { - return this.finder.getEntityName(); + public void gatherArtifactInfo(ArtifactInfoContext aic) { + aic.addEntityName(this.finder.getEntityName()); } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<entity-one/>"; + 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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByAnd.java Tue Jul 10 12:05:55 2012 @@ -21,61 +21,72 @@ package org.ofbiz.minilang.method.entity import java.util.List; import java.util.Map; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; 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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cfilterlistbyand%3E}}">Mini-language Reference</a> */ -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(); - } - - @Override - public String rawString() { - // TODO: something more than the empty tag - return "<filter-list-by-and/>"; + 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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FilterListByDate.java Tue Jul 10 12:05:55 2012 @@ -22,72 +22,82 @@ 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.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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cfilterlistbydate%3E}}">Mini-language Reference</a> */ -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(); - } - - @Override - public String rawString() { - // TODO: something more than the empty tag - return "<filter-list-by-date/>"; + 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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByAnd.java Tue Jul 10 12:05:55 2012 @@ -18,114 +18,134 @@ *******************************************************************************/ package org.ofbiz.minilang.method.entityops; +import java.util.Collection; import java.util.List; import java.util.Map; import org.ofbiz.base.util.Debug; -import org.ofbiz.base.util.UtilValidate; +import org.ofbiz.base.util.UtilMisc; +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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cfindbyand%3E}}">Mini-language Reference</a> */ -public class FindByAnd extends MethodOperation { +public final class FindByAnd extends MethodOperation { public static final String module = FindByAnd.class.getName(); - String delegatorName; - String entityName; - ContextAccessor<Object> listAcsr; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - ContextAccessor<List<String>> orderByListAcsr; - String useCacheStr; - String useIteratorStr; + private final FlexibleStringExpander delegatorNameFse; + private final FlexibleStringExpander entityNameFse; + private final FlexibleMapAccessor<Collection<String>> fieldsToSelectListFma; + 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; 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", "fields-to-select-list", "use-iterator", "list", "map", "order-by-list", "delegator-name"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "entity-name", "list", "map"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "map", "fields-to-select-list", "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")); + fieldsToSelectListFma = FlexibleMapAccessor.getInstance(element.getAttribute("fields-to-select-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()); + Collection<String> fieldsToSelectList = fieldsToSelectListFma.get(methodContext.getEnvMap()); Delegator delegator = methodContext.getDelegator(); - if (UtilValidate.isNotEmpty(delegatorName)) { + if (!delegatorName.isEmpty()) { delegator = DelegatorFactory.getDelegator(delegatorName); } try { + EntityCondition whereCond = null; + Map<String, ? extends Object> fieldMap = mapFma.get(methodContext.getEnvMap()); + if (fieldMap != null) { + whereCond = EntityCondition.makeCondition(fieldMap); + } if (useIterator) { - EntityCondition whereCond = null; - if (!mapAcsr.isEmpty()) { - whereCond = EntityCondition.makeCondition(mapAcsr.get(methodContext)); - } - listAcsr.put(methodContext, delegator.find(entityName, whereCond, null, null, orderByNames, null)); + listFma.put(methodContext.getEnvMap(), delegator.find(entityName, whereCond, null, UtilMisc.toSet(fieldsToSelectList), orderByNames, null)); } else { - if (useCache) { - listAcsr.put(methodContext, delegator.findByAndCache(entityName, mapAcsr.get(methodContext), orderByNames)); - } else { - listAcsr.put(methodContext, delegator.findByAnd(entityName, mapAcsr.get(methodContext), orderByNames)); - } + listFma.put(methodContext.getEnvMap(), delegator.findList(entityName, whereCond, UtilMisc.toSet(fieldsToSelectList), orderByNames, null, 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; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - public String getEntityName() { - return this.entityName; + public void gatherArtifactInfo(ArtifactInfoContext aic) { + aic.addEntityName(entityNameFse.toString()); } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<find-by-and/>"; + 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 (!fieldsToSelectListFma.isEmpty()) { + sb.append("fields-to-select-list=\"").append(this.fieldsToSelectListFma).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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/FindByPrimaryKey.java Tue Jul 10 12:05:55 2012 @@ -23,98 +23,124 @@ import java.util.Map; 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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cfindbyprimarykey%3E}}">Mini-language Reference</a> */ -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.toSet(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; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - public String getEntityName() { - return this.entityName; + public void gatherArtifactInfo(ArtifactInfoContext aic) { + aic.addEntityName(entityNameFse.toString()); } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<find-by-primary-key/>"; - } - + 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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelated.java Tue Jul 10 12:05:55 2012 @@ -22,93 +22,110 @@ import java.util.List; import java.util.Map; 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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cgetrelated%3E}}">Mini-language Reference</a> */ -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)); + listFma.put(methodContext.getEnvMap(), value.getRelatedCache(relationName, constraintMap, orderByNames)); } else { - listAcsr.put(methodContext, value.getRelated(relationName, constraintMap, orderByNames)); + listFma.put(methodContext.getEnvMap(), value.getRelated(relationName, constraintMap, orderByNames)); } } 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; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - public String getRelationName() { - return this.relationName; + public void gatherArtifactInfo(ArtifactInfoContext aic) { + aic.addEntityName(relationNameFse.toString()); } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<get-related/>"; - } - + 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/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/GetRelatedOne.java Tue Jul 10 12:05:55 2012 @@ -19,88 +19,98 @@ package org.ofbiz.minilang.method.entityops; 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. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cgetrelatedone%3E}}">Mini-language Reference</a> */ -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)); + toValueFma.put(methodContext.getEnvMap(), value.getRelatedOneCache(relationName)); } else { - toValueAcsr.put(methodContext, value.getRelatedOne(relationName)); + toValueFma.put(methodContext.getEnvMap(), value.getRelatedOne(relationName)); } } 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; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - public String getRelationName() { - return this.relationName; + public void gatherArtifactInfo(ArtifactInfoContext aic) { + aic.addEntityName(relationNameFse.toString()); } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<get-related-one/>"; + 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"; } Modified: ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeNextSeqId.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeNextSeqId.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeNextSeqId.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeNextSeqId.java Tue Jul 10 12:05:55 2012 @@ -18,79 +18,99 @@ *******************************************************************************/ package org.ofbiz.minilang.method.entityops; -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.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.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Look at existing values for a sub-entity with a sequenced secondary ID, and get the highest plus 1 + * Implements the <make-next-seq-id> element. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cmakenextseqid%3E}}">Mini-language Reference</a> */ -public class MakeNextSeqId extends MethodOperation { +public final class MakeNextSeqId extends MethodOperation { public static final String module = MakeNextSeqId.class.getName(); - String incrementByStr; - String numericPaddingStr; - String seqFieldName; - ContextAccessor<GenericValue> valueAcsr; + private final FlexibleStringExpander incrementByFse; + private final FlexibleStringExpander numericPaddingFse; + private final FlexibleStringExpander seqFieldNameFse; + private final FlexibleMapAccessor<GenericValue> valueFma; public MakeNextSeqId(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - seqFieldName = element.getAttribute("seq-field-name"); - valueAcsr = new ContextAccessor<GenericValue>(element.getAttribute("value-field"), element.getAttribute("value-name")); - numericPaddingStr = element.getAttribute("numeric-padding"); - incrementByStr = element.getAttribute("increment-by"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "seq-field-name", "increment-by", "numeric-padding"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "seq-field-name"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + seqFieldNameFse = FlexibleStringExpander.getInstance(element.getAttribute("seq-field-name")); + valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field")); + numericPaddingFse = FlexibleStringExpander.getInstance(element.getAttribute("numeric-padding")); + incrementByFse = FlexibleStringExpander.getInstance(element.getAttribute("increment-by")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String seqFieldName = methodContext.expandString(this.seqFieldName); - String numericPaddingStr = methodContext.expandString(this.numericPaddingStr); - String incrementByStr = methodContext.expandString(this.incrementByStr); + GenericValue value = valueFma.get(methodContext.getEnvMap()); + if (value == null) { + throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this); + } + String seqFieldName = seqFieldNameFse.expandString(methodContext.getEnvMap()); + String numericPaddingStr = numericPaddingFse.expandString(methodContext.getEnvMap()); + String incrementByStr = incrementByFse.expandString(methodContext.getEnvMap()); int numericPadding = 5; - int incrementBy = 1; - try { - if (UtilValidate.isNotEmpty(numericPaddingStr)) { + if (!numericPaddingStr.isEmpty()) { + try { numericPadding = Integer.parseInt(numericPaddingStr); + } catch (Exception e) { + throw new MiniLangRuntimeException("Invalid number in \"numeric-padding\" attribute", this); } - } catch (Exception e) { - Debug.logError(e, "numeric-padding format invalid for [" + numericPaddingStr + "]", module); } - try { - if (UtilValidate.isNotEmpty(incrementByStr)) { + int incrementBy = 1; + if (!incrementByStr.isEmpty()) { + try { incrementBy = Integer.parseInt(incrementByStr); + } catch (Exception e) { + throw new MiniLangRuntimeException("Invalid number in \"increment-by\" attribute", this); } - } catch (Exception e) { - Debug.logError(e, "increment-by format invalid for [" + incrementByStr + "]", module); } - GenericValue value = valueAcsr.get(methodContext); methodContext.getDelegator().setNextSubSeqId(value, seqFieldName, numericPadding, incrementBy); return true; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - @Override - public String rawString() { - // TODO: something more than the empty tag - return "<make-next-seq-id/>"; + public String toString() { + StringBuilder sb = new StringBuilder("<make-next-seq-id "); + sb.append("value-field=\"").append(this.valueFma).append("\" "); + sb.append("seq-field-name=\"").append(this.seqFieldNameFse).append("\" "); + if (!incrementByFse.isEmpty()) { + sb.append("increment-by=\"").append(this.incrementByFse).append("\" "); + } + if (!numericPaddingFse.isEmpty()) { + sb.append("numeric-padding=\"").append(this.numericPaddingFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <make-next-seq-id> element. + */ public static final class MakeNextSeqIdFactory implements Factory<MakeNextSeqId> { + @Override public MakeNextSeqId createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new MakeNextSeqId(element, simpleMethod); } + @Override public String getName() { return "make-next-seq-id"; } Modified: ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeValue.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeValue.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeValue.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/MakeValue.java Tue Jul 10 12:05:55 2012 @@ -20,59 +20,79 @@ package org.ofbiz.minilang.method.entity import java.util.Map; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; +import org.ofbiz.base.util.string.FlexibleStringExpander; 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 entity values by anding the map fields + * Implements the <make-value> element. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cmakevalue%3E}}">Mini-language Reference</a> */ -public class MakeValue extends MethodOperation { +public final class MakeValue extends MethodOperation { - String entityName; - ContextAccessor<Map<String, ? extends Object>> mapAcsr; - ContextAccessor<GenericValue> valueAcsr; + private final FlexibleStringExpander entityNameFse; + private final FlexibleMapAccessor<Map<String, ? extends Object>> mapFma; + private final FlexibleMapAccessor<GenericValue> valueFma; public MakeValue(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")); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "entity-name", "map"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field", "entity-name"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field", "map"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field")); + entityNameFse = FlexibleStringExpander.getInstance(element.getAttribute("entity-name")); + mapFma = FlexibleMapAccessor.getInstance(element.getAttribute("map")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - String entityName = methodContext.expandString(this.entityName); - Map<String, ? extends Object> ctxMap = (mapAcsr.isEmpty() ? null : mapAcsr.get(methodContext)); - valueAcsr.put(methodContext, methodContext.getDelegator().makeValidValue(entityName, ctxMap)); + String entityName = entityNameFse.expandString(methodContext.getEnvMap()); + if (entityName.isEmpty()) { + throw new MiniLangRuntimeException("Entity name not found.", this); + } + valueFma.put(methodContext.getEnvMap(), methodContext.getDelegator().makeValidValue(entityName, mapFma.get(methodContext.getEnvMap()))); return true; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - public String getEntityName() { - return this.entityName; + public void gatherArtifactInfo(ArtifactInfoContext aic) { + aic.addEntityName(entityNameFse.toString()); } @Override - public String rawString() { - // TODO: something more than the empty tag - return "<make-value/>"; + public String toString() { + StringBuilder sb = new StringBuilder("<make-value "); + sb.append("entity-name=\"").append(this.entityNameFse).append("\" "); + sb.append("value-field=\"").append(this.valueFma).append("\" "); + if (!mapFma.isEmpty()) { + sb.append("map=\"").append(this.mapFma).append("\" "); + } + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <make-value> element. + */ public static final class MakeValueFactory implements Factory<MakeValue> { + @Override public MakeValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new MakeValue(element, simpleMethod); } + @Override public String getName() { return "make-value"; } Modified: ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/OrderValueList.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/OrderValueList.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/OrderValueList.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/OrderValueList.java Tue Jul 10 12:05:55 2012 @@ -20,61 +20,72 @@ package org.ofbiz.minilang.method.entity import java.util.List; +import org.ofbiz.base.util.collections.FlexibleMapAccessor; 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; /** - * Order the given list of GenericValue objects + * Implements the <order-value-list> element. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Cordervaluelist%3E}}">Mini-language Reference</a> */ -public class OrderValueList extends MethodOperation { +public final class OrderValueList extends MethodOperation { - ContextAccessor<List<? extends GenericEntity>> listAcsr; - ContextAccessor<List<String>> orderByListAcsr; - ContextAccessor<List<? extends GenericEntity>> toListAcsr; + private final FlexibleMapAccessor<List<? extends GenericEntity>> listFma; + private final FlexibleMapAccessor<List<String>> orderByListFma; + private final FlexibleMapAccessor<List<? extends GenericEntity>> toListFma; public OrderValueList(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - listAcsr = new ContextAccessor<List<? extends GenericEntity>>(element.getAttribute("list"), element.getAttribute("list-name")); - toListAcsr = new ContextAccessor<List<? extends GenericEntity>>(element.getAttribute("to-list"), element.getAttribute("to-list-name")); - if (toListAcsr.isEmpty()) { - toListAcsr = listAcsr; + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "list", "order-by-list", "to-list"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "list", "order-by-list"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "list", "order-by-list", "to-list"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + listFma = FlexibleMapAccessor.getInstance(element.getAttribute("list")); + orderByListFma = FlexibleMapAccessor.getInstance(element.getAttribute("order-by-list")); + String toListAttribute = element.getAttribute("to-list"); + if (toListAttribute.isEmpty()) { + toListFma = listFma; + } else { + toListFma = FlexibleMapAccessor.getInstance(toListAttribute); } - orderByListAcsr = new ContextAccessor<List<String>>(element.getAttribute("order-by-list"), element.getAttribute("order-by-list-name")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - List<String> orderByList = null; - if (!orderByListAcsr.isEmpty()) { - orderByList = orderByListAcsr.get(methodContext); - } - toListAcsr.put(methodContext, EntityUtil.orderBy(listAcsr.get(methodContext), orderByList)); + List<String> orderByList = orderByListFma.get(methodContext.getEnvMap()); + toListFma.put(methodContext.getEnvMap(), EntityUtil.orderBy(listFma.get(methodContext.getEnvMap()), orderByList)); return true; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - @Override - public String rawString() { - // TODO: something more than the empty tag - return "<order-value-list/>"; + public String toString() { + StringBuilder sb = new StringBuilder("<order-value-list "); + sb.append("list=\"").append(this.listFma).append("\" "); + sb.append("order-by-list=\"").append(this.orderByListFma).append("\" "); + sb.append("to-list=\"").append(this.toListFma).append("\" "); + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <order-value-list> element. + */ public static final class OrderValueListFactory implements Factory<OrderValueList> { + @Override public OrderValueList createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new OrderValueList(element, simpleMethod); } + @Override public String getName() { return "order-value-list"; } Modified: ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/RefreshValue.java URL: http://svn.apache.org/viewvc/ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/RefreshValue.java?rev=1359626&r1=1359625&r2=1359626&view=diff ============================================================================== --- ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/RefreshValue.java (original) +++ ofbiz/branches/release12.04/framework/minilang/src/org/ofbiz/minilang/method/entityops/RefreshValue.java Tue Jul 10 12:05:55 2012 @@ -19,69 +19,81 @@ package org.ofbiz.minilang.method.entityops; 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.method.MethodContext; import org.ofbiz.minilang.method.MethodOperation; import org.w3c.dom.Element; /** - * Uses the delegator to refresh the specified value object entity from the datasource + * Implements the <refresh-value> element. + * + * @see <a href="https://cwiki.apache.org/OFBADMIN/mini-language-reference.html#Mini-languageReference-{{%3Crefreshvalue%3E}}">Mini-language Reference</a> */ -public class RefreshValue extends MethodOperation { +public final class RefreshValue extends MethodOperation { public static final String module = RemoveValue.class.getName(); - String doCacheClearStr; - ContextAccessor<GenericValue> valueAcsr; + private final FlexibleStringExpander doCacheClearFse; + private final FlexibleMapAccessor<GenericValue> valueFma; public RefreshValue(Element element, SimpleMethod simpleMethod) throws MiniLangException { super(element, simpleMethod); - valueAcsr = new ContextAccessor<GenericValue>(element.getAttribute("value-field"), element.getAttribute("value-name")); - doCacheClearStr = element.getAttribute("do-cache-clear"); + if (MiniLangValidate.validationOn()) { + MiniLangValidate.attributeNames(simpleMethod, element, "value-field", "do-cache-clear"); + MiniLangValidate.requiredAttributes(simpleMethod, element, "value-field"); + MiniLangValidate.expressionAttributes(simpleMethod, element, "value-field"); + MiniLangValidate.noChildElements(simpleMethod, element); + } + valueFma = FlexibleMapAccessor.getInstance(element.getAttribute("value-field")); + doCacheClearFse = FlexibleStringExpander.getInstance(element.getAttribute("do-cache-clear")); } @Override public boolean exec(MethodContext methodContext) throws MiniLangException { - boolean doCacheClear = !"false".equals(methodContext.expandString(doCacheClearStr)); - GenericValue value = valueAcsr.get(methodContext); + GenericValue value = valueFma.get(methodContext.getEnvMap()); if (value == null) { - String errMsg = "In remove-value a value was not found with the specified valueAcsr: " + valueAcsr + ", not removing"; - Debug.logWarning(errMsg, module); - methodContext.setErrorReturn(errMsg, simpleMethod); - return false; + throw new MiniLangRuntimeException("Entity value not found with name: " + valueFma, this); } + boolean doCacheClear = !"false".equals(doCacheClearFse.expandString(methodContext.getEnvMap())); try { methodContext.getDelegator().refresh(value, doCacheClear); } catch (GenericEntityException e) { - String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem removing the " + valueAcsr + " value: " + e.getMessage() + "]"; - Debug.logError(e, errMsg, module); - methodContext.setErrorReturn(errMsg, simpleMethod); + String errMsg = "Exception thrown while refreshing value: " + e.getMessage(); + Debug.logWarning(e, errMsg, module); + simpleMethod.addErrorMessage(methodContext, errMsg); return false; } return true; } @Override - public String expandedString(MethodContext methodContext) { - // TODO: something more than a stub/dummy - return this.rawString(); - } - - @Override - public String rawString() { - // TODO: something more than the empty tag - return "<refresh-value/>"; + public String toString() { + StringBuilder sb = new StringBuilder("<refresh-value "); + sb.append("value-field=\"").append(this.valueFma).append("\" "); + if (!doCacheClearFse.isEmpty()) { + sb.append("do-cache-clear=\"").append(this.doCacheClearFse).append("\" "); + } + sb.append("/>"); + return sb.toString(); } + /** + * A factory for the <refresh-value> element. + */ public static final class RefreshValueFactory implements Factory<RefreshValue> { + @Override public RefreshValue createMethodOperation(Element element, SimpleMethod simpleMethod) throws MiniLangException { return new RefreshValue(element, simpleMethod); } + @Override public String getName() { return "refresh-value"; } |
| Free forum by Nabble | Edit this page |
