Best Cerberus-source code snippet using org.cerberus.servlet.crud.transversaltables.ReadInvariant.findDistinctValuesOfColumn
Source:ReadInvariant.java
...86 JSONObject jsonResponse = new JSONObject();87 String access = request.getParameter("access");88 if (request.getParameter("idName") == null && access != null) {89 if (!Strings.isNullOrEmpty(request.getParameter("columnName"))) {90 answer = findDistinctValuesOfColumn(appContext, request, request.getParameter("columnName"), access);91 jsonResponse = (JSONObject) answer.getItem();92 } else {93 answer = findInvariantList(appContext, access, request, response);94 jsonResponse = (JSONObject) answer.getItem();95 }96 } else if (request.getParameter("value") == null) {97 //loads the list of invariants98 String idName = policy.sanitize(request.getParameter("idName"));99 answer = findInvariantListByIdName(appContext, access, idName);100 jsonResponse = (JSONObject) answer.getItem();101 } else {102 String idName = request.getParameter("idName");103 String value = request.getParameter("value");104 try {105 answer = findInvariantListBykey(appContext, idName, value);106 JSONObject inv = new JSONObject();107 inv = convertInvariantToJSONObject((Invariant) answer.getItem());108 inv.put("hasPermissionsUpdate", invariantService.hasPermissionsUpdate((Invariant) answer.getItem(), request));109 jsonResponse.put("contentTable", inv);110 } catch (CerberusException e) {111 answer = new AnswerItem<>();112 MessageEvent msg = new MessageEvent(MessageEventEnum.ACTION_FAILED);113 answer.setResultMessage(msg);114 }115 }116 jsonResponse.put("messageType", answer.getResultMessage().getMessage().getCodeString());117 jsonResponse.put("message", answer.getResultMessage().getDescription());118 jsonResponse.put("sEcho", echo); //TODO:FN check if this makes sense119 response.getWriter().print(jsonResponse.toString());120 } catch (JSONException e) {121 LOG.warn(e);122 //returns a default error message with the json format that is able to be parsed by the client-side123 response.getWriter().print(AnswerUtil.createGenericErrorAnswer());124 }125 }126 // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">127 /**128 * Handles the HTTP <code>GET</code> method.129 *130 * @param request servlet request131 * @param response servlet response132 * @throws ServletException if a servlet-specific error occurs133 * @throws IOException if an I/O error occurs134 */135 @Override136 protected void doGet(HttpServletRequest request, HttpServletResponse response)137 throws ServletException, IOException {138 processRequest(request, response);139 }140 /**141 * Handles the HTTP <code>POST</code> method.142 *143 * @param request servlet request144 * @param response servlet response145 * @throws ServletException if a servlet-specific error occurs146 * @throws IOException if an I/O error occurs147 */148 @Override149 protected void doPost(HttpServletRequest request, HttpServletResponse response)150 throws ServletException, IOException {151 processRequest(request, response);152 }153 /**154 * Returns a short description of the servlet.155 *156 * @return a String containing servlet description157 */158 @Override159 public String getServletInfo() {160 return "Short description";161 }// </editor-fold>162 private AnswerItem<JSONObject> findInvariantListByIdName(ApplicationContext appContext, String access, String idName) throws JSONException {163 AnswerList<Invariant> answerService;164 AnswerItem<JSONObject> answer = new AnswerItem<>();165 JSONObject object = new JSONObject();166 //finds the list of invariants by idname167 invariantService = appContext.getBean(InvariantService.class);168 answerService = invariantService.readByIdname(idName);169 JSONArray jsonArray = new JSONArray();170 if (answerService.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {171 //if the service returns an OK message then we can get the item and convert it to JSONformat172 for (Invariant inv : answerService.getDataList()) {173 jsonArray.put(convertInvariantToJSONObject(inv));174 }175 }176 object.put("contentTable", jsonArray);177 object.put("iTotalRecords", answerService.getTotalRows());178 object.put("iTotalDisplayRecords", answerService.getTotalRows());179 answer.setItem(object);180 answer.setResultMessage(answerService.getResultMessage());181 return answer;182 }183 private AnswerItem<Invariant> findInvariantListBykey(ApplicationContext appContext, String idName, String value) throws JSONException, CerberusException {184 AnswerItem<Invariant> answer = new AnswerItem<>();185 //finds the list of invariants by idname186 invariantService = appContext.getBean(InvariantService.class);187 answer.setItem(invariantService.convert(invariantService.readByKey(idName, value)));188 MessageEvent msg = new MessageEvent(MessageEventEnum.DATA_OPERATION_OK);189 msg.setDescription(msg.getDescription().replace("%ITEM%", "Invariant").replace("%OPERATION%", "SELECT"));190 answer.setResultMessage(msg);191 return answer;192 }193 private AnswerItem<JSONObject> findInvariantList(ApplicationContext appContext, String access, HttpServletRequest request, HttpServletResponse response) throws JSONException {194 AnswerItem<JSONObject> item = new AnswerItem<>();195 JSONObject jsonResponse = new JSONObject();196 invariantService = appContext.getBean(IInvariantService.class);197 int startPosition = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayStart"), "0"));198 int length = Integer.valueOf(ParameterParserUtil.parseStringParam(request.getParameter("iDisplayLength"), "0"));199 /*int sEcho = Integer.valueOf(request.getParameter("sEcho"));*/200 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");201 int columnToSortParameter = Integer.parseInt(ParameterParserUtil.parseStringParam(request.getParameter("iSortCol_0"), "0"));202 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"),203 "idname,value,sort,description,VeryShortDesc, gp1,gp2,gp3");204 String columnToSort[] = sColumns.split(",");205 String columnName = columnToSort[columnToSortParameter];206 String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");207 AnswerList<Invariant> answerInv;208 List<String> individualLike = new ArrayList<>(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));209 Map<String, List<String>> individualSearch = new HashMap<>();210 for (int a = 0; a < columnToSort.length; a++) {211 if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {212 List<String> search = new ArrayList<>(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));213 if(individualLike.contains(columnToSort[a])) {214 individualSearch.put(columnToSort[a]+":like", search);215 }else {216 individualSearch.put(columnToSort[a], search);217 } }218 }219 if ("PUBLIC".equals(access)) {220 answerInv = invariantService.readByPublicByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);221 } else if ("PRIVATE".equals(access)) {222 answerInv = invariantService.readByPrivateByCriteria(startPosition, length, columnName, sort, searchParameter, individualSearch);223 } else {224 answerInv = invariantService.readByCriteria(startPosition, length, columnName, sort, searchParameter, "");225 }226 JSONArray jsonArray = new JSONArray();227 //boolean userHasPermissions = request.isUserInRole("Integrator"); //TODO:need to chec228 if (answerInv.isCodeEquals(MessageEventEnum.DATA_OPERATION_OK.getCode())) {//the service was able to perform the query, then we should get all values229 for (Invariant inv : answerInv.getDataList()) {230 jsonArray.put(convertInvariantToJSONObject(inv));231 }232 }233 //jsonResponse.put("hasPermissions", userHasPermissions);234 jsonResponse.put("contentTable", jsonArray);235 jsonResponse.put("iTotalRecords", answerInv.getTotalRows());236 jsonResponse.put("iTotalDisplayRecords", answerInv.getTotalRows());237 item.setItem(jsonResponse);238 item.setResultMessage(answerInv.getResultMessage());239 return item;240 }241 private AnswerItem<JSONObject> findDistinctValuesOfColumn(ApplicationContext appContext, HttpServletRequest request, String columnName, String access) throws JSONException {242 AnswerItem<JSONObject> answer = new AnswerItem<>();243 JSONObject object = new JSONObject();244 invariantService = appContext.getBean(IInvariantService.class);245 String searchParameter = ParameterParserUtil.parseStringParam(request.getParameter("sSearch"), "");246 String sColumns = ParameterParserUtil.parseStringParam(request.getParameter("sColumns"), "idname,value,sort,description,VeryShortDesc, gp1,gp2,gp3");247 String columnToSort[] = sColumns.split(",");248 String column = ParameterParserUtil.parseStringParam(request.getParameter("columnName"), "");249 String sort = ParameterParserUtil.parseStringParam(request.getParameter("sSortDir_0"), "asc");250 List<String> individualLike = new ArrayList<>(Arrays.asList(ParameterParserUtil.parseStringParam(request.getParameter("sLike"), "").split(",")));251 Map<String, List<String>> individualSearch = new HashMap<>();252 for (int a = 0; a < columnToSort.length; a++) {253 if (null != request.getParameter("sSearch_" + a) && !request.getParameter("sSearch_" + a).isEmpty()) {254 List<String> search = new ArrayList<>(Arrays.asList(request.getParameter("sSearch_" + a).split(",")));255 if(individualLike.contains(columnToSort[a])) {...
findDistinctValuesOfColumn
Using AI Code Generation
1 public static List<String> findDistinctValuesOfColumn(String column) {2 List<String> result = new ArrayList<String>();3 String query = "select distinct " + column + " from invariant where 1=1";4 try (Connection connection = getConnection();5 PreparedStatement statement = connection.prepareStatement(query);6 ResultSet resultSet = statement.executeQuery();) {7 while (resultSet.next()) {8 result.add(resultSet.getString(column));9 }10 } catch (SQLException e) {11 LOG.error("Unable to execute query : " + query, e);12 }13 return result;14 }15}
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!!