JSONObject root = new JSONObject();
try {
JsonDataSink tradeSink = new JsonDataSink(BlotterUtils.getJsonBuildingConverters());
if (isOtc(security)) {
_otcTradeBuilder.extractTradeData(trade, tradeSink);
MetaBean securityMetaBean = s_metaBeansByTypeName.get(security.getClass().getSimpleName());
if (securityMetaBean == null) {
throw new DataNotFoundException("No MetaBean is registered for security type " + security.getClass().getName());
}
BeanVisitor<JSONObject> securityVisitor =
new BuildingBeanVisitor<>(security, new JsonDataSink(BlotterUtils.getJsonBuildingConverters()));
BeanTraverser securityTraverser = BlotterUtils.securityJsonBuildingTraverser();
JSONObject securityJson = (JSONObject) securityTraverser.traverse(securityMetaBean, securityVisitor);
if (security instanceof FinancialSecurity) {
UnderlyingSecurityVisitor visitor = new UnderlyingSecurityVisitor(VersionCorrection.LATEST, _securityMaster);
ManageableSecurity underlying = ((FinancialSecurity) security).accept(visitor);
if (underlying != null) {
BeanVisitor<JSONObject> underlyingVisitor =
new BuildingBeanVisitor<>(underlying, new JsonDataSink(BlotterUtils.getJsonBuildingConverters()));
MetaBean underlyingMetaBean = s_metaBeansByTypeName.get(underlying.getClass().getSimpleName());
JSONObject underlyingJson = (JSONObject) securityTraverser.traverse(underlyingMetaBean, underlyingVisitor);
root.put("underlying", underlyingJson);
}
}
root.put("security", securityJson);