/**
   * Inserts a new document.
   *
   * @param document the document, not null
   * @return the new document, not null
   */
  @Override
  protected MarketDataSnapshotDocument insert(final MarketDataSnapshotDocument document) {
    ArgumentChecker.notNull(document.getSnapshot(), "document.snapshot");
    ArgumentChecker.notNull(document.getName(), "document.name");

    final ManageableMarketDataSnapshot marketDataSnaphshot = document.getSnapshot();
    final long docId = nextId("snp_snapshot_seq");
    final long docOid =
        (document.getUniqueId() != null ? extractOid(document.getUniqueId()) : docId);
    // set the uniqueId (needs to go in Fudge message)
    final UniqueId uniqueId = createUniqueId(docOid, docId);
    marketDataSnaphshot.setUniqueId(uniqueId);
    document.setUniqueId(uniqueId);

    // the arguments for inserting into the marketDataSnaphshot table
    FudgeMsgEnvelope env = FUDGE_CONTEXT.toFudgeMsg(marketDataSnaphshot);
    byte[] bytes = FUDGE_CONTEXT.toByteArray(env.getMessage());
    final DbMapSqlParameterSource marketDataSnaphshotArgs =
        new DbMapSqlParameterSource()
            .addValue("doc_id", docId)
            .addValue("doc_oid", docOid)
            .addTimestamp("ver_from_instant", document.getVersionFromInstant())
            .addTimestampNullFuture("ver_to_instant", document.getVersionToInstant())
            .addTimestamp("corr_from_instant", document.getCorrectionFromInstant())
            .addTimestampNullFuture("corr_to_instant", document.getCorrectionToInstant())
            .addValue("name", document.getName())
            .addValue("detail", new SqlLobValue(bytes, getDialect().getLobHandler()), Types.BLOB);

    final String sql = getElSqlBundle().getSql("Insert", marketDataSnaphshotArgs);
    getJdbcTemplate().update(sql, marketDataSnaphshotArgs);
    return document;
  }
  /**
   * @return JSON {@code [{basisViewName: basisViewName1, snapshots: [{id: snapshot1Id, name:
   *     snapshot1Name}, ...]}, ...]}
   */
  @GET
  @Produces(MediaType.APPLICATION_JSON)
  public String getMarketDataSnapshotList() {
    MarketDataSnapshotSearchRequest snapshotSearchRequest = new MarketDataSnapshotSearchRequest();
    snapshotSearchRequest.setIncludeData(false);

    Multimap<String, ManageableMarketDataSnapshot> snapshotsByBasisView =
        LinkedListMultimap.create();
    for (MarketDataSnapshotDocument doc :
        MarketDataSnapshotSearchIterator.iterable(_snapshotMaster, snapshotSearchRequest)) {
      ManageableMarketDataSnapshot snapshot = doc.getSnapshot();
      if (snapshot.getUniqueId() == null) {
        s_logger.warn("Ignoring snapshot with null unique identifier {}", snapshot.getName());
        continue;
      }
      if (StringUtils.isBlank(snapshot.getName())) {
        s_logger.warn("Ignoring snapshot {} with no name", snapshot.getUniqueId());
        continue;
      }
      if (s_guidPattern.matcher(snapshot.getName()).find()) {
        s_logger.debug(
            "Ignoring snapshot which appears to have an auto-generated name: {}",
            snapshot.getName());
        continue;
      }
      String basisViewName =
          snapshot.getBasisViewName() != null ? snapshot.getBasisViewName() : "unknown";
      snapshotsByBasisView.put(basisViewName, snapshot);
    }
    // list of maps for each basis view: {"basisViewName": basisViewName, "snapshots", [...]}
    List<Map<String, Object>> basisViewSnapshotList = new ArrayList<Map<String, Object>>();
    for (String basisViewName : snapshotsByBasisView.keySet()) {
      Collection<ManageableMarketDataSnapshot> viewSnapshots =
          snapshotsByBasisView.get(basisViewName);
      // list of maps containing snapshot IDs and names: {"id", snapshotId, "name", snapshotName}
      List<Map<String, Object>> snapshotsList =
          new ArrayList<Map<String, Object>>(viewSnapshots.size());
      for (ManageableMarketDataSnapshot viewSnapshot : viewSnapshots) {
        // map for a single snapshot: {"id", snapshotId, "name", snapshotName}
        Map<String, Object> snapshotMap =
            ImmutableMap.<String, Object>of(
                ID, viewSnapshot.getUniqueId(), NAME, viewSnapshot.getName());
        snapshotsList.add(snapshotMap);
      }
      basisViewSnapshotList.add(
          ImmutableMap.of(BASIS_VIEW_NAME, basisViewName, SNAPSHOTS, snapshotsList));
    }
    return new JSONArray(basisViewSnapshotList).toString();
  }