/** @see java.lang.Object#toString() */
  public String toString() {
    StringBuilder ret = new StringBuilder();
    ret.append("Patients ");
    if (enrolledOnOrAfter != null) {
      ret.append("who enrolled on or after " + enrolledOnOrAfter + " ");
    }
    if (enrolledOnOrBefore != null) {
      ret.append("who enrolled on or before " + enrolledOnOrBefore + " ");
    }
    if (completedOnOrAfter != null) {
      ret.append("who completed on or after " + completedOnOrAfter + " ");
    }
    if (completedOnOrBefore != null) {
      ret.append("who completed on or before " + completedOnOrBefore + " ");
    }

    if (programs != null && programs.size() > 0) {
      ret.append(" in ");
      for (Program p : programs) {
        ret.append(p.getName() + " ");
      }
    }

    if (locationList != null && locationList.size() > 0) {
      ret.append(" at ");
      for (Location l : locationList) {
        ret.append(l.getName() + " ");
      }
    }
    return ret.toString();
  }
 /**
  * Patients who are in the specified program on ${onDate}
  *
  * @param program the program
  * @return
  */
 public CohortDefinition inProgram(Program program) {
   CalculationCohortDefinition cd = new CalculationCohortDefinition(new InProgramCalculation());
   cd.setName("in " + program.getName() + " on date");
   cd.addParameter(new Parameter("onDate", "On Date", Date.class));
   cd.addCalculationParameter("program", program);
   return cd;
 }
 /**
  * Compares two objects for similarity
  *
  * @param obj
  * @return boolean true/false whether or not they are the same objects
  */
 public boolean equals(Object obj) {
   if (obj instanceof Form2ProgramMap) {
     Form2ProgramMap p = (Form2ProgramMap) obj;
     boolean ret = true;
     if (program != null && p.getProgram() != null) ret = ret && program.equals(p.getProgram());
     if (encounterType != null && p.getEncounterType() != null)
       ret = ret && encounterType.equals(p.getEncounterType());
     return ret;
   }
   return false;
 }
  /**
   * Takes a "program_id:list" where program_id is the id of the program that this collection is for
   * (or not present, if it's a new program) and list is a space-separated list of concept ids. This
   * class is a bit of a hack, because I don't know a better way to do this. -DJ The purpose is to
   * retire and un-retire workflows where possible rather than deleting and creating them.
   */
  public void setAsText(String text) throws IllegalArgumentException {
    if (StringUtils.hasText(text)) {
      ConceptService cs = Context.getConceptService();
      ProgramWorkflowService pws = Context.getProgramWorkflowService();
      try {
        int ind = text.indexOf(":");
        String progIdStr = text.substring(0, ind);
        text = text.substring(ind + 1);
        if (program == null)
          // if a program wasn't passed in, try to look it up now
          program = pws.getProgram(Integer.valueOf(progIdStr));
      } catch (Exception ex) {
      }

      String[] conceptIds = text.split(" ");
      Set<ProgramWorkflow> oldSet =
          program == null ? new HashSet<ProgramWorkflow>() : program.getAllWorkflows();
      Set<Integer> newConceptIds = new HashSet<Integer>();

      for (String id : conceptIds) {
        if (id.trim().length() == 0) continue;
        log.debug("trying " + id);
        newConceptIds.add(Integer.valueOf(id.trim()));
      }

      // go through oldSet and see what we need to keep and what we need to unvoid
      Set<Integer> alreadyDone = new HashSet<Integer>();
      for (ProgramWorkflow pw : oldSet) {
        if (!newConceptIds.contains(pw.getConcept().getConceptId())) {
          pw.setRetired(true);
        } else if (pw.isRetired()) { // && newConceptIds.contains(pw...)
          pw.setRetired(false);
        }
        alreadyDone.add(pw.getConcept().getConceptId());
      }

      // now add any new ones
      newConceptIds.removeAll(alreadyDone);
      for (Integer conceptId : newConceptIds) {
        ProgramWorkflow pw = new ProgramWorkflow();
        pw.setProgram(program);
        pw.setConcept(cs.getConcept(conceptId));
        oldSet.add(pw);
      }

      setValue(oldSet);
    } else {
      setValue(null);
    }
  }
  private static String replaceIdsWithUuidsHelper(
      String formXmlData, String attribute, String objectType) {
    // pattern to find the specified attribute and pull out its values; regex matches any characters
    // within quotes after an equals, i.e. ="a2-32" would match a232
    Pattern substitutionPattern =
        Pattern.compile(attribute + "=\"(.*?)\"", Pattern.CASE_INSENSITIVE);
    Matcher matcher = substitutionPattern.matcher(formXmlData);

    // list to keep track of any "repeat" keys we are going to have to substitute out as well
    Set<String> keysToReplace = new HashSet<String>();

    StringBuffer buffer = new StringBuffer();

    while (matcher.find()) {
      // split the group into the various ids
      String[] ids = matcher.group(1).split(",");

      StringBuffer idBuffer = new StringBuffer();
      // now loop through each id
      for (String id : ids) {
        // see if this is a concept id (i.e., is made up of one or more digits), as opposed to a
        // mapping id or a uuid, or a key used in a repeat template)
        if (id.matches("^\\d+$")) {
          // now we need to fetch the appropriate object for this id, and append the uuid to the
          // buffer
          if ("concept".equalsIgnoreCase(objectType)) {
            Concept concept = Context.getConceptService().getConcept(Integer.valueOf(id));
            idBuffer.append(concept.getUuid() + ",");
          } else if ("location".equalsIgnoreCase(objectType)) {
            Location location = Context.getLocationService().getLocation(Integer.valueOf(id));
            idBuffer.append(location.getUuid() + ",");
          } else if ("program".equalsIgnoreCase(objectType)) {
            Program program = Context.getProgramWorkflowService().getProgram(Integer.valueOf(id));
            idBuffer.append(program.getUuid() + ",");
          } else if ("person".equalsIgnoreCase(objectType)) {
            Person person = Context.getPersonService().getPerson(Integer.valueOf(id));
            idBuffer.append(person.getUuid() + ",");
          }
        } else {
          // otherwise, leave the id only
          idBuffer.append(id + ",");

          // also, if this id is a key (i.e., something in curly braces) we need to keep track of it
          // so that we can perform key substitutions
          // pattern matches one or more characters of any type within curly braces
          Matcher keyMatcher = Pattern.compile("\\{(.+)\\}").matcher(id);
          if (keyMatcher.find()) {
            keysToReplace.add(keyMatcher.group(1));
          }
        }
      }

      // trim off the trailing comma
      idBuffer.deleteCharAt(idBuffer.length() - 1);

      // now do the replacement
      // append to the buffer the matched sequence, substituting out group(1) with the updated ids
      matcher.appendReplacement(
          buffer,
          matcher.group().substring(0, matcher.start(1) - matcher.start())
              + idBuffer.toString()
              + "\"");
    }

    // append the rest of htmlform
    matcher.appendTail(buffer);

    formXmlData = buffer.toString();

    // now recursively handle any keys we have discovered during this substitution
    for (String key : keysToReplace) {
      formXmlData = replaceIdsWithUuidsHelper(formXmlData, key);
    }

    return formXmlData;
  }