/**
   * Copies the content of the current CMR buffer to the Storage.
   *
   * @param storageData Storage to copy data to.
   * @param platformIdents List of agent IDs.
   * @param dataProcessors Processors that will be used for data writing.
   * @param autoFinalize If the storage where action is performed should be auto-finalized after the
   *     write.
   * @throws BusinessException If storage is used as a recording storage.
   * @throws SerializationException If storage needs to be created, and serialization fails.
   * @throws IOException If IO exception occurs.
   */
  public void copyBufferToStorage(
      StorageData storageData,
      List<Long> platformIdents,
      Collection<AbstractDataProcessor> dataProcessors,
      boolean autoFinalize)
      throws BusinessException, IOException, SerializationException {
    if (!isStorageExisting(storageData)) {
      this.createStorage(storageData);
    }
    StorageData local = getLocalStorageDataObject(storageData);
    if (!isStorageOpen(local)) {
      this.openStorage(local);
    }

    DefaultData oldestBufferElement = buffer.getOldestElement();
    // only copy if we have smth in the buffer
    if (null != oldestBufferElement) {

      // oldest date is the buffer oldest date, we don't include data from before
      Date fromDate = new Date(oldestBufferElement.getTimeStamp().getTime());
      Date toDate = null;

      // check if we have the time-frame limit
      for (AbstractDataProcessor dataProcessor : dataProcessors) {
        if (dataProcessor instanceof TimeFrameDataProcessor) {
          TimeFrameDataProcessor timeFrameDataProcessor = (TimeFrameDataProcessor) dataProcessor;
          // update dates
          if (timeFrameDataProcessor.getFromDate().after(fromDate)) {
            fromDate = timeFrameDataProcessor.getFromDate();
          }
          toDate = timeFrameDataProcessor.getToDate();
          break;
        }
      }

      for (Long platformId : platformIdents) {
        List<DefaultData> toWriteList =
            storageDataDao.getAllDefaultDataForAgent(platformId.longValue(), fromDate, toDate);
        this.writeToStorage(local, toWriteList, dataProcessors, true);
      }
    }

    if (autoFinalize) {
      this.closeStorage(local);
    }
    updateExistingStorageSize(local);
  }