Search in sources :

Example 41 with Navajo

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

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

the class IncludeMap method store.

@Override
public void store() throws MappableException, UserException {
    try {
        File f = new File(navajoObject);
        if (type == null || type.equals("tml") || type.equals("tsl")) {
            Navajo n = (type != null && type.equals("tsl") ? NavajoFactory.getInstance().createNavaScript(new FileInputStream(f)) : NavajoFactory.getInstance().createNavajo(new FileInputStream(f)));
            Message current = access.getCurrentOutMessage();
            List<Message> msgList = n.getAllMessages();
            for (int i = 0; i < msgList.size(); i++) {
                Message tbc = msgList.get(i);
                Message copy = tbc.copy(access.getOutputDoc());
                if (current != null) {
                    current.addMessage(copy);
                } else {
                    access.getOutputDoc().addMessage(copy);
                }
            }
        }
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage(), e);
    }
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) UserException(com.dexels.navajo.script.api.UserException) File(java.io.File) FileInputStream(java.io.FileInputStream) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 43 with Navajo

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

the class ProxyMap method store.

@Override
public void store() throws MappableException, UserException {
    if (server == null)
        throw new UserException(-1, "ProxyMap error: no server URI specified, e.g. localhost/servlet/Postman");
    try {
        ClientInterface nc = NavajoClientFactory.createClient();
        inMessage.removeHeader();
        nc.setUsername(username == null ? access.rpcUser : username);
        nc.setPassword(password == null ? access.rpcPwd : password);
        nc.setAllowCompression(true);
        nc.setServerUrl(server);
        Navajo out = nc.doSimpleSend(inMessage, (method == null ? access.rpcName : method));
        access.setOutputDoc(out);
    } catch (Exception e) {
        throw new UserException(-1, e.getMessage());
    }
}
Also used : UserException(com.dexels.navajo.script.api.UserException) ClientInterface(com.dexels.navajo.client.ClientInterface) Navajo(com.dexels.navajo.document.Navajo) UserException(com.dexels.navajo.script.api.UserException) MappableException(com.dexels.navajo.script.api.MappableException)

Example 44 with Navajo

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

the class RESTAdapter method setDoSend.

@Override
public void setDoSend(String method) throws UserException, ConditionErrorException, SystemException {
    if (messagesPerRequest < 1 || useCurrentMessages == null) {
        setDoSend(method, prepareOutDoc());
        serviceCalled = true;
    } else {
        String[] messages = useCurrentMessages.split(",");
        Navajo copy = outDoc.copy();
        Navajo indoc = null;
        if (inDoc == null) {
            indoc = NavajoFactory.getInstance().createNavajo();
        } else {
            indoc = inDoc.copy();
        }
        for (String msgName : messages) {
            Message msg = access.getOutputDoc().getMessage(msgName);
            if (msg != null && msg.isArrayMessage()) {
                Message outMsg = NavajoFactory.getInstance().createMessage(outDoc, msgName);
                outMsg.setType("array");
                int counter = 0;
                int totalCounter = 0;
                for (Message element : msg.getElements()) {
                    outMsg.addElement(element);
                    counter++;
                    totalCounter++;
                    if (counter >= messagesPerRequest || totalCounter >= msg.getArraySize()) {
                        outDoc.addMessage(outMsg);
                        setDoSend(method, outDoc);
                        indoc.merge(inDoc);
                        // Going to clear data
                        outDoc = copy.copy();
                        outMsg = NavajoFactory.getInstance().createMessage(outDoc, msgName);
                        outMsg.setType("array");
                        counter = 0;
                    }
                }
                inDoc = indoc.copy();
            } else {
                throw new UserException(2, "Message " + msgName + "not found or not array message!");
            }
        }
        serviceCalled = true;
    }
}
Also used : Message(com.dexels.navajo.document.Message) Navajo(com.dexels.navajo.document.Navajo) UserException(com.dexels.navajo.script.api.UserException)

Example 45 with Navajo

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

the class AccessMap method getRequestNavajo.

private String getRequestNavajo() {
    if (requestNavajo == null) {
        Navajo in = myAccess.getCompiledScript().getInDoc().copy();
        in.removeMessage("__globals__");
        in.removeMessage("__parms__");
        java.io.StringWriter sw = new java.io.StringWriter();
        in.write(sw);
        requestNavajo = sw.toString();
    }
    return requestNavajo;
}
Also used : Navajo(com.dexels.navajo.document.Navajo)

Aggregations

Navajo (com.dexels.navajo.document.Navajo)258 Message (com.dexels.navajo.document.Message)131 Test (org.junit.Test)109 Property (com.dexels.navajo.document.Property)86 NavajoException (com.dexels.navajo.document.NavajoException)31 Access (com.dexels.navajo.script.api.Access)30 IOException (java.io.IOException)28 StringWriter (java.io.StringWriter)27 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)26 FunctionInterface (com.dexels.navajo.expression.api.FunctionInterface)25 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)22 Selection (com.dexels.navajo.document.Selection)22 Header (com.dexels.navajo.document.Header)20 Operand (com.dexels.navajo.document.Operand)20 InputStream (java.io.InputStream)17 UserException (com.dexels.navajo.script.api.UserException)16 Optional (java.util.Optional)16 FatalException (com.dexels.navajo.script.api.FatalException)14 SystemException (com.dexels.navajo.script.api.SystemException)14 ContextExpression (com.dexels.navajo.expression.api.ContextExpression)13