Search in sources :

Example 11 with MiniLangRuntimeException

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

Example 12 with MiniLangRuntimeException

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

the class StringAppend method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    String value = stringFse.expandString(methodContext.getEnvMap());
    List<? extends Object> argList = argListFma.get(methodContext.getEnvMap());
    if (argList != null) {
        try {
            value = MessageFormat.format(value, argList.toArray());
        } catch (IllegalArgumentException e) {
            throw new MiniLangRuntimeException("Exception thrown while formatting the string attribute: " + e.getMessage(), this);
        }
    }
    if (!value.isEmpty()) {
        String prefixValue = prefixFse.expandString(methodContext.getEnvMap());
        String suffixValue = suffixFse.expandString(methodContext.getEnvMap());
        StringBuilder newValue = new StringBuilder();
        String oldValue = fieldFma.get(methodContext.getEnvMap());
        if (oldValue != null) {
            newValue.append(oldValue);
        }
        newValue.append(prefixValue).append(value).append(suffixValue);
        fieldFma.put(methodContext.getEnvMap(), newValue.toString());
    }
    return true;
}
Also used : MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException)

Example 13 with MiniLangRuntimeException

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

the class GetRelated 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 relationName = relationNameFse.expandString(methodContext.getEnvMap());
    boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
    List<String> orderByNames = orderByListFma.get(methodContext.getEnvMap());
    Map<String, ? extends Object> constraintMap = mapFma.get(methodContext.getEnvMap());
    try {
        listFma.put(methodContext.getEnvMap(), value.getRelated(relationName, constraintMap, orderByNames, useCache));
    } catch (GenericEntityException e) {
        String errMsg = "Exception thrown while finding related values: " + e.getMessage();
        Debug.logWarning(e, errMsg, module);
        simpleMethod.addErrorMessage(methodContext, errMsg);
        return false;
    }
    return true;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 14 with MiniLangRuntimeException

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

the class FindByPrimaryKey method exec.

@Override
public boolean exec(MethodContext methodContext) throws MiniLangException {
    String entityName = entityNameFse.expandString(methodContext.getEnvMap());
    boolean useCache = "true".equals(useCacheFse.expandString(methodContext.getEnvMap()));
    Delegator delegator = getDelegator(methodContext);
    Map<String, ? extends Object> inMap = mapFma.get(methodContext.getEnvMap());
    if (inMap == null) {
        throw new MiniLangRuntimeException("Primary key map \"" + mapFma + "\" not found", this);
    }
    if (entityName.isEmpty() && inMap instanceof GenericEntity) {
        GenericEntity inEntity = (GenericEntity) inMap;
        entityName = inEntity.getEntityName();
    }
    if (entityName.isEmpty()) {
        throw new MiniLangRuntimeException("Entity name not found", this);
    }
    Collection<String> fieldsToSelectList = fieldsToSelectListFma.get(methodContext.getEnvMap());
    try {
        if (fieldsToSelectList != null) {
            valueFma.put(methodContext.getEnvMap(), delegator.findByPrimaryKeyPartial(delegator.makePK(entityName, inMap), UtilMisc.toSet(fieldsToSelectList)));
        } else {
            valueFma.put(methodContext.getEnvMap(), EntityQuery.use(delegator).from(entityName).where(inMap).cache(useCache).queryOne());
        }
    } catch (GenericEntityException e) {
        String errMsg = "Exception thrown while performing entity find: " + e.getMessage();
        Debug.logWarning(e, errMsg, module);
        simpleMethod.addErrorMessage(methodContext, errMsg);
        return false;
    }
    return true;
}
Also used : Delegator(org.apache.ofbiz.entity.Delegator) MiniLangRuntimeException(org.apache.ofbiz.minilang.MiniLangRuntimeException) GenericEntity(org.apache.ofbiz.entity.GenericEntity) GenericEntityException(org.apache.ofbiz.entity.GenericEntityException)

Example 15 with MiniLangRuntimeException

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

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