public String getSqlSelectTagDistinctClassCode() {
   if (S_sqlSelectTagDistinctClassCode == null) {
     S_sqlSelectTagDistinctClassCode =
         "SELECT "
             + "DISTINCT ctag.ClassCode "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..tag AS ctag ";
   }
   return (S_sqlSelectTagDistinctClassCode);
 }
 public String getSqlSelectMemoDataDistinctClassCode() {
   if (S_sqlSelectMemoDataDistinctClassCode == null) {
     S_sqlSelectMemoDataDistinctClassCode =
         "SELECT "
             + "DISTINCT mmod.ClassCode "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..MemoData AS mmod ";
   }
   return (S_sqlSelectMemoDataDistinctClassCode);
 }
 public String getSqlSelectISOCountryDistinctClassCode() {
   if (S_sqlSelectISOCountryDistinctClassCode == null) {
     S_sqlSelectISOCountryDistinctClassCode =
         "SELECT "
             + "DISTINCT isoc.ClassCode "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..iso_cntry AS isoc ";
   }
   return (S_sqlSelectISOCountryDistinctClassCode);
 }
 public String getSqlSelectISOCountryBuff() {
   if (S_sqlSelectISOCountryBuff == null) {
     S_sqlSelectISOCountryBuff =
         "SELECT "
             + "isoc.id, "
             + "isoc.iso_code, "
             + "isoc.country_name, "
             + "isoc.revision "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..iso_cntry AS isoc ";
   }
   return (S_sqlSelectISOCountryBuff);
 }
 public String getSqlSelectTagBuff() {
   if (S_sqlSelectTagBuff == null) {
     S_sqlSelectTagBuff =
         "SELECT "
             + "ctag.tenantid, "
             + "ctag.id, "
             + "ctag.name, "
             + "ctag.descr, "
             + "ctag.revision "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..tag AS ctag ";
   }
   return (S_sqlSelectTagBuff);
 }
 public String getSqlSelectMemoDataBuff() {
   if (S_sqlSelectMemoDataBuff == null) {
     S_sqlSelectMemoDataBuff =
         "SELECT "
             + "mmod.tenantid, "
             + "mmod.memoid, "
             + "mmod.contactid, "
             + "mmod.memo, "
             + "mmod.revision "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..MemoData AS mmod ";
   }
   return (S_sqlSelectMemoDataBuff);
 }
 public String getSqlSelectContactTagBuff() {
   if (S_sqlSelectContactTagBuff == null) {
     S_sqlSelectContactTagBuff =
         "SELECT "
             + "cttg.tenantid, "
             + "cttg.contactid, "
             + "cttg.tagid, "
             + "cttg.tagvalue, "
             + "cttg.revision "
             + "FROM "
             + schema.getLowerDbSchemaName()
             + "..ctc_tag AS cttg ";
   }
   return (S_sqlSelectContactTagBuff);
 }
  public void updateMemoData(CFSecurityAuthorization Authorization, CFCrmMemoDataBuff Buff) {
    final String S_ProcName = "updateMemoData";
    if ("MMOD".equals(Buff.getClassCode())
        && (!schema.isTenantUser(Authorization, Buff.getRequiredTenantId(), "UpdateMemoData"))) {
      throw CFLib.getDefaultExceptionFactory()
          .newRuntimeException(
              getClass(),
              S_ProcName,
              "Permission denied -- User not part of TSecGroup UpdateMemoData");
    }
    try {
      Connection cnx = schema.getCnx();
      long TenantId = Buff.getRequiredTenantId();
      long MemoId = Buff.getRequiredMemoId();
      long ContactId = Buff.getRequiredContactId();
      String Memo = Buff.getRequiredMemo();
      int Revision = Buff.getRequiredRevision();
      CFCrmMemoDataPKey pkey = schema.getFactoryMemoData().newPKey();
      pkey.setRequiredTenantId(Buff.getRequiredTenantId());
      pkey.setRequiredMemoId(Buff.getRequiredMemoId());
      CFCrmMemoDataBuff readBuff = lockBuff(Authorization, pkey);
      if (readBuff == null) {
        throw CFLib.getDefaultExceptionFactory()
            .newStaleCacheDetectedException(
                getClass(),
                S_ProcName,
                "Attempted to update record which could not be locked/found",
                schema.getLowerDbSchemaName() + "..memodata",
                pkey);
      }
      int oldRevision = readBuff.getRequiredRevision();
      if (oldRevision != Revision) {
        throw CFLib.getDefaultExceptionFactory()
            .newCollisionDetectedException(getClass(), S_ProcName, Buff);
      }
      int newRevision = Revision + 1;
      String sql =
          "UPDATE "
              + schema.getLowerDbSchemaName()
              + "..MemoData "
              + "SET "
              + "tenantid = ?"
              + ", "
              + "memoid = ?"
              + ", "
              + "contactid = ?"
              + ", "
              + "memo = ?"
              + ", "
              + "updatedby = ?, "
              + "updatedat = sysdatetime() "
              + ", revision = ? "
              + " WHERE "
              + "tenantid = ? "
              + "AND "
              + "memoid = ? "
              + "AND "
              + "revision = ? ";
      if (stmtUpdateByPKey == null) {
        stmtUpdateByPKey = cnx.prepareStatement(sql);
      }
      int argIdx = 1;

      stmtUpdateByPKey.setLong(argIdx++, TenantId);
      stmtUpdateByPKey.setLong(argIdx++, MemoId);
      stmtUpdateByPKey.setLong(argIdx++, ContactId);
      stmtUpdateByPKey.setString(argIdx++, Memo);
      stmtUpdateByPKey.setString(argIdx++, Authorization.getSecUserId().toString());
      stmtUpdateByPKey.setInt(argIdx++, newRevision);
      stmtUpdateByPKey.setLong(argIdx++, TenantId);
      stmtUpdateByPKey.setLong(argIdx++, MemoId);
      stmtUpdateByPKey.setInt(argIdx++, Revision);
      ;
      int rowsAffected = stmtUpdateByPKey.executeUpdate();
      if (rowsAffected != 1) {
        throw CFLib.getDefaultExceptionFactory()
            .newRuntimeException(
                getClass(),
                S_ProcName,
                "Expected 1 row to be affected by update, not " + rowsAffected);
      }
      Buff.setRequiredRevision(newRevision);
      String sqlAuditUpdated =
          "INSERT INTO "
              + schema.getLowerDbSchemaName()
              + "..MemoData_h( auditclusterid, "
              + " auditsessionid, "
              + " auditstamp"
              + ", "
              + "tenantid"
              + ", "
              + "memoid"
              + ", "
              + "contactid"
              + ", "
              + "memo"
              + ", "
              + " revision, "
              + " auditaction ) "
              + "SELECT ?, ?, sysdatetime()"
              + ", "
              + "mmod.tenantid"
              + ", "
              + "mmod.memoid"
              + ", "
              + "mmod.contactid"
              + ", "
              + "mmod.memo"
              + ", "
              + " mmod.revision, "
              + " 1 "
              + "FROM "
              + schema.getLowerDbSchemaName()
              + "..MemoData AS mmod "
              + " WHERE "
              + "mmod.tenantid = ? "
              + "AND mmod.memoid = ? ";
      if (stmtAuditUpdatedByPKey == null) {
        stmtAuditUpdatedByPKey = cnx.prepareStatement(sqlAuditUpdated);
      }
      argIdx = 1;
      stmtAuditUpdatedByPKey.setLong(argIdx++, Authorization.getSecClusterId());
      stmtAuditUpdatedByPKey.setString(argIdx++, Authorization.getSecSessionId().toString());
      stmtAuditUpdatedByPKey.setLong(argIdx++, TenantId);
      stmtAuditUpdatedByPKey.setLong(argIdx++, MemoId);
      int rowsAudited = stmtAuditUpdatedByPKey.executeUpdate();
      if (rowsAudited != 1) {
        throw CFLib.getDefaultExceptionFactory()
            .newRuntimeException(
                getClass(),
                S_ProcName,
                "Expected 1 row to be affected by audit via insert-selected, not " + rowsAffected);
      }
    } catch (SQLException e) {
      throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    }
  }
  public void createMemoData(CFSecurityAuthorization Authorization, CFCrmMemoDataBuff Buff) {
    final String S_ProcName = "createMemoData";
    if ("MMOD".equals(Buff.getClassCode())
        && (!schema.isTenantUser(Authorization, Buff.getRequiredTenantId(), "CreateMemoData"))) {
      throw CFLib.getDefaultExceptionFactory()
          .newRuntimeException(
              getClass(),
              S_ProcName,
              "Permission denied -- User not part of TSecGroup CreateMemoData");
    }
    if (!schema.isTransactionOpen()) {
      throw CFLib.getDefaultExceptionFactory()
          .newUsageException(getClass(), S_ProcName, "Transaction not open");
    }
    try {
      Connection cnx = schema.getCnx();
      long TenantId = Buff.getRequiredTenantId();
      long MemoId = Buff.getRequiredMemoId();
      long ContactId = Buff.getRequiredContactId();
      String Memo = Buff.getRequiredMemo();
      int Revision = 1;
      String sql =
          "INSERT INTO "
              + schema.getLowerDbSchemaName()
              + "..MemoData( "
              + "forcesynclock, "
              + "createdby, "
              + "createdat, "
              + "updatedby, "
              + "updatedat, "
              + "tenantid, "
              + "memoid, "
              + "contactid, "
              + "memo"
              + ", revision )"
              + "VALUES ( "
              + "0, "
              + " ?, "
              + " getdate(), "
              + " ?, "
              + " getdate(), "
              + "?"
              + ", "
              + "?"
              + ", "
              + "?"
              + ", "
              + "?"
              + ", "
              + "1 )";
      if (stmtCreateByPKey == null) {
        stmtCreateByPKey = cnx.prepareStatement(sql);
      }
      int argIdx = 1;
      stmtCreateByPKey.setString(argIdx++, Authorization.getSecUserId().toString());
      stmtCreateByPKey.setString(argIdx++, Authorization.getSecUserId().toString());
      stmtCreateByPKey.setLong(argIdx++, TenantId);
      stmtCreateByPKey.setLong(argIdx++, MemoId);
      stmtCreateByPKey.setLong(argIdx++, ContactId);
      stmtCreateByPKey.setString(argIdx++, Memo);
      int rowsAffected = stmtCreateByPKey.executeUpdate();
      if (rowsAffected != 1) {
        throw CFLib.getDefaultExceptionFactory()
            .newRuntimeException(
                getClass(),
                S_ProcName,
                "Expected 1 row to be affected by insert, not " + rowsAffected);
      }
      Buff.setRequiredRevision(Revision);
      String sqlAuditCreated =
          "INSERT INTO "
              + schema.getLowerDbSchemaName()
              + "..MemoData_h( auditclusterid, "
              + " auditsessionid, "
              + " auditstamp"
              + ", "
              + "tenantid"
              + ", "
              + "memoid"
              + ", "
              + "contactid"
              + ", "
              + "memo"
              + ", "
              + " revision, "
              + " auditaction ) "
              + "SELECT ?, ?, sysdatetime()"
              + ", "
              + "mmod.tenantid"
              + ", "
              + "mmod.memoid"
              + ", "
              + "mmod.contactid"
              + ", "
              + "mmod.memo"
              + ", "
              + " mmod.revision, "
              + " 0"
              + "FROM "
              + schema.getLowerDbSchemaName()
              + "..MemoData AS mmod "
              + " WHERE "
              + "mmod.tenantid = ? "
              + "AND mmod.memoid = ? ";
      if (stmtAuditCreatedByPKey == null) {
        stmtAuditCreatedByPKey = cnx.prepareStatement(sqlAuditCreated);
      }
      argIdx = 1;
      stmtAuditCreatedByPKey.setLong(argIdx++, Authorization.getSecClusterId());
      stmtAuditCreatedByPKey.setString(argIdx++, Authorization.getSecSessionId().toString());
      stmtAuditCreatedByPKey.setLong(argIdx++, TenantId);
      stmtAuditCreatedByPKey.setLong(argIdx++, MemoId);
      int rowsAudited = stmtAuditCreatedByPKey.executeUpdate();
      if (rowsAudited != 1) {
        throw CFLib.getDefaultExceptionFactory()
            .newRuntimeException(
                getClass(),
                S_ProcName,
                "Expected 1 row to be affected by audit via insert-selected, not " + rowsAffected);
      }

    } catch (SQLException e) {
      throw CFLib.getDefaultExceptionFactory().newDbException(getClass(), S_ProcName, e);
    }
  }