Search in sources :

Example 1 with IFactorySqlLibrary

use of org.cerberus.crud.factory.IFactorySqlLibrary 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)

Aggregations

SqlLibrary (org.cerberus.crud.entity.SqlLibrary)1 IFactorySqlLibrary (org.cerberus.crud.factory.IFactorySqlLibrary)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 ISqlLibraryService (org.cerberus.crud.service.ISqlLibraryService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 Answer (org.cerberus.util.answer.Answer)1 JSONObject (org.json.JSONObject)1 ApplicationContext (org.springframework.context.ApplicationContext)1