Search in sources :

Example 66 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class MappingUtils method setProperty.

public static final Property setProperty(boolean parameter, Message msg, String name, Object value, String type, String subtype, String direction, String description, int length, Navajo outputDoc, Navajo tmlDoc, boolean remove) throws MappingException {
    Message ref = null;
    if (parameter) {
        if (msg == null) {
            msg = tmlDoc.getMessage("__parms__");
            if (msg == null) {
                // Create __parms__ message.
                msg = NavajoFactory.getInstance().createMessage(tmlDoc, "__parms__");
                tmlDoc.addMessage(msg);
            }
        }
        ref = getMessageObject(name, msg, false, tmlDoc, false, "", -1);
        if (ref == null) {
            // Can be null due to absolute param name (starting with '/'). In this case use
            // __parms__ as parent.
            ref = tmlDoc.getMessage("__parms__");
        }
    } else {
        ref = getMessageObject(name, msg, false, outputDoc, false, "", -1);
    }
    if (ref == null) {
        ref = msg;
    }
    String actualName = getStrippedPropertyName(name);
    if (ref == null) {
        throw new MappingException("Property can only be created under a message");
    }
    // with ../ constructions in the name of the new property, it is possible to create a property at the rootMessage of a NavajoDoc but that will never be serialized. Logger message to see how often it happens
    if (ref.equals(ref.getRootDoc().getRootMessage())) {
        logger.warn("WARNING - Adding property to rootMessage of NavajoDoc - property will not be findable");
    }
    Property prop = ref.getProperty(actualName);
    // Remove a parameter if remove flag is set.
    if (remove && prop != null && parameter) {
        ref.removeProperty(prop);
        return null;
    }
    if (prop == null && remove && parameter) {
        return null;
    }
    if (prop == null) {
        // Property does not exist.
        if (!parameter) {
            if (value instanceof Property) {
                // Value is a property itself!
                prop = (Property) ((Property) value).clone(name);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                // Legacy mode hack, many scripts do not expect null valued string properties.
                if (Property.STRING_PROPERTY.equals(type) && value == null) {
                    value = "";
                }
                prop = ref.getRootDoc().getNavajoFactory().createProperty(outputDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        } else {
            if (Property.EXPRESSION_LITERAL_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                prop.setValue(new NavajoExpression(value.toString()));
                prop.setType(type);
            } else if (Property.SELECTION_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, "1", description, direction);
                if (value instanceof Selection[]) {
                    prop.setCardinality("+");
                    prop.setValue((Selection[]) value);
                }
            } else if (Property.BINARY_PROPERTY.equals(type)) {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if (value instanceof Binary) {
                    prop.setValue((Binary) value);
                }
            } else {
                prop = ref.getRootDoc().getNavajoFactory().createProperty(tmlDoc, actualName, type, "", length, description, direction);
                if ((value instanceof StringLiteral)) {
                    value = value.toString();
                }
                prop.setAnyValue(value);
                prop.setType(type);
            }
        }
        ref.addProperty(prop);
    } else {
        // Existing property.
        prop.clearValue();
        if (value instanceof Property) {
            // Value is a property itself!
            prop = (Property) ((Property) value).clone(name);
        } else if (Property.BINARY_PROPERTY.equals(type)) {
            if (value instanceof Binary) {
                prop.setValue((Binary) value);
            } else {
                prop.clearValue();
            }
        } else if (Property.SELECTION_PROPERTY.equals(type) && value != null && value instanceof Selection[]) {
            prop.setCardinality("+");
            prop.setValue((Selection[]) value);
        } else if (!Property.SELECTION_PROPERTY.equals(type)) {
            if (value != null) {
                if ((value instanceof StringLiteral)) {
                    prop.setUnCheckedStringAsValue(((StringLiteral) value).toString());
                } else {
                    prop.setAnyValue(value);
                }
            } else {
                prop.clearValue();
            }
        }
        if (Property.DIR_IN.equals(direction) || Property.DIR_OUT.equals(direction)) {
            prop.setDirection(direction);
        }
        prop.setType(type);
        // Should not matter ;)
        prop.setName(actualName);
    }
    // Set subtype if not empty.
    if (subtype != null && !subtype.equals("")) {
        prop.setSubType(subtype);
    }
    // Set description if not empty.
    if (description != null && !description.equals("")) {
        prop.setDescription(description);
    }
    // Set length if not empty ( = -1) .
    if (length != -1) {
        prop.setLength(length);
    }
    return prop;
}
Also used : NavajoExpression(com.dexels.navajo.document.types.NavajoExpression) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Binary(com.dexels.navajo.document.types.Binary) Property(com.dexels.navajo.document.Property) MappingException(com.dexels.navajo.script.api.MappingException)

Example 67 with Property

use of com.dexels.navajo.document.Property 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 68 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class NavajoCompileScriptEvent method getEventNavajo.

/**
 * Return the event parameters as a Navajo object with a message __event__.
 */
@Override
public Navajo getEventNavajo() {
    Navajo input = NavajoFactory.getInstance().createNavajo();
    Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
    try {
        input.addMessage(event);
        Property eventWebService = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
        event.addProperty(eventWebService);
    } catch (NavajoException e) {
        logger.error("Error: ", e);
    }
    return input;
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 69 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class NavajoExceptionEvent method getEventNavajo.

/**
 * Return the event parameters as a Navajo object with a message __event__.
 */
@Override
public Navajo getEventNavajo() {
    Navajo input = NavajoFactory.getInstance().createNavajo();
    Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
    try {
        input.addMessage(event);
        Property webserviceProperty = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
        Property exception = NavajoFactory.getInstance().createProperty(input, "Exception", Property.STRING_PROPERTY, getException().getMessage(), 0, "", Property.DIR_OUT);
        Property accessIdProperty = NavajoFactory.getInstance().createProperty(input, "AccessId", Property.STRING_PROPERTY, getAccessId(), 0, "", Property.DIR_OUT);
        Property userProperty = NavajoFactory.getInstance().createProperty(input, "User", Property.STRING_PROPERTY, getUser(), 0, "", Property.DIR_OUT);
        event.addProperty(webserviceProperty);
        event.addProperty(exception);
        event.addProperty(accessIdProperty);
        event.addProperty(userProperty);
    } catch (NavajoException e) {
        logger.error("Error: ", e);
    }
    return input;
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 70 with Property

use of com.dexels.navajo.document.Property in project navajo by Dexels.

the class CacheExpiryEvent method getEventNavajo.

@Override
public Navajo getEventNavajo() {
    Navajo input = NavajoFactory.getInstance().createNavajo();
    Message event = NavajoFactory.getInstance().createMessage(input, "__event__");
    try {
        input.addMessage(event);
        Property eventWebService = NavajoFactory.getInstance().createProperty(input, "Webservice", Property.STRING_PROPERTY, getWebservice(), 0, "", Property.DIR_OUT);
        event.addProperty(eventWebService);
        Property eventKey = NavajoFactory.getInstance().createProperty(input, "Key", Property.STRING_PROPERTY, getKey(), 0, "", Property.DIR_OUT);
        event.addProperty(eventKey);
    } catch (NavajoException e) {
        logger.error("Error: ", e);
    }
    return input;
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Aggregations

Property (com.dexels.navajo.document.Property)253 Message (com.dexels.navajo.document.Message)148 Test (org.junit.Test)88 Navajo (com.dexels.navajo.document.Navajo)84 Selection (com.dexels.navajo.document.Selection)36 NavajoException (com.dexels.navajo.document.NavajoException)30 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)17 ArrayList (java.util.ArrayList)17 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)16 Binary (com.dexels.navajo.document.types.Binary)16 UserException (com.dexels.navajo.script.api.UserException)16 Access (com.dexels.navajo.script.api.Access)15 StringWriter (java.io.StringWriter)13 List (java.util.List)11 Operand (com.dexels.navajo.document.Operand)10 MappableException (com.dexels.navajo.script.api.MappableException)9 IOException (java.io.IOException)9 Writer (java.io.Writer)9 StringTokenizer (java.util.StringTokenizer)9 JSONTML (com.dexels.navajo.document.json.JSONTML)8