private PrivateSeriesLocal getPrivateSeries(
     SeriesLocal series, int type, PrivateStudyLocal privStudy, boolean includeInstances)
     throws FinderException, CreateException {
   Collection col = privSeriesHome.findBySeriesIuid(type, series.getSeriesIuid());
   PrivateSeriesLocal privSeries;
   if (col.isEmpty()) {
     if (privStudy == null) {
       privStudy = getPrivateStudy(series.getStudy(), type, null, false);
     }
     privSeries = privSeriesHome.create(type, series.getAttributes(true), privStudy);
   } else {
     privSeries = (PrivateSeriesLocal) col.iterator().next();
   }
   if (includeInstances) {
     for (Iterator iter = series.getInstances().iterator(); iter.hasNext(); ) {
       getPrivateInstance(
           (InstanceLocal) iter.next(), type, privSeries); // move also all instances
     }
   }
   return privSeries;
 }
  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;
  }