/**
   * Inserts a new record. This method always returns a non null result. The ID field holds the
   * Unique ID for the inserted record (when greater than zero) or the error number (when less than
   * zero). If the ID is zero, the record was inserted but the server did not returned the Unique
   * ID.
   */
  public ims.dto.Result insert() {
    ims.dto.Result reLoginResult = Connection.reLogin();
    if (reLoginResult != null)
      return new ims.dto.Result(reLoginResult.getMessage(), "DTO.Client.Go_mdt_hcps.Insert");

    if (DataCollection.count() == 0)
      return new ims.dto.Result("No data to insert", "DTO.Client.Go_mdt_hcps.Insert");

    if (DataCollection.count() > 1)
      return new ims.dto.Result(
          "Multiple object insert not allowed", "DTO.Client.Go_mdt_hcps.Insert");

    ims.dto.Result result = Connection.insert(serviceName, encodeNASMessage());
    if (result != null) return result;

    int recordID = 0;

    try {
      recordID = new Integer(Connection.getValueAt(2)).intValue();
    } catch (NumberFormatException ex) {
      return new ims.dto.Result("Invalid record ID returned", "DTO.Client.Go_mdt_hcps.Insert");
    }

    return new ims.dto.Result(
        recordID,
        "No error. The ID of the new record is in the ID field",
        "DTO.Client.Go_mdt_hcps.Insert");
  }
  /** Executes a specific action. This method always returns a non null result. */
  public ims.dto.Result executeAction(String action) {
    if (action.length() == 0)
      return new ims.dto.Result("Invalid action name", "DTO.Client.Go_mdt_hcps.ExecuteAction");

    ims.dto.Result reLoginResult = Connection.reLogin();
    if (reLoginResult != null)
      return new ims.dto.Result(reLoginResult.getMessage(), "DTO.Client.Go_mdt_hcps.ExecuteAction");

    if (DataCollection.count() == 0)
      return new ims.dto.Result("Data container is empty", "DTO.Client.Go_mdt_hcps.ExecuteAction");

    if (DataCollection.count() > 1)
      return new ims.dto.Result(
          "Multiple objects are not allowed", "DTO.Client.Go_mdt_hcps.ExecuteAction");

    ims.dto.Result result = Connection.executeAction(serviceName, encodeNASMessage(), action);
    if (result != null) return result;

    try {
      return new ims.dto.Result(
          new Integer(Connection.getValueAt(2)).intValue(),
          "No error",
          "DTO.Client.Go_mdt_hcps.ExecuteAction");
    } catch (NumberFormatException ex) {
      return new ims.dto.Result("Invalid server response", "DTO.Client.Go_mdt_hcps.ExecuteAction");
    }
  }
  /**
   * Performs data validation prior to update. If the result returned is not null an error occured.
   */
  public ims.dto.Result getForUpdate() {
    ims.dto.Result reLoginResult = Connection.reLogin();
    if (reLoginResult != null)
      return new ims.dto.Result(reLoginResult.getMessage(), "DTO.Client.Go_mdt_hcps.GetForUpdate");

    if (lastGetFilter == null)
      return new ims.dto.Result(
          "Last get method failed or not called", "DTO.Client.Go_mdt_hcps.GetForUpdate");

    ims.dto.Result result = Connection.getForUpdate(serviceName, encodeNASFilter(lastGetFilter));
    if (result != null) return result;

    if (Connection.countResponseItems(Connection.getValueAt(6)) == 0) return null;

    DataCollection.clear();
    decodeNASMessageWithRepeatingGroups();

    return new ims.dto.Result(
        "The data was changed by another user", "DTO.Client.Go_mdt_hcps.GetForUpdate");
  }
  private int decodeNASMessage() {
    String[] items = Connection.getResponseItems(Connection.getValueAt(6));
    if (items == null) return 0;
    int records = items.length;
    if (records == 0) return 0;

    for (int x = 0; x < records; x++) {
      Go_mdt_hcpsRecord record = new Go_mdt_hcpsRecord();
      String[] valueItems = Connection.splitResponseItem(items[x]);

      record.Go_mdt_hcps_id =
          Connection.decodeFieldValue(Connection.getValueFor(valueItems, "GO_MDT_HCPS_ID"));
      record.Pkey = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "PKEY"));
      record.Rdat = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RDAT"));
      record.Rtim = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RTIM"));
      record.Rhcp = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RHCP"));
      record.Rusr = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RUSR"));
      record.Udat = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UDAT"));
      record.Utim = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UTIM"));
      record.Uhcp = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UHCP"));
      record.Uusr = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UUSR"));
      record.Cdat = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "CDAT"));
      record.Ctim = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "CTIM"));
      record.Modu = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "MODU"));
      record.Ccs_epid = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "CCS_EPID"));
      record.Actind = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "ACTIND"));
      record.Tstp = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "TSTP"));
      record.Session_id =
          Connection.decodeFieldValue(Connection.getValueFor(valueItems, "SESSION_ID"));
      record.Sessiond_id =
          Connection.decodeFieldValue(Connection.getValueFor(valueItems, "SESSIOND_ID"));

      DataCollection.add(record);
    }

    return records;
  }
  private int decodeNASMessageWithRepeatingGroups() {
    String[] items = Connection.getResponseItems(Connection.getValueAt(6));
    if (items == null) return 0;
    int records = items.length;
    if (records == 0) return 0;

    int recCount = 0;
    int rgCount = 0;

    for (int x = 0; x < records; x++) {
      Go_mdt_hcpsRecord record = new Go_mdt_hcpsRecord();
      String[] valueItems = Connection.splitResponseItem(items[x]);

      record.Go_mdt_hcps_id =
          Connection.decodeFieldValue(Connection.getValueFor(valueItems, "GO_MDT_HCPS_ID"));
      record.Pkey = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "PKEY"));
      record.Rdat = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RDAT"));
      record.Rtim = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RTIM"));
      record.Rhcp = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RHCP"));
      record.Rusr = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "RUSR"));
      record.Udat = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UDAT"));
      record.Utim = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UTIM"));
      record.Uhcp = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UHCP"));
      record.Uusr = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "UUSR"));
      record.Cdat = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "CDAT"));
      record.Ctim = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "CTIM"));
      record.Modu = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "MODU"));
      record.Ccs_epid = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "CCS_EPID"));
      record.Actind = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "ACTIND"));
      record.Tstp = Connection.decodeFieldValue(Connection.getValueFor(valueItems, "TSTP"));
      record.Session_id =
          Connection.decodeFieldValue(Connection.getValueFor(valueItems, "SESSION_ID"));
      record.Sessiond_id =
          Connection.decodeFieldValue(Connection.getValueFor(valueItems, "SESSIOND_ID"));

      for (int i = 0; i < valueItems.length; i++)
        if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("seqno"))
          record.SeqnoCollection.add();

      rgCount = record.SeqnoCollection.count();
      if (rgCount > 0) {

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("seqno")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Seqno = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("attattended")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Attattended = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("attdeclined")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Attdeclined = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("attconfirmed")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Attconfirmed = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("methodinformed")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Methodinformed = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("dateinformed")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Dateinformed = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("hcp_txt")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Hcp_txt = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("hcp_id")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Hcp_id = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }

        recCount = 0;
        for (int i = 0; i < valueItems.length; i++) {
          if (Connection.getAttributeName(valueItems[i]).toLowerCase().equals("ct_ext_flag")) {
            Go_mdt_hcpsSeqnoRecord rgRecord = record.SeqnoCollection.get(recCount);
            rgRecord.Ct_ext_flag = Connection.getAttributeValue(valueItems[i]);
            recCount++;
          }

          if (rgCount == recCount) break;
        }
      }

      DataCollection.add(record);
    }

    return records;
  }