use of com.dexels.navajo.adapter.messagemap.PropertyAggregate in project navajo by Dexels.
the class MessageMap method getResultMessage.
public ResultMessage[] getResultMessage() throws UserException {
Message definitionMsg1 = null;
Message definitionMsg2 = null;
List<ResultMessage> resultingMessage = new ArrayList<>();
List<Message> children = this.msg1.getAllMessages();
// Determine definition message, unless groupBy is defined.
if (groupBy == null && this.msg1.getDefinitionMessage() != null) {
definitionMsg1 = this.msg1.getDefinitionMessage();
}
if (groupBy == null && definitionMsg1 != null && this.msg2 != null && this.msg2.getDefinitionMessage() != null) {
definitionMsg2 = this.msg2.getDefinitionMessage();
definitionMsg1.merge(definitionMsg2);
}
for (int i = 0; i < children.size(); i++) {
msg1pointer = children.get(i);
if (msg1pointer.getType().equals(Message.MSG_TYPE_DEFINITION)) {
// Skip definition messages
continue;
}
Object[] joinValues1 = new Object[joinConditions.size()];
for (int p = 0; p < joinConditions.size(); p++) {
JoinCondition jc = joinConditions.get(p);
Property prop = msg1pointer.getProperty(jc.property1);
if (prop == null) {
throw new UserException(-1, "Exception joining messages " + joinMessage1 + " and " + joinMessage2 + ": property not found: " + jc.property1);
}
joinValues1[p] = prop.getValue();
}
// Find c2;
msg2pointer = null;
boolean foundJoinMessage = false;
boolean isSingleMessage = false;
if (this.msg2 != null) {
List<Message> children2 = this.msg2.getAllMessages();
// ==========
if (this.msg2.getType().equals(Message.MSG_TYPE_SIMPLE)) {
// v SIMPLE MESSAGE if
isSingleMessage = true;
} else // ========== // v SIMPLE MESSAGE end if
{
for (int j = 0; j < children2.size(); j++) {
msg2pointer = children2.get(j);
if (msg2pointer.getType().equals(Message.MSG_TYPE_DEFINITION)) {
// Skip definition messages
continue;
}
Object[] joinValues2 = new Object[joinConditions.size()];
for (int p = 0; p < joinConditions.size(); p++) {
JoinCondition jc = joinConditions.get(p);
Property prop = msg2pointer.getProperty(jc.property2);
if (prop == null) {
throw new UserException(-1, "Exception joining messages " + joinMessage1 + " and " + joinMessage2 + ": property not found: " + jc.property2);
}
joinValues2[p] = prop.getValue();
}
// Compare joinValues...
boolean equal = true;
for (int jv = 0; jv < joinConditions.size(); jv++) {
if (joinValues1[jv] != null && joinValues2[jv] != null && !joinValues1[jv].equals(joinValues2[jv])) {
equal = false;
} else if (joinValues1[jv] == null && joinValues2[jv] != null) {
equal = false;
} else if (joinValues1[jv] != null && joinValues2[jv] == null) {
equal = false;
}
}
if (joinExpression != null) {
// Evaluate joinExpression.
try {
Operand ro = Expression.evaluate(joinExpression.toString(), myAccess.getInDoc(), myAccess.getCompiledScript().getCurrentMap(), myAccess.getCurrentInMessage());
equal = (Boolean) ro.value;
} catch (Exception e) {
throw new UserException(-1, "Exception joining messages: " + e.getMessage(), e);
}
}
if (equal) {
Message newMsg = NavajoFactory.getInstance().createMessage(myAccess.getOutputDoc(), "tmp");
// Check for duplicate property names. If found, rename to _1 _2 respectively
// DO NOT, CAN LEAD TO STRANGE BEHAVIOR: renameDuplicates(msg1pointer, msg2pointer);
newMsg.merge(msg2pointer);
newMsg.merge(msg1pointer);
ResultMessage rm = new ResultMessage();
rm.setMessage(definitionMsg1, newMsg, this.suppressProperties);
resultingMessage.add(rm);
foundJoinMessage = true;
}
}
// end for
}
// ==========
}
if (!foundJoinMessage && joinType.equals(OUTER_JOIN) && !isSingleMessage) {
// Append dummy message with empty property values in case no join condition match...
if (msg2pointer != null) {
Message newMsg = NavajoFactory.getInstance().createMessage(myAccess.getOutputDoc(), "tmp");
Message c2c = msg2pointer.copy();
clearPropertyValues(c2c);
newMsg.merge(c2c);
newMsg.merge(msg1pointer);
ResultMessage rm = new ResultMessage();
rm.setMessage(definitionMsg1, newMsg, this.suppressProperties);
resultingMessage.add(rm);
} else {
// Assume empty second array message
Message c1c = msg1pointer.copy();
ResultMessage rm = new ResultMessage();
rm.setMessage(definitionMsg1, c1c, this.suppressProperties);
resultingMessage.add(rm);
}
}
if (!foundJoinMessage && joinType.equals(OUTER_JOIN) && isSingleMessage) {
// if ( msg2pointer != null ) {
if (msg2 != null) {
// msg2 instead of msg2pointer if something goes wrong change back to msg2pointer
Message newMsg = NavajoFactory.getInstance().createMessage(myAccess.getOutputDoc(), "tmp");
Message c1c = msg1pointer.copy();
// clearPropertyValues(c1c);
newMsg.merge(c1c);
// newMsg.merge(msg1pointer);
// msg2 instead of msg2pointer if something goes wrong change back to msg2pointer
newMsg.merge(msg2);
ResultMessage rm = new ResultMessage();
rm.setMessage(definitionMsg1, newMsg, this.suppressProperties);
resultingMessage.add(rm);
} else {
// Assume empty second array message
Message c1c = msg1pointer.copy();
ResultMessage rm = new ResultMessage();
rm.setMessage(definitionMsg1, c1c, this.suppressProperties);
resultingMessage.add(rm);
}
}
}
if (children.isEmpty() && definitionMsg1 != null && msg1 != null) {
// Make sure definition message stays intact
Navajo out = myAccess.getOutputDoc();
Message newMessage = NavajoFactory.getInstance().createMessage(out, msg1.getName(), msg1.getType());
newMessage.addMessage(definitionMsg1.copy(myAccess.getOutputDoc()));
myAccess.getOutputDoc().addMessage(newMessage);
}
if (groupBy != null) {
removeDuplicates = true;
Map<String, PropertyAggregate> aggregates = new HashMap<>();
for (int i = 0; i < resultingMessage.size(); i++) {
Map<String, Object> group = new TreeMap<>();
ResultMessage rm = resultingMessage.get(i);
Message m = rm.getMsg();
List<Property> properties = m.getAllProperties();
for (int j = 0; j < properties.size(); j++) {
Property p = properties.get(j);
if (groupByProperties.contains(p.getName())) {
group.put(p.getName(), p.getTypedValue());
}
}
for (int j = 0; j < properties.size(); j++) {
Property p = properties.get(j);
if (!groupByProperties.contains(p.getName())) {
PropertyAggregate pa = aggregates.get(p.getName());
if (pa == null) {
pa = new PropertyAggregate();
aggregates.put(p.getName(), pa);
}
pa.addProperty(p, group);
m.removeProperty(p);
}
}
}
for (int i = 0; i < resultingMessage.size(); i++) {
resultingMessage.get(i).setAggregates(aggregates);
}
}
if (removeDuplicates) {
for (int i = 0; i < resultingMessage.size(); i++) {
Message m1 = resultingMessage.get(i).getMsg().copy();
resultingMessage.get(i).processSuppressedProperties(m1);
if (!resultingMessage.get(i).isRemove()) {
for (int j = i + 1; j < resultingMessage.size(); j++) {
Message m2 = resultingMessage.get(j).getMsg().copy();
resultingMessage.get(j).processSuppressedProperties(m2);
if (m1.isEqual(m2)) {
resultingMessage.get(j).setRemove(true);
}
}
}
}
}
Iterator<ResultMessage> iter = resultingMessage.iterator();
while (iter.hasNext()) {
ResultMessage c = iter.next();
if (c.isRemove()) {
iter.remove();
}
}
return resultingMessage.toArray(new ResultMessage[] {});
}
Aggregations