Search in sources :

Example 6 with SqlLibrary

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

the class CreateSqlLibrary 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);
    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 name = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("name"), null, charset);
    String type = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("type"), null, charset);
    String database = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("database"), null, charset);
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), null, charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String script = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("script"), null, charset);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(name)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "SqlLibrary").replace("%OPERATION%", "Create").replace("%REASON%", "SqlLibrary name is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        ISqlLibraryService sqlLibraryService = appContext.getBean(ISqlLibraryService.class);
        IFactorySqlLibrary factorySqlLibrary = appContext.getBean(IFactorySqlLibrary.class);
        SqlLibrary sqlLib = factorySqlLibrary.create(name, type, database, script, description);
        ans = sqlLibraryService.create(sqlLib);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateSqlLibrary", "CREATE", "Create SQLLibrary : " + name, 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();
}
Also used : Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) MessageEvent(org.cerberus.engine.entity.MessageEvent) ISqlLibraryService(org.cerberus.crud.service.ISqlLibraryService) IFactorySqlLibrary(org.cerberus.crud.factory.IFactorySqlLibrary) SqlLibrary(org.cerberus.crud.entity.SqlLibrary) ILogEventService(org.cerberus.crud.service.ILogEventService) IFactorySqlLibrary(org.cerberus.crud.factory.IFactorySqlLibrary)

Example 7 with SqlLibrary

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

the class DeleteSqlLibrary 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);
    response.setContentType("application/json");
    String charset = request.getCharacterEncoding();
    String name = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("name"), null, charset);
    ISqlLibraryService sqlLibraryService = appContext.getBean(ISqlLibraryService.class);
    /**
     * Checking all constrains before calling the services.
     */
    if (StringUtil.isNullOrEmpty(name)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "SqlLibrary").replace("%OPERATION%", "Delete").replace("%REASON%", "SqlLibrary ID (name) is missing!"));
        ans.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        AnswerItem resp = sqlLibraryService.readByKey(name);
        if (!(resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode()) && resp.getItem() != null)) {
            /**
             * 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%", "SqlLibrary").replace("%OPERATION%", "Delete").replace("%REASON%", "SqlLibrary does not exist."));
            ans.setResultMessage(msg);
        } else {
            /**
             * The service was able to perform the query and confirm the
             * object exist, then we can delete it.
             */
            SqlLibrary sql = (SqlLibrary) resp.getItem();
            ans = sqlLibraryService.delete(sql);
            if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                /**
                 * Adding Log entry.
                 */
                ILogEventService logEventService = appContext.getBean(LogEventService.class);
                logEventService.createForPrivateCalls("/DeleteSqlLibrary", "DELETE", "Delete SQLLibrary : " + name, 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();
}
Also used : Answer(org.cerberus.util.answer.Answer) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) MessageEvent(org.cerberus.engine.entity.MessageEvent) ISqlLibraryService(org.cerberus.crud.service.ISqlLibraryService) SqlLibrary(org.cerberus.crud.entity.SqlLibrary) ILogEventService(org.cerberus.crud.service.ILogEventService) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 8 with SqlLibrary

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

the class ReadSqlLibrary method findSqlLibraryBySystemByKey.

private AnswerItem findSqlLibraryBySystemByKey(String key, ApplicationContext appContext, boolean userHasPermissions) throws JSONException {
    sqlLibraryService = appContext.getBean(SqlLibraryService.class);
    AnswerItem resp = sqlLibraryService.readByKey(key);
    SqlLibrary p = null;
    if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
        // the service was able to perform the query, then we should get all values
        p = (SqlLibrary) resp.getItem();
    }
    JSONObject item = convertSqlLibraryToJSONObject(p);
    item.put("hasPermissions", userHasPermissions);
    resp.setItem(item);
    return resp;
}
Also used : JSONObject(org.json.JSONObject) SqlLibraryService(org.cerberus.crud.service.impl.SqlLibraryService) ISqlLibraryService(org.cerberus.crud.service.ISqlLibraryService) SqlLibrary(org.cerberus.crud.entity.SqlLibrary) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 9 with SqlLibrary

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

the class FactorySqlLibrary method create.

@Override
public SqlLibrary create(String name, String type, String database, String script, String description) {
    SqlLibrary sqlLibrary = new SqlLibrary();
    sqlLibrary.setName(name);
    sqlLibrary.setType(type);
    sqlLibrary.setDatabase(database);
    sqlLibrary.setScript(script);
    sqlLibrary.setDescription(description);
    return sqlLibrary;
}
Also used : IFactorySqlLibrary(org.cerberus.crud.factory.IFactorySqlLibrary) SqlLibrary(org.cerberus.crud.entity.SqlLibrary)

Example 10 with SqlLibrary

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

the class SqlLibraryDAO method findAllSqlLibrary.

@Override
public List<SqlLibrary> findAllSqlLibrary() {
    List<SqlLibrary> list = null;
    final String query = "SELECT * FROM SqlLibrary";
    Connection connection = this.databaseSpring.connect();
    try {
        PreparedStatement preStat = connection.prepareStatement(query);
        try {
            ResultSet resultSet = preStat.executeQuery();
            list = new ArrayList<SqlLibrary>();
            try {
                while (resultSet.next()) {
                    list.add(this.loadSqlLibraryFromResultSet(resultSet));
                }
            } catch (SQLException exception) {
                LOG.warn("Unable to execute query : " + exception.toString());
            } finally {
                resultSet.close();
            }
        } catch (SQLException exception) {
            LOG.warn("Unable to execute query : " + exception.toString());
        } finally {
            preStat.close();
        }
    } catch (SQLException exception) {
        LOG.warn("Unable to execute query : " + exception.toString());
    } finally {
        try {
            if (connection != null) {
                connection.close();
            }
        } catch (SQLException e) {
            LOG.warn(e.toString());
        }
    }
    return list;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SqlLibrary(org.cerberus.crud.entity.SqlLibrary) IFactorySqlLibrary(org.cerberus.crud.factory.IFactorySqlLibrary) FactorySqlLibrary(org.cerberus.crud.factory.impl.FactorySqlLibrary) PreparedStatement(java.sql.PreparedStatement)

Aggregations

SqlLibrary (org.cerberus.crud.entity.SqlLibrary)13 IFactorySqlLibrary (org.cerberus.crud.factory.IFactorySqlLibrary)7 ISqlLibraryService (org.cerberus.crud.service.ISqlLibraryService)6 JSONObject (org.json.JSONObject)6 Connection (java.sql.Connection)5 PreparedStatement (java.sql.PreparedStatement)5 ResultSet (java.sql.ResultSet)5 SQLException (java.sql.SQLException)5 FactorySqlLibrary (org.cerberus.crud.factory.impl.FactorySqlLibrary)5 MessageEvent (org.cerberus.engine.entity.MessageEvent)5 AnswerItem (org.cerberus.util.answer.AnswerItem)5 ApplicationContext (org.springframework.context.ApplicationContext)4 ILogEventService (org.cerberus.crud.service.ILogEventService)3 CerberusException (org.cerberus.exception.CerberusException)3 Answer (org.cerberus.util.answer.Answer)3 ArrayList (java.util.ArrayList)2 SqlLibraryService (org.cerberus.crud.service.impl.SqlLibraryService)2 MessageGeneral (org.cerberus.engine.entity.MessageGeneral)2 AnswerList (org.cerberus.util.answer.AnswerList)2 PrintWriter (java.io.PrintWriter)1