Search in sources :

Example 81 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class GetWeekDayDate method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object o = this.getOperand(0);
    Object f = this.getOperand(1);
    Object reference = null;
    int weekday = -1;
    boolean direction;
    if (getOperands().size() > 2) {
        reference = this.getOperand(2);
    }
    if (!(o instanceof Integer)) {
        if (o instanceof String) {
            weekday = parseWeekday((String) o);
        } else {
            throw new TMLExpressionException("Invalid operand type, Integer expected");
        }
    } else {
        weekday = ((Integer) o).intValue();
    }
    if (f instanceof Boolean) {
        direction = ((Boolean) f).booleanValue();
    } else {
        if (f instanceof String) {
            String s = (String) f;
            if (s.startsWith("forward")) {
                direction = false;
            } else if (s.startsWith("back")) {
                direction = true;
            } else {
                throw new TMLExpressionException("For direction operand: Enter 'forwards' or 'backwards'");
            }
        } else {
            throw new TMLExpressionException("Invalid operand type, Boolean OR String: 'forwards'/'backwards' expexted");
        }
    }
    if (reference != null && !(reference instanceof java.util.Date))
        throw new TMLExpressionException("Invalid operand type, Date expected");
    // Boolean past = (Boolean) f;
    Calendar today = Calendar.getInstance();
    if (reference != null) {
        java.util.Date d = (java.util.Date) reference;
        today.setTime(d);
    }
    if (today.get(Calendar.DAY_OF_WEEK) == weekday)
        return reset(today, direction);
    int factor = (direction ? -1 : 1);
    for (int i = 1; i <= 8; i++) {
        today.add(Calendar.DAY_OF_WEEK, factor);
        if (today.get(Calendar.DAY_OF_WEEK) == weekday) {
            return reset(today, direction);
        }
    }
    return null;
}
Also used : Calendar(java.util.Calendar) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 82 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class InMonthTurnInterval method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    int size = this.getOperands().size();
    if (size != 3) {
        throw new TMLExpressionException("Illegal number of arguments for function InMonthTurnInterval(): " + size);
    }
    Object date = this.getOperands().get(0);
    Object days = this.getOperands().get(1);
    Object forward = this.getOperands().get(2);
    java.util.Date dt;
    int ds;
    boolean fw;
    if (date instanceof java.util.Date) {
        dt = (java.util.Date) date;
    } else {
        throw new TMLExpressionException("Illegal argument type for date InMonthTurnInterval(): " + date.getClass().getName());
    }
    if (days instanceof Integer) {
        ds = ((Integer) days).intValue();
    } else {
        throw new TMLExpressionException("Illegal argument type for days InMonthTurnInterval(): " + days.getClass().getName());
    }
    if (forward instanceof java.lang.Boolean) {
        fw = ((java.lang.Boolean) forward).booleanValue();
    } else {
        throw new TMLExpressionException("Illegal argument type for first InMonthTurnInterval(): " + forward.getClass().getName());
    }
    Calendar c = Calendar.getInstance();
    c.setTime(dt);
    int dim = c.get(Calendar.DAY_OF_MONTH);
    if (fw) {
        if (dim <= ds) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    } else {
        int max = c.getMaximum(Calendar.DAY_OF_MONTH) - ds;
        if (dim > max) {
            return Boolean.TRUE;
        }
        return Boolean.FALSE;
    }
}
Also used : Calendar(java.util.Calendar) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 83 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class GetPropertySubType method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() != 2) {
        throw new TMLExpressionException(this, "Invalid function call");
    }
    Object o = operand(0).value;
    // Object subtypeObject = getOperand(1);
    // if(!(subtypeObject instanceof String)) {
    // throw new TMLExpressionException(this, "Invalid function call");
    // }
    String subType = getStringOperand(1);
    if (o instanceof Property) {
        Property p = (Property) o;
        return p.getSubType(subType);
    } else {
        if (!(o instanceof String)) {
            throw new TMLExpressionException(this, "String argument expected");
        }
        Message curMsg = getCurrentMessage();
        String propertyName = (String) o;
        Property p = (curMsg != null ? curMsg.getProperty(propertyName) : this.getNavajo().getProperty(propertyName));
        if (p == null) {
            throw new TMLExpressionException(this, "Property " + propertyName + " not found");
        }
        return p.getSubType(subType);
    }
}
Also used : Message(com.dexels.navajo.document.Message) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 84 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class CheckEmail method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object o = operand(0).value;
    if (!(o instanceof String)) {
        return Boolean.FALSE;
    // throw new TMLExpressionException(this, "Invalid email address, string expected");
    }
    String email = (String) o;
    try {
        Pattern re = Pattern.compile(EMAIL_PATTERN);
        boolean isMatch = re.matcher(email).matches();
        if (!isMatch) {
            return Boolean.FALSE;
        } else
            return Boolean.TRUE;
    } catch (Exception ree) {
        return Boolean.FALSE;
    }
}
Also used : Pattern(java.util.regex.Pattern) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 85 with TMLExpressionException

use of com.dexels.navajo.expression.api.TMLExpressionException in project navajo by Dexels.

the class CheckUniqueness method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            logger.debug("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(operand(0).value instanceof String && operand(1).value instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + operand(0).value.getClass() + " and " + operand(1).value.getClass());
    }
    String messageName = getStringOperand(0);
    String propertyName = getStringOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = getStringOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    boolean isUnique = true;
    HashSet<Object> values = new HashSet<Object>();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            Property p = m.getProperty(propertyName);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                if (p != null) {
                    Object o = null;
                    if (p.getType().equals(Property.SELECTION_PROPERTY)) {
                        o = p.getSelected().getValue();
                    } else {
                        o = p.getTypedValue();
                    }
                    if (values.contains(o)) {
                        return Boolean.FALSE;
                    } else {
                        values.add(o);
                    }
                }
            }
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
    return (isUnique ? Boolean.TRUE : Boolean.FALSE);
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) SystemException(com.dexels.navajo.script.api.SystemException) Property(com.dexels.navajo.document.Property) HashSet(java.util.HashSet)

Aggregations

TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)129 Message (com.dexels.navajo.document.Message)26 Navajo (com.dexels.navajo.document.Navajo)26 Binary (com.dexels.navajo.document.types.Binary)23 Property (com.dexels.navajo.document.Property)17 ArrayList (java.util.ArrayList)16 Operand (com.dexels.navajo.document.Operand)15 List (java.util.List)13 NavajoException (com.dexels.navajo.document.NavajoException)12 IOException (java.io.IOException)12 Selection (com.dexels.navajo.document.Selection)11 Access (com.dexels.navajo.script.api.Access)10 Calendar (java.util.Calendar)10 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)9 Test (org.junit.Test)9 Money (com.dexels.navajo.document.types.Money)8 SystemException (com.dexels.navajo.script.api.SystemException)8 StringTokenizer (java.util.StringTokenizer)8 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)7 ClockTime (com.dexels.navajo.document.types.ClockTime)7