Best Cerberus-source code snippet using org.cerberus.servlet.crud.countryenvironment.ReadSqlLibrary.processRequest
Source:ReadSqlLibrary.java
...61 * @param response servlet response62 * @throws ServletException if a servlet-specific error occurs63 * @throws IOException if an I/O error occurs64 */65 protected void processRequest(HttpServletRequest request, HttpServletResponse response)66 throws ServletException, IOException {67 //Get SqlLibrarys68 String echo = request.getParameter("sEcho");69 String columnName = ParameterParserUtil.parseStringParam(request.getParameter("columnName"), "");70 ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());71 response.setContentType("application/json");72 response.setCharacterEncoding("utf8");73 // Calling Servlet Transversal Util.74 ServletUtil.servletStart(request);75 // Default message to unexpected error.76 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_ERROR_UNEXPECTED);77 msg.setDescription(msg.getDescription().replace("%DESCRIPTION%", ""));78 PolicyFactory policy = Sanitizers.FORMATTING.and(Sanitizers.LINKS);79 /**80 * Parsing and securing all required sqlLibrarys.81 */82 // Nothing to do here as no sqlLibrary to check.83 //84 // Global boolean on the servlet that define if the user has permition to edit and delete object.85 boolean userHasPermissions = request.isUserInRole("TestAdmin");86 // Init Answer with potencial error from Parsing sqlLibrary.87 AnswerItem answer = new AnswerItem<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));88 try {89 JSONObject jsonResponse;90 String system;91 if (request.getParameter("name") == null && Strings.isNullOrEmpty(columnName)) {92 answer = findSqlLibraryList(appContext, userHasPermissions, request);93 jsonResponse = (JSONObject) answer.getItem();94 } else if (!Strings.isNullOrEmpty(columnName)) {95 answer = findDistinctValuesOfColumn(appContext, request, columnName);96 jsonResponse = (JSONObject) answer.getItem();97 } else {98 answer = findSqlLibraryBySystemByKey(request.getParameter("name"), appContext, userHasPermissions);99 jsonResponse = (JSONObject) answer.getItem();100 }101 jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());102 jsonResponse.put("message", answer.getResultMessage().getDescription());103 jsonResponse.put("sEcho", echo);104 response.getWriter().print(jsonResponse.toString());105 } catch (JSONException e) {106 LOG.warn(e);107 //returns a default error message with the json format that is able to be parsed by the client-side108 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());109 } catch (Exception e) {110 LOG.error(e, e);111 }112 }113 private AnswerItem<JSONObject> findSqlLibraryList(ApplicationContext appContext, boolean userHasPermissions, HttpServletRequest request) throws JSONException {114 AnswerItem<JSONObject> item = new AnswerItem<>();115 JSONObject object = new JSONObject();116 sqlLibraryService = appContext.getBean(SqlLibraryService.class);117 int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));118 int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));119 /*int sEcho = Integer.valueOf(request.getParameter("sEcho"));*/120 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");121 int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "2"));122 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "para,valC,valS,descr");123 String columnToSort[] = sColumns.split(",");124 String columnName = columnToSort[columnToSortParameter];125 String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");126 List<String> individualLike = new ArrayList<>(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));127 Map<String, List<String>> individualSearch = new HashMap<>();128 for (int a = 0; a < columnToSort.length; a++) {129 if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {130 List<String> search = new ArrayList<>(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));131 if (individualLike.contains(columnToSort[a])) {132 individualSearch.put(columnToSort[a] + ":like", search);133 } else {134 individualSearch.put(columnToSort[a], search);135 }136 }137 }138 AnswerList<SqlLibrary> resp = sqlLibraryService.readByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);139 JSONArray jsonArray = new JSONArray();140 if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values141 for (SqlLibrary param : resp.getDataList()) {142 jsonArray.put(convertSqlLibraryToJSONObject(param));143 }144 }145 object.put("hasPermissions", userHasPermissions);146 object.put("contentTable", jsonArray);147 object.put("iTotalRecords", resp.getTotalRows());148 object.put("iTotalDisplayRecords", resp.getTotalRows());149 item.setItem(object);150 item.setResultMessage(resp.getResultMessage());151 return item;152 }153 154 private AnswerItem<JSONObject> findSqlLibraryBySystemByKey(String key, ApplicationContext appContext, boolean userHasPermissions) throws JSONException {155 AnswerItem<JSONObject> answer = new AnswerItem<>(new MessageEvent(MessageEventEnum.DATA_OPERATION_OK));156 sqlLibraryService = appContext.getBean(SqlLibraryService.class);157 AnswerItem<SqlLibrary> resp = sqlLibraryService.readByKey(key);158 SqlLibrary p = null;159 if (resp.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values160 p = resp.getItem();161 }162 JSONObject item = convertSqlLibraryToJSONObject(p);163 item.put("hasPermissions", userHasPermissions);164 answer.setItem(item);165 LOG.debug(item.toString());166 return answer;167 }168 private AnswerItem<JSONObject> findDistinctValuesOfColumn(ApplicationContext appContext, HttpServletRequest request, String columnName) throws JSONException {169 AnswerItem<JSONObject> answer = new AnswerItem<>();170 JSONObject object = new JSONObject();171 sqlLibraryService = appContext.getBean(ISqlLibraryService.class);172 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");173 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "para,valC,valS,descr");174 String columnToSort[] = sColumns.split(",");175 List<String> individualLike = new ArrayList<>(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));176 Map<String, List<String>> individualSearch = new HashMap<>();177 for (int a = 0; a < columnToSort.length; a++) {178 if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {179 List<String> search = new ArrayList<>(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));180 if (individualLike.contains(columnToSort[a])) {181 individualSearch.put(columnToSort[a] + ":like", search);182 } else {183 individualSearch.put(columnToSort[a], search);184 }185 }186 }187 AnswerList applicationList = sqlLibraryService.readDistinctValuesByCriteria(searchParameter, individualSearch, columnName);188 object.put("distinctValues", applicationList.getDataList());189 answer.setItem(object);190 answer.setResultMessage(applicationList.getResultMessage());191 return answer;192 }193 private JSONObject convertSqlLibraryToJSONObject(SqlLibrary parameter) throws JSONException {194 Gson gson = new Gson();195 JSONObject result = new JSONObject(gson.toJson(parameter));196 return result;197 }198 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">199 /**200 * Handles the HTTP <code>GET</code> method.201 *202 * @param request servlet request203 * @param response servlet response204 * @throws ServletException if a servlet-specific error occurs205 * @throws IOException if an I/O error occurs206 */207 @Override208 protected void doGet(HttpServletRequest request, HttpServletResponse response)209 throws ServletException, IOException {210 processRequest(request, response);211 }212 /**213 * Handles the HTTP <code>POST</code> method.214 *215 * @param request servlet request216 * @param response servlet response217 * @throws ServletException if a servlet-specific error occurs218 * @throws IOException if an I/O error occurs219 */220 @Override221 protected void doPost(HttpServletRequest request, HttpServletResponse response)222 throws ServletException, IOException {223 processRequest(request, response);224 }225 /**226 * Returns a short description of the servlet.227 *228 * @return a String containing servlet description229 */230 @Override231 public String getServletInfo() {232 return "Short description";233 }// </editor-fold>234}...
processRequest
Using AI Code Generation
1ReadSqlLibrary readSqlLibrary = new ReadSqlLibrary();2String sqlLibraryID = request.getParameter("sqlLibraryID");3SqlLibrary sqlLibrary = readSqlLibrary.readByKey(sqlLibraryID);4String sqlLibraryName = sqlLibrary.getName();5String sqlLibraryDescription = sqlLibrary.getDescription();6String sqlLibraryDatabase = sqlLibrary.getDatabase();7String sqlLibraryQuery = sqlLibrary.getQuery();8String sqlLibraryType = sqlLibrary.getType();9String sqlLibraryGroup = sqlLibrary.getGroup();10String sqlLibraryNature = sqlLibrary.getNature();11String sqlLibraryUsrCreated = sqlLibrary.getUsrCreated();12String sqlLibraryDateCreated = sqlLibrary.getDateCreated();13String sqlLibraryUsrModif = sqlLibrary.getUsrModif();14String sqlLibraryDateModif = sqlLibrary.getDateModif();15String sqlLibrarySystem = sqlLibrary.getSystem();16String sqlLibraryDatabaseTable = sqlLibrary.getDatabaseTable();17String sqlLibraryDatabaseColumn = sqlLibrary.getDatabaseColumn();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!