Search in sources :

Example 1 with MiniLangException

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

the class CallService method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    if (methodContext.isTraceOn()) {
        outputTraceMessage(methodContext, "Begin call-service.");
    }
    String serviceName = serviceNameFse.expandString(methodContext.getEnvMap());
    String errorCode = this.errorCode;
    if (errorCode.isEmpty()) {
        errorCode = simpleMethod.getDefaultErrorCode();
    }
    String successCode = this.successCode;
    if (successCode.isEmpty()) {
        successCode = simpleMethod.getDefaultSuccessCode();
    }
    Map<String, Object> inMap = inMapFma.get(methodContext.getEnvMap());
    if (inMap == null) {
        inMap = new HashMap<String, Object>();
    }
    // before invoking the service, clear messages
    if (methodContext.getMethodType() == MethodContext.EVENT) {
        methodContext.removeEnv(simpleMethod.getEventErrorMessageName());
        methodContext.removeEnv(simpleMethod.getEventEventMessageName());
        methodContext.removeEnv(simpleMethod.getEventResponseCodeName());
    } else {
        methodContext.removeEnv(simpleMethod.getServiceErrorMessageName());
        methodContext.removeEnv(simpleMethod.getServiceSuccessMessageName());
        methodContext.removeEnv(simpleMethod.getServiceResponseMessageName());
    }
    // add UserLogin to context if expected
    if (includeUserLogin) {
        GenericValue userLogin = methodContext.getUserLogin();
        if (userLogin != null && inMap.get("userLogin") == null) {
            inMap.put("userLogin", userLogin);
        }
    }
    // always add Locale to context unless null
    Locale locale = methodContext.getLocale();
    if (locale != null) {
        inMap.put("locale", locale);
    }
    // invoke the service
    Map<String, Object> result = null;
    try {
        ModelService modelService = methodContext.getDispatcher().getDispatchContext().getModelService(serviceName);
        int timeout = modelService.transactionTimeout;
        if (this.transactionTimeout >= 0) {
            timeout = this.transactionTimeout;
        }
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Invoking service \"" + serviceName + "\", require-new-transaction = " + requireNewTransaction + ", transaction-timeout = " + timeout + ", IN attributes:", inMap.toString());
        }
        result = methodContext.getDispatcher().runSync(serviceName, inMap, timeout, requireNewTransaction);
    } catch (GenericServiceException e) {
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Service engine threw an exception: " + e.getMessage());
        }
        String errMsg = "ERROR: Could not complete the " + simpleMethod.getShortDescription() + " process [problem invoking the [" + serviceName + "] service with the map named [" + inMapFma + "] containing [" + inMap + "]: " + e.getMessage() + "]";
        Debug.logError(e, errMsg, module);
        if (breakOnError) {
            if (methodContext.getMethodType() == MethodContext.EVENT) {
                methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errMsg);
                methodContext.putEnv(simpleMethod.getEventResponseCodeName(), errorCode);
            } else {
                methodContext.putEnv(simpleMethod.getServiceErrorMessageName(), errMsg);
                methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), errorCode);
            }
            if (methodContext.isTraceOn()) {
                outputTraceMessage(methodContext, "break-on-error set to \"true\", halting script execution. End call-service.");
            }
            return false;
        } else {
            if (methodContext.isTraceOn()) {
                outputTraceMessage(methodContext, "End call-service.");
            }
            return true;
        }
    }
    if (resultsToMapList != null) {
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Processing " + resultsToMapList.size() + " <results-to-map> elements.");
        }
        for (String mapName : resultsToMapList) {
            methodContext.putEnv(mapName, UtilMisc.makeMapWritable(result));
        }
    }
    if (resultToFieldList != null) {
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Processing " + resultToFieldList.size() + " <result-to-field> elements.");
        }
        for (ResultToField rtfDef : resultToFieldList) {
            rtfDef.exec(methodContext, result);
        }
    }
    if (resultToResultList != null) {
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Processing " + resultToResultList.size() + " <result-to-result> elements.");
        }
        for (ResultToResult rtrDef : resultToResultList) {
            rtrDef.exec(methodContext, result);
        }
    }
    if (methodContext.getMethodType() == MethodContext.EVENT) {
        if (resultToRequestList != null) {
            if (methodContext.isTraceOn()) {
                outputTraceMessage(methodContext, "Processing " + resultToRequestList.size() + " <result-to-request> elements.");
            }
            for (ResultToRequest rtrDef : resultToRequestList) {
                rtrDef.exec(methodContext, result);
            }
        }
        if (resultToSessionList != null) {
            if (methodContext.isTraceOn()) {
                outputTraceMessage(methodContext, "Processing " + resultToSessionList.size() + " <result-to-session> elements.");
            }
            for (ResultToSession rtsDef : resultToSessionList) {
                rtsDef.exec(methodContext, result);
            }
        }
    }
    String errorPrefixStr = errorPrefix.getMessage(methodContext.getLoader(), methodContext);
    String errorSuffixStr = errorSuffix.getMessage(methodContext.getLoader(), methodContext);
    String successPrefixStr = successPrefix.getMessage(methodContext.getLoader(), methodContext);
    String successSuffixStr = successSuffix.getMessage(methodContext.getLoader(), methodContext);
    String messagePrefixStr = messagePrefix.getMessage(methodContext.getLoader(), methodContext);
    String messageSuffixStr = messageSuffix.getMessage(methodContext.getLoader(), methodContext);
    String errorMessage = null;
    List<String> errorMessageList = null;
    // See if there is a single message
    if (result.containsKey(ModelService.ERROR_MESSAGE)) {
        errorMessage = ServiceUtil.makeErrorMessage(result, messagePrefixStr, messageSuffixStr, errorPrefixStr, errorSuffixStr);
    } else if (result.containsKey(ModelService.ERROR_MESSAGE_LIST)) {
        errorMessageList = UtilGenerics.checkList(result.get(ModelService.ERROR_MESSAGE_LIST));
    }
    if ((UtilValidate.isNotEmpty(errorMessage) || UtilValidate.isNotEmpty(errorMessageList)) && breakOnError) {
        if (methodContext.getMethodType() == MethodContext.EVENT) {
            if (UtilValidate.isNotEmpty(errorMessage)) {
                if (Debug.verboseOn()) {
                    errorMessage += UtilProperties.getMessage(resource, "simpleMethod.error_show_service_name", UtilMisc.toMap("serviceName", serviceName, "methodName", simpleMethod.getMethodName()), locale);
                }
                methodContext.putEnv(simpleMethod.getEventErrorMessageName(), errorMessage);
            } else {
                if (Debug.verboseOn()) {
                    errorMessageList.add(UtilProperties.getMessage(resource, "simpleMethod.error_show_service_name", UtilMisc.toMap("serviceName", serviceName, "methodName", simpleMethod.getMethodName()), locale));
                }
                methodContext.putEnv(simpleMethod.getEventErrorMessageListName(), errorMessageList);
            }
        } else {
            ServiceUtil.addErrors(UtilMisc.<String, String>getListFromMap(methodContext.getEnvMap(), this.simpleMethod.getServiceErrorMessageListName()), UtilMisc.<String, String, Object>getMapFromMap(methodContext.getEnvMap(), this.simpleMethod.getServiceErrorMessageMapName()), result);
            Debug.logError(new Exception(errorMessage), module);
        }
    }
    String successMessage = ServiceUtil.makeSuccessMessage(result, messagePrefixStr, messageSuffixStr, successPrefixStr, successSuffixStr);
    if (UtilValidate.isNotEmpty(successMessage)) {
        if (methodContext.getMethodType() == MethodContext.EVENT) {
            methodContext.putEnv(simpleMethod.getEventEventMessageName(), successMessage);
        } else {
            methodContext.putEnv(simpleMethod.getServiceSuccessMessageName(), successMessage);
        }
    }
    String defaultMessageStr = defaultMessage.getMessage(methodContext.getLoader(), methodContext);
    if (UtilValidate.isEmpty(errorMessage) && UtilValidate.isEmpty(errorMessageList) && UtilValidate.isEmpty(successMessage) && UtilValidate.isNotEmpty(defaultMessageStr)) {
        if (methodContext.getMethodType() == MethodContext.EVENT) {
            methodContext.putEnv(simpleMethod.getEventEventMessageName(), defaultMessageStr);
        } else {
            methodContext.putEnv(simpleMethod.getServiceSuccessMessageName(), defaultMessageStr);
        }
    }
    String responseCode = result.containsKey(ModelService.RESPONSE_MESSAGE) ? (String) result.get(ModelService.RESPONSE_MESSAGE) : successCode;
    if (errorCode.equals(responseCode)) {
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Service returned an error.");
        }
        if (breakOnError) {
            if (methodContext.getMethodType() == MethodContext.EVENT) {
                methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
            } else {
                methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
            }
            if (methodContext.isTraceOn()) {
                outputTraceMessage(methodContext, "break-on-error set to \"true\", halting script execution. End call-service.");
            }
            return false;
        } else {
            if (methodContext.isTraceOn()) {
                outputTraceMessage(methodContext, "End call-service.");
            }
            return true;
        }
    } else {
        if (methodContext.isTraceOn()) {
            outputTraceMessage(methodContext, "Service ran successfully. End call-service.");
        }
        if (methodContext.getMethodType() == MethodContext.EVENT) {
            methodContext.putEnv(simpleMethod.getEventResponseCodeName(), responseCode);
        } else {
            methodContext.putEnv(simpleMethod.getServiceResponseMessageName(), responseCode);
        }
        return true;
    }
}
Also used : Locale(java.util.Locale) GenericValue(org.apache.ofbiz.entity.GenericValue) GenericServiceException(org.apache.ofbiz.service.GenericServiceException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException) ModelService(org.apache.ofbiz.service.ModelService) GenericServiceException(org.apache.ofbiz.service.GenericServiceException)

Example 2 with MiniLangException

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

the class CallSimpleMethod method gatherArtifactInfo.

@Override
public void gatherArtifactInfo(ArtifactInfoContext aic) {
    SimpleMethod simpleMethodToCall;
    try {
        simpleMethodToCall = SimpleMethod.getSimpleMethod(this.xmlURL, this.methodName);
        if (simpleMethodToCall != null) {
            if (!aic.hasVisited(simpleMethodToCall)) {
                aic.addSimpleMethod(simpleMethodToCall);
                simpleMethodToCall.gatherArtifactInfo(aic);
            }
        }
    } catch (MiniLangException e) {
        Debug.logWarning("Could not find <simple-method name=\"" + this.methodName + "\"> in XML document " + this.xmlResource + ": " + e.toString(), module);
    }
}
Also used : SimpleMethod(org.apache.ofbiz.minilang.SimpleMethod) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 3 with MiniLangException

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

the class MakeNextSeqId method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    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;
    if (!numericPaddingStr.isEmpty()) {
        try {
            numericPadding = Integer.parseInt(numericPaddingStr);
        } catch (Exception e) {
            throw new MiniLangRuntimeException("Invalid number in \"numeric-padding\" attribute", this);
        }
    }
    int incrementBy = 1;
    if (!incrementByStr.isEmpty()) {
        try {
            incrementBy = Integer.parseInt(incrementByStr);
        } catch (Exception e) {
            throw new MiniLangRuntimeException("Invalid number in \"increment-by\" attribute", this);
        }
    }
    value.getDelegator().setNextSubSeqId(value, seqFieldName, numericPadding, incrementBy);
    return true;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 4 with MiniLangException

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

the class RegexpCondition 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);
        }
    }
    String regExp = exprFse.expandString(methodContext.getEnvMap());
    Pattern pattern = null;
    try {
        pattern = PatternFactory.createOrGetPerl5CompiledPattern(regExp, true);
    } catch (MalformedPatternException e) {
        Debug.logError(e, "Regular Expression [" + regExp + "] is mal-formed: " + e.toString(), module);
        throw new MiniLangRuntimeException(e, this);
    }
    PatternMatcher matcher = new Perl5Matcher();
    if (matcher.matches((String) fieldVal, pattern)) {
        // Debug.logInfo("The string [" + fieldVal + "] matched the pattern expr [" + pattern.getPattern() + "]", module);
        return true;
    } else {
        // Debug.logInfo("The string [" + fieldVal + "] did NOT match the pattern expr [" + pattern.getPattern() + "]", module);
        return false;
    }
}
Also used : Pattern(org.apache.oro.text.regex.Pattern) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) Perl5Matcher(org.apache.oro.text.regex.Perl5Matcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) PatternMatcher(org.apache.oro.text.regex.PatternMatcher) MalformedPatternException(org.apache.oro.text.regex.MalformedPatternException) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

Example 5 with MiniLangException

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

the class SetCalendar method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    Object newValue = null;
    if (this.scriptlet != null) {
        try {
            newValue = this.scriptlet.executeScript(methodContext.getEnvMap());
        } catch (Exception exc) {
            Debug.logWarning(exc, "Error evaluating scriptlet [" + this.scriptlet + "]: " + exc, module);
        }
    } else if (!this.fromFma.isEmpty()) {
        newValue = this.fromFma.get(methodContext.getEnvMap());
    } else if (!this.valueFse.isEmpty()) {
        newValue = this.valueFse.expand(methodContext.getEnvMap());
    }
    if (ObjectType.isEmpty(newValue) && !this.defaultFse.isEmpty()) {
        newValue = this.defaultFse.expand(methodContext.getEnvMap());
    }
    if (!setIfNull && newValue == null) {
        return true;
    }
    Locale locale = null;
    TimeZone timeZone = null;
    Timestamp fromStamp = null;
    int years = 0;
    int months = 0;
    int days = 0;
    int hours = 0;
    int minutes = 0;
    int seconds = 0;
    int millis = 0;
    try {
        if (!this.localeFse.isEmpty()) {
            locale = (Locale) ObjectType.simpleTypeConvert(this.localeFse.expand(methodContext.getEnvMap()), "Locale", null, null);
        }
        if (locale == null) {
            locale = methodContext.getLocale();
        }
        if (locale == null) {
            locale = Locale.getDefault();
        }
        if (!this.timeZoneFse.isEmpty()) {
            timeZone = (TimeZone) ObjectType.simpleTypeConvert(this.timeZoneFse.expand(methodContext.getEnvMap()), "TimeZone", null, null);
        }
        if (timeZone == null) {
            timeZone = methodContext.getTimeZone();
        }
        if (timeZone == null) {
            timeZone = TimeZone.getDefault();
        }
        fromStamp = (Timestamp) MiniLangUtil.convertType(newValue, java.sql.Timestamp.class, locale, timeZone, UtilDateTime.getDateTimeFormat());
        if (!this.yearsFse.isEmpty()) {
            years = parseInt(this.yearsFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.monthsFse.isEmpty()) {
            months = parseInt(this.monthsFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.daysFse.isEmpty()) {
            days = parseInt(this.daysFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.hoursFse.isEmpty()) {
            hours = parseInt(this.hoursFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.minutesFse.isEmpty()) {
            minutes = parseInt(this.minutesFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.secondsFse.isEmpty()) {
            seconds = parseInt(this.secondsFse.expandString(methodContext.getEnvMap()));
        }
        if (!this.millisFse.isEmpty()) {
            millis = parseInt(this.millisFse.expandString(methodContext.getEnvMap()));
        }
    } catch (Exception e) {
        throw new MiniLangRuntimeException("Exception thrown while parsing attributes: " + e.getMessage(), this);
    }
    Calendar cal = UtilDateTime.toCalendar(fromStamp, timeZone, locale);
    cal.add(Calendar.MILLISECOND, millis);
    cal.add(Calendar.SECOND, seconds);
    cal.add(Calendar.MINUTE, minutes);
    cal.add(Calendar.HOUR, hours);
    cal.add(Calendar.DAY_OF_MONTH, days);
    cal.add(Calendar.MONTH, months);
    cal.add(Calendar.YEAR, years);
    Timestamp toStamp = new Timestamp(cal.getTimeInMillis());
    if (!periodAlignStart.isEmpty()) {
        String period = periodAlignStart.expandString(methodContext.getEnvMap());
        if ("day".equals(period)) {
            toStamp = UtilDateTime.getDayStart(toStamp, 0, timeZone, locale);
        } else if ("week".equals(period)) {
            toStamp = UtilDateTime.getWeekStart(toStamp, 0, timeZone, locale);
        } else if ("month".equals(period)) {
            toStamp = UtilDateTime.getMonthStart(toStamp, 0, timeZone, locale);
        } else if ("year".equals(period)) {
            toStamp = UtilDateTime.getYearStart(toStamp, 0, timeZone, locale);
        } else {
            throw new MiniLangRuntimeException("Invalid period-align-start attribute value: " + period, this);
        }
    } else if (!periodAlignEnd.isEmpty()) {
        String period = periodAlignEnd.expandString(methodContext.getEnvMap());
        if ("day".equals(period)) {
            toStamp = UtilDateTime.getDayEnd(toStamp, timeZone, locale);
        } else if ("week".equals(period)) {
            toStamp = UtilDateTime.getWeekEnd(toStamp, timeZone, locale);
        } else if ("month".equals(period)) {
            toStamp = UtilDateTime.getMonthEnd(toStamp, timeZone, locale);
        } else if ("year".equals(period)) {
            toStamp = UtilDateTime.getYearEnd(toStamp, timeZone, locale);
        } else {
            throw new MiniLangRuntimeException("Invalid period-align-end attribute value: " + period, this);
        }
    }
    this.fieldFma.put(methodContext.getEnvMap(), toStamp);
    return true;
}
Also used : Locale(java.util.Locale) TimeZone(java.util.TimeZone) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) Calendar(com.ibm.icu.util.Calendar) Timestamp(java.sql.Timestamp) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) MiniLangException(org.apache.ofbiz.minilang.MiniLangException)

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