use of org.apache.ofbiz.minilang.method.MethodOperation in project ofbiz-framework by apache.
the class SimpleMethod method readOperations.
public static List<MethodOperation> readOperations(Element simpleMethodElement, SimpleMethod simpleMethod) throws MiniLangException {
Assert.notNull("simpleMethodElement", simpleMethodElement, "simpleMethod", simpleMethod);
List<? extends Element> operationElements = UtilXml.childElementList(simpleMethodElement);
ArrayList<MethodOperation> methodOperations = new ArrayList<MethodOperation>(operationElements.size());
if (UtilValidate.isNotEmpty(operationElements)) {
for (Element curOperElem : operationElements) {
String nodeName = UtilXml.getNodeNameIgnorePrefix(curOperElem);
MethodOperation methodOp = null;
MethodOperation.Factory<MethodOperation> factory = methodOperationFactories.get(nodeName);
if (factory != null) {
methodOp = factory.createMethodOperation(curOperElem, simpleMethod);
} else if ("else".equals(nodeName)) {
// don't add anything, but don't complain either, this one is handled in the individual operations
} else {
MiniLangValidate.handleError("Invalid element found", simpleMethod, curOperElem);
}
if (methodOp == null) {
continue;
}
methodOperations.add(methodOp);
DeprecatedOperation depOp = methodOp.getClass().getAnnotation(DeprecatedOperation.class);
if (depOp != null) {
MiniLangValidate.handleError("The " + nodeName + " operation has been deprecated in favor of the " + depOp.value() + " operation", simpleMethod, curOperElem);
}
}
}
methodOperations.trimToSize();
return methodOperations;
}
use of org.apache.ofbiz.minilang.method.MethodOperation in project ofbiz-framework by apache.
the class Iterate method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
if (listFma.isEmpty()) {
if (Debug.verboseOn()) {
Debug.logVerbose("Collection not found, doing nothing: " + this, module);
}
return true;
}
Object oldEntryValue = entryFma.get(methodContext.getEnvMap());
Object objList = listFma.get(methodContext.getEnvMap());
if (objList instanceof EntityListIterator) {
try (EntityListIterator eli = (EntityListIterator) objList) {
GenericValue theEntry;
while ((theEntry = eli.next()) != null) {
entryFma.put(methodContext.getEnvMap(), theEntry);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
return false;
}
}
} catch (MiniLangException e) {
if (e instanceof BreakElementException) {
break;
}
if (e instanceof ContinueElementException) {
continue;
}
throw e;
}
}
} catch (GenericEntityException e) {
throw new MiniLangRuntimeException("Error with entityListIterator: " + e.getMessage(), this);
}
} else if (objList instanceof Collection<?>) {
Collection<Object> theCollection = UtilGenerics.checkCollection(objList);
if (theCollection.size() == 0) {
if (Debug.verboseOn()) {
Debug.logVerbose("Collection has zero entries, doing nothing: " + this, module);
}
return true;
}
for (Object theEntry : theCollection) {
entryFma.put(methodContext.getEnvMap(), theEntry);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
return false;
}
}
} catch (MiniLangException e) {
if (e instanceof BreakElementException) {
break;
}
if (e instanceof ContinueElementException) {
continue;
}
throw e;
}
}
} else if (objList instanceof Iterator<?>) {
Iterator<Object> theIterator = UtilGenerics.cast(objList);
if (!theIterator.hasNext()) {
if (Debug.verboseOn()) {
Debug.logVerbose("Iterator has zero entries, doing nothing: " + this, module);
}
return true;
}
while (theIterator.hasNext()) {
Object theEntry = theIterator.next();
entryFma.put(methodContext.getEnvMap(), theEntry);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
return false;
}
}
} catch (MiniLangException e) {
if (e instanceof BreakElementException) {
break;
}
if (e instanceof ContinueElementException) {
continue;
}
throw e;
}
}
} else {
if (Debug.verboseOn()) {
Debug.logVerbose("Cannot iterate over a " + objList == null ? "null object" : objList.getClass().getName() + ", doing nothing: " + this, module);
}
return true;
}
entryFma.put(methodContext.getEnvMap(), oldEntryValue);
return true;
}
use of org.apache.ofbiz.minilang.method.MethodOperation in project ofbiz-framework by apache.
the class IterateMap method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
if (mapFma.isEmpty()) {
throw new MiniLangRuntimeException("No map specified.", this);
}
Object oldKey = keyFma.get(methodContext.getEnvMap());
Object oldValue = valueFma.get(methodContext.getEnvMap());
if (oldKey != null) {
if (Debug.verboseOn()) {
Debug.logVerbose("In iterate-map the key had a non-null value before entering the loop for the operation: " + this, module);
}
}
if (oldValue != null) {
if (Debug.verboseOn()) {
Debug.logVerbose("In iterate-map the value had a non-null value before entering the loop for the operation: " + this, module);
}
}
Map<? extends Object, ? extends Object> theMap = mapFma.get(methodContext.getEnvMap());
if (theMap == null) {
if (Debug.verboseOn()) {
Debug.logVerbose("Map not found with name " + mapFma + ", doing nothing: " + this, module);
}
return true;
}
if (theMap.size() == 0) {
if (Debug.verboseOn()) {
Debug.logVerbose("Map with name " + mapFma + " has zero entries, doing nothing: " + this, module);
}
return true;
}
for (Map.Entry<? extends Object, ? extends Object> theEntry : theMap.entrySet()) {
keyFma.put(methodContext.getEnvMap(), theEntry.getKey());
valueFma.put(methodContext.getEnvMap(), theEntry.getValue());
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
return false;
}
}
} catch (MiniLangException e) {
if (e instanceof BreakElementException) {
break;
}
if (e instanceof ContinueElementException) {
continue;
}
throw e;
}
}
return true;
}
use of org.apache.ofbiz.minilang.method.MethodOperation in project ofbiz-framework by apache.
the class Loop method exec.
@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
String countStr = this.countFse.expandString(methodContext.getEnvMap());
int count = 0;
try {
count = Double.valueOf(countStr).intValue();
} catch (NumberFormatException e) {
throw new MiniLangRuntimeException("Error while converting \"" + countStr + "\" to a number: " + e.getMessage(), this);
}
if (count < 0) {
throw new MiniLangRuntimeException("Unable to execute loop operation because the count is negative: " + countStr, this);
}
for (int i = 0; i < count; i++) {
this.fieldFma.put(methodContext.getEnvMap(), i);
try {
for (MethodOperation methodOperation : subOps) {
if (!methodOperation.exec(methodContext)) {
return false;
}
}
} catch (MiniLangException e) {
if (e instanceof BreakElementException) {
break;
}
if (e instanceof ContinueElementException) {
continue;
}
throw e;
}
}
return true;
}
Aggregations