/* (non-Javadoc)
  * @see org.csstudio.archive.reader.ValueIterator#close()
  */
 @Override
 public void close() {
   try {
     synchronized (this) {
       if (stdStream != null) {
         stdStream.close();
       }
       if (minStream != null) {
         minStream.close();
       }
       if (maxStream != null) {
         maxStream.close();
       }
       if (countStream != null) {
         countStream.close();
       }
       super.close();
     }
   } catch (IOException e) {
     throw new IllegalStateException(e);
   }
 }
  /*
   * (non-Javadoc)
   * @see org.csstudio.archive.reader.appliance.ApplianceMeanValueIterator#fetchDataInternal(java.lang.String)
   */
  @Override
  protected void fetchDataInternal(String pvName) throws ArchiverApplianceException {
    super.fetchDataInternal(pvName);
    int interval = Math.max(1, (int) ((end.getSec() - start.getSec()) / requestedPoints));
    java.sql.Timestamp sqlStartTimestamp = TimestampHelper.toSQLTimestamp(start);
    java.sql.Timestamp sqlEndTimestamp = TimestampHelper.toSQLTimestamp(end);

    String std =
        new StringBuilder()
            .append(ApplianceArchiveReaderConstants.OP_STD)
            .append(interval)
            .append('(')
            .append(pvName)
            .append(')')
            .toString();
    DataRetrieval dataRetrieval = reader.createDataRetriveal(reader.getDataRetrievalURL());
    stdStream = dataRetrieval.getDataForPV(std, sqlStartTimestamp, sqlEndTimestamp);
    if (stdStream != null) {
      stdIterator = stdStream.iterator();
    } else {
      throw new ArchiverApplianceException("Could not fetch standard deviation data.");
    }

    String min =
        new StringBuilder()
            .append(ApplianceArchiveReaderConstants.OP_MIN)
            .append(interval)
            .append('(')
            .append(pvName)
            .append(')')
            .toString();
    dataRetrieval = reader.createDataRetriveal(reader.getDataRetrievalURL());
    minStream = dataRetrieval.getDataForPV(min, sqlStartTimestamp, sqlEndTimestamp);
    if (minStream != null) {
      minIterator = minStream.iterator();
    } else {
      throw new ArchiverApplianceException("Could not fetch minimum data.");
    }

    String max =
        new StringBuilder()
            .append(ApplianceArchiveReaderConstants.OP_MAX)
            .append(interval)
            .append('(')
            .append(pvName)
            .append(')')
            .toString();
    dataRetrieval = reader.createDataRetriveal(reader.getDataRetrievalURL());
    maxStream = dataRetrieval.getDataForPV(max, sqlStartTimestamp, sqlEndTimestamp);
    if (maxStream != null) {
      maxIterator = maxStream.iterator();
    } else {
      throw new ArchiverApplianceException("Could not fetch maximum data.");
    }

    String count =
        new StringBuilder()
            .append(ApplianceArchiveReaderConstants.OP_COUNT)
            .append(interval)
            .append('(')
            .append(pvName)
            .append(')')
            .toString();
    dataRetrieval = reader.createDataRetriveal(reader.getDataRetrievalURL());
    countStream = dataRetrieval.getDataForPV(count, sqlStartTimestamp, sqlEndTimestamp);
    if (countStream != null) {
      countIterator = countStream.iterator();
    } else {
      throw new ArchiverApplianceException("Could not fetch count data.");
    }
  }