Search in sources :

Example 1 with BreakElementException

use of org.apache.ofbiz.minilang.method.envops.Break.BreakElementException 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;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MethodOperation(org.apache.ofbiz.minilang.method.MethodOperation) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException) Iterator(java.util.Iterator) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) Collection(java.util.Collection) EntityListIterator(org.apache.ofbiz.entity.util.EntityListIterator) BreakElementException(org.apache.ofbiz.minilang.method.envops.Break.BreakElementException) ContinueElementException(org.apache.ofbiz.minilang.method.envops.Continue.ContinueElementException)

Example 2 with BreakElementException

use of org.apache.ofbiz.minilang.method.envops.Break.BreakElementException 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;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MethodOperation(org.apache.ofbiz.minilang.method.MethodOperation) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) Map(java.util.Map) BreakElementException(org.apache.ofbiz.minilang.method.envops.Break.BreakElementException) ContinueElementException(org.apache.ofbiz.minilang.method.envops.Continue.ContinueElementException)

Example 3 with BreakElementException

use of org.apache.ofbiz.minilang.method.envops.Break.BreakElementException 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;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MethodOperation(org.apache.ofbiz.minilang.method.MethodOperation) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) BreakElementException(org.apache.ofbiz.minilang.method.envops.Break.BreakElementException) ContinueElementException(org.apache.ofbiz.minilang.method.envops.Continue.ContinueElementException)

Aggregations

MiniLangException (org.apache.ofbiz.minilang.MiniLangException)3 MiniLangRuntimeException (org.apache.ofbiz.minilang.MiniLangRuntimeException)3 MethodOperation (org.apache.ofbiz.minilang.method.MethodOperation)3 BreakElementException (org.apache.ofbiz.minilang.method.envops.Break.BreakElementException)3 ContinueElementException (org.apache.ofbiz.minilang.method.envops.Continue.ContinueElementException)3 Collection (java.util.Collection)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)1 GenericValue (org.apache.ofbiz.entity.GenericValue)1 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)1