/**
  * Prefixes and suffixes a Java comment to match Java code problems with UI designer fields.
  *
  * @param node
  * @param text
  * @param fieldName name of field declared in xml component file (or other value for a manual
  *     processing)
  * @return
  */
 public static String getValueWithUIFieldKey(
     final INode node, final String text, String fieldName) {
   String value = getValue(node, text);
   String key = CodeGenerationUtils.buildProblemKey(node.getUniqueName(), fieldName);
   return CodeGenerationUtils.buildJavaStartFieldKey(key)
       + value
       + CodeGenerationUtils.buildJavaEndFieldKey(key);
 }
  public String generate(Object argument) {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);

    CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
    INode node = (INode) codeGenArgument.getArgument();

    String cid = node.getUniqueName();

    String dbtable = null;
    String dbschema = ElementParameterParser.getValue(node, "__ELT_SCHEMA_NAME__");
    String uniqueNameConnection = null;
    INode previousNode = null;

    String differenttable = ElementParameterParser.getValue(node, "__DIFFERENT_TABLE_NAME__");
    boolean useDifferentTable =
        "true".equals(ElementParameterParser.getValue(node, "__USE_DIFFERENT_TABLE__"));

    stringBuffer.append(TEXT_2);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_3);

    List<IConnection> connections = (List<IConnection>) node.getIncomingConnections();
    if (connections != null && connections.size() > 0 && connections.get(0) != null) {
      IConnection connection = connections.get(0);
      previousNode = connection.getSource();
      String previousComponentName = previousNode.getUniqueName();
      dbtable = connection.getName();
      uniqueNameConnection = connection.getUniqueName();

      stringBuffer.append(TEXT_4);
      stringBuffer.append(previousComponentName);
      stringBuffer.append(TEXT_5);
      stringBuffer.append(uniqueNameConnection);
      stringBuffer.append(TEXT_6);
    }

    if ((dbschema != null) && (!"\"\"".equals(dbschema.replaceAll(" ", "").trim()))) {

      stringBuffer.append(TEXT_7);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_8);
      stringBuffer.append(dbschema);
      stringBuffer.append(TEXT_9);
      stringBuffer.append(useDifferentTable ? differenttable : "\"" + dbtable + "\"");
      stringBuffer.append(TEXT_10);

    } else {

      stringBuffer.append(TEXT_11);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_12);
      stringBuffer.append(useDifferentTable ? differenttable : "\"" + dbtable + "\"");
      stringBuffer.append(TEXT_13);
    }

    String dbtypeDefinition = ElementParameterParser.getValue(node, "__TYPE__");

    String tableName = ElementParameterParser.getValue(node, "__TABLE__");

    String dataAction = ElementParameterParser.getValue(node, "__DATA_ACTION__");

    String dbhost = null;
    String dbport = null;
    String dbname = null;
    String dbuser = null;
    String dbpwd = null;
    String connectionType = null;
    boolean useExistingConn = false;
    String dbproperties = null;
    if (previousNode != null) {
      dbhost = ElementParameterParser.getValue(previousNode, "__HOST__");
      dbport = ElementParameterParser.getValue(previousNode, "__PORT__");
      dbname = ElementParameterParser.getValue(previousNode, "__DBNAME__");
      dbuser = ElementParameterParser.getValue(previousNode, "__USER__");
      dbpwd = ElementParameterParser.getValue(previousNode, "__PASS__");
      connectionType = ElementParameterParser.getValue(previousNode, "__CONNECTION_TYPE__");
      useExistingConn =
          ("true")
              .equals(ElementParameterParser.getValue(previousNode, "__USE_EXISTING_CONNECTION__"));
      dbproperties = ElementParameterParser.getValue(previousNode, "__PROPERTIES__");
    }

    //        String tableAction = ElementParameterParser.getValue(
    //            node,
    //            "__TABLE_ACTION__"
    //        );

    String whereClause = ElementParameterParser.getValue(node, "__WHERE_CLAUSE__");

    stringBuffer.append(TEXT_14);

    if (useExistingConn) {
      String connection = ElementParameterParser.getValue(previousNode, "__CONNECTION__");
      String conn = "conn_" + connection;

      stringBuffer.append(TEXT_15);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_16);
      stringBuffer.append(conn);
      stringBuffer.append(TEXT_17);

    } else {

      stringBuffer.append(TEXT_18);

      if (connectionType == null || ("ORACLE_SID").equals(connectionType)) {

        stringBuffer.append(TEXT_19);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_20);
        stringBuffer.append(dbhost);
        stringBuffer.append(TEXT_21);
        stringBuffer.append(dbport);
        stringBuffer.append(TEXT_22);
        stringBuffer.append(dbname);
        stringBuffer.append(TEXT_23);

      } else if (("ORACLE_SERVICE_NAME").equals(connectionType)) {

        stringBuffer.append(TEXT_24);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_25);
        stringBuffer.append(dbhost);
        stringBuffer.append(TEXT_26);
        stringBuffer.append(dbport);
        stringBuffer.append(TEXT_27);
        stringBuffer.append(dbname);
        stringBuffer.append(TEXT_28);

      } else if ("ORACLE_RAC".equals(connectionType)) {
        String rac_url = ElementParameterParser.getValue(previousNode, "__RAC_URL__");

        stringBuffer.append(TEXT_29);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_30);
        stringBuffer.append(rac_url);
        stringBuffer.append(TEXT_31);
      }

      stringBuffer.append(TEXT_32);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_33);
      stringBuffer.append(dbuser);
      stringBuffer.append(TEXT_34);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_35);
      stringBuffer.append(dbpwd);
      stringBuffer.append(TEXT_36);

      if (dbproperties == null || ("\"\"").equals(dbproperties) || ("").equals(dbproperties)) {

        stringBuffer.append(TEXT_37);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_38);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_39);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_40);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_41);

      } else {

        stringBuffer.append(TEXT_42);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_43);
        stringBuffer.append(dbproperties);
        stringBuffer.append(TEXT_44);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_45);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_46);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_47);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_48);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_49);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_50);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_51);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_52);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_53);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_54);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_55);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_56);
      }

      stringBuffer.append(TEXT_57);
    }

    stringBuffer.append(TEXT_58);

    List<IMetadataColumn> columnList = null;

    List<IMetadataTable> metadatas = node.getMetadataList();
    if (metadatas != null && metadatas.size() > 0) {
      IMetadataTable metadata = metadatas.get(0);
      if (metadata != null) {
        columnList = metadata.getListColumns();
      }
    }

    stringBuffer.append(TEXT_59);
    if (("INSERT_OR_UPDATE").equals(dataAction) || ("UPDATE_OR_INSERT").equals(dataAction)) {
      stringBuffer.append(TEXT_60);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_61);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_62);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_63);
    } else {
      stringBuffer.append(TEXT_64);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_65);
    }

    if (columnList != null && columnList.size() > 0) {

      class Column {

        IMetadataColumn column;

        String name;

        String sqlStmt;

        String value;

        String validateName;

        boolean addCol;

        List<Column> replacement = new ArrayList<Column>();

        public Column(String colName, String sqlStmt, boolean addCol) {
          this.column = null;
          this.name = colName;
          this.sqlStmt = sqlStmt;
          this.value = "?";
          this.addCol = addCol;
          this.validateName = MetadataToolHelper.validateValue(colName);
        }

        public Column(IMetadataColumn column) {
          this.column = column;
          this.name = column.getLabel();
          this.sqlStmt = "=?";
          this.value = "?";
          this.addCol = false;
          this.validateName = MetadataToolHelper.validateValue(column.getLabel());
        }

        public boolean isReplaced() {
          return replacement.size() > 0;
        }

        public void replace(Column column) {
          this.replacement.add(column);
        }

        public List<Column> getReplacement() {
          return this.replacement;
        }

        public void setColumn(IMetadataColumn column) {
          this.column = column;
        }

        public IMetadataColumn getColumn() {
          return this.column;
        }

        public void setName(String name) {
          this.name = name;
        }

        public String getName() {
          return this.name;
        }

        public void setIsAddCol(boolean isadd) {
          this.addCol = isadd;
        }

        public boolean isAddCol() {
          return this.addCol;
        }

        public void setSqlStmt(String sql) {
          this.sqlStmt = sql;
        }

        public String getSqlStmt() {
          return this.sqlStmt;
        }

        public void setValue(String value) {
          this.value = value;
        }

        public String getValue() {
          return this.value;
        }

        public String getValidateName() {
          return this.validateName;
        }
      }

      StringBuilder insertColName = new StringBuilder();

      StringBuilder insertValueStmt = new StringBuilder();

      StringBuilder updateSetStmt = new StringBuilder();

      StringBuilder updateWhereStmt = new StringBuilder();

      StringBuilder mergeCondition = new StringBuilder();

      List<Column> stmtStructure = new LinkedList<Column>();

      for (IMetadataColumn column : columnList) {

        stmtStructure.add(new Column(column));
      }

      int counterOuter = 0;
      boolean firstKey = true;
      boolean firstNoneKey = true;
      boolean isfirstKey = true;
      for (Column colStmt : stmtStructure) {
        String colName = colStmt.getName();
        if (!colName.equals(colStmt.getValidateName())) {
          colStmt.setName("\\\"" + colName + "\\\"");
        }
        String suffix = ",";

        if (colStmt.getColumn().isKey()) {
          if (isfirstKey) {
            isfirstKey = false;
          } else {
            mergeCondition.append(" AND ");
          }
          mergeCondition.append(
              "target."
                  + colStmt.getColumn().getLabel()
                  + "=source."
                  + colStmt.getColumn().getLabel());
        }
        if (colStmt.isReplaced()) {
          List<Column> replacedColumns = colStmt.getReplacement();
          int counterReplace = 0;
          if (counterOuter == (stmtStructure.size() - 1)
              && counterReplace == (replacedColumns.size() - 1)) {
            suffix = "";
          }
          for (Column replacement : replacedColumns) {
            insertColName.append(replacement.getName() + suffix);
            insertValueStmt.append(replacement.getSqlStmt() + suffix);
            if (!colStmt.getColumn().isKey()) {
              if (!firstNoneKey) {
                updateSetStmt.append(",");
              } else {
                firstNoneKey = false;
              }
              updateSetStmt.append(replacement.getName());
              updateSetStmt.append(replacement.getSqlStmt());
            } else {
              if (!firstKey) {
                updateWhereStmt.append(" AND ");
              } else {
                firstKey = false;
              }
              updateWhereStmt.append(replacement.getName());
              updateWhereStmt.append(replacement.getSqlStmt());
            }
            counterReplace++;
          }
        } else {
          if (counterOuter == (stmtStructure.size() - 1)) {
            suffix = "";
          }
          if (colStmt.isAddCol()) {
            insertColName.append(colStmt.getName() + suffix);
            insertValueStmt.append(colStmt.getSqlStmt() + suffix);
            updateSetStmt.append(colStmt.getName());
            updateSetStmt.append(colStmt.getSqlStmt() + suffix);
          } else {
            insertColName.append(colStmt.getName() + suffix);
            insertValueStmt.append(colStmt.getValue() + suffix);
            if (!colStmt.getColumn().isKey()) {
              if (!firstNoneKey) {
                updateSetStmt.append(",");
              } else {
                firstNoneKey = false;
              }
              updateSetStmt.append(colStmt.getName());
              updateSetStmt.append(colStmt.getSqlStmt());
            } else {
              if (!firstKey) {
                updateWhereStmt.append(" AND ");
              } else {
                firstKey = false;
              }
              updateWhereStmt.append(colStmt.getName());
              updateWhereStmt.append(colStmt.getSqlStmt());
            }
          }
        }
        counterOuter++;
      }

      ///// hint options/////
      boolean useHintOptions =
          ("true").equals(ElementParameterParser.getValue(node, "__USE_HINT_OPTIONS__"));
      Map<String, String> hintsValues = null;
      if (useHintOptions) {
        List<Map<String, String>> hintOptions =
            (List<Map<String, String>>)
                ElementParameterParser.getObjectValue(node, "__HINT_OPTIONS__");
        hintsValues = new HashMap<String, String>();
        String hintValue = null;
        boolean firstInsert = true;
        boolean firstUpdate = true;
        boolean firstDelete = true;
        boolean firstTableName = true;
        for (java.util.Map<String, String> option : hintOptions) {
          // get
          if (option.get("HINT").matches("\"/\\*NORMALIZED_HINT\\*/\"")) {
            String id = cid.replace(node.getComponent().getName() + "_", "");
            hintValue =
                "\"/*\"+"
                    + dbtable
                    + "+\".\" + "
                    + "\""
                    + option.get("SQL_STMT")
                    + ".\""
                    + "+"
                    + id
                    + "+\"*/\" ";
          } else if (option.get("HINT").matches("\"/\\*+.*\\*/\"")) {
            hintValue = option.get("HINT");
          }
          // set
          if ("INSERT".equalsIgnoreCase(option.get("SQL_STMT"))) {
            if (firstInsert) {
              hintsValues.put("INSERT", hintValue);
              firstInsert = false;
            } else {
              hintsValues.put("INSERT", hintsValues.get("INSERT") + "+" + hintValue);
            }
          } else if ("UPDATE".equalsIgnoreCase(option.get("SQL_STMT"))) {
            if (firstUpdate) {
              hintsValues.put("UPDATE", hintValue);
              firstUpdate = false;
            } else {
              hintsValues.put("UPDATE", hintsValues.get("UPDATE") + "+" + hintValue);
            }
          } else if ("DELETE".equalsIgnoreCase(option.get("SQL_STMT"))) {
            if (firstDelete) {
              hintsValues.put("DELETE", hintValue);
              firstDelete = false;
            } else {
              hintsValues.put("DELETE", hintsValues.get("DELETE") + "+" + hintValue);
            }
          } else if ("TABLE_NAME".equalsIgnoreCase(option.get("SQL_STMT"))) {
            if (firstTableName) {
              hintsValues.put("TABLE_NAME", hintValue);
              firstTableName = false;
            } else {
              hintsValues.put("TABLE_NAME", hintsValues.get("TABLE_NAME") + "+" + hintValue);
            }
          }
        }
      }
      //// hint options end ////

      if (("INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_66);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_67);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_68);

        if (useHintOptions && hintsValues.get("INSERT") != null) {

          stringBuffer.append(TEXT_69);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_70);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_71);
          stringBuffer.append(hintsValues.get("INSERT"));
          stringBuffer.append(TEXT_72);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_73);
        }

        stringBuffer.append(TEXT_74);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_75);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_76);

      } else if (("UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_77);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_78);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_79);

        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {

          stringBuffer.append(TEXT_80);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
        }

        stringBuffer.append(TEXT_81);
        if (useHintOptions && hintsValues.get("UPDATE") != null) {

          stringBuffer.append(TEXT_82);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_83);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_84);
          stringBuffer.append(hintsValues.get("UPDATE"));
          stringBuffer.append(TEXT_85);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_86);
        }

        stringBuffer.append(TEXT_87);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_88);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_89);

      } else if (("INSERT_OR_UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_90);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_91);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_92);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_93);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_94);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_95);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_96);
        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {
          stringBuffer.append(TEXT_97);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
        }
        stringBuffer.append(TEXT_98);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_99);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_100);

      } else if (("UPDATE_OR_INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_101);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_102);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_103);
        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {
          stringBuffer.append(TEXT_104);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
        }
        stringBuffer.append(TEXT_105);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_106);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_107);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_108);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_109);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_110);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_111);

      } else if (("DELETE").equals(dataAction)) {

        stringBuffer.append(TEXT_112);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_113);
        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {
          stringBuffer.append(TEXT_114);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
        }
        stringBuffer.append(TEXT_115);

        if (useHintOptions && hintsValues.get("DELETE") != null) {

          stringBuffer.append(TEXT_116);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_117);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_118);
          stringBuffer.append(hintsValues.get("DELETE"));
          stringBuffer.append(TEXT_119);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_120);
        }
        stringBuffer.append(TEXT_121);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_122);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_123);

      } else if ("MERGE".equals(dataAction)) {
        // get parameter
        boolean useMergeUpdate =
            "true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__USE_MERGE_UPDATE__"));
        boolean useMergeInsert =
            "true".equalsIgnoreCase(ElementParameterParser.getValue(node, "__USE_MERGE_INSERT__"));

        boolean useUpdateWhere =
            "true"
                .equalsIgnoreCase(
                    ElementParameterParser.getValue(node, "__USE_MERGE_UPDATE_WHERE__"));
        boolean useUpdateDeleteWhere =
            "true"
                .equalsIgnoreCase(
                    ElementParameterParser.getValue(node, "__USE_MERGE_UPDATE_DELETE_WHERE__"));
        boolean useInsertWhere =
            "true"
                .equalsIgnoreCase(
                    ElementParameterParser.getValue(node, "__USE_MERGE_INSERT_WHERE__"));

        List<Map<String, String>> updateColumns =
            (List<Map<String, String>>)
                ElementParameterParser.getObjectValue(node, "__SCHEMA_UPDATE__");
        List<Map<String, String>> insertColumns =
            (List<Map<String, String>>)
                ElementParameterParser.getObjectValue(node, "__SCHEMA_INSERT__");

        StringBuilder mergeUpdate = new StringBuilder();
        StringBuilder mergeInsertName = new StringBuilder();
        StringBuilder mergeInsertValue = new StringBuilder();

        stringBuffer.append(TEXT_124);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_125);
        stringBuffer.append(mergeCondition);
        stringBuffer.append(TEXT_126);

        if (!useMergeUpdate) useUpdateWhere = false;
        if (useMergeUpdate) {
          boolean isfirstUpdateSet = true;
          String keyColumnName = null;
          for (Map<String, String> keyColumn : updateColumns) {
            if (keyColumn.get("UPDATE").equals("true")) {
              if (isfirstUpdateSet) {
                isfirstUpdateSet = false;
              } else {
                mergeUpdate.append(",");
              }
              keyColumnName = keyColumn.get("SCHEMA_COLUMN");
              if (!(keyColumnName != null
                  && keyColumnName.equals(MetadataToolHelper.validateValue(keyColumnName)))) {
                keyColumnName = "\\\"" + keyColumnName + "\\\"";
              }
              mergeUpdate.append("target." + keyColumnName + "=source." + keyColumnName);
            }
          }

          stringBuffer.append(TEXT_127);
          stringBuffer.append(mergeUpdate);
          stringBuffer.append(TEXT_128);
          if (useUpdateWhere) {
            String useUpdateWhereString =
                ElementParameterParser.getValue(node, "__MERGE_UPDATE_WHERE_STRING__");
            if (useUpdateWhereString == null || useUpdateWhereString.length() == 0) {

              stringBuffer.append(TEXT_129);

            } else {
              stringBuffer.append(TEXT_130);
              stringBuffer.append(useUpdateWhereString);
              stringBuffer.append(TEXT_131);
            }
          }
          stringBuffer.append(TEXT_132);
          if (useUpdateDeleteWhere) {
            String useUpdateDeleteWhereString =
                ElementParameterParser.getValue(node, "__MERGE_UPDATE_DELETE_WHERE_STRING__");
            if (useUpdateDeleteWhereString == null || useUpdateDeleteWhereString.length() == 0) {

              stringBuffer.append(TEXT_133);

            } else {
              stringBuffer.append(TEXT_134);
              stringBuffer.append(useUpdateDeleteWhereString);
              stringBuffer.append(TEXT_135);
            }
          }
          stringBuffer.append(TEXT_136);
        }
        stringBuffer.append(TEXT_137);

        if (!useMergeInsert) useInsertWhere = false;
        if (useMergeInsert) {
          boolean isfirstInsertWhere = true;
          String keyColumnName = null;
          for (Map<String, String> keyColumn : insertColumns) {
            if (isfirstInsertWhere) {
              isfirstInsertWhere = false;
            } else {
              mergeInsertName.append(",");
              mergeInsertValue.append(",");
            }
            if (keyColumn.get("INSERT").equals("true")) {
              keyColumnName = keyColumn.get("SCHEMA_COLUMN");
              if (!(keyColumnName != null
                  && keyColumnName.equals(MetadataToolHelper.validateValue(keyColumnName)))) {
                keyColumnName = "\\\"" + keyColumnName + "\\\"";
              }
              mergeInsertName.append(keyColumnName);
              mergeInsertValue.append("source." + keyColumnName);
            }
          }

          stringBuffer.append(TEXT_138);
          stringBuffer.append(mergeInsertName);
          stringBuffer.append(TEXT_139);
          stringBuffer.append(mergeInsertValue);
          stringBuffer.append(TEXT_140);
        }
        if (useInsertWhere) {
          String useInsertWhereString =
              ElementParameterParser.getValue(node, "__MERGE_INSERT_WHERE_STRING__");
          if (useInsertWhereString == null || useInsertWhereString.length() == 0) {

            stringBuffer.append(TEXT_141);

          } else {
            stringBuffer.append(TEXT_142);
            stringBuffer.append(useInsertWhereString);
            stringBuffer.append(TEXT_143);
          }
        }

        stringBuffer.append(TEXT_144);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_145);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_146);
      }

      stringBuffer.append(TEXT_147);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_148);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_149);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_150);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_151);
    }

    // MAIN

    String incomingConnName = null;
    columnList = null;

    List<? extends IConnection> conns = node.getIncomingConnections();
    if (conns != null && conns.size() > 0) {
      IConnection conn = conns.get(0);
      incomingConnName = conn.getName();
    }

    metadatas = node.getMetadataList();

    if (metadatas != null && metadatas.size() > 0) {
      IMetadataTable metadata = metadatas.get(0);
      if (metadata != null) {
        columnList = metadata.getListColumns();
      }
    }

    if (incomingConnName != null && columnList != null) {
      if (("INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_152);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_153);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_154);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_155);

      } else if (("UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_156);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_157);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_158);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_159);

      } else if (("INSERT_OR_UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_160);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_161);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_162);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_163);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_164);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_165);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_166);

      } else if (("UPDATE_OR_INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_167);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_168);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_169);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_170);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_171);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_172);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_173);

      } else if (("DELETE").equals(dataAction)) {

        stringBuffer.append(TEXT_174);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_175);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_176);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_177);

      } else if (("MERGE").equals(dataAction)) {

        stringBuffer.append(TEXT_178);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_179);
      }
    }

    // END

    if (("INSERT_OR_UPDATE").equals(dataAction) || ("UPDATE_OR_INSERT").equals(dataAction)) {

      stringBuffer.append(TEXT_180);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_181);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_182);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_183);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_184);

    } else {

      stringBuffer.append(TEXT_185);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_186);
    }

    stringBuffer.append(TEXT_187);

    if (!useExistingConn) {

      stringBuffer.append(TEXT_188);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_189);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_190);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_191);
    }

    stringBuffer.append(TEXT_192);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_193);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_194);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_195);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_196);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_197);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_198);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_199);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_200);
    stringBuffer.append(TEXT_201);
    return stringBuffer.toString();
  }
  public String generate(Object argument) {
    final StringBuffer stringBuffer = new StringBuffer();
    stringBuffer.append(TEXT_1);

    CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
    INode node = (INode) codeGenArgument.getArgument();

    String cid = node.getUniqueName();

    String dbtable = null;
    String dbschema = ElementParameterParser.getValue(node, "__ELT_SCHEMA_NAME__");
    String uniqueNameConnection = null;
    INode previousNode = null;

    String differenttable = ElementParameterParser.getValue(node, "__DIFFERENT_TABLE_NAME__");
    boolean useDifferentTable =
        "true".equals(ElementParameterParser.getValue(node, "__USE_DIFFERENT_TABLE__"));

    stringBuffer.append(TEXT_2);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_3);

    List<IConnection> connections = (List<IConnection>) node.getIncomingConnections();
    if (connections != null && connections.size() > 0 && connections.get(0) != null) {
      IConnection connection = connections.get(0);
      previousNode = connection.getSource();
      String previousComponentName = previousNode.getUniqueName();
      dbtable = connection.getName();
      uniqueNameConnection = connection.getUniqueName();

      stringBuffer.append(TEXT_4);
      stringBuffer.append(previousComponentName);
      stringBuffer.append(TEXT_5);
      stringBuffer.append(uniqueNameConnection);
      stringBuffer.append(TEXT_6);
      stringBuffer.append(previousComponentName);
      stringBuffer.append(TEXT_7);
      stringBuffer.append(uniqueNameConnection);
      stringBuffer.append(TEXT_8);
    }

    stringBuffer.append(TEXT_9);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_10);
    stringBuffer.append(dbschema);
    stringBuffer.append(TEXT_11);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_12);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_13);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_14);
    stringBuffer.append(dbschema);
    stringBuffer.append(TEXT_15);
    stringBuffer.append(useDifferentTable ? differenttable : "\"" + dbtable + "\"");
    stringBuffer.append(TEXT_16);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_17);
    stringBuffer.append(useDifferentTable ? differenttable : "\"" + dbtable + "\"");
    stringBuffer.append(TEXT_18);

    String dataAction = ElementParameterParser.getValue(node, "__DATA_ACTION__");

    String dbhost = null;
    String dbport = null;
    String dbname = null;
    String dbuser = null;
    String dbpwd = null;
    boolean useExistingConn = false;
    if (previousNode != null) {
      dbhost = ElementParameterParser.getValue(previousNode, "__HOST__");
      dbport = ElementParameterParser.getValue(previousNode, "__PORT__");
      dbname = ElementParameterParser.getValue(previousNode, "__DBNAME__");
      dbuser = ElementParameterParser.getValue(previousNode, "__USER__");
      dbpwd = ElementParameterParser.getValue(previousNode, "__PASS__");
      useExistingConn =
          ("true")
              .equals(ElementParameterParser.getValue(previousNode, "__USE_EXISTING_CONNECTION__"));
    }
    String whereClause = ElementParameterParser.getValue(node, "__WHERE_CLAUSE__");

    stringBuffer.append(TEXT_19);

    if (useExistingConn) {
      String connection = ElementParameterParser.getValue(previousNode, "__CONNECTION__");
      String conn = "conn_" + connection;

      stringBuffer.append(TEXT_20);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_21);
      stringBuffer.append(conn);
      stringBuffer.append(TEXT_22);

    } else {

      stringBuffer.append(TEXT_23);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_24);
      stringBuffer.append(dbhost);
      stringBuffer.append(TEXT_25);
      stringBuffer.append(dbport);
      stringBuffer.append(TEXT_26);
      stringBuffer.append(dbname);
      stringBuffer.append(TEXT_27);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_28);
      stringBuffer.append(dbuser);
      stringBuffer.append(TEXT_29);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_30);
      stringBuffer.append(dbpwd);
      stringBuffer.append(TEXT_31);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_32);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_33);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_34);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_35);
    }
    List<IMetadataColumn> columnList = null;
    List<IMetadataTable> metadatas = node.getMetadataList();
    if (metadatas != null && metadatas.size() > 0) {
      IMetadataTable metadata = metadatas.get(0);
      if (metadata != null) {
        columnList = metadata.getListColumns();
      }
    }

    stringBuffer.append(TEXT_36);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_37);

    if (columnList != null && columnList.size() > 0) {

      class Column {

        IMetadataColumn column;

        String name;

        String sqlStmt;

        String value;

        boolean addCol;

        List<Column> replacement = new ArrayList<Column>();

        public Column(IMetadataColumn column) {
          this.column = column;
          String columname = column.getOriginalDbColumnName();
          if (columname != null && columname.trim().length() > 0) {
            this.name = columname;
          } else {
            this.name = column.getLabel();
          }
          this.sqlStmt = "=?";
          this.value = "?";
          this.addCol = false;
        }

        public boolean isReplaced() {
          return replacement.size() > 0;
        }

        public List<Column> getReplacement() {
          return this.replacement;
        }

        public IMetadataColumn getColumn() {
          return this.column;
        }

        public void setName(String name) {
          this.name = name;
        }

        public String getName() {
          return this.name;
        }

        public boolean isAddCol() {
          return this.addCol;
        }

        public String getSqlStmt() {
          return this.sqlStmt;
        }

        public String getValue() {
          return this.value;
        }
      }

      StringBuilder insertColName = new StringBuilder();

      StringBuilder insertValueStmt = new StringBuilder();

      StringBuilder updateSetStmt = new StringBuilder();

      StringBuilder updateWhereStmt = new StringBuilder();

      List<Column> stmtStructure = new LinkedList<Column>();

      for (IMetadataColumn column : columnList) {

        stmtStructure.add(new Column(column));
      }

      int counterOuter = 0;
      boolean firstKey = true;
      boolean firstNoneKey = true;
      boolean isfirstKey = true;
      for (Column colStmt : stmtStructure) {
        String suffix = ",";
        String colName = "\\\"" + colStmt.getName() + "\\\"";
        colStmt.setName(colName);
        if (colStmt.getColumn().isKey()) {
          if (isfirstKey) {
            isfirstKey = false;
          }
        }
        if (colStmt.isReplaced()) {
          List<Column> replacedColumns = colStmt.getReplacement();
          int counterReplace = 0;
          if (counterOuter == (stmtStructure.size() - 1)
              && counterReplace == (replacedColumns.size() - 1)) {
            suffix = "";
          }
          for (Column replacement : replacedColumns) {
            insertColName.append(replacement.getName() + suffix);
            insertValueStmt.append(replacement.getSqlStmt() + suffix);
            if (!colStmt.getColumn().isKey()) {
              if (!firstNoneKey) {
                updateSetStmt.append(",");
              } else {
                firstNoneKey = false;
              }
              updateSetStmt.append(replacement.getName());
              updateSetStmt.append(replacement.getSqlStmt());
            } else {
              if (!firstKey) {
                updateWhereStmt.append(" AND ");
              } else {
                firstKey = false;
              }
              updateWhereStmt.append(replacement.getName());
              updateWhereStmt.append(replacement.getSqlStmt());
            }
            counterReplace++;
          }
        } else {
          if (counterOuter == (stmtStructure.size() - 1)) {
            suffix = "";
          }
          if (colStmt.isAddCol()) {
            insertColName.append(colStmt.getName() + suffix);
            insertValueStmt.append(colStmt.getSqlStmt() + suffix);
            updateSetStmt.append(colStmt.getName());
            updateSetStmt.append(colStmt.getSqlStmt() + suffix);
          } else {
            insertColName.append(colStmt.getName() + suffix);
            insertValueStmt.append(colStmt.getValue() + suffix);
            if (!colStmt.getColumn().isKey()) {
              updateSetStmt.append(
                  colStmt.getName()
                      + "=\"+selectQueryColumnsName.split(\",\")[ "
                      + counterOuter
                      + "]+\""
                      + suffix);
            }
          }
        }
        counterOuter++;
      }

      if (("INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_38);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_39);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_40);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_41);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_42);

      } else if (("UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_43);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_44);
        stringBuffer.append(updateSetStmt.toString());
        stringBuffer.append(TEXT_45);

        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {

          stringBuffer.append(TEXT_46);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
          stringBuffer.append(TEXT_47);
        }

        stringBuffer.append(TEXT_48);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_49);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_50);

      } else if (("DELETE").equals(dataAction)) {

        stringBuffer.append(TEXT_51);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_52);

        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {

          stringBuffer.append(TEXT_53);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
          stringBuffer.append(TEXT_54);
        }

        stringBuffer.append(TEXT_55);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_56);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_57);
      }

      stringBuffer.append(TEXT_58);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_59);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_60);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_61);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_62);
    }
    if (dbtable != null && columnList != null) {
      if (("INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_63);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_64);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_65);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_66);

      } else if (("UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_67);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_68);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_69);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_70);

      } else if (("DELETE").equals(dataAction)) {

        stringBuffer.append(TEXT_71);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_72);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_73);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_74);
      }
    }

    stringBuffer.append(TEXT_75);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_76);

    if (!useExistingConn) {

      stringBuffer.append(TEXT_77);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_78);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_79);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_80);
    }

    stringBuffer.append(TEXT_81);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_82);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_83);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_84);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_85);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_86);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_87);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_88);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_89);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_90);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_91);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_92);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_93);
    stringBuffer.append(TEXT_94);
    return stringBuffer.toString();
  }
  public String generate(Object argument) {
    final StringBuffer stringBuffer = new StringBuffer();

    // this util class use by set log4j debug paramters
    class DefaultLog4jCodeGenerateUtil {

      INode node = null;
      String cid = null;
      String dbproperties = null;
      String dbhost = null;
      String dbport = null;
      String dbname = null;
      String dbuser = null;
      String dbpwd = null;
      String connectionType = null;
      String dbVersion = null;
      String dbSchema = null;
      String useCursor = null;
      String trimAllColumn = null;
      String noNullValues = null;
      String dbquery = null;
      String table = null;
      boolean isLog4jEnabled = false;

      public void beforeComponentProcess(INode node) {
        this.node = node;
        init();
      }

      private void init() {
        cid = node.getUniqueName();
        dbproperties = ElementParameterParser.getValue(node, "__PROPERTIES__");
        dbhost = ElementParameterParser.getValue(node, "__HOST__");
        dbport = ElementParameterParser.getValue(node, "__PORT__");
        dbname = ElementParameterParser.getValue(node, "__DBNAME__");
        dbuser = ElementParameterParser.getValue(node, "__USER__");
        dbpwd = ElementParameterParser.getValue(node, "__PASS__");
        connectionType = ElementParameterParser.getValue(node, "__CONNECTION_TYPE__");
        dbVersion = ElementParameterParser.getValue(node, "__DB_VERSION__");
        dbSchema = ElementParameterParser.getValue(node, "__SCHEMA_DB__");
        useCursor = ElementParameterParser.getValue(node, "__USE_CURSOR__");
        trimAllColumn = ElementParameterParser.getValue(node, "__TRIM_ALL_COLUMN__");
        noNullValues = ElementParameterParser.getValue(node, "__NO_NULL_VALUES__");
        dbquery = ElementParameterParser.getValue(node, "__QUERY__");
        dbquery = dbquery.replaceAll("\n", " ");
        dbquery = dbquery.replaceAll("\r", " ");
        table = ElementParameterParser.getValue(node, "__TABLE__");
        isLog4jEnabled =
            ("true")
                .equals(ElementParameterParser.getValue(node.getProcess(), "__LOG4J_ACTIVATE__"));
      }

      public void infoComponentStart(INode node) {
        beforeComponentProcess(node);
        if (isLog4jEnabled) {

          stringBuffer.append(TEXT_1);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_2);
        }
      }

      public void initDbDebugRowBuffer() {
        if (isLog4jEnabled) {

          stringBuffer.append(TEXT_3);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_4);
        }
      }

      public void debugDriverClassName(String driverClass) {
        if (isLog4jEnabled) {

          stringBuffer.append(TEXT_5);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_6);
          stringBuffer.append(driverClass);
          stringBuffer.append(TEXT_7);
        }
      }

      public void debugConnectionParams(INode node, String driverClass) {
        beforeComponentProcess(node);
        generateConnectionParams();
        debugDriverClassName(driverClass);
        if (isLog4jEnabled) {

          stringBuffer.append(TEXT_8);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_9);
        }
      }

      public void dbConnectionParamsForELT(INode previousNode) {
        cid = previousNode.getUniqueName();
        dbhost = ElementParameterParser.getValue(previousNode, "__HOST__");
        dbname = ElementParameterParser.getValue(previousNode, "__DBNAME__");
        dbuser = ElementParameterParser.getValue(previousNode, "__USER__");
        dbpwd = ElementParameterParser.getValue(previousNode, "__PASS__");
      }

      public void additionalParameter(INode node) {
        String needtoConvet2Upper = ElementParameterParser.getValue(node, "__Convert_to_UPPer__");
      }

      public void generateAdditionalParameters(INode node) {
        additionalParameter(node);
      }

      public void generateConnectionParams() {
        if (isLog4jEnabled) {

          stringBuffer.append(TEXT_10);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_11);
          if (dbproperties != null
              && !("\"\"").equals(dbproperties)
              && !("").equals(dbproperties)) {
            stringBuffer.append(TEXT_12);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_13);
            stringBuffer.append(dbproperties);
            stringBuffer.append(TEXT_14);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_15);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_16);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_17);
          }
          stringBuffer.append(TEXT_18);
          if (dbhost != null && !("\"\"").equals(dbhost) && !("").equals(dbhost)) {
            stringBuffer.append(TEXT_19);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_20);
            stringBuffer.append(dbhost);
            stringBuffer.append(TEXT_21);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_22);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_23);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_24);
          }
          stringBuffer.append(TEXT_25);
          if (dbport != null && !("\"\"").equals(dbport) && !("").equals(dbport)) {
            stringBuffer.append(TEXT_26);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_27);
            stringBuffer.append(dbport.replace("'", "\""));
            stringBuffer.append(TEXT_28);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_29);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_30);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_31);
          }
          stringBuffer.append(TEXT_32);
          if (dbname != null && !("\"\"").equals(dbname) && !("").equals(dbname)) {
            stringBuffer.append(TEXT_33);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_34);
            stringBuffer.append(dbname);
            stringBuffer.append(TEXT_35);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_36);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_37);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_38);
          }
          stringBuffer.append(TEXT_39);
          if (dbuser != null && !("\"\"").equals(dbuser) && !("").equals(dbuser)) {
            stringBuffer.append(TEXT_40);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_41);
            stringBuffer.append(dbuser);
            stringBuffer.append(TEXT_42);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_43);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_44);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_45);
          }
          stringBuffer.append(TEXT_46);
          if (connectionType != null
              && !("\"\"").equals(connectionType)
              && !("").equals(connectionType)) {
            stringBuffer.append(TEXT_47);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_48);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_49);
            stringBuffer.append(connectionType);
            stringBuffer.append(TEXT_50);
          }
          stringBuffer.append(TEXT_51);
          if (dbVersion != null && !("\"\"").equals(dbVersion) && !("").equals(dbVersion)) {
            stringBuffer.append(TEXT_52);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_53);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_54);
            stringBuffer.append(dbVersion);
            stringBuffer.append(TEXT_55);
          }
          stringBuffer.append(TEXT_56);
          if (dbSchema != null && !("\"\"").equals(dbSchema) && !("").equals(dbSchema)) {
            stringBuffer.append(TEXT_57);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_58);
            stringBuffer.append(dbSchema);
            stringBuffer.append(TEXT_59);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_60);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_61);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_62);
          }
          stringBuffer.append(TEXT_63);
          if (useCursor != null && !("\"\"").equals(useCursor) && !("").equals(useCursor)) {
            stringBuffer.append(TEXT_64);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_65);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_66);
            stringBuffer.append(useCursor);
            stringBuffer.append(TEXT_67);
          }
          stringBuffer.append(TEXT_68);
          if (trimAllColumn != null
              && !("\"\"").equals(trimAllColumn)
              && !("").equals(trimAllColumn)) {
            stringBuffer.append(TEXT_69);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_70);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_71);
            stringBuffer.append(trimAllColumn);
            stringBuffer.append(TEXT_72);
          }
          stringBuffer.append(TEXT_73);
          if (noNullValues != null
              && !("\"\"").equals(noNullValues)
              && !("").equals(noNullValues)) {
            stringBuffer.append(TEXT_74);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_75);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_76);
            stringBuffer.append(noNullValues);
            stringBuffer.append(TEXT_77);
          }
          stringBuffer.append(TEXT_78);
          if (table != null && !("\"\"").equals(table) && !("").equals(table)) {
            stringBuffer.append(TEXT_79);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_80);
            stringBuffer.append(table);
            stringBuffer.append(TEXT_81);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_82);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_83);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_84);
          }
          stringBuffer.append(TEXT_85);
          if (dbquery != null && !("\"\"").equals(dbquery) && !("").equals(dbquery)) {
            stringBuffer.append(TEXT_86);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_87);
            stringBuffer.append(dbquery);
            stringBuffer.append(TEXT_88);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_89);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_90);
            stringBuffer.append(cid);
            stringBuffer.append(TEXT_91);
          }
          stringBuffer.append(TEXT_92);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_93);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_94);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_95);
        }
      }
    }

    DefaultLog4jCodeGenerateUtil log4jCodeGenerateUtil = new DefaultLog4jCodeGenerateUtil();

    stringBuffer.append(TEXT_96);

    CodeGeneratorArgument codeGenArgument = (CodeGeneratorArgument) argument;
    INode node = (INode) codeGenArgument.getArgument();

    boolean isLog4jEnabled =
        ("true").equals(ElementParameterParser.getValue(node.getProcess(), "__LOG4J_ACTIVATE__"));
    String cid = node.getUniqueName();

    String dbtable = null;
    String uniqueNameConnection = null;
    INode previousNode = null;

    String differenttable = ElementParameterParser.getValue(node, "__DIFFERENT_TABLE_NAME__");
    boolean useDifferentTable =
        "true".equals(ElementParameterParser.getValue(node, "__USE_DIFFERENT_TABLE__"));

    stringBuffer.append(TEXT_97);

    List<IConnection> connections = (List<IConnection>) node.getIncomingConnections();
    if (connections != null && connections.size() > 0 && connections.get(0) != null) {
      IConnection connection = connections.get(0);
      previousNode = connection.getSource();
      String previousComponentName = previousNode.getUniqueName();
      dbtable = connection.getName();
      uniqueNameConnection = connection.getUniqueName();

      stringBuffer.append(TEXT_98);
      stringBuffer.append(previousComponentName);
      stringBuffer.append(TEXT_99);
      stringBuffer.append(uniqueNameConnection);
      stringBuffer.append(TEXT_100);
      stringBuffer.append(previousComponentName);
      stringBuffer.append(TEXT_101);
      stringBuffer.append(uniqueNameConnection);
      stringBuffer.append(TEXT_102);
    }

    String dataAction = ElementParameterParser.getValue(node, "__DATA_ACTION__");

    String dbhost = null;
    String dbname = null;
    String dbuser = null;
    String dbpwd = null;
    boolean useExistingConn = false;
    if (previousNode != null) {
      dbhost = ElementParameterParser.getValue(previousNode, "__HOST__");
      dbname = ElementParameterParser.getValue(previousNode, "__DBNAME__");
      dbuser = ElementParameterParser.getValue(previousNode, "__USER__");
      dbpwd = ElementParameterParser.getValue(previousNode, "__PASS__");
      useExistingConn =
          ("true")
              .equals(ElementParameterParser.getValue(previousNode, "__USE_EXISTING_CONNECTION__"));
    }

    String whereClause = ElementParameterParser.getValue(node, "__WHERE_CLAUSE__");

    stringBuffer.append(TEXT_103);

    List<IMetadataColumn> columnList = null;

    List<IMetadataTable> metadatas = node.getMetadataList();
    if (metadatas != null && metadatas.size() > 0) {
      IMetadataTable metadata = metadatas.get(0);
      if (metadata != null) {
        columnList = metadata.getListColumns();
      }
    }

    stringBuffer.append(TEXT_104);

    if (useExistingConn) {
      String connection = ElementParameterParser.getValue(previousNode, "__CONNECTION__");
      String conn = "conn_" + connection;

      stringBuffer.append(TEXT_105);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_106);
      stringBuffer.append(conn);
      stringBuffer.append(TEXT_107);
      if (isLog4jEnabled) {
        stringBuffer.append(TEXT_108);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_109);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_110);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_111);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_112);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_113);
      }
    } else {

      stringBuffer.append(TEXT_114);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_115);
      stringBuffer.append(dbhost);
      stringBuffer.append(TEXT_116);
      stringBuffer.append(dbname);
      stringBuffer.append(TEXT_117);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_118);
      stringBuffer.append(dbuser);
      stringBuffer.append(TEXT_119);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_120);
      stringBuffer.append(dbpwd);
      stringBuffer.append(TEXT_121);

      log4jCodeGenerateUtil.debugConnectionParams(previousNode, "com.teradata.jdbc.TeraDriver");

      stringBuffer.append(TEXT_122);
      if (isLog4jEnabled) {
        stringBuffer.append(TEXT_123);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_124);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_125);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_126);
      }
      stringBuffer.append(TEXT_127);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_128);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_129);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_130);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_131);
      if (isLog4jEnabled) {
        stringBuffer.append(TEXT_132);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_133);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_134);
      }
    }

    stringBuffer.append(TEXT_135);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_136);

    class Column {

      String name;

      String sqlStmt;

      String value;

      boolean addCol;

      public Column(IMetadataColumn column) {
        String columname = column.getOriginalDbColumnName();
        if (columname != null && columname.trim().length() > 0) {
          this.name = columname;
        } else {
          this.name = column.getLabel();
        }
        this.sqlStmt = "=?";
        this.value = "?";
        this.addCol = false;
      }

      public String getName() {
        return this.name;
      }

      public boolean isAddCol() {
        return this.addCol;
      }

      public String getSqlStmt() {
        return this.sqlStmt;
      }

      public String getValue() {
        return this.value;
      }
    }

    if (columnList != null && columnList.size() > 0) {

      StringBuilder insertColName = new StringBuilder();

      StringBuilder insertValueStmt = new StringBuilder();

      StringBuilder updateSetStmt = new StringBuilder();

      List<Column> stmtStructure = new LinkedList<Column>();

      for (IMetadataColumn column : columnList) {

        stmtStructure.add(new Column(column));
      }

      int counterOuter = 0;

      for (Column colStmt : stmtStructure) {
        String suffix = ",";

        if (counterOuter == (stmtStructure.size() - 1)) {
          suffix = "";
        }
        if (colStmt.isAddCol()) {
          insertColName.append(colStmt.getName() + suffix);
          insertValueStmt.append(colStmt.getSqlStmt() + suffix);
          updateSetStmt.append(colStmt.getName());
          updateSetStmt.append(colStmt.getSqlStmt() + suffix);
        } else {
          insertColName.append(colStmt.getName() + suffix);
          insertValueStmt.append(colStmt.getValue() + suffix);
          updateSetStmt.append(
              colStmt.getName()
                  + "= src\"+ selectQueryColumnsName.split(\",\")[ "
                  + counterOuter
                  + "].substring(selectQueryColumnsName.split(\",\")["
                  + counterOuter
                  + "].indexOf(\".\"))+\""
                  + suffix);
        }
        counterOuter++;
      }

      stringBuffer.append(TEXT_137);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_138);
      stringBuffer.append(useDifferentTable ? differenttable : "\"" + dbtable + "\"");
      stringBuffer.append(TEXT_139);

      if (("INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_140);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_141);
        stringBuffer.append(insertColName.toString());
        stringBuffer.append(TEXT_142);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_143);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_144);

      } else if (("UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_145);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_146);
        stringBuffer.append(updateSetStmt.toString());
        stringBuffer.append(TEXT_147);
        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {
          stringBuffer.append(TEXT_148);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
        }
        stringBuffer.append(TEXT_149);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_150);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_151);

      } else if (("DELETE").equals(dataAction)) {

        stringBuffer.append(TEXT_152);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_153);
        if (CodeGenerationUtils.hasAlphaNumericCharacter(whereClause)) {
          stringBuffer.append(TEXT_154);
          stringBuffer.append(CodeGenerationUtils.replaceAllCrBySpace(whereClause));
        }
        stringBuffer.append(TEXT_155);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_156);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_157);
      }

      stringBuffer.append(TEXT_158);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_159);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_160);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_161);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_162);
    }

    if (dbtable != null && columnList != null) {
      if (("INSERT").equals(dataAction)) {

        stringBuffer.append(TEXT_163);
        if (isLog4jEnabled) {
          stringBuffer.append(TEXT_164);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_165);
        }
        stringBuffer.append(TEXT_166);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_167);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_168);
        if (isLog4jEnabled) {
          stringBuffer.append(TEXT_169);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_170);
        }
        stringBuffer.append(TEXT_171);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_172);

      } else if (("UPDATE").equals(dataAction)) {

        stringBuffer.append(TEXT_173);
        if (isLog4jEnabled) {
          stringBuffer.append(TEXT_174);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_175);
        }
        stringBuffer.append(TEXT_176);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_177);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_178);
        if (isLog4jEnabled) {
          stringBuffer.append(TEXT_179);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_180);
        }
        stringBuffer.append(TEXT_181);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_182);

      } else if (("DELETE").equals(dataAction)) {

        stringBuffer.append(TEXT_183);
        if (isLog4jEnabled) {
          stringBuffer.append(TEXT_184);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_185);
        }
        stringBuffer.append(TEXT_186);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_187);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_188);
        if (isLog4jEnabled) {
          stringBuffer.append(TEXT_189);
          stringBuffer.append(cid);
          stringBuffer.append(TEXT_190);
        }
        stringBuffer.append(TEXT_191);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_192);
      }
    }

    stringBuffer.append(TEXT_193);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_194);

    if (!useExistingConn) {

      stringBuffer.append(TEXT_195);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_196);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_197);
      if (isLog4jEnabled) {
        stringBuffer.append(TEXT_198);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_199);
      }
      stringBuffer.append(TEXT_200);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_201);
      if (isLog4jEnabled) {
        stringBuffer.append(TEXT_202);
        stringBuffer.append(cid);
        stringBuffer.append(TEXT_203);
      }
      stringBuffer.append(TEXT_204);
    }

    stringBuffer.append(TEXT_205);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_206);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_207);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_208);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_209);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_210);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_211);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_212);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_213);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_214);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_215);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_216);
    stringBuffer.append(cid);
    stringBuffer.append(TEXT_217);
    if (isLog4jEnabled) {
      stringBuffer.append(TEXT_218);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_219);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_220);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_221);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_222);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_223);
      stringBuffer.append(cid);
      stringBuffer.append(TEXT_224);
    }
    stringBuffer.append(TEXT_225);
    return stringBuffer.toString();
  }