Search in sources :

Example 31 with Navajo

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

the class DomainObjectMapperTest method testSetExcludedProperties.

@Test
public void testSetExcludedProperties() throws Exception {
    // TestPOJO tp = new TestPOJO();
    DomainObjectMapper dom = new DomainObjectMapper();
    dom.setObjectName("com.dexels.navajo.mapping.bean.TestPOJO");
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(doc, "Test");
    System.err.println("m = " + m);
    doc.addMessage(m);
    Property p1 = NavajoFactory.getInstance().createProperty(doc, "name", Property.STRING_PROPERTY, "nice", 0, "", "out");
    m.addProperty(p1);
    java.util.Date d = new java.util.Date();
    Property p2 = NavajoFactory.getInstance().createProperty(doc, "birthdate", Property.DATE_PROPERTY, null, 0, "", "out");
    p2.setAnyValue(d);
    m.addProperty(p2);
    Property p3 = NavajoFactory.getInstance().createProperty(doc, "something", Property.STRING_PROPERTY, "notnice", 0, "", "out");
    m.addProperty(p3);
    Access a = new Access();
    a.setInDoc(doc);
    a.setOutputDoc(NavajoFactory.getInstance().createNavajo());
    dom.load(a);
    dom.setExcludedProperties("something");
    dom.setCurrentMessageName("Test");
    dom.store();
    TestPOJO tp = (TestPOJO) dom.getMyObject();
    assertEquals("nice", tp.getName());
    assertEquals(d, tp.getBirthdate());
    assertNull(tp.getSomething());
}
Also used : Message(com.dexels.navajo.document.Message) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

Example 32 with Navajo

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

the class TestRun method testTimestamp.

@Test
public void testTimestamp() {
    ImmutableMessage msg = ReplicationFactory.getInstance().parseStream(getClass().getResourceAsStream("test3.json")).message();
    Navajo nn = JsonTmlFactory.getInstance().toFlatNavajo("Match", msg);
    Message match = nn.getMessage("Match");
    assertNotNull(match.getProperty("matchtime"));
    Property matchtime = match.getProperty("matchtime");
    assertEquals(Property.DATE_PROPERTY, matchtime.getType());
    Date matchtimeobj = (Date) matchtime.getTypedValue();
    Calendar cal = Calendar.getInstance();
    cal.setTime(matchtimeobj);
    assertEquals(1971, cal.get(Calendar.YEAR));
    assertEquals(12, cal.get(Calendar.HOUR_OF_DAY));
    assertEquals(0, cal.get(Calendar.MINUTE));
    Property matchtime2 = match.getProperty("matchtime2");
    assertEquals(Property.CLOCKTIME_PROPERTY, matchtime2.getType());
}
Also used : Message(com.dexels.navajo.document.Message) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Calendar(java.util.Calendar) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property) Date(java.util.Date) Test(org.junit.Test)

Example 33 with Navajo

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

the class ExpressionTest method testExpression.

@Test
public void testExpression() throws Exception {
    ExpressionEvaluator ee = NavajoFactory.getInstance().getExpressionEvaluator();
    Operand o = ee.evaluate("1+1", null, null, null);
    assertEquals(2, o.value);
    o = ee.evaluate("TODAY + 0#0#2#0#0#0", null, null, null);
    System.err.println(o.value);
    Navajo testDoc = NavajoFactory.getInstance().createNavajo();
    Message m = NavajoFactory.getInstance().createMessage(testDoc, "MyTop");
    testDoc.addMessage(m);
    Message a = NavajoFactory.getInstance().createMessage(testDoc, "MyArrayMessage", "array");
    m.addMessage(a);
    for (int i = 0; i < 5; i++) {
        Message a1 = NavajoFactory.getInstance().createMessage(testDoc, "MyArrayMessage");
        a.addMessage(a1);
        Property p = NavajoFactory.getInstance().createProperty(testDoc, "MyProp", "string", "noot" + i, 0, "", "in");
        a1.addProperty(p);
        Property p2 = NavajoFactory.getInstance().createProperty(testDoc, "MyProp2", "string", "aap" + i, 0, "", "in");
        a1.addProperty(p2);
    }
    o = ee.evaluate("'hallo:' + [/MyTop/MyArrayMessage@MyProp=noot1/MyProp2]", testDoc, null, null);
    assertEquals("hallo:aap1", o.value);
    o = ee.evaluate("'hallo:' + [/MyTop/MyArrayMessage@2/MyProp2]", testDoc, null, null);
    assertEquals("hallo:aap2", o.value);
}
Also used : ImmutableMessage(com.dexels.immutable.api.ImmutableMessage) Message(com.dexels.navajo.document.Message) Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo) ExpressionEvaluator(com.dexels.navajo.document.ExpressionEvaluator) CachedExpressionEvaluator(com.dexels.navajo.parser.compiled.api.CachedExpressionEvaluator) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

Example 34 with Navajo

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

the class Exists 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.TRUE;
                    }
                }
            } catch (SystemException ex) {
                logger.error("Error: ", ex);
            }
        }
    } catch (NavajoException ex) {
        throw new TMLExpressionException(this, "Error evaluating message path");
    }
    return Boolean.FALSE;
}
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 35 with Navajo

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

the class JsonToTml method evaluate.

@Override
public final Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    Object s = this.getOperands().get(0);
    Object m = null;
    if (getOperands().size() != 1) {
        m = this.getOperands().get(1);
    }
    if (s == null) {
        return null;
    }
    if (s instanceof String) {
        Navajo n;
        try {
            Reader r = new StringReader((String) s);
            if (m == null) {
                n = JSONTMLFactory.getInstance().parse(r);
            } else {
                n = JSONTMLFactory.getInstance().parse(r, m.toString());
            }
            return n;
        } catch (Exception e) {
            return null;
        }
    }
    return null;
}
Also used : StringReader(java.io.StringReader) StringReader(java.io.StringReader) Reader(java.io.Reader) 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