/**
   * Prepares the Dataset representing the search key in C-FIND. As no values are set, the keys
   * match to every content in the archive. The user has to specify concret values to limit the
   * searchSee PS 3.4 - C.6.2.1.2 Study level.
   *
   * <p>As the result for C-FIND these keys are filled with the values found in the archive.
   *
   * @param cfg the configuration properties for this class.
   * @throws ParseException if a given properties for the keys was not found.
   */
  private void initKeys(ConfigProperties cfg) throws ParseException {
    // Remove all keys
    keys = dof.newDataset();

    // Query/Retrieve Level. PS 3.4 - C.6.2 Study Root SOP Class Group
    keys.putCS(Tags.QueryRetrieveLevel, getQueryRetrieveLevel(STUDY_LEVEL));

    // UNIQUE STUDY LEVEL KEY FOR THE STUDY. See PS 3.4 - C.6.2.1.2 Study level
    keys.putUI(Tags.StudyInstanceUID);

    // REQUIRED STUDY LEVEL KEY FOR THE STUDY. See PS 3.4 - C.6.2.1.2 Study level
    keys.putDA(Tags.StudyDate);
    // Not defined: StudyTime
    // Not defined: AccessionNumber
    keys.putPN(Tags.PatientName);
    keys.putLO(Tags.PatientID);
    // Not defined: StudyID

    // OPTIONAL STUDY LEVEL KEY FOR THE STUDY. See PS 3.4 - C.6.2.1.2 Study level
    keys.putUS(Tags.NumberOfStudyRelatedSeries);
    keys.putUS(Tags.NumberOfStudyRelatedInstances);
    // mutch more defined...

    // Add the keys found in the configuration properties
    addQueryKeys(cfg);
  }
  private Collection getObserverContextItems(String personName) {
    Dataset ds = dof.newDataset();
    ds.putCS(Tags.RelationshipType, "HAS OBS CONTEXT");
    ds.putCS(Tags.ValueType, "CODE");
    DcmElement cnSq = ds.putSQ(Tags.ConceptNameCodeSeq);
    Dataset cnDS = cnSq.addNewItem();
    cnDS.putSH(Tags.CodeValue, "121005");
    cnDS.putSH(Tags.CodingSchemeDesignator, "DCM");
    cnDS.putLO(Tags.CodeMeaning, "ObserverType");
    DcmElement ccSq = ds.putSQ(Tags.ConceptCodeSeq);
    Dataset ccDS = ccSq.addNewItem();
    ccDS.putSH(Tags.CodeValue, "121006");
    ccDS.putSH(Tags.CodingSchemeDesignator, "DCM");
    ccDS.putLO(Tags.CodeMeaning, "Person");

    Dataset ds1 = dof.newDataset();
    ds1.putCS(Tags.RelationshipType, "HAS OBS CONTEXT");
    ds1.putCS(Tags.ValueType, "PNAME");
    DcmElement cnSq1 = ds1.putSQ(Tags.ConceptNameCodeSeq);
    Dataset cnDS1 = cnSq1.addNewItem();
    cnDS1.putSH(Tags.CodeValue, "121008");
    cnDS1.putSH(Tags.CodingSchemeDesignator, "DCM");
    cnDS1.putLO(Tags.CodeMeaning, "Person Observer Name");
    ds1.putPN(Tags.PersonName, personName);
    ArrayList col = new ArrayList();
    col.add(ds);
    col.add(ds1);
    return col;
  }
  public void addWorklistItem(
      Long studyPk, String templateFile, String humanPerformerCode, Long scheduleDate)
      throws Exception {
    String uri =
        FileUtils.resolve(new File(templatePath, templateFile + ".xml")).toURI().toString();
    if (log.isDebugEnabled()) log.debug("load template file: " + uri);
    Dataset ds = DatasetUtils.fromXML(new InputSource(uri));

    ContentManager cm = getContentManager();
    // patient
    Dataset patDS = cm.getPatientForStudy(studyPk.longValue());
    if (log.isDebugEnabled()) {
      log.debug("Patient Dataset:");
      log.debug(patDS);
    }

    ds.putAll(patDS.subSet(PAT_ATTR_TAGS));
    //
    Dataset sopInstRef = cm.getSOPInstanceRefMacro(studyPk.longValue(), false);
    String studyIUID = sopInstRef.getString(Tags.StudyInstanceUID);
    ds.putUI(Tags.SOPInstanceUID, UIDGenerator.getInstance().createUID());
    ds.putUI(Tags.StudyInstanceUID, studyIUID);
    DcmElement inSq = ds.putSQ(Tags.InputInformationSeq);
    inSq.addItem(sopInstRef);

    // Scheduled Human Performer Seq
    DcmElement schedHPSq = ds.putSQ(Tags.ScheduledHumanPerformersSeq);
    Dataset item = schedHPSq.addNewItem();
    DcmElement hpCodeSq = item.putSQ(Tags.HumanPerformerCodeSeq);
    Dataset dsCode = (Dataset) this.humanPerformer.get(humanPerformerCode);
    log.info(dsCode);
    if (dsCode != null) {
      hpCodeSq.addItem(dsCode);
      item.putPN(Tags.HumanPerformerName, dsCode.getString(Tags.CodeMeaning));
    }

    // Scheduled Procedure Step Start Date and Time
    ds.putDT(Tags.SPSStartDateAndTime, new Date(scheduleDate.longValue()));

    if (log.isDebugEnabled()) {
      log.debug("GPSPS Dataset:");
      log.debug(ds);
    }

    addWorklistItem(ds);
  }
  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;
  }