Search in sources :

Example 1 with ComparatorManager

use of com.dexels.navajo.document.comparatormanager.ComparatorManager 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)

Aggregations

ExpressionChangedException (com.dexels.navajo.document.ExpressionChangedException)1 NavajoException (com.dexels.navajo.document.NavajoException)1 Property (com.dexels.navajo.document.Property)1 ComparatorManager (com.dexels.navajo.document.comparatormanager.ComparatorManager)1 IOException (java.io.IOException)1 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)1 Comparator (java.util.Comparator)1 LinkedList (java.util.LinkedList)1 StringTokenizer (java.util.StringTokenizer)1