private void saveEvent(List<Object> list, HistoryEvent event, long entityInternalId) {
    Map<String, Object> e = new LinkedHashMap<String, Object>();

    if (event.getRecordable().getInternalId() != entityInternalId) {
      // Only save the internal id as a reference to entities which aren't this one.
      putIfExists(e, fields.getHistoryRecordable(), event.getRecordable().getInternalId());
    }
    putIfExists(e, fields.getHistoryAction(), event.getAction());
    putIfExists(e, fields.getHistoryType(), event.getEventType());
    putIfExists(e, fields.getHistoryParameters(), event.getParameters());
    putIfExists(e, fields.getHistoryReferrer(), event.getReferrer());
    putIfExists(e, fields.getHistoryTimestamp(), event.getTimestamp());

    list.add(e);

    if (HistoryEvents.MERGED_TYPE.equalsIgnoreCase(event.getEventType())
        && event.getParameters() != null
        && event.getParameters(HistoryEvents.PARAM_MERGED_ID).isPresent()) {
      Optional<String> mergedId = event.getParameters(HistoryEvents.PARAM_MERGED_ID);
      Integer id = Ints.tryParse(mergedId.get());
      if (id != null) {
        Collection<HistoryEvent> mergedEvents = documentHistory.getHistory(id);
        if (mergedEvents != null) {
          saveEvents(list, mergedEvents, entityInternalId);
        } else {
          getMonitor().warn("Null history for {}", id);
        }
      } else {
        getMonitor().warn("No merge id for merge history of {}", event.getRecordable());
      }
    }
  }
  /**
   * Convert from an entity to a map.
   *
   * @param entity the entity to convert
   * @return a map containing the entity's fields (and history is required)
   */
  public Map<String, Object> convertEntity(Entity entity) {
    Map<String, Object> map = Maps.newHashMap();

    convertFeatures(map, entity);

    if (outputHistory && documentHistory != null) {
      Collection<HistoryEvent> events = documentHistory.getHistory(entity.getInternalId());
      convertHistory(map, events, entity.getInternalId());
    }
    putIfExists(map, fields.getExternalId(), entity.getExternalId());

    return map;
  }