/**
   * 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;
  }
 /**
  * Returns the version history of a market data snapshot.
  *
  * @param snapshotId An snapshot {@link ObjectId}
  * @return JSON array of the snapshot's history
  *     <pre>
  *   [{"uniqueId": "DbSnp~12345~2",
  *     "correctionFrom": "2012-05-23T10:54:10.124293Z",
  *     "correctionTo": null,
  *     "versionFrom": "2012-05-23T10:54:10.124293Z",
  *     "versionTo": null}]
  * </pre>
  */
 @GET
 @Produces(MediaType.APPLICATION_JSON)
 @Path("{snapshotId}")
 public String getMarketDataSnapshotHistory(@PathParam("snapshotId") String snapshotId) {
   ObjectId id = ObjectId.parse(snapshotId);
   MarketDataSnapshotHistoryResult result =
       _snapshotMaster.history(new MarketDataSnapshotHistoryRequest(id));
   List<MarketDataSnapshotDocument> documents = result.getDocuments();
   List<Map<String, Object>> json = Lists.newArrayListWithCapacity(documents.size());
   for (MarketDataSnapshotDocument document : documents) {
     Map<String, Object> map = Maps.newHashMapWithExpectedSize(5);
     map.put("uniqueId", document.getUniqueId());
     map.put("versionFrom", document.getVersionFromInstant());
     map.put("versionTo", document.getVersionToInstant());
     map.put("correctionFrom", document.getCorrectionFromInstant());
     map.put("correctionTo", document.getCorrectionToInstant());
     json.add(map);
   }
   return new JSONArray(json).toString();
 }