Search in sources :

Example 1 with AppServiceContent

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

the class CreateAppService 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 = 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 service = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("service"), null, charset);
    String group = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("group"), "", charset);
    String description = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("description"), "", charset);
    String attachementurl = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("attachementurl"), "", charset);
    String operation = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("operation"), "", charset);
    String application = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("application"), null, charset);
    String type = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("type"), "", charset);
    String method = ParameterParserUtil.parseStringParamAndDecodeAndSanitize(request.getParameter("method"), "", charset);
    // Parameter that we cannot secure as we need the html --> We DECODE them
    String servicePath = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("servicePath"), "", charset);
    String serviceRequest = ParameterParserUtil.parseStringParamAndDecode(request.getParameter("serviceRequest"), null, charset);
    // 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(service)) {
        msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_EXPECTED);
        msg.setDescription(msg.getDescription().replace("%ITEM%", "SoapLibrary").replace("%OPERATION%", "Create").replace("%REASON%", "SoapLibrary name is missing!"));
        finalAnswer.setResultMessage(msg);
    } else {
        /**
         * All data seems cleans so we can call the services.
         */
        appServiceService = appContext.getBean(IAppServiceService.class);
        appServiceFactory = appContext.getBean(IFactoryAppService.class);
        appServiceHeaderService = appContext.getBean(IAppServiceHeaderService.class);
        appServiceContentService = appContext.getBean(IAppServiceContentService.class);
        appServiceContentFactory = appContext.getBean(IFactoryAppServiceContent.class);
        appServiceHeaderFactory = appContext.getBean(IFactoryAppServiceHeader.class);
        AppService appService = appServiceFactory.create(service, type, method, application, group, serviceRequest, description, servicePath, attachementurl, operation, request.getRemoteUser(), null, null, null);
        ans = appServiceService.create(appService);
        finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        if (ans.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {
            /**
             * Adding Log entry.
             */
            logEventService = appContext.getBean(ILogEventService.class);
            logEventService.createForPrivateCalls("/CreateAppService", "CREATE", "Create AppService : " + service, request);
        }
        // Update content
        if (request.getParameter("contentList") != null) {
            JSONArray objContentArray = new JSONArray(request.getParameter("contentList"));
            List<AppServiceContent> contentList = new ArrayList();
            contentList = getContentListFromRequest(request, appContext, service, objContentArray);
            // Update the Database with the new list.
            ans = appServiceContentService.compareListAndUpdateInsertDeleteElements(service, contentList);
            finalAnswer = AnswerUtil.agregateAnswer(finalAnswer, (Answer) ans);
        }
        // Update header
        if (request.getParameter("headerList") != null) {
            JSONArray objHeaderArray = new JSONArray(request.getParameter("headerList"));
            List<AppServiceHeader> headerList = new ArrayList();
            headerList = getHeaderListFromRequest(request, appContext, service, objHeaderArray);
            // Update the Database with the new list.
            ans = appServiceHeaderService.compareListAndUpdateInsertDeleteElements(service, headerList);
            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 : IFactoryAppService(org.cerberus.crud.factory.IFactoryAppService) AppService(org.cerberus.crud.entity.AppService) MessageEvent(org.cerberus.engine.entity.MessageEvent) IAppServiceHeaderService(org.cerberus.crud.service.IAppServiceHeaderService) JSONArray(org.json.JSONArray) IFactoryAppServiceContent(org.cerberus.crud.factory.IFactoryAppServiceContent) AppServiceContent(org.cerberus.crud.entity.AppServiceContent) ArrayList(java.util.ArrayList) IFactoryAppServiceHeader(org.cerberus.crud.factory.IFactoryAppServiceHeader) AppServiceHeader(org.cerberus.crud.entity.AppServiceHeader) IAppServiceService(org.cerberus.crud.service.IAppServiceService) IFactoryAppServiceHeader(org.cerberus.crud.factory.IFactoryAppServiceHeader) Answer(org.cerberus.util.answer.Answer) IFactoryAppService(org.cerberus.crud.factory.IFactoryAppService) ApplicationContext(org.springframework.context.ApplicationContext) JSONObject(org.json.JSONObject) IAppServiceContentService(org.cerberus.crud.service.IAppServiceContentService) IFactoryAppServiceContent(org.cerberus.crud.factory.IFactoryAppServiceContent) ILogEventService(org.cerberus.crud.service.ILogEventService)

Example 2 with AppServiceContent

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

the class AppServiceContentDAO method readByKey.

@Override
public AnswerItem<AppServiceContent> readByKey(String service, String key) {
    AnswerItem ans = new AnswerItem();
    AppServiceContent result = null;
    final String query = "SELECT * FROM `appservicecontent` src WHERE `service` = ? and `key` = ?";
    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.service : " + service);
        LOG.debug("SQL.param.key : " + key);
    }
    try (Connection connection = this.databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query)) {
        preStat.setString(1, service);
        preStat.setString(2, key);
        try (ResultSet resultSet = preStat.executeQuery()) {
            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()));
        }
    } 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()));
    }
    // sets the message
    ans.setResultMessage(msg);
    return ans;
}
Also used : SQLException(java.sql.SQLException) MessageEvent(org.cerberus.engine.entity.MessageEvent) IFactoryAppServiceContent(org.cerberus.crud.factory.IFactoryAppServiceContent) FactoryAppServiceContent(org.cerberus.crud.factory.impl.FactoryAppServiceContent) AppServiceContent(org.cerberus.crud.entity.AppServiceContent) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AnswerItem(org.cerberus.util.answer.AnswerItem)

Example 3 with AppServiceContent

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

the class AppServiceContentDAO method readByVariousByCriteria.

@Override
public AnswerList<AppServiceContent> readByVariousByCriteria(String service, String active, int start, int amount, String column, String dir, String searchTerm, Map<String, List<String>> individualSearch) {
    AnswerList response = new AnswerList();
    MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);
    msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));
    List<AppServiceContent> objectList = new ArrayList<AppServiceContent>();
    StringBuilder searchSQL = new StringBuilder();
    List<String> individalColumnSearchValues = new ArrayList<String>();
    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 appservicecontent src ");
    searchSQL.append(" where 1=1 ");
    if (!StringUtil.isNullOrEmpty(searchTerm)) {
        searchSQL.append(" and (src.`service` like ?");
        searchSQL.append(" or src.`key` like ?");
        searchSQL.append(" or src.`value` like ?");
        searchSQL.append(" or src.`sort` like ?");
        searchSQL.append(" or src.`active` like ?");
        searchSQL.append(" or src.`usrCreated` like ?");
        searchSQL.append(" or src.`usrModif` like ?");
        searchSQL.append(" or src.`dateCreated` like ?");
        searchSQL.append(" or src.`dateModif` like ?");
        searchSQL.append(" or src.`description` like ?)");
    }
    if (individualSearch != null && !individualSearch.isEmpty()) {
        searchSQL.append(" and ( 1=1 ");
        for (Map.Entry<String, List<String>> entry : individualSearch.entrySet()) {
            searchSQL.append(" and ");
            searchSQL.append(SqlUtil.getInSQLClauseForPreparedStatement(entry.getKey(), entry.getValue()));
            individalColumnSearchValues.addAll(entry.getValue());
        }
        searchSQL.append(" )");
    }
    if (!StringUtil.isNullOrEmpty(service)) {
        searchSQL.append(" and (`service` = ? )");
    }
    if (!StringUtil.isNullOrEmpty(active)) {
        searchSQL.append(" and (`active` = ? )");
    }
    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());
    }
    try (Connection connection = this.databaseSpring.connect();
        PreparedStatement preStat = connection.prepareStatement(query.toString());
        Statement stm = connection.createStatement()) {
        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 + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
            preStat.setString(i++, "%" + searchTerm + "%");
        }
        for (String individualColumnSearchValue : individalColumnSearchValues) {
            preStat.setString(i++, individualColumnSearchValue);
        }
        if (!StringUtil.isNullOrEmpty(service)) {
            preStat.setString(i++, service);
        }
        if (!StringUtil.isNullOrEmpty(active)) {
            preStat.setString(i++, active);
        }
        try (ResultSet resultSet = preStat.executeQuery();
            ResultSet rowSet = stm.executeQuery("SELECT FOUND_ROWS()")) {
            // gets the data
            while (resultSet.next()) {
                objectList.add(this.loadFromResultSet(resultSet));
            }
            // get the total number of rows
            int nrTotalRows = 0;
            if (rowSet != null && rowSet.next()) {
                nrTotalRows = rowSet.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()));
        }
    } 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()));
    }
    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) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) IFactoryAppServiceContent(org.cerberus.crud.factory.IFactoryAppServiceContent) FactoryAppServiceContent(org.cerberus.crud.factory.impl.FactoryAppServiceContent) AppServiceContent(org.cerberus.crud.entity.AppServiceContent) ArrayList(java.util.ArrayList) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) ResultSet(java.sql.ResultSet) AnswerList(org.cerberus.util.answer.AnswerList) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Example 4 with AppServiceContent

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

the class FactoryAppServiceContent method create.

@Override
public AppServiceContent create(String service, String key, String value, String active, int sort, String description, String usrCreated, Timestamp dateCreated, String usrModif, Timestamp dateModif) {
    AppServiceContent newObject = new AppServiceContent();
    newObject.setService(service);
    newObject.setKey(key);
    newObject.setValue(value);
    newObject.setActive(active);
    newObject.setSort(sort);
    newObject.setDescription(description);
    newObject.setUsrCreated(usrCreated);
    newObject.setUsrModif(usrModif);
    newObject.setDateCreated(dateCreated);
    newObject.setDateModif(dateModif);
    return newObject;
}
Also used : IFactoryAppServiceContent(org.cerberus.crud.factory.IFactoryAppServiceContent) AppServiceContent(org.cerberus.crud.entity.AppServiceContent)

Example 5 with AppServiceContent

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

the class FactoryAppServiceContent method create.

@Override
public AppServiceContent create(String service) {
    AppServiceContent newObject = new AppServiceContent();
    newObject.setService(service);
    return newObject;
}
Also used : IFactoryAppServiceContent(org.cerberus.crud.factory.IFactoryAppServiceContent) AppServiceContent(org.cerberus.crud.entity.AppServiceContent)

Aggregations

AppServiceContent (org.cerberus.crud.entity.AppServiceContent)14 ArrayList (java.util.ArrayList)9 IFactoryAppServiceContent (org.cerberus.crud.factory.IFactoryAppServiceContent)8 MessageEvent (org.cerberus.engine.entity.MessageEvent)7 AppService (org.cerberus.crud.entity.AppService)6 AppServiceHeader (org.cerberus.crud.entity.AppServiceHeader)6 AnswerItem (org.cerberus.util.answer.AnswerItem)5 IFactoryAppService (org.cerberus.crud.factory.IFactoryAppService)4 JSONObject (org.json.JSONObject)4 IFactoryAppServiceHeader (org.cerberus.crud.factory.IFactoryAppServiceHeader)3 CerberusException (org.cerberus.exception.CerberusException)3 Connection (java.sql.Connection)2 PreparedStatement (java.sql.PreparedStatement)2 ResultSet (java.sql.ResultSet)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 FactoryAppServiceContent (org.cerberus.crud.factory.impl.FactoryAppServiceContent)2 IAppServiceContentService (org.cerberus.crud.service.IAppServiceContentService)2 IAppServiceHeaderService (org.cerberus.crud.service.IAppServiceHeaderService)2 IAppServiceService (org.cerberus.crud.service.IAppServiceService)2