Search in sources :

Example 21 with MiniLangException

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

the class ValidateMethodCondition method checkCondition.

@Override
public boolean checkCondition(MethodContext methodContext) throws MiniLangException {
    Object fieldVal = fieldFma.get(methodContext.getEnvMap());
    if (fieldVal == null) {
        fieldVal = "";
    } else if (!(fieldVal instanceof String)) {
        try {
            fieldVal = MiniLangUtil.convertType(fieldVal, String.class, methodContext.getLocale(), methodContext.getTimeZone(), null);
        } catch (Exception e) {
            throw new MiniLangRuntimeException(e, this);
        }
    }
    Object[] params = new Object[] { fieldVal };
    try {
        Class<?> valClass = methodContext.getLoader().loadClass(className);
        Method valMethod = valClass.getMethod(methodName, paramTypes);
        Boolean resultBool = (Boolean) valMethod.invoke(null, params);
        return resultBool.booleanValue();
    } catch (Exception e) {
        throw new MiniLangRuntimeException(e, this);
    }
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) Method(java.lang.reflect.Method) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 22 with MiniLangException

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

the class CreateObject method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    Object[] args = null;
    Class<?>[] parameterTypes = null;
    if (parameters != null) {
        args = new Object[parameters.size()];
        parameterTypes = new Class<?>[parameters.size()];
        int i = 0;
        for (MethodObject<?> methodObjectDef : parameters) {
            args[i] = methodObjectDef.getObject(methodContext);
            Class<?> typeClass = null;
            try {
                typeClass = methodObjectDef.getTypeClass(methodContext);
            } catch (ClassNotFoundException e) {
                String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [Parameter type not found with name " + methodObjectDef.getTypeName() + "]";
                Debug.logWarning(e, errMsg, module);
                simpleMethod.addErrorMessage(methodContext, errMsg);
                return false;
            }
            parameterTypes[i] = typeClass;
            i++;
        }
    }
    try {
        Constructor<?> constructor = targetClass.getConstructor(parameterTypes);
        fieldFma.put(methodContext.getEnvMap(), constructor.newInstance(args));
    } catch (Exception e) {
        throw new MiniLangRuntimeException(e, this);
    }
    return true;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) StringObject(org.apache.ofbiz.minilang.method.StringObject) FieldObject(org.apache.ofbiz.minilang.method.FieldObject) MethodObject(org.apache.ofbiz.minilang.method.MethodObject) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 23 with MiniLangException

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

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

the class SimpleEventHandler method invoke.

/**
 * @see org.apache.ofbiz.webapp.event.EventHandler#invoke(ConfigXMLReader.Event, ConfigXMLReader.RequestMap, javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
 */
public String invoke(Event event, RequestMap requestMap, HttpServletRequest request, HttpServletResponse response) throws EventHandlerException {
    boolean beganTransaction = false;
    String xmlResource = event.path;
    String eventName = event.invoke;
    Locale locale = UtilHttp.getLocale(request);
    if (Debug.verboseOn())
        Debug.logVerbose("[Set path/method]: " + xmlResource + " / " + eventName, module);
    if (xmlResource == null) {
        throw new EventHandlerException("XML Resource (eventPath) cannot be null");
    }
    if (eventName == null) {
        throw new EventHandlerException("Event Name (eventMethod) cannot be null");
    }
    if (Debug.verboseOn())
        Debug.logVerbose("[Processing]: SIMPLE Event", module);
    try {
        beganTransaction = TransactionUtil.begin();
        String eventReturn = SimpleMethod.runSimpleEvent(xmlResource, eventName, request, response);
        if (Debug.verboseOn())
            Debug.logVerbose("[Event Return]: " + eventReturn, module);
        return eventReturn;
    } catch (MiniLangException e) {
        Debug.logError(e, module);
        String errMsg = UtilProperties.getMessage(SimpleEventHandler.err_resource, "simpleEventHandler.event_not_completed", (locale != null ? locale : Locale.getDefault())) + ": ";
        request.setAttribute("_ERROR_MESSAGE_", errMsg + e.getMessage());
        return "error";
    } catch (GenericTransactionException e) {
        Debug.logError(e, module);
        return "error";
    } finally {
        try {
            TransactionUtil.commit(beganTransaction);
        } catch (GenericTransactionException e) {
            Debug.logError(e, module);
        }
    }
}
Also used : Locale(java.util.Locale) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) GenericTransactionException(org.apache.ofbiz.entity.transaction.GenericTransactionException)

Example 25 with MiniLangException

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

Aggregations

MiniLangException (org.apache.ofbiz.minilang.MiniLangException)27 GenericValue (org.apache.ofbiz.entity.GenericValue)13 Locale (java.util.Locale)9 HashMap (java.util.HashMap)8 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)8 MiniLangRuntimeException (org.apache.ofbiz.minilang.MiniLangRuntimeException)8 LinkedList (java.util.LinkedList)7 Delegator (org.apache.ofbiz.entity.Delegator)7 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)6 HttpSession (javax.servlet.http.HttpSession)5 SimpleMethod (org.apache.ofbiz.minilang.SimpleMethod)5 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)5 Timestamp (java.sql.Timestamp)4 Map (java.util.Map)4 IOException (java.io.IOException)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 FileNotFoundException (java.io.FileNotFoundException)2 HashSet (java.util.HashSet)2