/** @see org.openmrs.module.patientflags.api.FlagService#getFlaggedPatients(List<Flag>) */
  public Cohort getFlaggedPatients(List<Flag> flags) {
    Cohort resultCohort = new Cohort();

    // test each Flag
    for (Flag flag : flags) {
      resultCohort = Cohort.union(resultCohort, flag.evalCohort(null));
    }
    return resultCohort;
  }
  /** @see org.openmrs.module.patientflags.api.FlagService#purgePriority(Integer) */
  public void purgePriority(Integer priorityId) {
    // first we need to make sure that this priority is not currently referenced by a flag
    Priority priority = getPriority(priorityId);
    List<Flag> flags = getAllFlags();
    for (Flag flag : flags) {
      if (flag.getPriority().equals(priority)) {
        throw (new APIException(
            "Cannot delete priority, because it's referenced by an existing flag."));
      }
    }

    // remove the flag from the DB table
    dao.purgePriority(priorityId);
  }
 /** @see org.openmrs.module.patientflags.api.FlagService#getFlaggedPatients(Flag) */
 public Cohort getFlaggedPatients(Flag flag) {
   if (flag != null) {
     return flag.evalCohort(null);
   } else {
     return new Cohort();
   }
 }
  /**
   * @see org.openmrs.module.patientflags.api.FlagService#generateFlagsForPatient(Patient, Filter)
   */
  public List<Flag> generateFlagsForPatient(Patient patient, Filter filter) {
    List<Flag> results = new ArrayList<Flag>();

    // we can get rid of this once onStartup is implemented
    if (!isInitialized) refreshCache();

    // test each Flag in the cache against the specific Patient
    for (Flag flag : filter.filter(flagCache)) {
      // trap bad flags so that they don't hang the system
      try {
        if (flag.eval(patient)) results.add(flag);
      } catch (Exception e) {
        log.error("Unable to test flag " + flag.getName() + " on patient #" + patient.getId(), e);
      }
    }
    return results;
  }