private PrivateInstanceLocal getPrivateInstance(
     InstanceLocal instance, int type, PrivateSeriesLocal privSeries)
     throws FinderException, CreateException {
   Collection col = privInstHome.findBySopIuid(type, instance.getSopIuid());
   PrivateInstanceLocal privInstance;
   if (col.isEmpty()) {
     if (privSeries == null) {
       privSeries = getPrivateSeries(instance.getSeries(), type, null, false);
     }
     privInstance = privInstHome.create(type, instance.getAttributes(true), privSeries);
   } else {
     privInstance = (PrivateInstanceLocal) col.iterator().next();
   }
   Object[] files = instance.getFiles().toArray();
   FileLocal file;
   for (int i = 0; i < files.length; i++) {
     file = (FileLocal) files[i];
     privFileHome.create(
         file.getFilePath(),
         file.getFileTsuid(),
         file.getFileSize(),
         file.getFileMd5(),
         file.getFileStatus(),
         privInstance,
         file.getFileSystem());
     try {
       file.remove();
     } catch (Exception x) {
       log.warn("Can not remove File record:" + file, x);
     }
   }
   return privInstance;
 }
  private Dataset getStudyMgtDataset(StudyLocal study, Map mapSeries) {
    Dataset ds = dof.newDataset();
    ds.putUI(Tags.StudyInstanceUID, study.getStudyIuid());
    ds.putOB(PrivateTags.StudyPk, Convert.toBytes(study.getPk().longValue()));
    ds.putSH(Tags.AccessionNumber, study.getAccessionNumber());
    ds.putLO(Tags.PatientID, study.getPatient().getPatientId());
    ds.putLO(Tags.IssuerOfPatientID, study.getPatient().getIssuerOfPatientId());
    ds.putPN(Tags.PatientName, study.getPatient().getPatientName());

    log.debug("getStudyMgtDataset: studyIUID:" + study.getStudyIuid());
    DcmElement refSeriesSeq = ds.putSQ(Tags.RefSeriesSeq);

    Iterator iter =
        (mapSeries == null) ? study.getSeries().iterator() : mapSeries.keySet().iterator();
    while (iter.hasNext()) {
      SeriesLocal sl = (SeriesLocal) iter.next();
      Dataset dsSer = refSeriesSeq.addNewItem();
      dsSer.putUI(Tags.SeriesInstanceUID, sl.getSeriesIuid());
      Collection instances =
          (mapSeries == null) ? sl.getInstances() : (Collection) mapSeries.get(sl);
      Iterator iter2 = instances.iterator();
      DcmElement refSopSeq = null;
      if (iter2.hasNext()) refSopSeq = dsSer.putSQ(Tags.RefSOPSeq);
      while (iter2.hasNext()) {
        InstanceLocal il = (InstanceLocal) iter2.next();
        Dataset dsInst = refSopSeq.addNewItem();
        dsInst.putUI(Tags.RefSOPClassUID, il.getSopCuid());
        dsInst.putUI(Tags.RefSOPInstanceUID, il.getSopIuid());
        dsInst.putAE(Tags.RetrieveAET, il.getRetrieveAETs());
      }
    }
    if (log.isDebugEnabled()) {
      log.debug("return StgMgtDataset:");
      log.debug(ds);
    }
    return ds;
  }