/** * This method set fixed state of data table. * * @param incomingData * @return * @throws Exception */ @POST @Path("/submit") @Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) @Produces(MediaType.APPLICATION_JSON) public Response submitData(String incomingData) throws Exception { String returnString = null; JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); SchemaPostgresDB dao = new SchemaPostgresDB(); try { JSONObject data = new JSONObject(incomingData); System.out.println("jsonData: " + data.toString()); // we store info about how many pages and streams we have in our presentation int http_code = dao.submitData( data.optInt("lastpage"), data.optInt("laststream"), data.optInt("totalstreams")); if (http_code == 200) { jsonObject.put("HTTP_CODE", "200"); jsonObject.put("MSG", "Data has beed succesfully submit"); returnString = jsonArray.put(jsonObject).toString(); } else { return Response.status(500).entity("Unable to erase items").build(); } System.out.println("returnString: " + returnString); } catch (Exception e) { e.printStackTrace(); return Response.status(500).entity("Server was not able to process your request").build(); } return Response.ok(returnString).build(); }
/** * This method allow to insert data to the data table. * * @param incomingData * @return * @throws Exception */ @POST @Path("/insert") @Consumes({MediaType.APPLICATION_FORM_URLENCODED, MediaType.APPLICATION_JSON}) @Produces(MediaType.APPLICATION_JSON) public Response addData(String incomingData) throws Exception { String returnString = null; JSONArray jsonArray = new JSONArray(); JSONObject jsonObject = new JSONObject(); SchemaPostgresDB dao = new SchemaPostgresDB(); try { /* * We can create a new instance and it will accept a JSON string * By doing this, we can now access the data. */ JSONObject data = new JSONObject(incomingData); System.out.println("jsonData: " + data.toString()); /* * Example: * data.optString("stream"); * The optString example above will also return data but if stream does not * exist, it will return a BLANK string. * * data.optString("stream", NULL); * You can add a second parameter, it will return NULL if stream does not * exist. */ int http_code = dao.insertIntoData( data.optInt("stream"), data.optInt("page"), data.optString("content"), data.optInt("jumpToPage"), data.optInt("jumpToStream"), data.optString("desc")); if (http_code == 200) { /* * The put method allows you to add data to a JSONObject. * The first parameter is the KEY (no spaces) * The second parameter is the Value */ jsonObject.put("HTTP_CODE", "200"); jsonObject.put("MSG", "Item has been entered successfully"); /* * When you are dealing with JSONArrays, the put method is used to add * JSONObjects into JSONArray. */ returnString = jsonArray.put(jsonObject).toString(); } else { return Response.status(500).entity("Unable to enter Item").build(); } System.out.println("returnString: " + returnString); } catch (Exception e) { e.printStackTrace(); return Response.status(500).entity("Server was not able to process your request").build(); } return Response.ok(returnString).build(); }