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;
}
Aggregations