/**
   * Encodes a list of queue items into JSON text
   *
   * @param items
   * @return
   */
  public static StringBuffer encode(List items) {
    StringBuffer json = new StringBuffer();
    Iterator itr = items.iterator();
    Calendar current = Calendar.getInstance();
    current.get(Calendar.DATE);
    if (items.size() <= 0) json.append("{}");
    else {
      while (itr.hasNext()) {
        QueueItem it = (QueueItem) itr.next();
        PersonName name = it.patient.getPersonName();
        json.append("{");

        json.append("\"status\":\"").append(it.status).append("\",");
        json.append("\"patientIdentifier\":\"")
            .append(it.patient.getPatientIdentifier())
            .append("\",");
        json.append("\"Procedure\":\"").append(it.getProcedureTitle()).append("\",");
        json.append("\"age\":\"").append(it.patient.getAge(null)).append("\",");
        json.append("\"sex\":\"").append(it.patient.getGender()).append("\",");
        json.append("\"dateCreated\":\"").append(it.dateCreated).append("\",");
        json.append("\"name\":{");
        boolean hasContent = addOptionalElement(json, "prefix", name.getPrefix());
        hasContent |= addOptionalElement(json, "givenName", name.getGivenName());
        hasContent |= addOptionalElement(json, "familyName", name.getFamilyName());
        if (hasContent)
          json.deleteCharAt(json.length() - 1); // delete last comma if at least something was added
        json.append("}");
        json.append(",\"patientId\":\"").append(it.patient.getPatientId()).append("\",");
        json.append("\"encounterId\":\"").append(it.encounter.getEncounterId()).append("\"");
        json.append("},");
      }
      json.deleteCharAt(json.length() - 1);
    }
    return json;
  }
  public QueueItem saveQueueItem(QueueItem queueItem) {
    log.debug("saveQueueItem(). Entering");
    boolean isNew = false;
    Date newDate = queueItem.getEncounter().getEncounterDatetime();
    log.debug("Encounter date: " + newDate);
    Date originalDate = null;

    if (queueItem.getQueueItemId() == null) {
      isNew = true;
      log.debug("Got a  new queue item");
    }

    // Not new we update some of the Encounter info
    if (!isNew) {
      log.info("Updating previously queued encounter!");
      Encounter encounter = queueItem.getEncounter();
      Patient p = encounter.getPatient();
      originalDate = encounter.getEncounterDatetime();
      if (OpenmrsUtil.compare(originalDate, newDate) != 0) {

        // if the obs datetime is the same as the
        // original encounter datetime, fix it
        if (OpenmrsUtil.compare(queueItem.getDateCreated(), originalDate) == 0) {
          encounter.setEncounterDatetime(newDate);
        }
      }

      // if the Person in the encounter doesn't match the Patient in the ,
      // fix it
      if (!encounter.getPatient().getPersonId().equals(p.getPatientId())) {
        encounter.setPatient(p);
      }

      for (Obs obs : encounter.getAllObs(true)) {
        // if the date was changed
        if (OpenmrsUtil.compare(originalDate, newDate) != 0) {

          // if the obs datetime is the same as the
          // original encounter datetime, fix it
          if (OpenmrsUtil.compare(obs.getObsDatetime(), originalDate) == 0) {
            obs.setObsDatetime(newDate);
          }
        }

        // if the Person in the obs doesn't match the Patient in the
        // encounter, fix it
        if (!obs.getPerson().getPersonId().equals(p.getPatientId())) {
          obs.setPerson(p);
        }
      }
    }
    log.debug("Saving queu item.");
    dao.saveQueueItem(queueItem);
    return queueItem;
  }