@Override
 public synchronized YieldCurveDefinitionDocument update(YieldCurveDefinitionDocument document) {
   ArgumentChecker.notNull(document, "document");
   ArgumentChecker.notNull(document.getYieldCurveDefinition(), "document.yieldCurveDefinition");
   final Currency currency = document.getYieldCurveDefinition().getCurrency();
   final String name = document.getYieldCurveDefinition().getName();
   final UniqueId uid = UniqueId.of(getUniqueIdScheme(), name + "_" + currency.getCode());
   if (!uid.equals(document.getUniqueId())) {
     throw new IllegalArgumentException("Invalid unique identifier");
   }
   final Pair<Currency, String> key = Pair.of(currency, name);
   final TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
   if (value == null) {
     throw new DataNotFoundException("UID '" + uid + "' not found");
   }
   if (_sourceVersionCorrection.getVersionAsOf() != null) {
     // Don't need to keep the old values before the one needed by "versionAsOfInstant"
     final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
     value.headMap(oldestNeeded).clear();
   } else {
     // Don't need any old values
     value.clear();
   }
   Instant now = Instant.now();
   value.put(now, document.getYieldCurveDefinition());
   document.setUniqueId(uid);
   changeManager().entityChanged(ChangeType.CHANGED, uid.getObjectId(), null, null, now);
   return document;
 }
 @Override
 public synchronized void remove(ObjectIdentifiable objectIdentifiable) {
   ArgumentChecker.notNull(objectIdentifiable, "objectIdentifiable");
   if (!getUniqueIdScheme().equals(objectIdentifiable.getObjectId().getScheme())) {
     throw new DataNotFoundException(
         "Scheme '"
             + objectIdentifiable.getObjectId().getScheme()
             + "' not valid for '"
             + getUniqueIdScheme()
             + "'");
   }
   final int i = objectIdentifiable.getObjectId().getValue().indexOf('_');
   if (i <= 0) {
     throw new DataNotFoundException(
         "Identifier '"
             + objectIdentifiable.getObjectId().getValue()
             + "' not valid for '"
             + getUniqueIdScheme()
             + "'");
   }
   final String name = objectIdentifiable.getObjectId().getValue().substring(0, i);
   final String iso = objectIdentifiable.getObjectId().getValue().substring(i + 1);
   final Currency currency;
   try {
     currency = Currency.of(iso);
   } catch (IllegalArgumentException e) {
     throw new DataNotFoundException(
         "Identifier '"
             + objectIdentifiable.getObjectId().getValue()
             + "' not valid for '"
             + getUniqueIdScheme()
             + "'",
         e);
   }
   final Pair<Currency, String> key = Pair.of(currency, name);
   if (_sourceVersionCorrection.getVersionAsOf() != null) {
     final TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
     if (value == null) {
       throw new DataNotFoundException("Curve definition not found");
     }
     // Don't need to keep the old values before the one needed by "versionAsOfInstant"
     final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
     if (oldestNeeded != null) {
       value.headMap(oldestNeeded).clear();
     }
     // Store a null to indicate the delete
     value.put(Instant.now(), null);
   } else {
     if (_definitions.remove(key) == null) {
       throw new DataNotFoundException("Curve definition not found");
     }
   }
   changeManager()
       .entityChanged(
           ChangeType.REMOVED, objectIdentifiable.getObjectId(), null, null, Instant.now());
 }
  // -------------------------------------------------------------------------
  public MarketDataSnapshotSearchResult search(final MarketDataSnapshotSearchRequest request) {
    ArgumentChecker.notNull(request, "request");
    ArgumentChecker.notNull(request.getPagingRequest(), "request.pagingRequest");
    ArgumentChecker.notNull(request.getVersionCorrection(), "request.versionCorrection");
    s_logger.debug("search {}", request);

    final MarketDataSnapshotSearchResult result = new MarketDataSnapshotSearchResult();

    final VersionCorrection vc = request.getVersionCorrection().withLatestFixed(now());
    final DbMapSqlParameterSource args = new DbMapSqlParameterSource();
    args.addTimestamp("version_as_of_instant", vc.getVersionAsOf());
    args.addTimestamp("corrected_to_instant", vc.getCorrectedTo());
    args.addValueNullIgnored("name", getDialect().sqlWildcardAdjustValue(request.getName()));
    args.addValue("details", request.isIncludeData());
    args.addValue("paging_offset", request.getPagingRequest().getFirstItem());
    args.addValue("paging_fetch", request.getPagingRequest().getPagingSize());

    String[] sql = {
      getElSqlBundle().getSql("Search", args), getElSqlBundle().getSql("SearchCount", args)
    };
    searchWithPaging(
        request.getPagingRequest(),
        sql,
        args,
        new MarketDataSnapshotDocumentExtractor(request.isIncludeData()),
        result);
    return result;
  }
Exemplo n.º 4
0
 @Override
 public SecurityDocument get(ObjectIdentifiable objectId, VersionCorrection versionCorrection) {
   final RestTarget target = _targetSecurity.resolve(objectId.getObjectId().toString());
   if (versionCorrection != null && versionCorrection.getVersionAsOf() != null) {
     target.resolveQuery(
         "versionAsOf", Collections.singletonList(versionCorrection.getVersionAsOf().toString()));
   }
   if (versionCorrection != null && versionCorrection.getCorrectedTo() != null) {
     target.resolveQuery(
         "correctedTo", Collections.singletonList(versionCorrection.getCorrectedTo().toString()));
   }
   s_logger.debug("get-get to {}", target);
   final FudgeMsg message = getRestClient().getMsg(target);
   if (message == null) {
     s_logger.debug("get-recv NULL");
     throw new DataNotFoundException("Security with identifier " + objectId);
   }
   s_logger.debug("get-recv {}", message);
   return getFudgeDeserializer().fudgeMsgToObject(SecurityDocument.class, message);
 }
 // -------------------------------------------------------------------------
 @Override
 public synchronized YieldCurveDefinition getDefinition(Currency currency, String name) {
   ArgumentChecker.notNull(currency, "currency");
   ArgumentChecker.notNull(name, "name");
   final TreeMap<Instant, YieldCurveDefinition> definitions =
       _definitions.get(Pair.of(currency, name));
   if (definitions == null) {
     return null;
   }
   final Map.Entry<Instant, YieldCurveDefinition> entry;
   if (_sourceVersionCorrection.getVersionAsOf() == null) {
     entry = definitions.lastEntry();
   } else {
     entry = definitions.floorEntry(_sourceVersionCorrection.getVersionAsOf());
   }
   if (entry == null) {
     return null;
   }
   return entry.getValue();
 }
 @Override
 public synchronized YieldCurveDefinitionDocument addOrUpdate(
     YieldCurveDefinitionDocument document) {
   ArgumentChecker.notNull(document, "document");
   ArgumentChecker.notNull(document.getYieldCurveDefinition(), "document.yieldCurveDefinition");
   final Currency currency = document.getYieldCurveDefinition().getCurrency();
   final String name = document.getYieldCurveDefinition().getName();
   final Pair<Currency, String> key = Pair.of(currency, name);
   TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
   final UniqueId uid = UniqueId.of(getUniqueIdScheme(), name + "_" + currency.getCode());
   Instant now = Instant.now();
   if (value != null) {
     if (_sourceVersionCorrection.getVersionAsOf() != null) {
       // Don't need to keep the old values before the one needed by "versionAsOfInstant"
       final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
       if (oldestNeeded != null) {
         value.headMap(oldestNeeded).clear();
       }
     } else {
       // Don't need any old values
       value.clear();
     }
     value.put(now, document.getYieldCurveDefinition());
     changeManager().entityChanged(ChangeType.CHANGED, uid.getObjectId(), null, null, now);
   } else {
     value = new TreeMap<Instant, YieldCurveDefinition>();
     value.put(now, document.getYieldCurveDefinition());
     _definitions.put(key, value);
     changeManager()
         .entityChanged(
             ChangeType.ADDED,
             uid.getObjectId(),
             document.getVersionFromInstant(),
             document.getVersionToInstant(),
             now);
   }
   document.setUniqueId(uid);
   return document;
 }
 @Override
 public YieldCurveDefinition getDefinition(
     final Currency currency, final String name, final VersionCorrection versionCorrection) {
   ArgumentChecker.notNull(currency, "currency");
   ArgumentChecker.notNull(name, "name");
   final TreeMap<Instant, YieldCurveDefinition> definitions =
       _definitions.get(Pair.of(currency, name));
   if (definitions == null) {
     return null;
   }
   final Map.Entry<Instant, YieldCurveDefinition> entry =
       definitions.floorEntry(versionCorrection.getVersionAsOf());
   if (entry == null) {
     return null;
   }
   return entry.getValue();
 }
  @Override
  public List<UniqueId> replaceVersions(
      ObjectIdentifiable objectIdentifiable,
      List<YieldCurveDefinitionDocument> replacementDocuments) {
    ArgumentChecker.notNull(replacementDocuments, "replacementDocuments");
    ArgumentChecker.notNull(objectIdentifiable, "objectIdentifiable");

    final Instant now = Instant.now();

    for (YieldCurveDefinitionDocument replacementDocument : replacementDocuments) {
      ArgumentChecker.notNull(replacementDocument, "document");
      ArgumentChecker.notNull(
          replacementDocument.getYieldCurveDefinition(), "document.yieldCurveDefinition");
      final Currency currency = replacementDocument.getYieldCurveDefinition().getCurrency();
      final String name = replacementDocument.getYieldCurveDefinition().getName();
      final UniqueId id = UniqueId.of(getUniqueIdScheme(), name + "_" + currency.getCode());
      ArgumentChecker.isTrue(id.equals(objectIdentifiable), "Invalid object identifier");
    }

    YieldCurveDefinitionDocument storedDocument = get(objectIdentifiable, null);
    if (storedDocument == null) {
      throw new DataNotFoundException("Document not found: " + objectIdentifiable);
    }
    final Currency currency = storedDocument.getYieldCurveDefinition().getCurrency();
    final String name = storedDocument.getYieldCurveDefinition().getName();
    Pair<Currency, String> key = Pair.of(currency, name);

    final TreeMap<Instant, YieldCurveDefinition> value = _definitions.get(key);
    if (value == null) {
      throw new DataNotFoundException("OID '" + objectIdentifiable + "' not found");
    }
    if (_sourceVersionCorrection.getVersionAsOf() != null) {
      // Don't need to keep the old values before the one needed by "versionAsOfInstant"
      final Instant oldestNeeded = value.floorKey(_sourceVersionCorrection.getVersionAsOf());
      value.headMap(oldestNeeded).clear();
    } else {
      // Don't need any old values
      value.clear();
    }

    Instant lowestCurrentVersionFrom = value.firstKey();

    List<YieldCurveDefinitionDocument> orderedReplacementDocuments =
        MasterUtils.adjustVersionInstants(
            now, lowestCurrentVersionFrom, null, replacementDocuments);

    final Instant lowestVersionFrom = orderedReplacementDocuments.get(0).getVersionFromInstant();
    final Instant highestVersionTo =
        orderedReplacementDocuments
            .get(orderedReplacementDocuments.size() - 1)
            .getVersionToInstant();

    if (orderedReplacementDocuments.size() > 0) {
      value.subMap(lowestVersionFrom, true, highestVersionTo, false).clear();
    }

    for (YieldCurveDefinitionDocument replacementDocument : orderedReplacementDocuments) {
      value.put(
          replacementDocument.getVersionFromInstant(),
          replacementDocument.getYieldCurveDefinition());
      changeManager()
          .entityChanged(ChangeType.CHANGED, replacementDocument.getObjectId(), null, null, now);
    }
    return MasterUtils.mapToUniqueIDs(orderedReplacementDocuments);
  }