Search in sources :

Example 26 with NavajoException

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

the class ASTTmlNode method interpretToLambda.

@Override
public final ContextExpression interpretToLambda(List<String> problems, String expression, Function<String, FunctionClassification> functionClassifier, Function<String, Optional<Node>> mapResolver) {
    return new ContextExpression() {

        @Override
        public boolean isLiteral() {
            return false;
        }

        @Override
        public Operand apply(Navajo doc, Message parentMsg, Message parentParamMsg, Selection parentSel, MappableTreeNode mapNode, TipiLink tipiLink, Access access, Optional<ImmutableMessage> immutableMessage, Optional<ImmutableMessage> paramMessage) {
            List<Property> match = null;
            List<Object> resultList = new ArrayList<>();
            boolean singleMatch = true;
            if (val.equals("[") || val.equals("[/")) {
                return immutableMessage.map(msg -> Operand.ofImmutable(msg)).orElse(parentMsg != null ? Operand.ofMessage(parentMsg) : Operand.NULL);
            }
            String[] parts = val.split("\\|");
            String text = parts.length > 1 ? parts[1] : val;
            boolean isParam = false;
            Property prop = null;
            if (parentSel != null) {
                String dum = text;
                if (dum.length() > 1 && dum.startsWith("[")) {
                    dum = dum.substring(1, dum.length());
                }
                if (dum.equals("name") || selectionOption.equals("name")) {
                    return Operand.ofString(parentSel.getName());
                } else if (dum.equals("value") || selectionOption.equals("value")) {
                    return Operand.ofString(parentSel.getValue());
                } else if (dum.equals("selected") || selectionOption.equals("selected")) {
                    return Operand.ofBoolean(parentSel.isSelected());
                }
            }
            if (!exists) {
                if (text.startsWith("[")) {
                    text = text.substring(1, text.length());
                }
            } else {
                if (text.startsWith("?[")) {
                    text = text.substring(2, text.length());
                }
            }
            if (text.length() > 0 && text.charAt(0) == '@') {
                // relative param property.
                isParam = true;
                text = text.substring(1);
            }
            if (text.startsWith("/@")) {
                // Absolute param property, exclude the '[/@]' expression
                isParam = true;
                if (!text.equals("/@")) {
                    parentParamMsg = doc.getMessage("__parms__");
                    text = text.substring(2);
                }
            }
            if (text.contains("__globals__")) {
                // Absolute globals property.
                parentMsg = doc.getMessage("__globals__");
                int length = "__globals__".length();
                if (text.startsWith("/")) {
                    length += 1;
                }
                // trailing /
                length += 1;
                text = text.substring(length);
            }
            if (Util.isRegularExpression(text))
                singleMatch = false;
            else
                singleMatch = true;
            try {
                if (!isParam && immutableMessage != null && immutableMessage.isPresent()) {
                    ImmutableMessage rm = immutableMessage.get();
                    return parseImmutablePath(text, rm);
                }
                if (isParam && paramMessage != null && paramMessage.isPresent()) {
                    ImmutableMessage rm = paramMessage.get();
                    return parseImmutablePath(text, rm);
                }
                if (parentMsg == null && !isParam) {
                    if (text.indexOf(Navajo.MESSAGE_SEPARATOR) != -1) {
                        if (doc == null) {
                            throw new NullPointerException("Can't evaluate TML node: No parent message and no document found.");
                        }
                        match = doc.getProperties(text);
                        if (match.size() > 1) {
                            singleMatch = false;
                        }
                    } else {
                        throw new TMLExpressionException("No parent message present for property: " + text + " -> " + ImmutableFactory.getInstance().describe(immutableMessage.orElse(ImmutableFactory.empty())));
                    }
                } else if (parentParamMsg == null && isParam) {
                    parentParamMsg = doc.getMessage("__parms__");
                    if (text.indexOf(Navajo.MESSAGE_SEPARATOR) != -1) {
                        match = doc.getProperties(text);
                        if (match.size() > 1) {
                            singleMatch = false;
                        }
                    } else
                        throw new TMLExpressionException("No parent message present for param: " + text);
                } else {
                    if (text.indexOf(Navajo.MESSAGE_SEPARATOR) != -1) {
                        match = (!isParam ? parentMsg.getProperties(text) : parentParamMsg.getProperties(text));
                        if (match.size() > 1)
                            singleMatch = false;
                    } else {
                        match = new ArrayList<>();
                        match.add((!isParam ? parentMsg.getProperty(text) : parentParamMsg.getProperty(text)));
                    }
                }
            } catch (NavajoException te) {
                throw new TMLExpressionException(te.getMessage(), te);
            }
            for (int j = 0; j < match.size(); j++) {
                prop = (Property) match.get(j);
                if (!exists && (prop == null))
                    if (parentMsg != null) {
                        throw new TMLExpressionException("TML property does not exist: " + text + " parent message: " + parentMsg.getFullMessageName());
                    } else {
                        throw new TMLExpressionException("TML property does not exist: " + text + " exists? " + exists);
                    }
                else if (exists) {
                    // Check for existence and datatype validity.
                    if (prop != null) {
                        // Check type. If integer, float or date type and if is empty
                        String type = prop.getType();
                        // of binary properties. Should be equivalent, and MUCH faster.
                        if (prop.getTypedValue() == null && !type.equals(Property.SELECTION_PROPERTY)) {
                            return Operand.FALSE;
                        }
                        if (type.equals(Property.INTEGER_PROPERTY)) {
                            try {
                                Integer.parseInt(prop.getValue());
                                return Operand.TRUE;
                            } catch (Exception e) {
                                return Operand.FALSE;
                            }
                        } else if (type.equals(Property.FLOAT_PROPERTY)) {
                            try {
                                Double.parseDouble(prop.getValue());
                                return Operand.TRUE;
                            } catch (Exception e) {
                                return Operand.FALSE;
                            }
                        } else if (type.equals(Property.DATE_PROPERTY)) {
                            try {
                                if (prop.getTypedValue() instanceof Date) {
                                    return Operand.TRUE;
                                } else {
                                    return Operand.FALSE;
                                }
                            } catch (Exception e) {
                                return Operand.FALSE;
                            }
                        } else if (type.equals(Property.CLOCKTIME_PROPERTY)) {
                            try {
                                ClockTime ct = new ClockTime(prop.getValue());
                                if (ct.calendarValue() == null) {
                                    return Operand.FALSE;
                                }
                                return Operand.TRUE;
                            } catch (Exception e) {
                                return Operand.FALSE;
                            }
                        } else
                            return Operand.TRUE;
                    } else
                        return Operand.FALSE;
                }
                String type = prop.getType();
                Object value = prop.getTypedValue();
                /**
                 * LEGACY MODE!
                 */
                if (value instanceof NavajoType && ((NavajoType) value).isEmpty()) {
                    value = null;
                }
                if (value == null && !type.equals(Property.SELECTION_PROPERTY)) {
                    // If value attribute does not exist AND property is not selection property assume null value
                    resultList.add(null);
                } else if (type.equals(Property.SELECTION_PROPERTY)) {
                    if (!prop.getCardinality().equals("+")) {
                        // Uni-selection property.
                        try {
                            List<Selection> list = prop.getAllSelectedSelections();
                            if (!list.isEmpty()) {
                                Selection sel = list.get(0);
                                resultList.add((selectionOption.equals("name") ? sel.getName() : sel.getValue()));
                            } else {
                                return Operand.NULL;
                            }
                        } catch (com.dexels.navajo.document.NavajoException te) {
                            throw new TMLExpressionException(te.getMessage());
                        }
                    } else {
                        // Multi-selection property.
                        try {
                            List<Selection> list = prop.getAllSelectedSelections();
                            List<Object> result = new ArrayList<>();
                            for (int i = 0; i < list.size(); i++) {
                                Selection sel = list.get(i);
                                Object o = (selectionOption.equals("name")) ? sel.getName() : sel.getValue();
                                result.add(o);
                            }
                            resultList.add(result);
                        } catch (NavajoException te) {
                            throw new TMLExpressionException(te.getMessage(), te);
                        }
                    }
                } else if (type.equals(Property.DATE_PROPERTY)) {
                    if (value == null)
                        resultList.add(null);
                    else {
                        if (!option.equals("")) {
                            try {
                                Date a = (Date) prop.getTypedValue();
                                Calendar cal = Calendar.getInstance();
                                cal.setTime(a);
                                int altA = 0;
                                if (option.equals("month")) {
                                    altA = cal.get(Calendar.MONTH) + 1;
                                } else if (option.equals("day")) {
                                    altA = cal.get(Calendar.DAY_OF_MONTH);
                                } else if (option.equals("year")) {
                                    altA = cal.get(Calendar.YEAR);
                                } else if (option.equals("hour")) {
                                    altA = cal.get(Calendar.HOUR_OF_DAY);
                                } else if (option.equals("minute")) {
                                    altA = cal.get(Calendar.MINUTE);
                                } else if (option.equals("second")) {
                                    altA = cal.get(Calendar.SECOND);
                                } else {
                                    throw new TMLExpressionException("Option not supported: " + option + ", for type: " + type);
                                }
                                resultList.add(altA);
                            } catch (Exception ue) {
                                throw new TMLExpressionException("Invalid date: " + prop.getValue(), ue);
                            }
                        } else {
                            try {
                                Date a = (Date) prop.getTypedValue();
                                resultList.add(a);
                            } catch (java.lang.Exception pe) {
                                resultList.add(null);
                            }
                        }
                    }
                } else if (type.equals(Property.EXPRESSION_PROPERTY)) {
                    resultList.add(prop.getTypedValue());
                } else {
                    try {
                        resultList.add(value);
                    } catch (Exception e) {
                        throw new TMLExpressionException(e.getMessage(), e);
                    }
                }
            }
            if (!singleMatch)
                return Operand.ofList(resultList);
            else if (!resultList.isEmpty())
                return Operand.ofDynamic(resultList.get(0));
            else if (!exists)
                throw new TMLExpressionException("Property does not exist: " + text);
            else
                return Operand.FALSE;
        }

        private Operand parseImmutablePath(String text, ImmutableMessage rm) {
            if ("".equals(text) || "/@".equals(text)) {
                return Operand.ofImmutable(rm);
            }
            if (text.endsWith("/")) {
                String trunc = text.substring(0, text.length() - 1);
                List<String> parts = Arrays.asList(trunc.split("/"));
                return parseImmutableMessagePath(parts, rm);
            }
            List<String> parts = Arrays.asList(text.split("/"));
            return parseImmutablePath(parts, rm);
        }

        private Operand parseImmutableMessagePath(List<String> path, ImmutableMessage rm) {
            if (path.isEmpty()) {
                return Operand.ofImmutable(rm);
            }
            String first = path.get(0);
            Optional<ImmutableMessage> sub = rm.subMessage(first);
            if (!sub.isPresent()) {
                throw new TMLExpressionException("Missing submessage: " + first);
            }
            List<String> copy = new ArrayList<>(path);
            copy.remove(0);
            return parseImmutableMessagePath(copy, sub.get());
        }

        private Operand parseImmutablePath(List<String> path, ImmutableMessage rm) {
            if (path.size() > 1) {
                Optional<ImmutableMessage> imm = rm.subMessage(path.get(0));
                if (imm.isPresent()) {
                    List<String> parts = new LinkedList<>(path);
                    parts.remove(0);
                    return parseImmutablePath(parts, imm.get());
                }
                return null;
            }
            String type = rm.columnType(path.get(0));
            if (type != null) {
                return Operand.ofCustom(rm.value(path.get(0)).orElse(null), type);
            }
            return Operand.ofDynamic(rm.value(path.get(0)).orElse(null));
        }

        @Override
        public Optional<String> returnType() {
            return Optional.empty();
        }

        @Override
        public String expression() {
            return expression;
        }
    };
}
Also used : Arrays(java.util.Arrays) Date(java.util.Date) NavajoException(com.dexels.navajo.document.NavajoException) Function(java.util.function.Function) ArrayList(java.util.ArrayList) ClockTime(com.dexels.navajo.document.types.ClockTime) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Util(com.dexels.navajo.util.Util) Calendar(java.util.Calendar) Operand(com.dexels.navajo.document.Operand) Selection(com.dexels.navajo.document.Selection) LinkedList(java.util.LinkedList) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Navajo(com.dexels.navajo.document.Navajo) TipiLink(com.dexels.navajo.expression.api.TipiLink) Access(com.dexels.navajo.script.api.Access) NavajoType(com.dexels.navajo.document.types.NavajoType) Message(com.dexels.navajo.document.Message) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) ImmutableFactory(com.dexels.immutable.factory.ImmutableFactory) List(java.util.List) FunctionClassification(com.dexels.navajo.expression.api.FunctionClassification) Optional(java.util.Optional) Property(com.dexels.navajo.document.Property) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) MappableTreeNode(com.dexels.navajo.script.api.MappableTreeNode) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Selection(com.dexels.navajo.document.Selection) Access(com.dexels.navajo.script.api.Access) ArrayList(java.util.ArrayList) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) Property(com.dexels.navajo.document.Property) Optional(java.util.Optional) NavajoType(com.dexels.navajo.document.types.NavajoType) ContextExpression(com.dexels.navajo.expression.api.ContextExpression) Calendar(java.util.Calendar) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) ClockTime(com.dexels.navajo.document.types.ClockTime) NavajoException(com.dexels.navajo.document.NavajoException) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Date(java.util.Date) LinkedList(java.util.LinkedList) TipiLink(com.dexels.navajo.expression.api.TipiLink) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage)

Example 27 with NavajoException

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

the class TestAsyncService method test.

public void test(Navajo n) {
    try {
        logger.info("Checking asynchronous progress..");
        Property p = n.getProperty("/Input/iter");
        p.setValue(10000000);
        Property q = n.getProperty("/Input/d");
        q.setValue(1.5);
        registry.doServerAsyncSend(n, "ProcessAsyncTest", new ServerAsyncListener() {

            @Override
            public void setProgress(String id, int d) {
                logger.info("Progress on id: {} d: {}", id, d);
            }

            @Override
            public void serviceStarted(String id) {
                logger.info("Service: {}", id);
            }

            @Override
            public void receiveServerAsync(Navajo n, String method, String serverId, String clientId) {
                try {
                    StringWriter sw = new StringWriter();
                    n.write(sw);
                    logger.info("Result: {}", sw);
                } catch (NavajoException e) {
                    logger.error("Error: ", e);
                }
            }

            @Override
            public void handleException(Exception e) {
                logger.error("Exception in service call: ", e);
            }
        }, "aap", 5000);
    } catch (Exception e) {
        logger.error("Error: ", e);
    }
}
Also used : StringWriter(java.io.StringWriter) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) ClientException(com.dexels.navajo.client.ClientException) NavajoException(com.dexels.navajo.document.NavajoException)

Example 28 with NavajoException

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

the class ITAsyncClient method testPost.

@Test
public void testPost() throws Exception {
    final ManualAsyncClient ac = new AsyncClientImpl();
    String service = "ProcessPrintGenericBirt";
    System.err.println(TestConfig.NAVAJO_TEST_SERVER.getValue());
    ac.setServer(TestConfig.NAVAJO_TEST_SERVER.getValue());
    ac.setUsername(TestConfig.NAVAJO_TEST_USER.getValue());
    ac.setPassword(TestConfig.NAVAJO_TEST_PASS.getValue());
    Navajo input = NavajoFactory.getInstance().createNavajo(getClass().getResourceAsStream("test.xml"));
    final NavajoResponseHandler showOutput = new NavajoResponseHandler() {

        @Override
        public void onResponse(Navajo n) {
            logger.info("Navajo finished!");
            try {
                StringWriter sw = new StringWriter();
                n.write(sw);
                Binary b = (Binary) n.getMessage("Result").getProperty("Data").getTypedValue();
                BinaryOpenerFactory.getInstance().open(b);
                logger.info("Response2 : {}", sw);
            } catch (NavajoException e) {
                logger.error("Error: ", e);
            }
        }

        @Override
        public void onFail(Throwable t) {
            logger.error("whoops: ", t);
        }

        @Override
        public Throwable getCaughtException() {
            return null;
        }
    };
    ac.callService(input, service, showOutput);
    logger.info("Exchange sent");
    Thread.sleep(10000);
}
Also used : ManualAsyncClient(com.dexels.navajo.client.async.ManualAsyncClient) StringWriter(java.io.StringWriter) NavajoException(com.dexels.navajo.document.NavajoException) NavajoResponseHandler(com.dexels.navajo.client.NavajoResponseHandler) Navajo(com.dexels.navajo.document.Navajo) Binary(com.dexels.navajo.document.types.Binary) Test(org.junit.Test)

Example 29 with NavajoException

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

the class NavajoClient method generateConnectionError.

protected void generateConnectionError(Navajo n, int id, String description) {
    try {
        Message conditionError = NavajoFactory.getInstance().createMessage(n, "ConditionErrors", Message.MSG_TYPE_ARRAY);
        n.addMessage(conditionError);
        Message conditionErrorElt = NavajoFactory.getInstance().createMessage(n, "ConditionErrors");
        conditionError.addMessage(conditionErrorElt);
        Property p1 = NavajoFactory.getInstance().createProperty(n, "Id", Property.INTEGER_PROPERTY, id + "", 10, "Id", Property.DIR_OUT);
        Property p2 = NavajoFactory.getInstance().createProperty(n, "Description", Property.INTEGER_PROPERTY, description, 10, "Omschrijving", Property.DIR_OUT);
        Property p3 = NavajoFactory.getInstance().createProperty(n, "FailedExpression", Property.INTEGER_PROPERTY, "", 10, "FailedExpression", Property.DIR_OUT);
        Property p4 = NavajoFactory.getInstance().createProperty(n, "EvaluatedExpression", Property.INTEGER_PROPERTY, "", 10, "EvaluatedExpression", Property.DIR_OUT);
        conditionErrorElt.addProperty(p1);
        conditionErrorElt.addProperty(p2);
        conditionErrorElt.addProperty(p3);
        conditionErrorElt.addProperty(p4);
    } catch (NavajoException ex) {
        logger.error("Error: ", ex);
    }
}
Also used : Message(com.dexels.navajo.document.Message) NavajoException(com.dexels.navajo.document.NavajoException) Property(com.dexels.navajo.document.Property)

Example 30 with NavajoException

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

the class DefaultExpressionEvaluator method processRefreshQueue.

// private boolean dependsOn(Property expressionProperty, Property target) {
// 
// }
// 
private final List<Property> processRefreshQueue(List<Property> queue) throws NavajoException {
    Object o = null;
    Object p = null;
    List<Property> refreshQueue = null;
    for (int i = 0; i < queue.size(); i++) {
        Property current = queue.get(i);
        o = current.peekEvaluatedValue();
        try {
            try {
                current.refreshExpression();
            } catch (ExpressionChangedException e) {
            }
        } catch (NavajoException e) {
            logger.info("Expression failed: " + current.getValue());
        } catch (Throwable e) {
            logger.info("Expression changed");
        }
        p = current.peekEvaluatedValue();
        if (o == null && p == null) {
            continue;
        }
        if (o == null || p == null) {
            if (refreshQueue == null) {
                refreshQueue = new ArrayList<Property>();
            }
            refreshQueue.add(current);
            continue;
        }
        // } else {
        if (refreshQueue == null) {
            refreshQueue = new ArrayList<Property>();
        }
        refreshQueue.add(current);
    // }
    }
    if (refreshQueue == null) {
        refreshQueue = new ArrayList<Property>();
    }
    return refreshQueue;
}
Also used : ExpressionChangedException(com.dexels.navajo.document.ExpressionChangedException) NavajoException(com.dexels.navajo.document.NavajoException) Property(com.dexels.navajo.document.Property)

Aggregations

NavajoException (com.dexels.navajo.document.NavajoException)46 Message (com.dexels.navajo.document.Message)28 Property (com.dexels.navajo.document.Property)25 Navajo (com.dexels.navajo.document.Navajo)21 TMLExpressionException (com.dexels.navajo.expression.api.TMLExpressionException)12 SystemException (com.dexels.navajo.script.api.SystemException)9 Operand (com.dexels.navajo.document.Operand)8 UserException (com.dexels.navajo.script.api.UserException)8 IOException (java.io.IOException)8 StringWriter (java.io.StringWriter)6 Selection (com.dexels.navajo.document.Selection)5 MappableException (com.dexels.navajo.script.api.MappableException)5 ArrayList (java.util.ArrayList)4 ImmutableMessage (com.dexels.immutable.api.ImmutableMessage)3 ManualAsyncClient (com.dexels.navajo.client.async.ManualAsyncClient)3 FatalException (com.dexels.navajo.script.api.FatalException)3 File (java.io.File)3 ClientException (com.dexels.navajo.client.ClientException)2 NavajoResponseHandler (com.dexels.navajo.client.NavajoResponseHandler)2 Header (com.dexels.navajo.document.Header)2