List of usage examples for org.json.simple JSONArray JSONArray
JSONArray
From source file:Manager.managerFunctions.java
/** * Processes requests for both HTTP <code>GET</code> and <code>POST</code> * methods./* ww w . j av a 2 s . c o m*/ * * @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 { //json to pass back to our ajax request JSONArray jsonArray = new JSONArray(); Connection con = null; try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); con = DriverManager .getConnection("jdbc:sqlserver://localhost;user=sa;password=nopw;allowMultiQueries=true"); Statement st = con.createStatement(); if (request.getParameter("func").equals("getRev")) { String query = "SELECT * FROM [MatchesFromAbove].[dbo].[DATE] WHERE DATEPART(month, DATE_TIME) = " + request.getParameter("month") + " AND DATEPART(year, DATE_TIME) = " + request.getParameter("year"); ResultSet rs = st.executeQuery(query); //loop through result set and create the json objects while (rs.next()) { JSONObject dateToAdd = new JSONObject(); dateToAdd.put("fee", rs.getString("Fee")); dateToAdd.put("time", rs.getDate("Date_Time").toString()); //add the json object that we're passing into the json array jsonArray.add(dateToAdd); } //set the content type of our response response.setContentType("application/json"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); printout.print(jsonArray); printout.flush(); System.out.println("rev generated"); } if (request.getParameter("func").equals("getRevByDate")) { double totalRev = 0; String[] arrr = request.getParameter("date").split("/"); String query = "SELECT * FROM [MatchesFromAbove].[dbo].[DATE] WHERE DATEPART(month, DATE_TIME) = " + arrr[0] + " AND DATEPART(year, DATE_TIME) = " + arrr[2] + " AND DATEPART(day, DATE_TIME) = " + arrr[1]; ResultSet rs = st.executeQuery(query); //loop through result set and create the json objects while (rs.next()) { totalRev = totalRev + Double.parseDouble(rs.getString("Fee")); JSONObject dateToAdd = new JSONObject(); dateToAdd.put("fee", rs.getString("Fee")); dateToAdd.put("time", rs.getDate("Date_Time").toString()); //add the json object that we're passing into the json array jsonArray.add(dateToAdd); } JSONObject dateToAdd = new JSONObject(); dateToAdd.put("total", totalRev); jsonArray.add(dateToAdd); //set the content type of our response response.setContentType("application/json"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); printout.print(jsonArray); printout.flush(); } if (request.getParameter("func").equals("getRevBySSN")) { double totalRev = 0; String ssn = request.getParameter("SSN"); String query = "SELECT *" + "FROM [MatchesFromAbove].[dbo].[DATE],[MatchesFromAbove].[dbo].[Profile]" + "WHERE ([MatchesFromAbove].[dbo].[DATE].Profile1Id = [MatchesFromAbove].[dbo].[Profile].ProfileId AND [MatchesFromAbove].[dbo].[Profile].OwnerSSN ='" + ssn + "') OR " + "([MatchesFromAbove].[dbo].[DATE].Profile2Id = [MatchesFromAbove].[dbo].[Profile].ProfileId AND [MatchesFromAbove].[dbo].[Profile].OwnerSSN ='" + ssn + "')"; ResultSet rs = st.executeQuery(query); //loop through result set and create the json objects while (rs.next()) { totalRev = totalRev + Double.parseDouble(rs.getString("Fee")); JSONObject dateToAdd = new JSONObject(); dateToAdd.put("fee", rs.getString("Fee")); dateToAdd.put("time", rs.getDate("Date_Time").toString()); //add the json object that we're passing into the json array jsonArray.add(dateToAdd); } JSONObject dateToAdd = new JSONObject(); dateToAdd.put("total", totalRev); jsonArray.add(dateToAdd); //set the content type of our response response.setContentType("application/json"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); printout.print(jsonArray); printout.flush(); } if (request.getParameter("func").equals("getBestRep")) { try { st.execute( "CREATE VIEW booob AS SELECT [MatchesFromAbove].[dbo].[DATE].CustomerRep, SUM([MatchesFromAbove].[dbo].[DATE].Fee) AS sumFee FROM [MatchesFromAbove].[dbo].[DATE] GROUP BY [MatchesFromAbove].[dbo].[DATE].CustomerRep"); } catch (Exception e) { System.out.println("view exists "); st.execute("DROP VIEW booob"); st.execute( "CREATE VIEW booob AS SELECT [MatchesFromAbove].[dbo].[DATE].CustomerRep, SUM([MatchesFromAbove].[dbo].[DATE].Fee) AS sumFee FROM [MatchesFromAbove].[dbo].[DATE] GROUP BY [MatchesFromAbove].[dbo].[DATE].CustomerRep"); } String query = "SELECT * FROM booob"; ResultSet rs = st.executeQuery(query); //loop through result set and create the json objects Double max = 0.0; String name = ""; while (rs.next()) { double b = rs.getDouble("sumFee"); System.out.println(b + " " + rs.getString("CustomerRep")); if (b > max) { max = b; name = rs.getString("CustomerRep"); } } String query2 = "SELECT * FROM PERSON WHERE SSN = '" + name + "'"; ResultSet rs2 = st.executeQuery(query2); while (rs2.next()) { name = (rs2).getString("FirstName") + " " + (rs2).getString("LastName"); } //set the content type of our response response.setContentType("text/html"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); printout.print("NAME: " + name + " Revenue Generated: $" + (long) (max * 100 + 0.5) / 100.0); printout.flush(); } if (request.getParameter("func").equals("getBestCust")) { ResultSet rs = st.executeQuery( " SELECT [MatchesFromAbove].[dbo].[PROFILE].OwnerSSN, SUM([MatchesFromAbove].[dbo].[DATE].Fee) AS sumFee " + " FROM [MatchesFromAbove].[dbo].[PROFILE], [MatchesFromAbove].[dbo].[DATE] " + " WHERE [MatchesFromAbove].[dbo].[DATE].Profile1Id = [MatchesFromAbove].[dbo].[PROFILE].ProfileId " + " GROUP BY OwnerSSN "); Map<String, Integer> my = new HashMap<String, Integer>(); while (rs.next()) { System.out.println("tack"); if (!my.containsKey(rs.getString("OwnerSSN"))) { my.put(rs.getString("OwnerSSN"), rs.getInt("sumFee")); } else { my.put(rs.getString("OwnerSSN"), (rs.getInt("sumFee")) + my.get(rs.getString("OwnerSSN"))); } System.out.println("2"); } ResultSet rs1 = st.executeQuery( " SELECT [MatchesFromAbove].[dbo].[PROFILE].OwnerSSN, SUM([MatchesFromAbove].[dbo].[DATE].Fee) AS sumFee " + " FROM [MatchesFromAbove].[dbo].[PROFILE], [MatchesFromAbove].[dbo].[DATE] " + " WHERE [MatchesFromAbove].[dbo].[DATE].Profile2Id = [MatchesFromAbove].[dbo].[PROFILE].ProfileId " + " GROUP BY OwnerSSN "); System.out.println("lalaland"); if (rs1 != null) while (rs1.next()) { System.out.println("tack"); if (!my.containsKey(rs1.getString("OwnerSSN"))) { my.put(rs1.getString("OwnerSSN"), rs1.getInt("sumFee")); } else { my.put(rs1.getString("OwnerSSN"), (rs1.getInt("sumFee")) + my.get(rs1.getString("OwnerSSN"))); } System.out.println("2"); } String ssn = ""; int max = 0; Iterator it = my.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); System.out.println(pairs.getKey() + " " + pairs.getValue()); if ((Integer) pairs.getValue() > max) { ssn = (String) pairs.getKey(); max = (Integer) pairs.getValue(); } it.remove(); // avoids a ConcurrentModificationException } String query = "SELECT * FROM PERSON WHERE SSN = '" + ssn + "'"; ResultSet t = st.executeQuery(query); t.next(); String name = t.getString("FirstName") + " " + t.getString("LastName"); System.out.println(my); //set the content type of our response response.setContentType("text/html"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); printout.print("NAME: " + name + " Revenue Generated: $" + max); printout.flush(); } if (request.getParameter("func").equals("bestRatedCust")) { String query = "SELECT * FROM Customer C WHERE Rating > 3"; ResultSet rs = st.executeQuery(query); response.setContentType("text/html"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); //loop through result set and create the json objects Double max = 0.0; String name = ""; String rating = ""; while (rs.next()) { name = rs.getString("SSN"); rating = rs.getString("Rating"); String query2 = "SELECT * FROM PERSON WHERE SSN = '" + name + "'"; ResultSet rs2 = st.executeQuery(query2); while (rs2.next()) { name = (rs2).getString("FirstName") + " " + (rs2).getString("LastName"); printout.print("<p>NAME: " + name + " Rating for Customer is: " + rating + "</p>"); } } } if (request.getParameter("func").equals("bestDateDays")) { String query = "SELECT CAST([MatchesFromAbove].[dbo].[DATE].Date_Time AS DATE) as Date, SUM([MatchesFromAbove].[dbo].[DATE].User1Rating+[MatchesFromAbove].[dbo].[DATE].User2Rating) as Score FROM [MatchesFromAbove].[dbo].[PROFILE], [MatchesFromAbove].[dbo].[DATE] GROUP BY CAST([MatchesFromAbove].[dbo].[DATE].Date_Time AS DATE) HAVING (SUM([MatchesFromAbove].[dbo].[DATE].User1Rating+[MatchesFromAbove].[dbo].[DATE].User2Rating) >= 1) ORDER BY Score DESC"; ResultSet rs = st.executeQuery(query); response.setContentType("text/html"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); int i = 0; while (rs.next()) { i++; if (i == 4) { break; } printout.print("<p>RANK: " + i + "________ DAY FOR DATE: " + rs.getString("Date").substring(5) + "________ SCORE FOR THIS DAY: " + rs.getString("Score")); } //set the content type of our response printout.flush(); } if (request.getParameter("func").equals("mostActCust")) { String query; query = "SELECT [MatchesFromAbove].[dbo].[PROFILE].OwnerSSN, COUNT(*) AS numDates\n" + " FROM [MatchesFromAbove].[dbo].[PROFILE], [MatchesFromAbove].[dbo].[DATE]\n" + " WHERE [MatchesFromAbove].[dbo].[DATE].Profile1Id = [MatchesFromAbove].[dbo].[PROFILE].ProfileId\n" + " GROUP BY OwnerSSN\n" + " UNION ALL\n" + " SELECT [MatchesFromAbove].[dbo].[PROFILE].OwnerSSN, COUNT(*) AS numDates\n" + " FROM [MatchesFromAbove].[dbo].[PROFILE], [MatchesFromAbove].[dbo].[DATE]\n" + " WHERE [MatchesFromAbove].[dbo].[DATE].Profile2Id = [MatchesFromAbove].[dbo].[PROFILE].ProfileId\n" + " GROUP BY OwnerSSN"; System.out.println(query); ResultSet rs = st.executeQuery(query); Map<String, Integer> my = new HashMap<String, Integer>(); while (rs.next()) { if (!my.containsKey(rs.getString("OwnerSSN"))) { my.put(rs.getString("OwnerSSN"), 1); } else { my.put(rs.getString("OwnerSSN"), (Integer) my.get(rs.getString("OwnerSSN")) + 1); } } System.out.println(my); query = "SELECT [MatchesFromAbove].[dbo].[PROFILE].OwnerSSN, COUNT(*) AS numLikes\n" + " FROM [MatchesFromAbove].[dbo].[PROFILE], [MatchesFromAbove].[dbo].[LIKES]\n" + " WHERE [MatchesFromAbove].[dbo].[LIKES].LikerId = [MatchesFromAbove].[dbo].[PROFILE].ProfileId\n" + " GROUP BY OwnerSSN"; rs = st.executeQuery(query); while (rs.next()) { if (!my.containsKey(rs.getString("OwnerSSN"))) { my.put(rs.getString("OwnerSSN"), 1); } else { my.put(rs.getString("OwnerSSN"), (Integer) my.get(rs.getString("OwnerSSN")) + 1); } } response.setContentType("application/json"); //printout prints it to our ajax call and it shows up there as data. you can use this data in the success function. PrintWriter printout = response.getWriter(); // String s = " buns"; // while (rs.next()) { // System.out.println("bub"); // s = s+" "+ rs.getString("OwnerSSN"); // // } Iterator it = my.entrySet().iterator(); while (it.hasNext()) { Map.Entry pairs = (Map.Entry) it.next(); JSONObject dateToAdd = new JSONObject(); query = "SELECT * FROM PERSON WHERE SSN = '" + pairs.getKey() + "'"; ResultSet t = st.executeQuery(query); t.next(); dateToAdd.put("name", t.getString("FirstName") + " " + t.getString("LastName")); dateToAdd.put("level", pairs.getValue()); if ((Integer) pairs.getValue() > 1) jsonArray.add(dateToAdd); it.remove(); // avoids a ConcurrentModificationException } printout.print(jsonArray); //set the content type of our response printout.flush(); } con.close(); } catch (Exception e) { System.out.println(e.getMessage() + "managerFuncetionsClass"); if (con != null) try { con.close(); } catch (SQLException ex) { Logger.getLogger(managerFunctions.class.getName()).log(Level.SEVERE, null, ex); } } }
From source file:edu.illinois.cs.cogcomp.wikifier.utils.freebase.MQLQueryWrapper.java
@SuppressWarnings("unchecked") public MQLQueryWrapper(String namespace, String value) { this.value = value; JSONObject obj = new JSONObject(); JSONArray key = new JSONArray(); JSONArray type = new JSONArray(); obj.put("mid", null); obj.put("type", type); JSONObject contents = new JSONObject(); contents.put("namespace", namespace); contents.put("value", QueryMQL.encodeMQL(value)); key.add(contents);//from w ww.ja va 2 s . c o m obj.put("key", key); this.MQLquery = StringEscapeUtils.unescapeJavaScript(obj.toJSONString()); }
From source file:app.model.game.ExeChooserModel.java
public JSONArray listfJSON(String directoryName) { JSONArray files = new JSONArray(); File directory = new File(directoryName); // get all the files from a directory File[] fList = directory.listFiles(); for (File file : fList) { if (file.isFile()) { if (listableFileTypes.contains(FilenameUtils.getExtension(file.getName()).toLowerCase())) { Map f = new LinkedHashMap(); f.put("type", "file"); f.put("name", file.getName()); files.add(f);/* w ww . j a va2 s. c o m*/ } } else if (file.isDirectory()) { Map folder = new LinkedHashMap(); folder.put("type", "folder"); folder.put("name", file.getName()); folder.put("child", listfJSON(file.getAbsolutePath())); files.add(folder); } } return files; }
From source file:control.ProcesoVertimientosServlets.RegistrarTasaRetibutiva.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/* w w w . j av a2 s . c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int codigoProceso = Integer.parseInt(request.getParameter("codigoProceso")); String cargasParam = request.getParameter("cargasParam"); String valorTasaCobrada = request.getParameter("valorTasaCobrada"); String valorTotalTasaPagar = request.getParameter("valorTotalTasaPagar"); Object obj = JSONValue.parse(cargasParam); JSONArray jsonArray = new JSONArray(); jsonArray = (JSONArray) obj; for (int i = 0; i < jsonArray.size(); i++) { //Obtenemos los valores por parametro JSONObject jsonObject = (JSONObject) jsonArray.get(i); String valorTarifa = (String) jsonObject.get("valorTarifa"); String procentajeRemocion = (String) jsonObject.get("procentajeRemocion"); String valorTasa = (String) jsonObject.get("valorTasa"); String valorCarga = (String) jsonObject.get("valorCarga"); String codigoParametro = (String) jsonObject.get("codigoParametro"); try { //Creamos el manager y guardamos la informacion. ProgramarMonitoreo manager = new ProgramarMonitoreo(); manager.registrarTasaRetributiva(codigoProceso, Integer.parseInt(codigoParametro), valorTarifa, procentajeRemocion, valorTasa, valorCarga, valorTasaCobrada, valorTotalTasaPagar); } catch (Exception ex) { } } String codigoParametro = ""; String valorTarifa = request.getParameter("codigoProceso"); String procentajeRemocion = request.getParameter("codigoProceso"); String valorTasa = request.getParameter("codigoProceso"); String valorCarga = request.getParameter("codigoProceso"); }
From source file:com.saludtec.web.UsuariosWeb.java
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); try (PrintWriter out = response.getWriter()) { String servicio = request.getRequestURI().replace("/HCEMed/Usuarios/", ""); switch (servicio) { case "login": try { login(request).writeJSONString(out); } catch (Exception ex) { Logger.getLogger(UsuariosWeb.class.getName()).log(Level.SEVERE, null, ex); }//w w w .j a v a 2 s. c o m break; case "logout": logout(request).writeJSONString(out); break; case "isLogin": verificarSesion(request).writeJSONString(out); break; default: obj = new JSONObject(); objArray = new JSONArray(); obj.put("error", "404 - El servicio " + servicio + " no existe"); objArray.add(obj); objArray.writeJSONString(out); break; } } }
From source file:com.aerothai.database.os.OsService.java
public JSONObject GetOsAt(int id) throws Exception { Connection dbConn = null;/*from ww w .j ava 2 s. c o m*/ JSONObject obj = new JSONObject(); JSONArray objList = new JSONArray(); int no = 1; //obj.put("draw", 2); obj.put("tag", "listat"); obj.put("msg", "error"); obj.put("status", false); try { dbConn = DBConnection.createConnection(); Statement stmt = dbConn.createStatement(); String query = "SELECT * FROM os where idos = " + id; System.out.println(query); ResultSet rs = stmt.executeQuery(query); while (rs.next()) { JSONObject jsonData = new JSONObject(); jsonData.put("id", rs.getInt("idos")); jsonData.put("name", rs.getString("name")); jsonData.put("no", no); objList.add(jsonData); no++; } obj.put("msg", "done"); obj.put("status", true); obj.put("data", objList); } catch (SQLException sqle) { throw sqle; } catch (Exception e) { // TODO Auto-generated catch block if (dbConn != null) { dbConn.close(); } throw e; } finally { if (dbConn != null) { dbConn.close(); } } return obj; }
From source file:control.ProcesoVertimientosServlets.SeleccionarFechasVisita.java
/** * Handles the HTTP <code>POST</code> method. * * @param request servlet request/*from w w w .java 2 s . c o m*/ * @param response servlet response * @throws ServletException if a servlet-specific error occurs * @throws IOException if an I/O error occurs */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { //Parametros que envia el plugin jQuery FullCalendar, estos son enviados en formato TimeStamp String start = request.getParameter("start"); String end = request.getParameter("end"); String clase = request.getParameter("clase"); int codigoProceso = Integer.parseInt(request.getParameter("codigoProceso")); JSONArray jsonArray = new JSONArray(); Calendar fechaInicial = GregorianCalendar.getInstance(); Calendar fechaFinal = GregorianCalendar.getInstance(); /*Se transforaman las fechas de TimeStamp a Calendar*/ fechaInicial.setTimeInMillis((Long.parseLong(start) * 1000)); fechaFinal.setTimeInMillis((Long.parseLong(end) * 1000)); start = fechaInicial.get(GregorianCalendar.DAY_OF_MONTH) + "/" + (fechaInicial.get(GregorianCalendar.MONTH) + 1) + "/" + fechaInicial.get(GregorianCalendar.YEAR); end = fechaFinal.get(GregorianCalendar.DAY_OF_MONTH) + "/" + (fechaFinal.get(GregorianCalendar.MONTH) + 1) + "/" + fechaFinal.get(GregorianCalendar.YEAR); Visitas manager = new Visitas(); jsonArray = manager.getFechasVisitasPorProceso(codigoProceso, start, end, clase); //Armamos la respuesta JSON y la enviamos response.setContentType("application/json"); for (Object jsonObject : jsonArray) { response.getWriter().write(jsonObject.toString()); } } catch (SQLException ex) { // Logger.getLogger(SeleccionarFechasVisitas.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:com.mobicage.rogerthat.form.LongListWidgetResult.java
@SuppressWarnings("unchecked") @Override/*from w w w . j ava2s. co m*/ public JSONObject toJSONObject() { JSONObject obj = new JSONObject(); if (this.values == null) obj.put("values", null); else { JSONArray array = new JSONArray(); for (Long val : this.values) { array.add(val); } obj.put("values", array); } return obj; }
From source file:com.mobicage.rogerthat.form.FloatListWidgetResult.java
@SuppressWarnings("unchecked") @Override/* w ww. ja v a 2s . c o m*/ public JSONObject toJSONObject() { JSONObject obj = new JSONObject(); if (this.values == null) obj.put("values", null); else { JSONArray array = new JSONArray(); for (Double val : this.values) { array.add(val); } obj.put("values", array); } return obj; }
From source file:com.mobicage.rogerthat.form.UnicodeListWidgetResult.java
@SuppressWarnings("unchecked") @Override/* www. j a va 2 s .c om*/ public JSONObject toJSONObject() { JSONObject obj = new JSONObject(); if (this.values == null) obj.put("values", null); else { JSONArray array = new JSONArray(); for (String val : this.values) { array.add(val); } obj.put("values", array); } return obj; }