Search in sources :

Example 31 with Message

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

the class ForAll method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    /**
     *@todo Implement this com.dexels.navajo.parser.FunctionInterface abstract method
     */
    Message arrayMessage = null;
    String messagePath = null;
    if (getOperand(0) instanceof Message) {
        arrayMessage = (Message) getOperand(0);
    } else {
        messagePath = (String) getOperand(0);
    }
    String expression = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    // try {
    try {
        List<Message> arrayMsg = null;
        if (arrayMessage != null) {
            arrayMsg = arrayMessage.getAllMessages();
        } else {
            arrayMsg = (parent != null ? parent.getMessages(messagePath) : doc.getMessages(messagePath));
        }
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messagePath);
        }
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message current = arrayMsg.get(i);
            try {
                boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, current, getAccess()) : true);
                if (evaluate) {
                    Operand result = Expression.evaluate(expression, doc, null, current);
                    if (result == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
                    }
                    if (result.value == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + expression + " in message: " + current.getFullMessageName());
                    }
                    String res2 = "" + result.value;
                    Operand result2 = Expression.evaluate(res2, doc, null, current);
                    if (result2 == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
                    }
                    if (result2.value == null) {
                        throw new TMLExpressionException(this, "Error evaluating expression: " + res2 + " in message: " + current.getFullMessageName());
                    }
                    boolean res = ((Boolean) result2.value).booleanValue();
                    if (!res) {
                        return Boolean.FALSE;
                    }
                }
            } catch (SystemException ex) {
                logger.error("Error: ", ex);
            }
        }
    } catch (NavajoException ex) {
        throw new TMLExpressionException(this, "Error evaluating message path");
    }
    return Boolean.TRUE;
}
Also used : Message(com.dexels.navajo.document.Message) SystemException(com.dexels.navajo.script.api.SystemException) Operand(com.dexels.navajo.document.Operand) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 32 with Message

use of com.dexels.navajo.document.Message 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 Message

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

the class GetPropertyValue 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 m = operand(0).value;
    ;
    if (m == null) {
        throw new TMLExpressionException(this, "Message or navajo argument expected. This one is null");
    }
    // Object o = getOperand(1);
    // if (!(o instanceof String)) {
    // throw new TMLExpressionException(this, "String argument expected");
    // }
    String propertyName = getStringOperand(1);
    if (!(m instanceof Message) && !(m instanceof Navajo)) {
        throw new TMLExpressionException(this, "Message or navajo argument expected");
    }
    if (m instanceof Navajo) {
        Navajo n = (Navajo) m;
        return n.getProperty(propertyName).getTypedValue();
    }
    if (m instanceof Message) {
        Message message = (Message) m;
        return message.getProperty(propertyName).getTypedValue();
    }
    return null;
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException)

Example 34 with Message

use of com.dexels.navajo.document.Message 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 Message

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

the class FileMap method setMessage.

public void setMessage(Object o) throws MappableException {
    if (o instanceof Message) {
        Message arrraymessage = (Message) o;
        if (!arrraymessage.getType().equals(Message.MSG_TYPE_ARRAY)) {
            throw new MappableException("SetMssage only accepts array message");
        }
        for (Message m : arrraymessage.getElements()) {
            FileLineMap fileLineMap = new FileLineMap();
            fileLineMap.setSeparator(separator);
            for (Property p : m.getAllProperties()) {
                fileLineMap.setColumn(p.getTypedValue().toString());
            }
            setLine(fileLineMap);
        }
    } else if (o instanceof List) {
        @SuppressWarnings("rawtypes") List maps = (List) o;
        for (Object mapobject : maps) {
            if (mapobject instanceof com.dexels.navajo.adapter.navajomap.MessageMap) {
                com.dexels.navajo.adapter.navajomap.MessageMap map = (com.dexels.navajo.adapter.navajomap.MessageMap) mapobject;
                FileLineMap fileLineMap = new FileLineMap();
                fileLineMap.setSeparator(separator);
                for (Property p : map.getMsg().getAllProperties()) {
                    if (p.getName().equals("__id")) {
                        continue;
                    }
                    if (p.getTypedValue() != null) {
                        fileLineMap.setColumn(p.getTypedValue().toString());
                    } else {
                        fileLineMap.setColumn("");
                    }
                }
                setLine(fileLineMap);
            }
        }
    } else {
        throw new MappableException("SetMessage only accepts array message or List, but got a " + o.getClass());
    }
}
Also used : MappableException(com.dexels.navajo.script.api.MappableException) Message(com.dexels.navajo.document.Message) FileLineMap(com.dexels.navajo.adapter.filemap.FileLineMap) ArrayList(java.util.ArrayList) List(java.util.List) Property(com.dexels.navajo.document.Property)

Aggregations

Message (com.dexels.navajo.document.Message)312 Property (com.dexels.navajo.document.Property)149 Navajo (com.dexels.navajo.document.Navajo)127 Test (org.junit.Test)95 NavajoException (com.dexels.navajo.document.NavajoException)39 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)35 Access (com.dexels.navajo.script.api.Access)27 UserException (com.dexels.navajo.script.api.UserException)27 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 Selection (com.dexels.navajo.document.Selection)25 Operand (com.dexels.navajo.document.Operand)22 IOException (java.io.IOException)19 ArrayList (java.util.ArrayList)19 MappableException (com.dexels.navajo.script.api.MappableException)18 Ignore (org.junit.Ignore)17 SystemException (com.dexels.navajo.script.api.SystemException)16 Optional (java.util.Optional)15 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)13 MappableTreeNode (com.dexels.navajo.script.api.MappableTreeNode)13 HashMap (java.util.HashMap)13