/**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  final void processRequest(final HttpServletRequest request, final HttpServletResponse response)
      throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    ApplicationContext appContext =
        WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    Answer ans = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    ans.setResultMessage(msg);

    response.setContentType("text/html;charset=UTF-8");
    String charset = request.getCharacterEncoding();

    // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String testbattery =
        ParameterParserUtil.parseStringParamAndDecodeAndSanitize(
            URLDecoder.decode(request.getParameter("testBattery"), "UTF-8"), null, charset);
    String description =
        ParameterParserUtil.parseStringParamAndDecodeAndSanitize(
            URLDecoder.decode(request.getParameter("description"), "UTF-8"), null, charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String batteryContent =
        ParameterParserUtil.parseStringParam(request.getParameter("batteryContent"), null);
    Answer finalAnswer = new Answer();
    if (StringUtil.isNullOrEmpty(testbattery)) {
      msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
      msg.setDescription(
          msg.getDescription()
              .replace("%ITEM%", "Battery")
              .replace("%OPERATION%", "Create")
              .replace("%REASON%", "Battery name is missing!"));
      finalAnswer.setResultMessage(msg);
    } else {
      ITestBatteryService testBatteryService = appContext.getBean(ITestBatteryService.class);
      IFactoryTestBattery factoryTestBattery = appContext.getBean(IFactoryTestBattery.class);

      TestBattery te = factoryTestBattery.create(0, testbattery, description);
      finalAnswer = testBatteryService.create(te);

      if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {

        /** Adding Log entry. */
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createPrivateCalls(
            "/CreateTestBattery", "CREATE", "Create Test Battery : " + testbattery, request);

        if (batteryContent != null) {

          JSONArray batteriesContent = new JSONArray(batteryContent);
          ITestBatteryContentService testBatteryContentService =
              appContext.getBean(ITestBatteryContentService.class);
          IFactoryTestBatteryContent factoryTestBatteryContent =
              appContext.getBean(IFactoryTestBatteryContent.class);
          ArrayList<TestBatteryContent> arr = new ArrayList<>();
          for (int i = 0; i < batteriesContent.length(); i++) {
            JSONObject bat = batteriesContent.getJSONObject(i);
            TestBatteryContent co =
                factoryTestBatteryContent.create(
                    0, bat.getString("test"), bat.getString("testCase"), testbattery);
            arr.add(co);
          }

          finalAnswer =
              testBatteryContentService.compareListAndUpdateInsertDeleteElements(
                  te.getTestbattery(), arr);
          if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /** Adding Log entry. */
            logEventService.createPrivateCalls(
                "/CreateTestBattery",
                "Create",
                "Create Test battery : " + te.getTestbattery(),
                request);
          }
        }
      }
    }

    /** Formating and returning the json result. */
    jsonResponse.put("messageType", finalAnswer.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", finalAnswer.getResultMessage().getDescription());

    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
  }
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, CerberusException {
    response.setContentType("text/html;charset=UTF-8");
    PrintWriter out = response.getWriter();

    ApplicationContext appContext =
        WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
    DatabaseSpring database = appContext.getBean(DatabaseSpring.class);
    ITestCaseCountryPropertiesService propertiesService =
        appContext.getBean(ITestCaseCountryPropertiesService.class);
    IFactoryTestCaseCountryProperties propertiesFactory =
        appContext.getBean(IFactoryTestCaseCountryProperties.class);

    Connection connection = database.connect();
    try {
      Statement stmt4 = connection.createStatement();
      try {

        /*
         * PROPERTIES
         */
        /*
         * Get Test, TestCase and Country to update
         */
        String test_testcase_no_format_prop = this.getStringParameter("testcase_hidden", request);
        this.getStringParameter("Test", request);
        this.getStringParameter("TestCase", request);
        String country_list_no_format = this.getStringParameter("CountryList", request);

        /*
         * Properties Insert/Update
         */
        String[] testcase_properties_delete = request.getParameterValues("properties_delete");
        // this.getStringTable("old_property_hidden", request);
        // String[] testcase_properties_countr_list = this.getStringTable(
        // "old_property_hidden", request);
        String[] testcase_properties_countr_list =
            request.getParameterValues("old_property_hidden");
        if (testcase_properties_countr_list == null) {
          testcase_properties_countr_list = new String[0];
        }
        String[] testcase_properties_propertyrow = request.getParameterValues("property_hidden");
        String[] testcase_properties_property = this.getStringTable("properties_property", request);
        String[] testcase_properties_description =
            this.getStringTable("properties_description", request);
        String[] testcase_properties_country = this.getStringTable("properties_country", request);
        String[] testcase_properties_type = this.getStringTable("properties_type", request);
        String[] testcase_properties_value = this.getStringTable("properties_value", request);
        String[] testcase_properties_value2 = this.getStringTable("properties_value2", request);
        // String[] testcase_properties_value =
        //         request.getParameterValues("properties_type");

        String[] testcase_properties_length = this.getStringTable("properties_length", request);
        String[] testcase_properties_rowlimit = this.getStringTable("properties_rowlimit", request);
        String[] testcase_properties_retrynb = this.getStringTable("properties_retrynb", request);
        String[] testcase_properties_retryperiod =
            this.getStringTable("properties_retryperiod", request);
        String[] testcase_properties_nature = this.getStringTable("properties_nature", request);
        String[] testcase_properties_database = this.getStringTable("properties_dtb", request);

        /*
         * 0 : Test 1 : TestCase
         */
        String[] test_testcase_format_prop = new String[] {"", ""};
        test_testcase_format_prop = test_testcase_no_format_prop.split(" - ");
        String[] country_list = country_list_no_format.split(" - ");

        /*
         * Properties
         */
        List<String[]> testcase_properties_propertycountry = new ArrayList<String[]>();
        testcase_properties_propertycountry.add(testcase_properties_country);

        List<String[]> testcase_properties_info = new ArrayList<String[]>();
        testcase_properties_info.add(testcase_properties_propertyrow);
        testcase_properties_info.add(testcase_properties_property);
        testcase_properties_info.add(testcase_properties_type);
        testcase_properties_info.add(testcase_properties_value);
        testcase_properties_info.add(testcase_properties_value2);
        testcase_properties_info.add(testcase_properties_length);
        testcase_properties_info.add(testcase_properties_rowlimit);
        testcase_properties_info.add(testcase_properties_retrynb);
        testcase_properties_info.add(testcase_properties_retryperiod);
        testcase_properties_info.add(testcase_properties_nature);
        testcase_properties_info.add(testcase_properties_database);

        List<String[]> countr_list = new ArrayList<String[]>();
        countr_list.add(testcase_properties_countr_list);

        // To Delete the country unselected
        Collection<String> listOne = new ArrayList<String>();
        Collection<String> listTwo = new ArrayList<String>();
        Collection<String> similar = new ArrayList<String>();
        Collection<String> different = new ArrayList<String>();

        listOne.addAll(Arrays.asList(testcase_properties_countr_list));
        listTwo.addAll(Arrays.asList(testcase_properties_country));

        similar.addAll(listOne);
        similar.retainAll(listTwo);
        different.addAll(listOne);
        different.removeAll(similar);
        List<String> differentlist = new ArrayList<String>(different);
        //

        ResultSet rs_Properties =
            stmt4.executeQuery(
                "SELECT * "
                    + " FROM testcasecountryproperties "
                    + " WHERE Test = '"
                    + test_testcase_format_prop[0]
                    + "'"
                    + " AND TestCase = '"
                    + test_testcase_format_prop[1]
                    + "'");
        try {
          // the country property already exists????
          if (rs_Properties.first()) {

            for (int i = 0; i < testcase_properties_property.length; i++) {
              for (int j = 0; j < differentlist.size(); j++) {
                String[] tc_country = new String[] {"", ""};
                tc_country = differentlist.get(j).split(" - ");

                if (testcase_properties_propertyrow[i].equals(tc_country[0])) {
                  // if the number of the line is the same for the
                  // country and the property:
                  Statement stmt3 = connection.createStatement();
                  try {
                    stmt3.execute(
                        "DELETE FROM testcasecountryproperties "
                            + " WHERE Test = '"
                            + test_testcase_format_prop[0]
                            + "' "
                            + " AND TestCase = '"
                            + test_testcase_format_prop[1]
                            + "' "
                            + " AND Country = '"
                            + tc_country[1]
                            + "' "
                            + " AND Property = '"
                            + testcase_properties_property[i]
                            + "'");
                  } finally {
                    stmt3.close();
                  }
                  // Is the country exist in the database??
                } // end of the if loop
              } // end of the loop for differnet list
            } // end of the property loop >>>>>The country unselected have
            // been removed

            // Delete the property which have been renamed
            for (int i = 0; i < testcase_properties_property.length; i++) {
              for (int p = 0; p < testcase_properties_countr_list.length; p++) {
                String[] tc_prop = new String[] {"", "", ""};
                tc_prop = testcase_properties_countr_list[p].split(" - ");
                if (testcase_properties_propertyrow[i].equals(tc_prop[0])
                    && !testcase_properties_property[i].equals(tc_prop[2])) {
                  Statement stmt3 = connection.createStatement();
                  try {
                    stmt3.execute(
                        "DELETE FROM testcasecountryproperties "
                            + " WHERE Test = '"
                            + test_testcase_format_prop[0]
                            + "' "
                            + " AND TestCase = '"
                            + test_testcase_format_prop[1]
                            + "' "
                            + " AND Country = '"
                            + tc_prop[1]
                            + "' "
                            + " AND Property = '"
                            + tc_prop[2]
                            + "'");
                  } finally {
                    stmt3.close();
                  }
                }
              }
            }
            // for each line, insert the property when not exist and update
            // when exist.
            for (int i = 0; i < testcase_properties_property.length; i++) {
              // for each country flagged in the page
              for (int j = 0; j < testcase_properties_country.length; j++) {
                // separate the number of the line to the country:
                // example: 1 - AT
                String[] testcase_country = new String[] {"", ""};
                testcase_country = testcase_properties_country[j].split(" - ");
                // if the number of the line is the same for the country
                // and the property:
                if (testcase_properties_propertyrow[i].equals(testcase_country[0])) {
                  Statement stmt3 = connection.createStatement();
                  try {
                    ResultSet rs_numberOfTestCasesCountryProperties =
                        stmt3.executeQuery(
                            "SELECT Test, TestCase, Country, Property "
                                + " FROM testcasecountryproperties "
                                + " WHERE Test = '"
                                + test_testcase_format_prop[0]
                                + "'"
                                + " AND TestCase = '"
                                + test_testcase_format_prop[1]
                                + "'"
                                + " AND Country = '"
                                + testcase_country[1]
                                + "'"
                                + " AND Property = '"
                                + testcase_properties_property[i]
                                + "'");
                    try {
                      // Is the country exist in the database??
                      // the country property already exists, make an
                      // update
                      if (rs_numberOfTestCasesCountryProperties.first()) {

                        String test = test_testcase_format_prop[0];
                        String testcase = test_testcase_format_prop[1];
                        String country = testcase_country[1];
                        String property = testcase_properties_property[i];
                        String description = testcase_properties_description[i];
                        String nature = testcase_properties_nature[i];
                        int rowlimit = Integer.parseInt(testcase_properties_rowlimit[i]);
                        int length = Integer.parseInt(testcase_properties_length[i]);
                        int retryNb = Integer.parseInt(testcase_properties_retrynb[i]);
                        int retryPeriod = Integer.parseInt(testcase_properties_retryperiod[i]);
                        String value1 = testcase_properties_value[i];
                        String value2 = testcase_properties_value2[i];
                        String type = testcase_properties_type[i];
                        String dtb = testcase_properties_database[i];

                        TestCaseCountryProperties tccp =
                            propertiesFactory.create(
                                test,
                                testcase,
                                country,
                                property,
                                description,
                                type,
                                dtb,
                                value1,
                                value2,
                                length,
                                rowlimit,
                                nature,
                                retryNb,
                                retryPeriod);

                        propertiesService.updateTestCaseCountryProperties(tccp);

                      } else // the country property does'nt extist, make an
                      // insert :
                      {
                        /*
                         * Insert new rows
                         */

                        String test = test_testcase_format_prop[0];
                        String testcase = test_testcase_format_prop[1];
                        String country = testcase_country[1];
                        String property = testcase_properties_property[i];
                        String description = testcase_properties_description[i];
                        String nature = testcase_properties_nature[i];
                        int rowlimit = Integer.parseInt(testcase_properties_rowlimit[i]);
                        int length = Integer.parseInt(testcase_properties_length[i]);
                        int retryNb = Integer.parseInt(testcase_properties_retrynb[i]);
                        int retryPeriod = Integer.parseInt(testcase_properties_retryperiod[i]);
                        String value1 = testcase_properties_value[i];
                        String value2 = testcase_properties_value2[i];
                        String type = testcase_properties_type[i];
                        String dtb = testcase_properties_database[i];

                        TestCaseCountryProperties tccp =
                            propertiesFactory.create(
                                test,
                                testcase,
                                country,
                                property,
                                description,
                                type,
                                dtb,
                                value1,
                                value2,
                                length,
                                rowlimit,
                                nature,
                                retryNb,
                                retryPeriod);

                        propertiesService.insertTestCaseCountryProperties(tccp);
                      } // end of the else loop
                    } finally {
                      rs_numberOfTestCasesCountryProperties.close();
                    }
                  } finally {
                    stmt3.close();
                  }
                } // end of the if loop
              } // Close the loop for country
            } // Close the loop for (property)
          } // end of the if loop (property already exists??
          else // The property is a first one
          {
            for (int i = 0; i < testcase_properties_property.length; i++) {
              // for each country flagged in the page
              for (int j = 0; j < testcase_properties_country.length; j++) {
                // separate the number of the line to the country:
                // example: 1 - AT
                String[] testcase_country = new String[] {"", ""};
                testcase_country = testcase_properties_country[j].split(" - ");
                // if the number of the line is the same for the country
                // and the property:
                if (testcase_properties_propertyrow[i].equals(testcase_country[0])) {
                  String test = test_testcase_format_prop[0];
                  String testcase = test_testcase_format_prop[1];
                  String country = testcase_country[1];
                  String property = testcase_properties_property[i];
                  String description = testcase_properties_description[i];
                  String nature = testcase_properties_nature[i];
                  int rowlimit = Integer.parseInt(testcase_properties_rowlimit[i]);
                  int length = Integer.parseInt(testcase_properties_length[i]);
                  int retryNb = Integer.parseInt(testcase_properties_retrynb[i]);
                  int retryPeriod = Integer.parseInt(testcase_properties_retryperiod[i]);
                  String value1 = testcase_properties_value[i];
                  String value2 = testcase_properties_value2[i];
                  String type = testcase_properties_type[i];
                  String dtb = testcase_properties_database[i];

                  TestCaseCountryProperties tccp =
                      propertiesFactory.create(
                          test,
                          testcase,
                          country,
                          property,
                          description,
                          type,
                          dtb,
                          value1,
                          value2,
                          length,
                          rowlimit,
                          nature,
                          retryNb,
                          retryPeriod);

                  propertiesService.insertTestCaseCountryProperties(tccp);
                } // Close the condition on the row number
              } // Close the loop for (country)
            } // Close the else condition
          }
        } finally {
          rs_Properties.close();
        }

        /*
         * Delete Properties
         */
        if (testcase_properties_delete != null) { // If some properties
          // check for delete
          Statement stmt3 = connection.createStatement();
          try {
            for (String element : testcase_properties_delete) {
              String property_and_country[] = element.split(" - ");
              for (int i = 1; i < property_and_country.length; i++) {
                stmt3.execute(
                    "DELETE FROM testcasecountryproperties "
                        + " WHERE Test = '"
                        + test_testcase_format_prop[0]
                        + "' "
                        + " AND TestCase = '"
                        + test_testcase_format_prop[1]
                        + "' "
                        + " AND Country = '"
                        + property_and_country[i]
                        + "' "
                        + " AND Property = '"
                        + property_and_country[0]
                        + "'");
              }
            }
          } finally {
            stmt3.close();
          }
        }

        String sqlupd =
            "UPDATE testcase "
                + "SET LastModifier = '"
                + request.getUserPrincipal().getName()
                + "' WHERE Test = '"
                + test_testcase_format_prop[0]
                + "' AND TestCase = '"
                + test_testcase_format_prop[1]
                + "' ";
        stmt4.execute(sqlupd);

        /*
         * END OF PROPERTIES
         */
        /*
         * Get Test, TestCase and Country to update
         */
        String test_testcase_no_format = this.getStringParameter("testcase_hidden", request);
        this.getStringParameter("Test", request);
        this.getStringParameter("TestCase", request);

        /*
         * Get TestCase Step Informations
         */
        /*
         * Step Number & Description
         */
        String step_delete[] = request.getParameterValues("testcasestep_delete");
        String step_id[] = this.getStringTable("testcasestep_hidden", request);
        String step_desc[] = this.getStringTable("step_description", request, true);
        String step_number_toadd[] = this.getStringTable("step_number_add", request);
        String step_desc_toadd[] = this.getStringTable("step_description_add", request, true);

        /*
         * Step actions
         */
        String step_action_delete[] = request.getParameterValues("actions_delete");
        String step_number_hide[] = this.getStringTable("stepnumber_hidden", request);
        String step_sequence[] = this.getStringTable("actions_sequence", request);
        String step_action[] = this.getStringTable("actions_action", request);
        String step_conditionOper[] = this.getStringTable("actions_conditionOper", request);
        String step_conditionVal1[] = this.getStringTable("actions_conditionVal1", request);
        String step_conditionVal2[] = this.getStringTable("actions_conditionVal2", request);
        String step_value1[] = this.getStringTable("actions_value1", request);
        String step_value2[] = this.getStringTable("actions_value2", request);
        String step_description[] = this.getStringTable("actions_description", request, true);
        /*
         * Get TestCase Step Controls Informations
         */
        String controls_delete[] = request.getParameterValues("controls_delete");
        String controls_step[] = this.getStringTable("controls_step", request);
        String controls_sequence[] = this.getStringTable("controls_sequence", request);
        String controls_controlsequence[] =
            this.getStringTable("controls_controlsequence", request);
        String controls_conditionoper[] =
            this.getStringTable("controls_conditionoper", request, true);
        String controls_conditionval1[] =
            this.getStringTable("controls_conditionval1", request, true);
        String controls_conditionval2[] =
            this.getStringTable("controls_conditionval2", request, true);
        String controls_control[] = this.getStringTable("controls_control", request);
        String controls_value1[] = this.getStringTable("controls_value1", request);
        String controls_value2[] = this.getStringTable("controls_value2", request);
        String controls_description[] = this.getStringTable("controls_description", request, true);
        String controls_fatal[] = this.getStringTable("controls_fatal", request);

        /*
         * Properties Insert/Update
         */
        String[] test_testcase_format = test_testcase_no_format.split(" - ");

        /*
         * Properties
         */
        /*
         * Actions
         */
        List<String[]> testcase_actions_info = new ArrayList<String[]>();
        testcase_actions_info.add(step_number_hide);
        testcase_actions_info.add(step_sequence);
        testcase_actions_info.add(step_conditionOper);
        testcase_actions_info.add(step_conditionVal1);
        testcase_actions_info.add(step_conditionVal2);
        testcase_actions_info.add(step_action);
        testcase_actions_info.add(step_value1);
        testcase_actions_info.add(step_value2);
        testcase_actions_info.add(step_description);

        /*
         * Controls
         */
        List<String[]> testcase_controls_info = new ArrayList<String[]>();
        testcase_controls_info.add(controls_step);
        testcase_controls_info.add(controls_sequence);
        testcase_controls_info.add(controls_controlsequence);
        testcase_controls_info.add(controls_description);
        testcase_controls_info.add(controls_conditionoper);
        testcase_controls_info.add(controls_conditionval1);
        testcase_controls_info.add(controls_conditionval2);
        testcase_controls_info.add(controls_control);
        testcase_controls_info.add(controls_value1);
        testcase_controls_info.add(controls_value2);
        testcase_controls_info.add(controls_fatal);

        /*
         * Test Case Step Description
         */
        /*
         * Update Test Case Step
         */
        int numberOfSteps = 0;
        Integer steps = 0;
        ResultSet rs_step =
            stmt4.executeQuery(
                "SELECT COUNT(*) "
                    + " FROM testcasestep "
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "'"
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "'");
        try {

          if (rs_step.next()) {
            numberOfSteps = rs_step.getInt(1);
          }
          if (request.getParameter("step_number_add") != null) {
            steps = Integer.parseInt(request.getParameter("step_number_add"));
          }
          if (steps == null || steps == 0) {
            steps = step_id.length;
          }
        } finally {
          rs_step.close();
        }

        for (int i = 0; i < steps; i++) {
          int i2 = i + 1;
          String step_batch[] = request.getParameterValues("batch-" + i2);

          if (i < numberOfSteps) {
            stmt4.executeUpdate(
                "UPDATE testcasestep "
                    + " SET Description = '"
                    + step_desc[i]
                    + "' "
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "'"
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' "
                    + " AND Step = "
                    + step_id[i]
                    + "");

            stmt4.executeUpdate(
                "DELETE FROM testcasestepbatch WHERE  "
                    + " test = '"
                    + test_testcase_format[0]
                    + "' "
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' AND STEP = '"
                    + step_id[i]
                    + "' ");

            if (step_batch != null) {
              for (int j = 0; j < step_batch.length; j++) {
                try {
                  stmt4.executeUpdate(
                      "INSERT INTO testcasestepbatch (`Test`, `TestCase`, `Step`, `Batch`) VALUES ( "
                          + "'"
                          + test_testcase_format[0]
                          + "', "
                          + "'"
                          + test_testcase_format[1]
                          + "', "
                          + "'"
                          + step_id[i]
                          + "', "
                          + "'"
                          + step_batch[j]
                          + "' )");

                } catch (Exception e) {
                  MyLogger.log(
                      UpdateTestCaseDetail.class.getName(),
                      Level.FATAL,
                      "Error on insert into TestCaseStepBatch: " + e.toString());
                }
              }
            }
          }
        }

        /*
         * Insert Test Case Step
         */
        for (int i = 0; i < step_number_toadd.length; i++) {
          if (this.formIsFill(step_number_toadd[i]) && this.formIsFill(step_desc_toadd[i])) {
            String sql =
                ("INSERT INTO testcasestep (`Test`,`Testcase`,`Step`,`Description`) "
                    + " VALUES('"
                    + test_testcase_format[0]
                    + "', "
                    + "'"
                    + test_testcase_format[1]
                    + "', "
                    + step_number_toadd[i]
                    + ", "
                    + "'"
                    + step_desc_toadd[i]
                    + "')");
            stmt4.execute(sql);
          }

          stmt4.executeUpdate(
              "DELETE FROM testcasestepbatch WHERE  "
                  + " test = '"
                  + test_testcase_format[0]
                  + "' "
                  + " AND TestCase = '"
                  + test_testcase_format[1]
                  + "' AND STEP = '"
                  + request.getParameter("step_number_add")
                  + "' ");

          String step_batch[] = request.getParameterValues("batch-" + step_number_toadd[i]);

          if (step_batch != null) {
            for (int j = 0; j < step_batch.length; j++) {
              try {

                String sql =
                    "INSERT INTO testcasestepbatch (`Test`, `TestCase`, `Step`, `Batch`) VALUES ( "
                        + "'"
                        + test_testcase_format[0]
                        + "', "
                        + "'"
                        + test_testcase_format[1]
                        + "', "
                        + "'"
                        + step_number_toadd[i]
                        + "', "
                        + "'"
                        + step_batch[j]
                        + "' )";
                stmt4.executeUpdate(sql);

              } catch (Exception e) {
                MyLogger.log(
                    UpdateTestCaseDetail.class.getName(),
                    Level.FATAL,
                    "Error on insert into TestCaseStepBatch: " + e.toString());
              }
            }
          }
        }

        /*
         * Test Case Step Actions
         */
        for (int i = 0; i < step_number_hide.length; i++) {

          /*
           * Select to know if need to update or insert
           */
          if (this.formIsFill(step_sequence[i]) && (step_sequence[i].length() > 0)) {
            String sql =
                ("SELECT * "
                    + " FROM testcasestepaction"
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "'"
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' "
                    + " AND Step = "
                    + step_number_hide[i]
                    + " "
                    + " AND Sequence = "
                    + step_sequence[i]);
            ResultSet rs_stepaction = stmt4.executeQuery(sql);
            try {
              IFactoryTestCaseStepAction actionFactory =
                  appContext.getBean(IFactoryTestCaseStepAction.class);
              ITestCaseStepActionService actionService =
                  appContext.getBean(ITestCaseStepActionService.class);
              TestCaseStepAction tcsa =
                  actionFactory.create(
                      test_testcase_format[0],
                      test_testcase_format[1],
                      Integer.parseInt(step_number_hide[i]),
                      Integer.parseInt(step_sequence[i]),
                      Integer.parseInt(step_sequence[i]),
                      step_conditionOper[i],
                      step_conditionVal1[i],
                      step_conditionVal2[i],
                      step_action[i],
                      step_value1[i],
                      step_value2[i],
                      "",
                      step_description[i],
                      "");

              if (rs_stepaction.next()) {
                actionService.updateTestCaseStepAction(tcsa);

              } else if (this.formIsFullFill(testcase_actions_info, i)) {
                actionService.insertTestCaseStepAction(tcsa);
              }
            } finally {
              rs_stepaction.close();
            }
          }
        }

        /*
         * TestCase Step Controls
         */
        /*
         * Get Number of actual testcase controls
         */
        for (int i = 0; i < controls_controlsequence.length; i++) {

          /*
           * Select to know if need to update or insert
           */
          if (this.formIsFill(controls_sequence[i]) && (controls_sequence[i].length() > 0)) {
            String sql =
                ("SELECT * "
                    + " FROM testcasestepactioncontrol"
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "'"
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' "
                    + " AND Step = "
                    + controls_step[i]
                    + " "
                    + " AND Sequence = "
                    + controls_sequence[i]
                    + " "
                    + " AND controlSequence = "
                    + controls_controlsequence[i]);
            ResultSet rs_stepactioncontrol = stmt4.executeQuery(sql);
            try {
              IFactoryTestCaseStepActionControl controlFactory =
                  appContext.getBean(IFactoryTestCaseStepActionControl.class);
              ITestCaseStepActionControlService controlService =
                  appContext.getBean(ITestCaseStepActionControlService.class);
              TestCaseStepActionControl control =
                  controlFactory.create(
                      test_testcase_format[0],
                      test_testcase_format[1],
                      Integer.parseInt(controls_step[i]),
                      Integer.parseInt(controls_sequence[i]),
                      Integer.parseInt(controls_controlsequence[i]),
                      0,
                      controls_conditionoper[i],
                      controls_conditionval1[i],
                      controls_conditionval2[i],
                      controls_control[i],
                      (controls_value1[i]),
                      controls_value2[i],
                      controls_fatal[i],
                      controls_description[i],
                      "");

              if (rs_stepactioncontrol.next()) {

                controlService.updateTestCaseStepActionControl(control);

              } else {
                //                        if (this.formIsFullFill(testcase_controls_info, i)) {

                controlService.insertTestCaseStepActionControl(control);

                //                        }
              }

            } finally {
              rs_stepactioncontrol.close();
            }
          }
        }

        /*
         * DELETE
         */
        if (step_delete != null) {
          for (String step_to_delete : step_delete) {
            stmt4.execute(
                "DELETE FROM testcasestep "
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "' "
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' "
                    + " AND Step = "
                    + step_to_delete
                    + "");
          }
        }

        /*
         * Delete Actions
         */
        if (step_action_delete != null) { // If some actions to delete
          for (String action_to_delete : step_action_delete) { // Delete
            // with
            // key

            String[] step_action_split = action_to_delete.split("-");

            stmt4.execute(
                "DELETE FROM testcasestepaction "
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "' "
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' "
                    + " AND Step = "
                    + step_action_split[0]
                    + " "
                    + " AND Sequence = "
                    + step_action_split[1]
                    + "");
          }
        }

        /*
         * Delete Controls
         */
        if (controls_delete != null) { // If controls to delete
          for (String keys_to_delete : controls_delete) {

            String[] key_delete = keys_to_delete.split("-"); // Get
            // Step,
            // Sequence
            // and
            // Control

            stmt4.execute(
                "DELETE FROM testcasestepactioncontrol "
                    + " WHERE Test = '"
                    + test_testcase_format[0]
                    + "' "
                    + " AND TestCase = '"
                    + test_testcase_format[1]
                    + "' "
                    + " AND Step = "
                    + key_delete[0]
                    + " "
                    + " AND Sequence = "
                    + key_delete[1]
                    + " "
                    + " AND ControlSequence = "
                    + key_delete[2]
                    + "");
          }
        }

        stmt4.execute(
            "UPDATE testcase "
                + "SET LastModifier = '"
                + request.getUserPrincipal().getName()
                + "' "
                + "WHERE Test = '"
                + test_testcase_format[0]
                + "' "
                + " AND TestCase = '"
                + test_testcase_format[1]
                + "' ");

        /** Adding Log entry. */
        ILogEventService logEventService = appContext.getBean(LogEventService.class);
        logEventService.createPrivateCalls(
            "/UpdateTestCaseDetail",
            "UPDATE",
            "Update testcase detail : ['"
                + test_testcase_format_prop[0]
                + "'|'"
                + test_testcase_format_prop[1]
                + "']",
            request);

        /*
         * Redirect
         */
        response.sendRedirect(
            "TestCase.jsp?Load=Load&Test="
                + test_testcase_format[0]
                + "&TestCase="
                + test_testcase_format[1]);
        return;
      } finally {
        stmt4.close();
      }
    } catch (SQLException ex) {
      MyLogger.log(UpdateTestCaseDetail.class.getName(), Level.FATAL, "" + ex);
      // out.println ( UpdateTestCase.class.getName ( ) + ex ) ;
    } catch (NullPointerException ex) {
      MyLogger.log(UpdateTestCaseDetail.class.getName(), Level.FATAL, "" + ex);
    } catch (ArrayIndexOutOfBoundsException ex) {
      MyLogger.log(UpdateTestCaseDetail.class.getName(), Level.FATAL, "" + ex);
    } finally {
      try {
        if (connection != null) {
          connection.close();
        }
      } catch (SQLException e) {
        MyLogger.log(UpdateTestCaseDetail.class.getName(), Level.WARN, e.toString());
      }
    }

    response.sendRedirect(
        "TestCase.jsp?Load=Load&Test="
            + request.getParameter("Test")
            + "&TestCase="
            + request.getParameter("TestCase"));
    out.close();
  }
Exemple #3
0
  /**
   * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
   *
   * @param request servlet request
   * @param response servlet response
   * @throws ServletException if a servlet-specific error occurs
   * @throws IOException if an I/O error occurs
   */
  protected void processRequest(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException, CerberusException, JSONException {
    JSONObject jsonResponse = new JSONObject();
    Answer ans = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    ans.setResultMessage(msg);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);

    response.setContentType("application/json");

    /** Parsing and securing all required parameters. */
    String robot = policy.sanitize(request.getParameter("robot"));
    String host = policy.sanitize(request.getParameter("host"));
    String port = policy.sanitize(request.getParameter("port"));
    String platform = policy.sanitize(request.getParameter("platform"));
    String browser = policy.sanitize(request.getParameter("browser"));
    String version = policy.sanitize(request.getParameter("version"));
    String active = policy.sanitize(request.getParameter("active"));
    String description = policy.sanitize(request.getParameter("description"));
    String userAgent = policy.sanitize(request.getParameter("useragent"));
    Integer robotid = 0;
    boolean robotid_error = true;
    try {
      if (request.getParameter("robotid") != null && !request.getParameter("robotid").equals("")) {
        robotid = Integer.valueOf(policy.sanitize(request.getParameter("robotid")));
        robotid_error = false;
      }
    } catch (Exception ex) {
      robotid_error = true;
    }

    /** Checking all constrains before calling the services. */
    if (StringUtil.isNullOrEmpty(robot)) {
      msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
      msg.setDescription(
          msg.getDescription()
              .replace("%ITEM%", "Robot")
              .replace("%OPERATION%", "Update")
              .replace("%REASON%", "Robot name is missing."));
      ans.setResultMessage(msg);
    } else if (robotid_error) {
      msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
      msg.setDescription(
          msg.getDescription()
              .replace("%ITEM%", "Robot")
              .replace("%OPERATION%", "Update")
              .replace(
                  "%REASON%",
                  "Could not manage to convert robotid to an integer value or robotid is missing."));
      ans.setResultMessage(msg);
    } else {
      /** All data seems cleans so we can call the services. */
      ApplicationContext appContext =
          WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
      IRobotService robotService = appContext.getBean(IRobotService.class);

      AnswerItem resp = robotService.readByKeyTech(robotid);
      if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()))) {
        /** Object could not be found. We stop here and report the error. */
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(
            msg.getDescription()
                .replace("%ITEM%", "Robot")
                .replace("%OPERATION%", "Update")
                .replace("%REASON%", "Robot does not exist."));
        ans.setResultMessage(msg);

      } else {
        /**
         * The service was able to perform the query and confirm the object exist, then we can
         * update it.
         */
        Robot robotData = (Robot) resp.getItem();
        robotData.setRobot(robot);
        robotData.setHost(host);
        robotData.setPort(port);
        robotData.setPlatform(platform);
        robotData.setBrowser(browser);
        robotData.setVersion(version);
        robotData.setActive(active);
        robotData.setDescription(description);
        robotData.setUserAgent(userAgent);
        ans = robotService.update(robotData);

        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
          /** Update was succesfull. Adding Log entry. */
          ILogEventService logEventService = appContext.getBean(LogEventService.class);
          logEventService.createPrivateCalls(
              "/UpdateRobot",
              "UPDATE",
              "Updated Robot : ['" + robotid + "'|'" + robot + "']",
              request);
        }
      }
    }

    /** Formating and returning the json result. */
    jsonResponse.put("messageType", ans.getResultMessage().getMessage().getCodeString());
    jsonResponse.put("message", ans.getResultMessage().getDescription());

    response.getWriter().print(jsonResponse);
    response.getWriter().flush();
  }