Search in sources :

Example 31 with Property

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

the class GetSelectedValue method evaluate.

@Override
@SuppressWarnings("unchecked")
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() != 1) {
        throw new TMLExpressionException(this, "Invalid function call, need one parameter");
    }
    Object o = operand(0).value;
    if (o == null) {
        throw new TMLExpressionException(this, "Invalid function call in GetSelectedValue: Parameter null");
    }
    if (o instanceof Property) {
        Property p = (Property) o;
        if (p.getSelected() != null) {
            return (p.getAllSelectedSelections().size() > 0 ? p.getSelected().getValue() : null);
        } else {
            return null;
        }
    }
    if (!(o instanceof List)) {
        throw new TMLExpressionException(this, "Invalid function call in GetSelectedValue: Not a selection property");
    }
    List<Selection> l = (List<Selection>) o;
    for (Selection selection : l) {
        if (selection.isSelected()) {
            return selection.getValue();
        }
    }
    return null;
}
Also used : Selection(com.dexels.navajo.document.Selection) List(java.util.List) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 32 with Property

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

the class FindElement method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Message in = null;
    String propertyName = (String) getOperand(0);
    Object propertyValue = getOperand(1);
    if (getOperands().size() == 2) {
        in = getCurrentMessage();
        if (in == null) {
            throw new TMLExpressionException("Can not FindElement: No supplied message and no currentMessage");
        }
    } else {
        in = (Message) getOperand(2);
    }
    if (!Message.MSG_TYPE_ARRAY.equals(in.getType())) {
        in = in.getArrayParentMessage();
    }
    if (in == null) {
        throw new TMLExpressionException("Can not FindElement: Supplied message is not an array message or array element");
    }
    String type = in.getType();
    if (!Message.MSG_TYPE_ARRAY.equals(type)) {
        throw new TMLExpressionException("FindElement resolved message is still no array message (?)");
    }
    for (Message element : in.getAllMessages()) {
        Property p = element.getProperty(propertyName);
        if (p != null) {
            if (propertyValue.equals(p.getTypedValue())) {
                return element;
            }
        }
    }
    logger.debug("No property found in message. Property name: " + propertyName + " value: " + propertyValue);
    return null;
}
Also used : Message(com.dexels.navajo.document.Message) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 33 with Property

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

the class GetPropertyAttribute method getAttribute.

public Object getAttribute(Object operand, String attribute) throws com.dexels.navajo.expression.api.TMLExpressionException {
    Property p = null;
    if (operand instanceof Property) {
        p = (Property) operand;
    } else {
        String propertyName = operand.toString();
        p = (getCurrentMessage() != null ? getCurrentMessage().getProperty(propertyName) : this.getNavajo().getProperty(propertyName));
    }
    if (p == null) {
        throw new TMLExpressionException(this, "Property " + operand.toString() + " not found");
    }
    if (attribute.equals("direction")) {
        return p.getDirection();
    }
    if (attribute.equals("type")) {
        return p.getType();
    }
    if (attribute.equals("cardinality")) {
        return p.getCardinality();
    }
    try {
        throw new TMLExpressionException(this, "attribute " + attribute + " not found for property " + p.getFullPropertyName());
    } catch (NavajoException e) {
        throw new TMLExpressionException(this, "attribute " + attribute + " not found for unknown property ");
    }
}
Also used : NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 34 with Property

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

the class MessageMap method getResultMessage.

public ResultMessage[] getResultMessage() throws UserException {
    Message definitionMsg1 = null;
    Message definitionMsg2 = null;
    List<ResultMessage> resultingMessage = new ArrayList<>();
    List<Message> children = this.msg1.getAllMessages();
    // Determine definition message, unless groupBy is defined.
    if (groupBy == null && this.msg1.getDefinitionMessage() != null) {
        definitionMsg1 = this.msg1.getDefinitionMessage();
    }
    if (groupBy == null && definitionMsg1 != null && this.msg2 != null && this.msg2.getDefinitionMessage() != null) {
        definitionMsg2 = this.msg2.getDefinitionMessage();
        definitionMsg1.merge(definitionMsg2);
    }
    for (int i = 0; i < children.size(); i++) {
        msg1pointer = children.get(i);
        if (msg1pointer.getType().equals(Message.MSG_TYPE_DEFINITION)) {
            // Skip definition messages
            continue;
        }
        Object[] joinValues1 = new Object[joinConditions.size()];
        for (int p = 0; p < joinConditions.size(); p++) {
            JoinCondition jc = joinConditions.get(p);
            Property prop = msg1pointer.getProperty(jc.property1);
            if (prop == null) {
                throw new UserException(-1, "Exception joining messages " + joinMessage1 + " and " + joinMessage2 + ": property not found: " + jc.property1);
            }
            joinValues1[p] = prop.getValue();
        }
        // Find c2;
        msg2pointer = null;
        boolean foundJoinMessage = false;
        boolean isSingleMessage = false;
        if (this.msg2 != null) {
            List<Message> children2 = this.msg2.getAllMessages();
            // ==========
            if (this.msg2.getType().equals(Message.MSG_TYPE_SIMPLE)) {
                // v SIMPLE MESSAGE if
                isSingleMessage = true;
            } else // ========== // v SIMPLE MESSAGE end if
            {
                for (int j = 0; j < children2.size(); j++) {
                    msg2pointer = children2.get(j);
                    if (msg2pointer.getType().equals(Message.MSG_TYPE_DEFINITION)) {
                        // Skip definition messages
                        continue;
                    }
                    Object[] joinValues2 = new Object[joinConditions.size()];
                    for (int p = 0; p < joinConditions.size(); p++) {
                        JoinCondition jc = joinConditions.get(p);
                        Property prop = msg2pointer.getProperty(jc.property2);
                        if (prop == null) {
                            throw new UserException(-1, "Exception joining messages " + joinMessage1 + " and " + joinMessage2 + ": property not found: " + jc.property2);
                        }
                        joinValues2[p] = prop.getValue();
                    }
                    // Compare joinValues...
                    boolean equal = true;
                    for (int jv = 0; jv < joinConditions.size(); jv++) {
                        if (joinValues1[jv] != null && joinValues2[jv] != null && !joinValues1[jv].equals(joinValues2[jv])) {
                            equal = false;
                        } else if (joinValues1[jv] == null && joinValues2[jv] != null) {
                            equal = false;
                        } else if (joinValues1[jv] != null && joinValues2[jv] == null) {
                            equal = false;
                        }
                    }
                    if (joinExpression != null) {
                        // Evaluate joinExpression.
                        try {
                            Operand ro = Expression.evaluate(joinExpression.toString(), myAccess.getInDoc(), myAccess.getCompiledScript().getCurrentMap(), myAccess.getCurrentInMessage());
                            equal = (Boolean) ro.value;
                        } catch (Exception e) {
                            throw new UserException(-1, "Exception joining messages: " + e.getMessage(), e);
                        }
                    }
                    if (equal) {
                        Message newMsg = NavajoFactory.getInstance().createMessage(myAccess.getOutputDoc(), "tmp");
                        // Check for duplicate property names. If found, rename to _1 _2 respectively
                        // DO NOT, CAN LEAD TO STRANGE BEHAVIOR: renameDuplicates(msg1pointer, msg2pointer);
                        newMsg.merge(msg2pointer);
                        newMsg.merge(msg1pointer);
                        ResultMessage rm = new ResultMessage();
                        rm.setMessage(definitionMsg1, newMsg, this.suppressProperties);
                        resultingMessage.add(rm);
                        foundJoinMessage = true;
                    }
                }
            // end for
            }
        // ==========
        }
        if (!foundJoinMessage && joinType.equals(OUTER_JOIN) && !isSingleMessage) {
            // Append dummy message with empty property values in case no join condition match...
            if (msg2pointer != null) {
                Message newMsg = NavajoFactory.getInstance().createMessage(myAccess.getOutputDoc(), "tmp");
                Message c2c = msg2pointer.copy();
                clearPropertyValues(c2c);
                newMsg.merge(c2c);
                newMsg.merge(msg1pointer);
                ResultMessage rm = new ResultMessage();
                rm.setMessage(definitionMsg1, newMsg, this.suppressProperties);
                resultingMessage.add(rm);
            } else {
                // Assume empty second array message
                Message c1c = msg1pointer.copy();
                ResultMessage rm = new ResultMessage();
                rm.setMessage(definitionMsg1, c1c, this.suppressProperties);
                resultingMessage.add(rm);
            }
        }
        if (!foundJoinMessage && joinType.equals(OUTER_JOIN) && isSingleMessage) {
            // if ( msg2pointer != null ) {
            if (msg2 != null) {
                // msg2 instead of msg2pointer if something goes wrong change back to msg2pointer
                Message newMsg = NavajoFactory.getInstance().createMessage(myAccess.getOutputDoc(), "tmp");
                Message c1c = msg1pointer.copy();
                // clearPropertyValues(c1c);
                newMsg.merge(c1c);
                // newMsg.merge(msg1pointer);
                // msg2 instead of msg2pointer if something goes wrong change back to msg2pointer
                newMsg.merge(msg2);
                ResultMessage rm = new ResultMessage();
                rm.setMessage(definitionMsg1, newMsg, this.suppressProperties);
                resultingMessage.add(rm);
            } else {
                // Assume empty second array message
                Message c1c = msg1pointer.copy();
                ResultMessage rm = new ResultMessage();
                rm.setMessage(definitionMsg1, c1c, this.suppressProperties);
                resultingMessage.add(rm);
            }
        }
    }
    if (children.isEmpty() && definitionMsg1 != null && msg1 != null) {
        // Make sure definition message stays intact
        Navajo out = myAccess.getOutputDoc();
        Message newMessage = NavajoFactory.getInstance().createMessage(out, msg1.getName(), msg1.getType());
        newMessage.addMessage(definitionMsg1.copy(myAccess.getOutputDoc()));
        myAccess.getOutputDoc().addMessage(newMessage);
    }
    if (groupBy != null) {
        removeDuplicates = true;
        Map<String, PropertyAggregate> aggregates = new HashMap<>();
        for (int i = 0; i < resultingMessage.size(); i++) {
            Map<String, Object> group = new TreeMap<>();
            ResultMessage rm = resultingMessage.get(i);
            Message m = rm.getMsg();
            List<Property> properties = m.getAllProperties();
            for (int j = 0; j < properties.size(); j++) {
                Property p = properties.get(j);
                if (groupByProperties.contains(p.getName())) {
                    group.put(p.getName(), p.getTypedValue());
                }
            }
            for (int j = 0; j < properties.size(); j++) {
                Property p = properties.get(j);
                if (!groupByProperties.contains(p.getName())) {
                    PropertyAggregate pa = aggregates.get(p.getName());
                    if (pa == null) {
                        pa = new PropertyAggregate();
                        aggregates.put(p.getName(), pa);
                    }
                    pa.addProperty(p, group);
                    m.removeProperty(p);
                }
            }
        }
        for (int i = 0; i < resultingMessage.size(); i++) {
            resultingMessage.get(i).setAggregates(aggregates);
        }
    }
    if (removeDuplicates) {
        for (int i = 0; i < resultingMessage.size(); i++) {
            Message m1 = resultingMessage.get(i).getMsg().copy();
            resultingMessage.get(i).processSuppressedProperties(m1);
            if (!resultingMessage.get(i).isRemove()) {
                for (int j = i + 1; j < resultingMessage.size(); j++) {
                    Message m2 = resultingMessage.get(j).getMsg().copy();
                    resultingMessage.get(j).processSuppressedProperties(m2);
                    if (m1.isEqual(m2)) {
                        resultingMessage.get(j).setRemove(true);
                    }
                }
            }
        }
    }
    Iterator<ResultMessage> iter = resultingMessage.iterator();
    while (iter.hasNext()) {
        ResultMessage c = iter.next();
        if (c.isRemove()) {
            iter.remove();
        }
    }
    return resultingMessage.toArray(new ResultMessage[] {});
}
Also used : Message(com.dexels.navajo.document.Message) ResultMessage(com.dexels.navajo.adapter.messagemap.ResultMessage) HashMap(java.util.HashMap) Operand(com.dexels.navajo.document.Operand) ArrayList(java.util.ArrayList) Navajo(com.dexels.navajo.document.Navajo) ResultMessage(com.dexels.navajo.adapter.messagemap.ResultMessage) TreeMap(java.util.TreeMap) NavajoException(com.dexels.navajo.document.NavajoException) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException) PropertyAggregate(com.dexels.navajo.adapter.messagemap.PropertyAggregate) UserException(com.dexels.navajo.script.api.UserException) Property(com.dexels.navajo.document.Property)

Example 35 with Property

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

the class NavajoMap method getPropertyObject.

public Property getPropertyObject(String fullName) throws UserException {
    waitForResult();
    Property p = null;
    if (msgPointer != null) {
        p = msgPointer.getProperty(fullName);
    } else {
        p = inDoc.getProperty(fullName);
    }
    if (p == null)
        throw new UserException(-1, "Property " + fullName + " does not exists in response document");
    return p;
}
Also used : UserException(com.dexels.navajo.script.api.UserException) 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