Search in sources :

Example 21 with MiniLangRuntimeException

use of org.apache.ofbiz.minilang.MiniLangRuntimeException 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 22 with MiniLangRuntimeException

use of org.apache.ofbiz.minilang.MiniLangRuntimeException 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 23 with MiniLangRuntimeException

use of org.apache.ofbiz.minilang.MiniLangRuntimeException 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)

Example 24 with MiniLangRuntimeException

use of org.apache.ofbiz.minilang.MiniLangRuntimeException in project ofbiz-framework by apache.

the class StringToList method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    String valueStr = stringFse.expandString(methodContext.getEnvMap());
    List<? extends Object> argList = argListFma.get(methodContext.getEnvMap());
    if (argList != null) {
        try {
            valueStr = MessageFormat.format(valueStr, argList.toArray());
        } catch (IllegalArgumentException e) {
            throw new MiniLangRuntimeException("Exception thrown while formatting the string attribute: " + e.getMessage(), this);
        }
    }
    Object value;
    if (UtilValidate.isNotEmpty(this.messageFieldName)) {
        value = new MessageString(valueStr, this.messageFieldName, true);
    } else {
        value = valueStr;
    }
    List<Object> toList = listFma.get(methodContext.getEnvMap());
    if (toList == null) {
        toList = new LinkedList<Object>();
        listFma.put(methodContext.getEnvMap(), toList);
    }
    toList.add(value);
    return true;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MessageString(org.apache.ofbiz.base.util.MessageString) MessageString(org.apache.ofbiz.base.util.MessageString)

Example 25 with MiniLangRuntimeException

use of org.apache.ofbiz.minilang.MiniLangRuntimeException in project ofbiz-framework by apache.

the class WebappPropertyToField method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    if (methodContext.getMethodType() == MethodContext.EVENT) {
        String resource = resourceFse.expandString(methodContext.getEnvMap());
        ServletContext servletContext = (ServletContext) methodContext.getRequest().getAttribute("servletContext");
        URL propsUrl = null;
        try {
            propsUrl = servletContext.getResource(resource);
        } catch (java.net.MalformedURLException e) {
            throw new MiniLangRuntimeException("Exception thrown while finding properties file " + resource + ": " + e.getMessage(), this);
        }
        if (propsUrl == null) {
            throw new MiniLangRuntimeException("Properties file " + resource + " not found.", this);
        }
        String property = propertyFse.expandString(methodContext.getEnvMap());
        String fieldVal = UtilProperties.getPropertyValue(propsUrl, property);
        if (fieldVal == null) {
            fieldVal = defaultFse.expandString(methodContext.getEnvMap());
        }
        fieldFma.put(methodContext.getEnvMap(), fieldVal);
    }
    return true;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) ServletContext(javax.servlet.ServletContext) URL(java.net.URL)

Aggregations

MiniLangRuntimeException (org.apache.ofbiz.minilang.MiniLangRuntimeException)26 GenericValue (org.apache.ofbiz.entity.GenericValue)10 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)8 MiniLangException (org.apache.ofbiz.minilang.MiniLangException)8 Delegator (org.apache.ofbiz.entity.Delegator)5 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 SimpleMethod (org.apache.ofbiz.minilang.SimpleMethod)2 FieldObject (org.apache.ofbiz.minilang.method.FieldObject)2 MethodObject (org.apache.ofbiz.minilang.method.MethodObject)2 StringObject (org.apache.ofbiz.minilang.method.StringObject)2 Calendar (com.ibm.icu.util.Calendar)1 Method (java.lang.reflect.Method)1 URL (java.net.URL)1 Timestamp (java.sql.Timestamp)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedList (java.util.LinkedList)1