Skip to content

Commit

Permalink
AGNT-378 Support Enhanced tags in MessageML
Browse files Browse the repository at this point in the history
  • Loading branch information
mohamed-rojbeni committed Feb 12, 2024
1 parent 8dc8971 commit e751afa
Show file tree
Hide file tree
Showing 27 changed files with 1,073 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import freemarker.template.TemplateExceptionHandler;
import org.apache.commons.io.input.ReaderInputStream;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.symphonyoss.symphony.messageml.bi.BiContext;
import org.symphonyoss.symphony.messageml.bi.BiFields;
Expand All @@ -39,6 +40,7 @@
import org.symphonyoss.symphony.messageml.elements.ExpandableCard;
import org.symphonyoss.symphony.messageml.elements.ExpandableCardBody;
import org.symphonyoss.symphony.messageml.elements.ExpandableCardHeader;
import org.symphonyoss.symphony.messageml.elements.Tag;
import org.symphonyoss.symphony.messageml.elements.Form;
import org.symphonyoss.symphony.messageml.elements.FormElement;
import org.symphonyoss.symphony.messageml.elements.FormatEnum;
Expand Down Expand Up @@ -83,6 +85,9 @@
import org.symphonyoss.symphony.messageml.util.IDataProvider;
import org.symphonyoss.symphony.messageml.util.NoOpEntityResolver;
import org.symphonyoss.symphony.messageml.util.NullErrorHandler;
import org.symphonyoss.symphony.messageml.util.instrument.resolver.InstrumentKind;
import org.symphonyoss.symphony.messageml.util.instrument.resolver.InstrumentResolution;
import org.symphonyoss.symphony.messageml.util.instrument.resolver.ResolutionResults;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
Expand All @@ -102,6 +107,7 @@
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
Expand Down Expand Up @@ -366,8 +372,8 @@ private MessageML parseMessageML(String messageML, String version) throws Invali

MessageML result = new MessageML(messageFormat, version);
result.buildAll(this, docElement);
result.enhanceFinancialTags(result, dataProvider);
result.validate();

return result;
}

Expand Down Expand Up @@ -629,6 +635,9 @@ public Element createElement(org.w3c.dom.Element element, Element parent) throws
case Superscript.MESSAGEML_TAG:
return new Superscript(parent);

case Tag.MESSAGEML_TAG:
return new Tag(parent, ++index);

default:
throw new InvalidInputException("Invalid MessageML content at element \"" + tag + "\"");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public enum BiFields {
MENTIONS("mentions", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
HASHTAGS("hashtags", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
CASHTAGS("cashtags", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
FINTAGS("fintags", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
BULLET_LIST("lists", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
CARD("cards", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
EXPANDABLE_CARDS_COLLAPSED("expandablecards_collapsed", BiEventType.MESSAGEML_MESSAGE_SENT, "0"),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,28 @@ public Integer countChildrenOfType(Class<? extends Element> type) {
return count;
}

/**
* This method applies a breadth-first traversal of a tree of elements getting list of
* elements found which
* belong to the class type passed as input
*/
public List<Element> getChildrenOfType(Class<? extends Element> type) {
List<Element> elements = new ArrayList<>();
Stack<Element> stack = new Stack<>();
Element current = this;
stack.push(current);
while (!stack.isEmpty()) {
current = stack.pop();
if (current.getClass() == type) {
elements.add(current);
}
for (Element child : current.getChildren()) {
stack.push(child);
}
}
return elements;
}

/**
* Return the EntityJSON representation of the node.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,23 @@

import com.fasterxml.jackson.databind.node.JsonNodeFactory;
import com.fasterxml.jackson.databind.node.ObjectNode;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.apache.commons.lang3.tuple.Pair;
import org.commonmark.node.Document;
import org.symphonyoss.symphony.messageml.MessageMLContext;
import org.symphonyoss.symphony.messageml.MessageMLParser;
import org.symphonyoss.symphony.messageml.exceptions.InvalidInputException;
import org.symphonyoss.symphony.messageml.util.IDataProvider;
import org.symphonyoss.symphony.messageml.util.XmlPrintStream;
import org.symphonyoss.symphony.messageml.util.instrument.resolver.InstrumentKind;
import org.symphonyoss.symphony.messageml.util.instrument.resolver.InstrumentResolution;
import org.symphonyoss.symphony.messageml.util.instrument.resolver.ResolutionResults;
import org.w3c.dom.Node;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;


/**
Expand Down Expand Up @@ -182,4 +191,56 @@ private void validateTargetIdForUIActions() throws InvalidInputException {
uiAction.setAttribute(TARGET_ID, dialog.getPresentationMlIdAttribute());
}
}

public void enhanceFinancialTags(MessageML result, IDataProvider dataProvider)
throws InvalidInputException {

List<Tag> elements = result.getChildrenOfType(Tag.class)
.stream()
.map(element -> Tag.class.cast(element))
.collect(Collectors.toList());
if (elements != null && !elements.isEmpty()) {processFinancialTags(elements, dataProvider);}
}

private void processFinancialTags(List<Tag> elements, IDataProvider dataProvider)
throws InvalidInputException {
List<Pair<InstrumentResolution, Tag>> instrumentResolutionMap =
IntStream.range(0, elements.size())
.mapToObj(index -> buildInstrumentResolutionRequest(elements.get(index), index))
.collect(Collectors.toList());
// Build resolver api request
List<InstrumentResolution> criteria =
instrumentResolutionMap.stream().map(Pair::getLeft).collect(Collectors.toList());
ResolutionResults results = dataProvider.getFinTagPresentation(criteria);
// update financial tag element data
instrumentResolutionMap.forEach(entry -> {
String resolutionId = entry.getLeft().getResolutionId();
if (results != null && results.getInstruments() != null && results.getInstruments()
.containsKey(resolutionId)) {
entry.getRight()
.setInstrument(
results.getInstruments().get(resolutionId).getInstrument());
}
}
);
for (Tag element : elements) {element.validateFallBackTicker();}
}

private Pair<InstrumentResolution, Tag> buildInstrumentResolutionRequest(Tag tag,
Integer order) {
InstrumentResolution resolution = new InstrumentResolution();
resolution.setResolutionId(order.toString());
resolution.setFullBbgCompTicker(tag.getTagAttributes().getFullBbgCompTicker());
resolution.setFigi(tag.getTagAttributes().getFigi());
resolution.setFigiTicker(tag.getTagAttributes().getFigiTicker());
resolution.setIsin(tag.getTagAttributes().getIsin());
resolution.setUsCode(tag.getTagAttributes().getUscode());
resolution.setFullBbgCompTicker(tag.getTagAttributes().getFullBbgCompTicker());
resolution.setLocalCode(tag.getTagAttributes().getLocalcode());
resolution.setOperationalMic(tag.getTagAttributes().getOperationalMic());
resolution.setInstrumentClass(
InstrumentKind.fromValue(tag.getTagAttributes().getInstrumentclass()));
return new ImmutablePair<>(resolution, tag);
}

}
Loading

0 comments on commit e751afa

Please sign in to comment.