Search in sources :

Example 1 with IFactoryCampaign

use of org.cerberus.crud.factory.IFactoryCampaign in project cerberus-source by cerberustesting.

the class CreateCampaign 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
 */
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 = null;
    Answer finalAnswer = new Answer();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    response.setContentType("application/json");
    response.setCharacterEncoding("utf8");
    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("Campaign"), null, charset);
    String notifyStart = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("NotifyStart"), "N", charset);
    String notifyEnd = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("NotifyEnd"), "N", charset);
    String desc = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("Description"), null, charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String distribList = ParameterParserUtil.parseStringParam(request.getParameter("DistribList"), "");
    // String battery = ParameterParserUtil.parseStringParam(request.getParameter("Batteries"), null);
    String parameter = ParameterParserUtil.parseStringParam(request.getParameter("Parameters"), null);
    String label = ParameterParserUtil.parseStringParam(request.getParameter("Labels"), null);
    if (StringUtil.isNullOrEmpty(name)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "Campaign").replace("%OPERATION%", "Create").replace("%REASON%", "Campaign name is missing!"));
        finalAnswer.setResultMessage(msg);
    } else {
        ICampaignService campaignService = appContext.getBean(ICampaignService.class);
        IFactoryCampaign factoryCampaign = appContext.getBean(IFactoryCampaign.class);
        Campaign camp = factoryCampaign.create(0, name, distribList, notifyStart, notifyEnd, desc);
        finalAnswer = campaignService.create(camp);
        if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Adding Log entry.
             */
            ILogEventService logEventService = appContext.getBean(LogEventService.class);
            logEventService.createForPrivateCalls("/CreateCampaign", "CREATE", "Create Campaign : " + camp.getCampaign(), request);
            if (parameter != null) {
                JSONArray parameters = new JSONArray(parameter);
                ICampaignParameterService campaignParameterService = appContext.getBean(ICampaignParameterService.class);
                IFactoryCampaignParameter factoryCampaignParameter = appContext.getBean(IFactoryCampaignParameter.class);
                ans = campaignParameterService.deleteByCampaign(name);
                int i = 0;
                while (i < parameters.length() && ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    JSONArray bat = parameters.getJSONArray(i);
                    CampaignParameter co = factoryCampaignParameter.create(0, bat.getString(0), bat.getString(2), bat.getString(3));
                    ans = campaignParameterService.create(co);
                    i++;
                    if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                        /**
                         * Adding Log entry.
                         */
                        logEventService.createForPrivateCalls("/CreateCampaign", "CREATE", "Update Campaign Parameter : " + co.getCampaign() + ", " + co.getValue(), request);
                    }
                }
            }
            if (label != null) {
                JSONArray labels = new JSONArray(label);
                ICampaignLabelService campaignLabelService = appContext.getBean(ICampaignLabelService.class);
                IFactoryCampaignLabel factoryCampaignLabel = appContext.getBean(IFactoryCampaignLabel.class);
                ArrayList<CampaignLabel> arr = new ArrayList<>();
                for (int i = 0; i < labels.length(); i++) {
                    JSONArray bat = labels.getJSONArray(i);
                    CampaignLabel co = factoryCampaignLabel.create(0, bat.getString(0), Integer.valueOf(bat.getString(2)), request.getRemoteUser(), null, request.getRemoteUser(), null);
                    arr.add(co);
                }
                finalAnswer = campaignLabelService.compareListAndUpdateInsertDeleteElements(name, arr);
                if (finalAnswer.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                    /**
                     * Adding Log entry.
                     */
                    logEventService.createForPrivateCalls("/CreateCampaign", "CREATE", "Create Campaign Label : " + camp.getCampaign(), request);
                }
            }
            if (ans != null && !ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
                finalAnswer = 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 : IFactoryCampaignParameter(org.cerberus.crud.factory.IFactoryCampaignParameter) ICampaignService(org.cerberus.crud.service.ICampaignService) MessageEvent(org.cerberus.engine.entity.MessageEvent) ICampaignParameterService(org.cerberus.crud.service.ICampaignParameterService) JSONArray(org.json.JSONArray) ArrayList(java.util.ArrayList) IFactoryCampaignParameter(org.cerberus.crud.factory.IFactoryCampaignParameter) CampaignParameter(org.cerberus.crud.entity.CampaignParameter) Answer(org.cerberus.util.answer.Answer) IFactoryCampaignLabel(org.cerberus.crud.factory.IFactoryCampaignLabel) ApplicationContext(org.springframework.context.ApplicationContext) Campaign(org.cerberus.crud.entity.Campaign) IFactoryCampaign(org.cerberus.crud.factory.IFactoryCampaign) JSONObject(org.json.JSONObject) IFactoryCampaign(org.cerberus.crud.factory.IFactoryCampaign) ILogEventService(org.cerberus.crud.service.ILogEventService) ICampaignLabelService(org.cerberus.crud.service.ICampaignLabelService) IFactoryCampaignLabel(org.cerberus.crud.factory.IFactoryCampaignLabel) CampaignLabel(org.cerberus.crud.entity.CampaignLabel)

Aggregations

ArrayList (java.util.ArrayList)1 Campaign (org.cerberus.crud.entity.Campaign)1 CampaignLabel (org.cerberus.crud.entity.CampaignLabel)1 CampaignParameter (org.cerberus.crud.entity.CampaignParameter)1 IFactoryCampaign (org.cerberus.crud.factory.IFactoryCampaign)1 IFactoryCampaignLabel (org.cerberus.crud.factory.IFactoryCampaignLabel)1 IFactoryCampaignParameter (org.cerberus.crud.factory.IFactoryCampaignParameter)1 ICampaignLabelService (org.cerberus.crud.service.ICampaignLabelService)1 ICampaignParameterService (org.cerberus.crud.service.ICampaignParameterService)1 ICampaignService (org.cerberus.crud.service.ICampaignService)1 ILogEventService (org.cerberus.crud.service.ILogEventService)1 MessageEvent (org.cerberus.engine.entity.MessageEvent)1 Answer (org.cerberus.util.answer.Answer)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1 ApplicationContext (org.springframework.context.ApplicationContext)1