Search in sources :

Example 76 with Property

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

the class SumProperties method main.

public static void main(String[] args) throws Exception {
    System.setProperty("com.dexels.navajo.DocumentImplementation", "com.dexels.navajo.document.nanoimpl.NavajoFactoryImpl");
    Navajo doc = NavajoFactory.getInstance().createNavajo();
    Message top = NavajoFactory.getInstance().createMessage(doc, "Top");
    doc.addMessage(top);
    Message array = NavajoFactory.getInstance().createMessage(doc, "MyArray", Message.MSG_TYPE_ARRAY);
    top.addMessage(array);
    for (int i = 0; i < 9; i++) {
        Message elt = NavajoFactory.getInstance().createMessage(doc, "MyArray", Message.MSG_TYPE_ARRAY_ELEMENT);
        array.addMessage(elt);
        Message array2 = NavajoFactory.getInstance().createMessage(doc, "NogEenArraytje" + i, Message.MSG_TYPE_ARRAY);
        elt.addMessage(array2);
        for (int j = 0; j < 2; j++) {
            Message elt2 = NavajoFactory.getInstance().createMessage(doc, "NogEenArraytje" + i, Message.MSG_TYPE_ARRAY_ELEMENT);
            array2.addMessage(elt2);
            Property p = NavajoFactory.getInstance().createProperty(doc, "MyBoolean", Property.BOOLEAN_PROPERTY, Property.TRUE, 0, "", Property.DIR_OUT);
            elt2.addProperty(p);
        }
    }
    // doc.write(System.err);
    Operand result = Expression.evaluate("SumProperties('/Top/MyArray.*/NogEenArraytje.*', 'MyBoolean')", doc);
    System.err.println("result = " + result.value);
}
Also used : Message(com.dexels.navajo.document.Message) Operand(com.dexels.navajo.document.Operand) Navajo(com.dexels.navajo.document.Navajo) Property(com.dexels.navajo.document.Property)

Example 77 with Property

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

the class SumProperties method evaluate.

@Override
public Object evaluate() throws com.dexels.navajo.expression.api.TMLExpressionException {
    if (getOperands().size() < 2) {
        for (int i = 0; i < getOperands().size(); i++) {
            Object o = getOperands().get(i);
            logger.info("Operand # " + i + " is: " + o.toString() + " - " + o.getClass());
        }
        throw new TMLExpressionException(this, "Wrong number of arguments: " + getOperands().size());
    }
    if (!(getOperand(0) instanceof String && getOperand(1) instanceof String)) {
        throw new TMLExpressionException(this, "Wrong argument types: " + getOperand(0).getClass() + " and " + getOperand(1).getClass());
    }
    String messageName = (String) getOperand(0);
    String propertyName = (String) getOperand(1);
    String filter = null;
    if (getOperands().size() > 2) {
        filter = (String) getOperand(2);
    }
    Message parent = getCurrentMessage();
    Navajo doc = getNavajo();
    try {
        List<Message> arrayMsg = (parent != null ? parent.getMessages(messageName) : doc.getMessages(messageName));
        if (arrayMsg == null) {
            throw new TMLExpressionException(this, "Empty or non existing array message: " + messageName);
        }
        String sumType = "int";
        double sum = 0;
        for (int i = 0; i < arrayMsg.size(); i++) {
            Message m = arrayMsg.get(i);
            Property p = m.getProperty(propertyName);
            boolean evaluate = (filter != null ? Condition.evaluate(filter, doc, null, m, getAccess()) : true);
            if (evaluate) {
                if (p != null) {
                    Object o = p.getTypedValue();
                    if (o == null) {
                        continue;
                    }
                    if (!(o instanceof Integer || o instanceof Double || o instanceof Float || o instanceof Money || o instanceof Percentage || o instanceof Boolean || o instanceof String)) {
                        throw new TMLExpressionException(this, "Only numbers are supported a sum. Not: " + (o.getClass().toString()) + " value: " + o);
                    }
                    if (o instanceof String) {
                        if ("".equals(o)) {
                        // ignore
                        } else {
                            logger.error("Only numbers are supported a sum. Not strings. Value:  " + o);
                            throw new TMLExpressionException(this, "Only numbers are supported a sum. Not strings. Value:  " + o + (o.getClass().toString()));
                        }
                    }
                    if (o instanceof Integer) {
                        sumType = "int";
                        sum += ((Integer) o).doubleValue();
                    } else if (o instanceof Double) {
                        // if (!((Double)o).equals(Double.valueOf(Double.NaN))) {
                        sumType = "float";
                        sum += ((Double) o).doubleValue();
                    // }
                    } else if (o instanceof Float) {
                        // if (!((Float)o).equals(new Float(Float.NaN))) {
                        sumType = "float";
                        sum += ((Float) o).doubleValue();
                    // }
                    } else if (o instanceof Money) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "money";
                        sum += ((Money) o).doubleValue();
                    // }
                    } else if (o instanceof Percentage) {
                        // if (!Double.valueOf(((Money)o).doubleValue()).equals(Double.valueOf(Float.NaN))) {
                        sumType = "percentage";
                        sum += ((Percentage) o).doubleValue();
                    // }
                    } else if (o instanceof Boolean) {
                        sumType = "int";
                        sum += ((Boolean) o).booleanValue() ? 1 : 0;
                    }
                } else {
                    throw new TMLExpressionException(this, "Property does not exist: " + propertyName);
                }
            }
        }
        if (sumType.equals("int")) {
            return Integer.valueOf((int) sum);
        } else if (sumType.equals("money")) {
            return new Money(sum);
        } else if (sumType.equals("percentage")) {
            return new Percentage(sum);
        } else {
            return Double.valueOf(sum);
        }
    } catch (NavajoException ne) {
        throw new TMLExpressionException(this, ne.getMessage());
    } catch (SystemException se) {
        throw new TMLExpressionException(this, se.getMessage());
    }
}
Also used : Message(com.dexels.navajo.document.Message) Percentage(com.dexels.navajo.document.types.Percentage) NavajoException(com.dexels.navajo.document.NavajoException) Navajo(com.dexels.navajo.document.Navajo) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Money(com.dexels.navajo.document.types.Money) SystemException(com.dexels.navajo.script.api.SystemException) Property(com.dexels.navajo.document.Property)

Example 78 with Property

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

the class SetAllProperties method evaluate.

@Override
public Object evaluate() throws TMLExpressionException {
    // input (ArrayList, Object).
    if (this.getOperands().size() != 3)
        throw new TMLExpressionException("SetAllProperties(Message, String, Object) expected");
    Object a = this.getOperands().get(0);
    if (!(a instanceof Message))
        throw new TMLExpressionException("SetAllProperties(Message, String, Object) expected");
    Object b = this.getOperands().get(1);
    if (!(b instanceof String))
        throw new TMLExpressionException("SetAllProperties(Message, String, Object) expected");
    Object c = this.getOperands().get(2);
    Message source = (Message) a;
    String propertyName = (String) b;
    for (Iterator<Message> iter = source.getAllMessages().iterator(); iter.hasNext(); ) {
        Message element = iter.next();
        Property p = element.getProperty(propertyName);
        if (p != null) {
            p.setAnyValue(c);
        }
    }
    return null;
}
Also used : Message(com.dexels.navajo.document.Message) TMLExpressionException(com.dexels.navajo.expression.api.TMLExpressionException) Property(com.dexels.navajo.document.Property)

Example 79 with Property

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

the class ServiceCommand method appendTokenAttributes.

private void appendTokenAttributes(ArticleRuntime runtime, Navajo n) {
    Map<String, Object> extraParams = new HashMap<String, Object>();
    if (runtime.getToken() != null && runtime.getToken().getUser() != null) {
        extraParams.put("USERID", runtime.getToken().getUser().getUserId());
        extraParams.put("USERNAME", runtime.getToken().getUser().getUsername());
    } else {
        extraParams.put("USERID", -1);
        extraParams.put("USERNAME", runtime.getUsername());
    }
    extraParams.put("TENANT", runtime.getInstance());
    extraParams.put("PERSONID", "");
    extraParams.put("DOMAIN", "");
    extraParams.put("UNIONID", "");
    GlobalManagerImpl.appendMapToAAA(n, extraParams);
    if (n.getMessage(Message.MSG_TOKEN_BLOCK) == null) {
        Message tokenMsg = NavajoFactory.getInstance().createMessage(n, Message.MSG_TOKEN_BLOCK);
        n.addMessage(tokenMsg);
    }
    Message tokenMsg = n.getMessage(Message.MSG_TOKEN_BLOCK);
    // Add attributes
    for (String key : runtime.getUserAttributes().keySet()) {
        Object value = runtime.getUserAttributes().get(key);
        Property p2 = NavajoFactory.getInstance().createProperty(n, key, "", "", Property.DIR_OUT);
        p2.setAnyValue(value);
        tokenMsg.addProperty(p2);
    }
}
Also used : Message(com.dexels.navajo.document.Message) HashMap(java.util.HashMap) Property(com.dexels.navajo.document.Property)

Example 80 with Property

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

the class MessageMapTest method test4.

@Test
public void test4() throws Exception {
    // message1 array and message2 array test
    /* building message 1 array*/
    Navajo n = NavajoFactory.getInstance().createNavajo();
    Message array = NavajoFactory.getInstance().createMessage(n, "productArray");
    n.addMessage(array);
    array.setType(Message.MSG_TYPE_ARRAY);
    /*Creating the first element of the message array productArray*/
    Message c1e1 = NavajoFactory.getInstance().createMessage(n, "productArray");
    c1e1.setIndex(0);
    c1e1.setType(Message.MSG_TYPE_ARRAY_ELEMENT);
    array.addElement(c1e1);
    Property c1e1p1 = NavajoFactory.getInstance().createProperty(n, "Product", Property.STRING_PROPERTY, "PC", 0, "", "out");
    Property c1e1p1a = NavajoFactory.getInstance().createProperty(n, "Sub", Property.STRING_PROPERTY, "Laptop", 0, "", "out");
    Property c1e1p2 = NavajoFactory.getInstance().createProperty(n, "Age", Property.INTEGER_PROPERTY, (0) + "", 0, "", "out");
    c1e1.addProperty(c1e1p1);
    c1e1.addProperty(c1e1p1a);
    c1e1.addProperty(c1e1p2);
    /*Creating the second element of the message array productArray */
    Message c1e2 = NavajoFactory.getInstance().createMessage(n, "productArray");
    c1e2.setIndex(1);
    c1e2.setType(Message.MSG_TYPE_ARRAY_ELEMENT);
    array.addElement(c1e2);
    Property c1e2p1 = NavajoFactory.getInstance().createProperty(n, "Product", Property.STRING_PROPERTY, "PC", 0, "", "out");
    Property c1e2p1a = NavajoFactory.getInstance().createProperty(n, "Sub", Property.STRING_PROPERTY, "Desktop", 0, "", "out");
    Property c1e2p2 = NavajoFactory.getInstance().createProperty(n, "Age", Property.INTEGER_PROPERTY, (20) + "", 0, "", "out");
    c1e2.addProperty(c1e2p1);
    c1e2.addProperty(c1e2p1a);
    c1e2.addProperty(c1e2p2);
    n.write(System.err);
    /* building message 2 array*/
    Navajo n2 = NavajoFactory.getInstance().createNavajo();
    Message array2 = NavajoFactory.getInstance().createMessage(n2, "coreArray");
    n2.addMessage(array2);
    array2.setType(Message.MSG_TYPE_ARRAY);
    /*Creating the first element of the message array coreArray */
    Message c2e1 = NavajoFactory.getInstance().createMessage(n2, "coreArray");
    c2e1.setIndex(0);
    c2e1.setType(Message.MSG_TYPE_ARRAY_ELEMENT);
    array2.addElement(c2e1);
    Property c2e1p1 = NavajoFactory.getInstance().createProperty(n2, "Product", Property.STRING_PROPERTY, "PC", 0, "", "out");
    Property c2e1p2 = NavajoFactory.getInstance().createProperty(n2, "Sub", Property.STRING_PROPERTY, "Laptop", 0, "", "out");
    Property c2e1p3 = NavajoFactory.getInstance().createProperty(n2, "Core", Property.STRING_PROPERTY, "I", 0, "", "out");
    c2e1.addProperty(c2e1p1);
    c2e1.addProperty(c2e1p2);
    c2e1.addProperty(c2e1p3);
    /*Creating the second element of the message array coreArray */
    Message c2e2 = NavajoFactory.getInstance().createMessage(n2, "coreArray");
    c2e2.setIndex(0);
    c2e2.setType(Message.MSG_TYPE_ARRAY_ELEMENT);
    array2.addElement(c2e2);
    Property c2e2p1 = NavajoFactory.getInstance().createProperty(n2, "Product", Property.STRING_PROPERTY, "PC", 0, "", "out");
    Property c2e2p2 = NavajoFactory.getInstance().createProperty(n2, "Sub", Property.STRING_PROPERTY, "Laptop", 0, "", "out");
    Property c2e2p3 = NavajoFactory.getInstance().createProperty(n2, "Core", Property.STRING_PROPERTY, "A", 0, "", "out");
    c2e2.addProperty(c2e2p1);
    c2e2.addProperty(c2e2p2);
    c2e2.addProperty(c2e2p3);
    n2.write(System.err);
    Access a = new Access();
    a.setOutputDoc(n);
    MessageMap mm = new MessageMap();
    mm.load(a);
    mm.setJoinMessage1("productArray");
    /*for the 2nd message*/
    a.setOutputDoc(n2);
    mm.load(a);
    mm.setJoinMessage2("coreArray");
    mm.setJoinType("inner");
    mm.setJoinCondition("Sub=Sub");
    Message resultMessage = NavajoFactory.getInstance().createMessage(n, "ResultingMessage");
    resultMessage.setType("array");
    n.addMessage(resultMessage);
    a.setCurrentOutMessage(resultMessage);
    ResultMessage[] result = mm.getResultMessage();
    a.setCurrentOutMessage(null);
    mm.store();
    for (int l = 0; l < result.length; l++) {
        assertEquals("Laptop", result[l].getProperty("Sub"));
    }
}
Also used : Message(com.dexels.navajo.document.Message) ResultMessage(com.dexels.navajo.adapter.messagemap.ResultMessage) Access(com.dexels.navajo.script.api.Access) Navajo(com.dexels.navajo.document.Navajo) ResultMessage(com.dexels.navajo.adapter.messagemap.ResultMessage) Property(com.dexels.navajo.document.Property) Test(org.junit.Test)

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