Search in sources :

Example 1 with ConditionData

use of com.dexels.navajo.server.ConditionData in project navajo by Dexels.

the class CompiledScript method run.

/*
     * (non-Javadoc)
     * 
     * @see
     * com.dexels.navajo.script.api.CompiledScriptInterface#run(com.dexels.navajo
     * .api.Access)
     */
@Override
public final void run(Access access) throws Exception {
    myAccess = access;
    long start = System.currentTimeMillis();
    try {
        dumpRequest();
        setValidations();
        currentParamMsg = access.getInDoc().getMessage("__parms__");
        ConditionData[] conditions = getValidationRules(access);
        boolean conditionsFailed = false;
        if (conditions != null && conditions.length > 0) {
            Navajo outMessage = access.getOutputDoc();
            Message[] failed = checkValidationRules(conditions, access.getInDoc(), outMessage, access);
            if (failed != null) {
                conditionsFailed = true;
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                Message msg = NavajoFactory.getInstance().createMessage(outMessage, "ConditionErrors");
                outMessage.addMessage(msg);
                msg.setType(Message.MSG_TYPE_ARRAY);
                for (int i = 0; i < failed.length; i++) {
                    msg.addMessage(failed[i]);
                }
            }
        }
        if (!conditionsFailed) {
            try {
                execute(access);
                access.setExitCode(Access.EXIT_OK);
            } catch (com.dexels.navajo.mapping.BreakEvent be) {
                access.setExitCode(Access.EXIT_BREAK);
                throw be;
            } catch (UserException e) {
                access.setExitCode(Access.EXIT_USEREXCEPTION);
                throw e;
            } catch (ConditionErrorException e) {
                access.setExitCode(Access.EXIT_VALIDATION_ERR);
                throw e;
            } catch (Exception e) {
                access.setExitCode(Access.EXIT_EXCEPTION);
                throw e;
            } finally {
                finalBlock(access);
            }
        }
    } finally {
        dumpResponse();
        // Release acquired locks.
        for (Lock l : acquiredLocks) {
            try {
                l.unlock();
            } catch (Throwable t) {
            }
        }
        acquiredLocks.clear();
        access.processingTime = (int) (System.currentTimeMillis() - start);
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) CompilationException(com.dexels.navajo.script.api.CompilationException) Lock(java.util.concurrent.locks.Lock) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) UserException(com.dexels.navajo.script.api.UserException)

Example 2 with ConditionData

use of com.dexels.navajo.server.ConditionData in project navajo by Dexels.

the class CompiledScript method checkValidationRules.

/**
 * Deprecated method to check validation errors. Use <validations> block
 * inside webservice script instead.
 *
 * @param conditions
 * @param inMessage
 * @param outMessage
 * @return
 * @throws NavajoException
 * @throws SystemException
 * @throws UserException
 */
private final Message[] checkValidationRules(ConditionData[] conditions, Navajo inMessage, Navajo outMessage, Access a) throws SystemException, UserException {
    if (conditions == null) {
        return null;
    }
    List<Message> messages = new ArrayList<>();
    int index = 0;
    for (int i = 0; i < conditions.length; i++) {
        ConditionData condition = conditions[i];
        boolean valid = false;
        try {
            valid = com.dexels.navajo.parser.Condition.evaluate(condition.condition, inMessage, a);
        } catch (com.dexels.navajo.expression.api.TMLExpressionException ee) {
            throw new UserException(-1, "Invalid condition: " + ee.getMessage(), ee);
        }
        if (!valid) {
            String eval = com.dexels.navajo.parser.Expression.replacePropertyValues(condition.condition, inMessage);
            Message msg = NavajoFactory.getInstance().createMessage(outMessage, "failed" + (index++));
            Property prop0 = NavajoFactory.getInstance().createProperty(outMessage, "Id", Property.STRING_PROPERTY, condition.id + "", 0, "", Property.DIR_OUT);
            // Evaluate comment as expression.
            String description = "";
            try {
                Operand o = Expression.evaluate(condition.comment, inMessage);
                description = o.value + "";
            } catch (Exception e) {
                description = condition.comment;
            }
            Property prop1 = NavajoFactory.getInstance().createProperty(outMessage, "Description", Property.STRING_PROPERTY, description, 0, "", Property.DIR_OUT);
            msg.addProperty(prop0);
            msg.addProperty(prop1);
            if (System.getenv("DEBUG_SCRIPTS") != null && System.getenv("DEBUG_SCRIPTS").equals("true")) {
                Property prop2 = NavajoFactory.getInstance().createProperty(outMessage, "FailedExpression", Property.STRING_PROPERTY, condition.condition, 0, "", Property.DIR_OUT);
                Property prop3 = NavajoFactory.getInstance().createProperty(outMessage, "EvaluatedExpression", Property.STRING_PROPERTY, eval, 0, "", Property.DIR_OUT);
                msg.addProperty(prop2);
                msg.addProperty(prop3);
            }
            messages.add(msg);
        }
    }
    if (!messages.isEmpty()) {
        Message[] msgArray = new Message[messages.size()];
        messages.toArray(msgArray);
        return msgArray;
    } else {
        return null;
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) Message(com.dexels.navajo.document.Message) Operand(com.dexels.navajo.document.Operand) ArrayList(java.util.ArrayList) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) SystemException(com.dexels.navajo.script.api.SystemException) ConditionErrorException(com.dexels.navajo.server.ConditionErrorException) CompilationException(com.dexels.navajo.script.api.CompilationException) UserException(com.dexels.navajo.script.api.UserException) Property(com.dexels.navajo.document.Property)

Example 3 with ConditionData

use of com.dexels.navajo.server.ConditionData in project navajo by Dexels.

the class CompiledScript method getValidationRules.

private final ConditionData[] getValidationRules(Access access) throws SystemException {
    Navajo inMessage = access.getInDoc();
    if (conditionArray != null) {
        List<ConditionData> conditions = new ArrayList<>();
        for (int i = 0; i < conditionArray.length; i++) {
            // TODO conditionArray[i].equals("") always returns false. What was the intention here?
            boolean check = (conditionArray[i].equals("") ? true : Condition.evaluate(conditionArray[i], inMessage, access));
            if (check) {
                ConditionData cd = new ConditionData();
                cd.id = codeArray[i];
                cd.comment = (descriptionArray != null ? descriptionArray[i] : "empty");
                cd.condition = ruleArray[i];
                conditions.add(cd);
            }
        }
        ConditionData[] conditionArray = new ConditionData[conditions.size()];
        conditionArray = conditions.toArray(conditionArray);
        return conditionArray;
    } else {
        return null;
    }
}
Also used : ConditionData(com.dexels.navajo.server.ConditionData) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo)

Aggregations

ConditionData (com.dexels.navajo.server.ConditionData)3 Message (com.dexels.navajo.document.Message)2 Navajo (com.dexels.navajo.document.Navajo)2 NavajoException (com.dexels.navajo.document.NavajoException)2 CompilationException (com.dexels.navajo.script.api.CompilationException)2 MappableException (com.dexels.navajo.script.api.MappableException)2 SystemException (com.dexels.navajo.script.api.SystemException)2 UserException (com.dexels.navajo.script.api.UserException)2 ConditionErrorException (com.dexels.navajo.server.ConditionErrorException)2 ArrayList (java.util.ArrayList)2 Operand (com.dexels.navajo.document.Operand)1 Property (com.dexels.navajo.document.Property)1 Lock (java.util.concurrent.locks.Lock)1