Search in sources :

Example 46 with Property

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

the class BaseMessageImpl method compareTo.

@Override
@SuppressWarnings({ "unchecked", "rawtypes" })
public final int compareTo(Message m) {
    if (m != null) {
        if (getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT)) {
            if (getArrayParentMessage() != null) {
                String order = this.getArrayParentMessage().getOrderBy();
                if (!"".equals(order)) {
                    // Parse the orderby attribute
                    // Put them in a set
                    StringTokenizer tok = new StringTokenizer(order, ",");
                    List<String> orderValues = new LinkedList<>();
                    while (tok.hasMoreTokens()) {
                        String token = tok.nextToken();
                        orderValues.add(token.trim());
                    }
                    // while messages are equal and there are more
                    // orderValues
                    // keep ordering
                    int compare = 0;
                    Iterator<String> it = orderValues.iterator();
                    while (it.hasNext() && compare == 0) {
                        String oV = it.next();
                        // If DESC we flip the direction
                        int desc = -1;
                        if (oV.indexOf(' ') > 0) {
                            String sort = oV.substring(oV.indexOf(' ') + 1);
                            oV = oV.substring(0, oV.indexOf(' '));
                            if ("DESC".equalsIgnoreCase(sort)) {
                                desc = 1;
                            }
                        }
                        // Check whether oV is a function instead of a property.
                        if (oV.indexOf("(") != -1) {
                            // It is a function.
                            String compareFunction = oV.substring(0, oV.indexOf("("));
                            Comparator c = null;
                            try {
                                final ComparatorManager instance = ComparatorManagerFactory.getInstance();
                                c = instance.getComparator(compareFunction);
                                if (c == null) {
                                    logger.error("Comparator not found: {}. Not sorting.", compareFunction);
                                    compare = 0;
                                } else {
                                    compare = c.compare(this, m);
                                }
                            } catch (Exception e) {
                                logger.error("Error on compare message using {}", compareFunction, e);
                                compare = 0;
                            }
                        } else {
                            // Now we assume oV is an existing property in both messages
                            Property myOvProp = getProperty(oV);
                            if (myOvProp == null) {
                                logger.info("WARNING: error while sorting message. Could not sort property named: " + oV);
                                return 0;
                            }
                            Property compOvProp = m.getProperty(oV);
                            compare = desc * compOvProp.compareTo(myOvProp);
                        }
                    }
                    return compare;
                }
            }
        }
    }
    return 0;
}
Also used : StringTokenizer(java.util.StringTokenizer) ComparatorManager(com.dexels.navajo.document.comparatormanager.ComparatorManager) Property(com.dexels.navajo.document.Property) LinkedList(java.util.LinkedList) NavajoException(com.dexels.navajo.document.NavajoException) ExpressionChangedException(com.dexels.navajo.document.ExpressionChangedException) IOException(java.io.IOException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Comparator(java.util.Comparator)

Example 47 with Property

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

the class BaseMessageImpl method printElementJSONTypeless.

private void printElementJSONTypeless(String name, final Writer sw, String[] propertyFilter) throws IOException {
    ArrayList<Message> messages = getAllMessages();
    ArrayList<Property> properties = getAllProperties();
    if (getType().equals(Message.MSG_TYPE_ARRAY)) {
        writeElement(sw, "\"" + name + "\" : [");
        int cnt = 0;
        if (propertyFilter != null) {
            for (Message m : messages) {
                if (cnt > 0) {
                    writeElement(sw, ", ");
                }
                ((BaseMessageImpl) m).printElementJSONTypeless(name, sw, propertyFilter);
                cnt++;
            }
        } else {
            for (Message m : messages) {
                if (cnt > 0) {
                    writeElement(sw, ", ");
                }
                ((BaseNode) m).printElementJSONTypeless(sw);
                cnt++;
            }
        }
        writeElement(sw, "]");
    } else if (getType().equals(Message.MSG_TYPE_ARRAY_ELEMENT)) {
        writeElement(sw, "{");
        int cnt = 0;
        if (propertyFilter != null) {
            for (String s : propertyFilter) {
                if (cnt > 0) {
                    writeElement(sw, ", ");
                }
                Property p = getProperty(s);
                if (p != null) {
                    ((BaseNode) p).printElementJSONTypeless(sw);
                    cnt++;
                }
            }
        } else {
            for (Property p : properties) {
                if (cnt > 0) {
                    writeElement(sw, ", ");
                }
                ((BaseNode) p).printElementJSONTypeless(sw);
                cnt++;
            }
        }
        writeElement(sw, "}");
    } else if (getType().equals(Message.MSG_TYPE_SIMPLE)) {
        writeElement(sw, "\"" + getName() + "\" : {");
        int cnt = 0;
        if (propertyFilter != null) {
            for (String s : propertyFilter) {
                if (cnt > 0) {
                    writeElement(sw, ", ");
                }
                Property p = getProperty(s);
                ((BaseNode) p).printElementJSONTypeless(sw);
                cnt++;
            }
        } else {
            for (Property p : properties) {
                if (cnt > 0) {
                    writeElement(sw, ", ");
                }
                ((BaseNode) p).printElementJSONTypeless(sw);
                cnt++;
            }
        }
        writeElement(sw, "}");
        cnt = 0;
        for (Message m : messages) {
            if (cnt > 0) {
                writeElement(sw, ", ");
            }
            ((BaseNode) m).printElementJSONTypeless(sw);
            cnt++;
        }
        writeElement(sw, "}");
    }
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 48 with Property

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

the class BaseMessageImpl method getPropertyByPath.

public final Property getPropertyByPath(String pth) {
    String path = null;
    if (pth.length() > 0 && pth.charAt(0) == '/') {
        path = pth.substring(1);
    } else {
        path = pth;
    }
    if (path.startsWith("..")) {
        if (getParentMessage() == null) {
            return null;
        }
        return getParentMessage().getProperty(path.substring(3));
    }
    int slash = path.indexOf("/");
    if (slash < 0) {
        if (propertyList == null || propertyMap == null) {
            return null;
        }
        if (propertyList.size() != propertyMap.size()) {
            logger.info("Warning: Propertymap sizE: " + propertyMap.size() + " listsize: " + propertyList.size());
        }
        Property pp = propertyMap.get(path);
        if (pp == null && !Message.MSG_TYPE_DEFINITION.equals(getType())) {
            // check for definition messages (except if I'm a definition
            // message myself)
            Message arrayP = getArrayParentMessage();
            if (arrayP != null) {
                Message def = arrayP.getDefinitionMessage();
                if (def != null && def.getProperty(path) != null) {
                    Property res = def.getProperty(path).copy(getRootDoc());
                    if (def.getType() == null || "".equals(def.getType())) {
                        throw new IllegalStateException("DEFINITION PROPERTY FOUND WITHOUT TYPE!");
                    }
                    res.setType(res.getType());
                    addProperty(res);
                    return res;
                }
            }
        }
        return pp;
    } else {
        String msgname = path.substring(0, slash);
        String propname = path.substring(slash, path.length());
        BaseMessageImpl ms = (BaseMessageImpl) getMessage(msgname);
        if (ms != null) {
            return ms.getPropertyByPath(propname);
        }
        return null;
    }
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

Example 49 with Property

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

the class BaseMessageImpl method setValue.

@Override
public void setValue(String propertyName, Object value) {
    Property p = getProperty(propertyName);
    if (p == null) {
        p = NavajoFactory.getInstance().createProperty(getRootDoc(), propertyName, Property.STRING_PROPERTY, "", 0, "", Property.DIR_OUT);
        addProperty(p);
    }
    p.setAnyValue(value);
}
Also used : Property(com.dexels.navajo.document.Property)

Example 50 with Property

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

the class BaseMessageImpl method isEqual.

@Override
public final boolean isEqual(Message o, String skipProperties) {
    BaseMessageImpl other = (BaseMessageImpl) o;
    if (!other.getName().equals(this.getName())) {
        return false;
    }
    // Check sub message structure.
    List<Message> allOther = other.messageList;
    if (allOther != null && messageList == null) {
        return false;
    }
    if (messageList != null && allOther == null) {
        return false;
    }
    if (allOther != null && allOther.size() != messageList.size()) {
        return false;
    }
    if (allOther != null) {
        for (int i = 0; i < allOther.size(); i++) {
            Message otherMsg = allOther.get(i);
            boolean match = false;
            for (int j = 0; j < messageList.size(); j++) {
                Message myMsg = messageList.get(j);
                if (myMsg.isEqual(otherMsg, skipProperties)) {
                    match = true;
                    j = messageList.size() + 1;
                }
            }
            if (!match) {
                return false;
            }
        }
    }
    // Check property structure.
    ArrayList<Property> allOtherProps = other.getAllProperties();
    ArrayList<Property> allMyProps = this.getAllProperties();
    if (allOtherProps.size() != allMyProps.size()) {
        return false;
    }
    for (int i = 0; i < allOtherProps.size(); i++) {
        Property otherProp = allOtherProps.get(i);
        boolean match = false;
        // Check whether property name exists in skipProperties list.
        if (skipProperties.indexOf(otherProp.getName()) != -1) {
            match = true;
        } else {
            for (int j = 0; j < allMyProps.size(); j++) {
                Property myProp = allMyProps.get(j);
                if (myProp.isEqual(otherProp)) {
                    match = true;
                    j = allMyProps.size() + 1;
                }
            }
        }
        if (!match) {
            return false;
        }
    }
    return true;
}
Also used : Message(com.dexels.navajo.document.Message) Property(com.dexels.navajo.document.Property)

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