Search in sources :

Example 1 with CountryEnvironmentDatabase

use of org.cerberus.crud.entity.CountryEnvironmentDatabase in project cerberus-source by cerberustesting.

the class CountryEnvironmentDatabaseDAO method readByKey.

@Override
public AnswerItem readByKey(String system, String country, String environment, String database) {
    AnswerItem ans = new AnswerItem();
    CountryEnvironmentDatabase result = null;
    final String query = "SELECT * FROM countryenvironmentdatabase ceb WHERE ceb.database = ? AND ceb.environment = ? AND ceb.country = ? AND ceb.system = ?";
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query);
        LOG.debug("SQL.param.database : " + database);
        LOG.debug("SQL.param.environment : " + environment);
        LOG.debug("SQL.param.country : " + country);
        LOG.debug("SQL.param.system : " + system);
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            preStat.setString(1, database);
            preStat.setString(2, environment);
            preStat.setString(3, country);
            preStat.setString(4, system);
            ResultSet resultSet = preStat.executeQuery();
            try {
                if (resultSet.first()) {
                    result = loadFromResultSet(resultSet);
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    ans.setItem(result);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem) IFactoryCountryEnvironmentDatabase(org.cerberus.crud.factory.IFactoryCountryEnvironmentDatabase) CountryEnvironmentDatabase(org.cerberus.crud.entity.CountryEnvironmentDatabase)

Example 2 with CountryEnvironmentDatabase

use of org.cerberus.crud.entity.CountryEnvironmentDatabase in project cerberus-source by cerberustesting.

the class CountryEnvironmentDatabaseDAO method readByVariousByCriteria.

@Override
public AnswerList readByVariousByCriteria(String system, String country, String environment, int start, int amount, String column, String dir, String searchTerm, String individualSearch) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<CountryEnvironmentDatabase> objectList = new ArrayList<CountryEnvironmentDatabase>();
    StringBuilder searchSQL = new StringBuilder();
    StringBuilder query = new StringBuilder();
    // SQL_CALC_FOUND_ROWS allows to retrieve the total number of columns by disrearding the limit clauses that
    // were applied -- used for pagination p
    query.append("SELECT SQL_CALC_FOUND_ROWS * FROM countryenvironmentdatabase ceb ");
    searchSQL.append(" where 1=1 ");
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (ceb.`system` like ?");
        searchSQL.append(" or ceb.`Country` like ?");
        searchSQL.append(" or ceb.`Environment` like ?");
        searchSQL.append(" or ceb.`Database` like ?");
        searchSQL.append(" or ceb.`ConnectionPoolName` like ?");
        searchSQL.append(" or ceb.`SoapUrl` like ?)");
    }
    if (!StringUtil.isNullOrEmpty(individualSearch)) {
        searchSQL.append(" and (`?`)");
    }
    if (!StringUtil.isNullOrEmpty(system)) {
        searchSQL.append(" and (ceb.`System` = ? )");
    }
    if (!StringUtil.isNullOrEmpty(country)) {
        searchSQL.append(" and (ceb.`Country` = ? )");
    }
    if (!StringUtil.isNullOrEmpty(environment)) {
        searchSQL.append(" and (ceb.`Environment` = ? )");
    }
    query.append(searchSQL);
    if (!StringUtil.isNullOrEmpty(column)) {
        query.append(" order by `").append(column).append("` ").append(dir);
    }
    if ((amount <= 0) || (amount >= MAX_ROW_SELECTED)) {
        query.append(" limit ").append(start).append(" , ").append(MAX_ROW_SELECTED);
    } else {
        query.append(" limit ").append(start).append(" , ").append(amount);
    }
    // Debug message on SQL.
    if (LOG.isDebugEnabled()) {
        LOG.debug("SQL : " + query.toString());
    }
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        try {
            int i = 1;
            if (!StringUtil.isNullOrEmpty(searchTerm)) {
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
                preStat.setString(i++, "%" + searchTerm + "%");
            }
            if (!StringUtil.isNullOrEmpty(individualSearch)) {
                preStat.setString(i++, individualSearch);
            }
            if (!StringUtil.isNullOrEmpty(system)) {
                preStat.setString(i++, system);
            }
            if (!StringUtil.isNullOrEmpty(country)) {
                preStat.setString(i++, country);
            }
            if (!StringUtil.isNullOrEmpty(environment)) {
                preStat.setString(i++, environment);
            }
            ResultSet resultSet = preStat.executeQuery();
            try {
                // gets the data
                while (resultSet.next()) {
                    objectList.add(this.loadFromResultSet(resultSet));
                }
                // get the total number of rows
                resultSet = preStat.executeQuery("SELECT FOUND_ROWS()");
                int nrTotalRows = 0;
                if (resultSet != null && resultSet.next()) {
                    nrTotalRows = resultSet.getInt(1);
                }
                if (objectList.size() >= MAX_ROW_SELECTED) {
                    // Result of SQl was limited by MAX_ROW_SELECTED constrain. That means that we may miss some lines in the resultList.
                    LOG.error("Partial Result in the query.");
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_WARNING_PARTIAL_RESULT);
                    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", "Maximum row reached : " + MAX_ROW_SELECTED));
                    response = new AnswerList(objectList, nrTotalRows);
                } else if (objectList.size() <= 0) {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_NO_DATA_FOUND);
                    response = new AnswerList(objectList, nrTotalRows);
                } else {
                    msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);
                    msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "SELECT"));
                    response = new AnswerList(objectList, nrTotalRows);
                }
            } catch (SQLException exception) {
                LOG.error("Unable to execute query : " + exception.toString());
                msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
                msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
            } finally {
                if (resultSet != null) {
                    resultSet.close();
                }
            }
        } catch (SQLException exception) {
            LOG.error("Unable to execute query : " + exception.toString());
            msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
            msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
        } finally {
            if (preStat != null) {
                preStat.close();
            }
        }
    } catch (SQLException exception) {
        LOG.error("Unable to execute query : " + exception.toString());
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
        msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", exception.toString()));
    } finally {
        try {
            if (!this.databaseSpring.isOnTransaction()) {
                if (connection != null) {
                    connection.close();
                }
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to close connection : " + exception.toString());
        }
    }
    response.setResultMessage(msg);
    response.setDataList(objectList);
    return response;
}
Also used : AnswerList(org.cerberus.util.answer.AnswerList) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) IFactoryCountryEnvironmentDatabase(org.cerberus.crud.factory.IFactoryCountryEnvironmentDatabase) CountryEnvironmentDatabase(org.cerberus.crud.entity.CountryEnvironmentDatabase)

Example 3 with CountryEnvironmentDatabase

use of org.cerberus.crud.entity.CountryEnvironmentDatabase in project cerberus-source by cerberustesting.

the class SQLService method executeCallableStatement.

@Override
public MessageEvent executeCallableStatement(String system, String country, String environment, String database, String sql) {
    String connectionName;
    CountryEnvironmentDatabase countryEnvironmentDatabase;
    MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    try {
        countryEnvironmentDatabase = this.countryEnvironmentDatabaseService.convert(this.countryEnvironmentDatabaseService.readByKey(system, country, environment, database));
        if (countryEnvironmentDatabase != null) {
            connectionName = countryEnvironmentDatabase.getConnectionPoolName();
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_GENERIC);
            msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
            if (!(StringUtil.isNullOrEmpty(connectionName))) {
                if (connectionName.contains("cerberus")) {
                    return new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_AGAINST_CERBERUS);
                } else {
                    try (Connection connection = this.databaseSpring.connect(connectionName);
                        CallableStatement cs = connection.prepareCall(sql)) {
                        Integer sqlTimeout = parameterService.getParameterIntegerByKey("cerberus_actionexecutesqlstoredprocedure_timeout", system, 60);
                        cs.setQueryTimeout(sqlTimeout);
                        try {
                            cs.execute();
                            int nbUpdate = cs.getUpdateCount();
                            msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_EXECUTESQLSTOREDPROCEDURE).resolveDescription("NBROWS", String.valueOf(nbUpdate)).resolveDescription("JDBC", connectionName).resolveDescription("SQL", sql);
                        } catch (SQLTimeoutException exception) {
                            LOG.warn(exception.toString());
                            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_TIMEOUT);
                            msg.setDescription(msg.getDescription().replace("%SQL%", sql));
                            msg.setDescription(msg.getDescription().replace("%TIMEOUT%", String.valueOf(sqlTimeout)));
                            msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                        } catch (SQLException exception) {
                            LOG.warn(exception.toString());
                            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_ERROR);
                            msg.setDescription(msg.getDescription().replace("%SQL%", sql));
                            msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                        } finally {
                            cs.close();
                        }
                    } catch (SQLException exception) {
                        LOG.warn(exception.toString());
                        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_ERROR);
                        msg.setDescription(msg.getDescription().replace("%SQL%", sql));
                        msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                    } catch (NullPointerException exception) {
                        LOG.warn(exception.toString());
                        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_CANNOTACCESSJDBC);
                        msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
                        msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                    }
                }
            } else {
                msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_DATABASECONFIGUREDBUTJDBCPOOLEMPTY);
                msg.setDescription(msg.getDescription().replace("%SYSTEM%", system));
                msg.setDescription(msg.getDescription().replace("%COUNTRY%", country));
                msg.setDescription(msg.getDescription().replace("%ENV%", environment));
                msg.setDescription(msg.getDescription().replace("%DATABASE%", database));
            }
        } else {
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_DATABASENOTCONFIGURED);
            msg.setDescription(msg.getDescription().replace("%SYSTEM%", system));
            msg.setDescription(msg.getDescription().replace("%COUNTRY%", country));
            msg.setDescription(msg.getDescription().replace("%ENV%", environment));
            msg.setDescription(msg.getDescription().replace("%DATABASE%", database));
        }
    } catch (CerberusException ex) {
        LOG.error(ex.toString());
    }
    return msg;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) CallableStatement(java.sql.CallableStatement) Connection(java.sql.Connection) SQLTimeoutException(java.sql.SQLTimeoutException) CountryEnvironmentDatabase(org.cerberus.crud.entity.CountryEnvironmentDatabase)

Example 4 with CountryEnvironmentDatabase

use of org.cerberus.crud.entity.CountryEnvironmentDatabase in project cerberus-source by cerberustesting.

the class SQLService method executeUpdate.

@Override
public MessageEvent executeUpdate(String system, String country, String environment, String database, String sql) {
    String connectionName;
    CountryEnvironmentDatabase countryEnvironmentDatabase;
    MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);
    try {
        countryEnvironmentDatabase = this.countryEnvironmentDatabaseService.convert(this.countryEnvironmentDatabaseService.readByKey(system, country, environment, database));
        if (countryEnvironmentDatabase != null) {
            connectionName = countryEnvironmentDatabase.getConnectionPoolName();
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_GENERIC);
            msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
            if (!(StringUtil.isNullOrEmpty(connectionName))) {
                if (connectionName.equals("cerberus" + System.getProperty("org.cerberus.environment"))) {
                    return new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_AGAINST_CERBERUS);
                } else {
                    try (Connection connection = this.databaseSpring.connect(connectionName);
                        PreparedStatement preStat = connection.prepareStatement(sql)) {
                        Integer sqlTimeout = parameterService.getParameterIntegerByKey("cerberus_actionexecutesqlupdate_timeout", system, 60);
                        preStat.setQueryTimeout(sqlTimeout);
                        try {
                            LOG.info("Sending to external Database (executeUpdate) : '" + connectionName + "' SQL '" + sql + "'");
                            preStat.executeUpdate();
                            int nbUpdate = preStat.getUpdateCount();
                            msg = new MessageEvent(MessageEventEnum.ACTION_SUCCESS_EXECUTESQLUPDATE).resolveDescription("NBROWS", String.valueOf(nbUpdate)).resolveDescription("JDBC", connectionName).resolveDescription("SQL", sql);
                        } catch (SQLTimeoutException exception) {
                            LOG.warn(exception.toString());
                            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_TIMEOUT);
                            msg.setDescription(msg.getDescription().replace("%SQL%", sql));
                            msg.setDescription(msg.getDescription().replace("%TIMEOUT%", String.valueOf(sqlTimeout)));
                            msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                        } catch (SQLException exception) {
                            LOG.warn(exception.toString());
                            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_ERROR);
                            msg.setDescription(msg.getDescription().replace("%SQL%", sql));
                            msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                        }
                    } catch (SQLException exception) {
                        LOG.warn(exception.toString());
                        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_ERROR);
                        msg.setDescription(msg.getDescription().replace("%SQL%", sql));
                        msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                    } catch (NullPointerException exception) {
                        LOG.warn(exception.toString());
                        msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_CANNOTACCESSJDBC);
                        msg.setDescription(msg.getDescription().replace("%JDBC%", "jdbc/" + connectionName));
                        msg.setDescription(msg.getDescription().replace("%EX%", exception.toString()));
                    }
                }
            } else {
                msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_DATABASECONFIGUREDBUTJDBCPOOLEMPTY);
                msg.setDescription(msg.getDescription().replace("%SYSTEM%", system));
                msg.setDescription(msg.getDescription().replace("%COUNTRY%", country));
                msg.setDescription(msg.getDescription().replace("%ENV%", environment));
                msg.setDescription(msg.getDescription().replace("%DATABASE%", database));
            }
        } else {
            msg = new MessageEvent(MessageEventEnum.ACTION_FAILED_SQL_DATABASENOTCONFIGURED);
            msg.setDescription(msg.getDescription().replace("%SYSTEM%", system));
            msg.setDescription(msg.getDescription().replace("%COUNTRY%", country));
            msg.setDescription(msg.getDescription().replace("%ENV%", environment));
            msg.setDescription(msg.getDescription().replace("%DATABASE%", database));
        }
    } catch (CerberusException ex) {
        LOG.error(ex.toString());
    }
    return msg;
}
Also used : CerberusException(org.cerberus.exception.CerberusException) SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) Connection(java.sql.Connection) SQLTimeoutException(java.sql.SQLTimeoutException) PreparedStatement(java.sql.PreparedStatement) CountryEnvironmentDatabase(org.cerberus.crud.entity.CountryEnvironmentDatabase)

Example 5 with CountryEnvironmentDatabase

use of org.cerberus.crud.entity.CountryEnvironmentDatabase in project cerberus-source by cerberustesting.

the class UpdateCountryEnvParam method processRequest.

/**
 * 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();
    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);
    PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);
    String charset = request.getCharacterEncoding();
    ICountryEnvironmentDatabaseService cebService = appContext.getBean(ICountryEnvironmentDatabaseService.class);
    ICountryEnvironmentParametersService ceaService = appContext.getBean(ICountryEnvironmentParametersService.class);
    ICountryEnvDeployTypeService cedService = appContext.getBean(ICountryEnvDeployTypeService.class);
    ICountryEnvLinkService celService = appContext.getBean(ICountryEnvLinkService.class);
    response.setContentType("application/json");
    // Calling Servlet Transversal Util.
    ServletUtil.servletStart(request);
    /**
     * Parsing and securing all required parameters.
     */
    // Parameter that are already controled by GUI (no need to decode) --> We SECURE them
    String system = policy.sanitize(request.getParameter("system"));
    String country = policy.sanitize(request.getParameter("country"));
    String environment = policy.sanitize(request.getParameter("environment"));
    String type = policy.sanitize(request.getParameter("type"));
    String chain = policy.sanitize(request.getParameter("chain"));
    boolean maintenanceAct = ParameterParserUtil.parseBooleanParam(request.getParameter("maintenanceAct"), true);
    // Parameter that needs to be secured --> We SECURE+DECODE them
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String maintenanceStr = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("maintenanceStr"), "01:00:00", charset);
    String maintenanceEnd = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("maintenanceEnd"), "01:00:00", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String distribList = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("distribList"), "", charset);
    String eMailBodyRevision = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyRevision"), "", charset);
    String eMailBodyChain = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyChain"), "", charset);
    String eMailBodyDisableEnvironment = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("eMailBodyDisableEnvironment"), "", charset);
    // Getting list of database from JSON Call
    JSONArray objDatabaseArray = new JSONArray(request.getParameter("database"));
    List<CountryEnvironmentDatabase> cebList;
    cebList = getCountryEnvironmentDatabaseFromParameter(request, appContext, system, country, environment, objDatabaseArray);
    // Getting list of application from JSON Call
    JSONArray objApplicationArray = new JSONArray(request.getParameter("application"));
    List<CountryEnvironmentParameters> ceaList;
    ceaList = getCountryEnvironmentApplicationFromParameter(request, appContext, system, country, environment, objApplicationArray);
    // Getting list of database from JSON Call
    JSONArray objDeployTypeArray = new JSONArray(request.getParameter("deployType"));
    List<CountryEnvDeployType> cedList;
    cedList = getCountryEnvironmentDeployTypeFromParameter(request, appContext, system, country, environment, objDeployTypeArray);
    // Getting list of database from JSON Call
    JSONArray objDepArray = new JSONArray(request.getParameter("dependencies"));
    List<CountryEnvLink> celList;
    celList = getCountryEnvironmentLinkFromParameter(request, appContext, system, country, environment, objDepArray);
    // Prepare the final answer.
    MessageEvent msg1 = new MessageEvent(MessageEventEnum.GENERIC_OK);
    Answer finalAnswer = new Answer(msg1);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(system)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "System is missing"));
        ans.setResultMessage(msg);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    } else if (StringUtil.isNullOrEmpty(country)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Country is missing"));
        ans.setResultMessage(msg);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    } else if (StringUtil.isNullOrEmpty(environment)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", OBJECT_NAME).replace("%OPERATION%", "Update").replace("%REASON%", "Environment is missing"));
        ans.setResultMessage(msg);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ICountryEnvParamService cepService = appContext.getBean(ICountryEnvParamService.class);
        AnswerItem resp = cepService.readByKey(system, country, environment);
        if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
            /**
             * Object could not be found. We stop here and report the error.
             */
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) resp);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can update it.
             */
            CountryEnvParam cepData = (CountryEnvParam) resp.getItem();
            cepData.setDescription(description);
            cepData.setDistribList(distribList);
            cepData.seteMailBodyRevision(eMailBodyRevision);
            cepData.setType(type);
            cepData.seteMailBodyChain(eMailBodyChain);
            cepData.seteMailBodyDisableEnvironment(eMailBodyDisableEnvironment);
            if (request.getParameter("maintenanceAct") != null) {
                cepData.setMaintenanceAct(maintenanceAct);
            }
            cepData.setMaintenanceStr(maintenanceStr);
            cepData.setMaintenanceEnd(maintenanceEnd);
            cepData.setChain(chain);
            ans = cepService.update(cepData);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Update was successful. Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/UpdateCountryEnvParam", "UPDATE", "Updated CountryEnvParam : ['" + system + "','" + country + "','" + environment + "']", request);
            }
            // Update the Database with the new list.
            ans = cebService.compareListAndUpdateInsertDeleteElements(system, country, environment, cebList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            // Update the Database with the new list.
            ans = ceaService.compareListAndUpdateInsertDeleteElements(system, country, environment, ceaList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            // Update the Database with the new list.
            ans = cedService.compareListAndUpdateInsertDeleteElements(system, country, environment, cedList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
            // Update the Database with the new list.
            ans = celService.compareListAndUpdateInsertDeleteElements(system, country, environment, celList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        }
    }
    /**
     * 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();
}
Also used : CountryEnvDeployType(org.cerberus.crud.entity.CountryEnvDeployType) IFactoryCountryEnvDeployType(org.cerberus.crud.factory.IFactoryCountryEnvDeployType) PolicyFactory(org.owasp.html.PolicyFactory) MessageEvent(org.cerberus.engine.entity.MessageEvent) JSONArray(org.json.JSONArray) ICountryEnvDeployTypeService(org.cerberus.crud.service.ICountryEnvDeployTypeService) AnswerItem(org.cerberus.util.answer.AnswerItem) ICountryEnvironmentParametersService(org.cerberus.crud.service.ICountryEnvironmentParametersService) Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) CountryEnvLink(org.cerberus.crud.entity.CountryEnvLink) IFactoryCountryEnvLink(org.cerberus.crud.factory.IFactoryCountryEnvLink) ICountryEnvLinkService(org.cerberus.crud.service.ICountryEnvLinkService) IFactoryCountryEnvironmentParameters(org.cerberus.crud.factory.IFactoryCountryEnvironmentParameters) CountryEnvironmentParameters(org.cerberus.crud.entity.CountryEnvironmentParameters) ILogEventService(org.cerberus.crud.service.ILogEventService) ICountryEnvironmentDatabaseService(org.cerberus.crud.service.ICountryEnvironmentDatabaseService) ICountryEnvParamService(org.cerberus.crud.service.ICountryEnvParamService) CountryEnvParam(org.cerberus.crud.entity.CountryEnvParam) CountryEnvironmentDatabase(org.cerberus.crud.entity.CountryEnvironmentDatabase) IFactoryCountryEnvironmentDatabase(org.cerberus.crud.factory.IFactoryCountryEnvironmentDatabase)

Aggregations

CountryEnvironmentDatabase (org.cerberus.crud.entity.CountryEnvironmentDatabase)13 MessageEvent (org.cerberus.engine.entity.MessageEvent)9 CerberusException (org.cerberus.exception.CerberusException)7 ArrayList (java.util.ArrayList)5 IFactoryCountryEnvironmentDatabase (org.cerberus.crud.factory.IFactoryCountryEnvironmentDatabase)5 AnswerItem (org.cerberus.util.answer.AnswerItem)5 Connection (java.sql.Connection)4 SQLException (java.sql.SQLException)4 CerberusEventException (org.cerberus.exception.CerberusEventException)4 JSONObject (org.json.JSONObject)4 PreparedStatement (java.sql.PreparedStatement)3 List (java.util.List)3 AppService (org.cerberus.crud.entity.AppService)3 ICountryEnvironmentDatabaseService (org.cerberus.crud.service.ICountryEnvironmentDatabaseService)3 ResultSet (java.sql.ResultSet)2 SQLTimeoutException (java.sql.SQLTimeoutException)2 IFactoryAppService (org.cerberus.crud.factory.IFactoryAppService)2 AnswerList (org.cerberus.util.answer.AnswerList)2 PolicyFactory (org.owasp.html.PolicyFactory)2 ApplicationContext (org.springframework.context.ApplicationContext)2