@Path("/updateRetailer") @POST public javax.ws.rs.core.Response updateRetailer(String retailer_information) throws JSONException { System.out.println(retailer_information); rds api = rds.getInstance(); JSONObject data = new JSONObject(); data.put("success", api.update("retailer", retailer_information)); return javax.ws.rs.core.Response.ok().entity(data).build(); }
@Path("/createUser") @POST public javax.ws.rs.core.Response createUser(String user_information) throws JSONException { System.out.println(user_information); rds api = rds.getInstance(); // api.init(); JSONObject data = new JSONObject(); data.put("success", api.insert("twitter_user", user_information)); return javax.ws.rs.core.Response.ok().entity(data).build(); }
@PUT @Path("/edit") @Consumes(MediaType.APPLICATION_JSON) public void editNote(String inputData) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { if (inputJSON.has("noteID")) { int id = Integer.parseInt(inputJSON.getString("noteID")); editNote(inputData, id); } // w przeciwnym wypadku nic nie robi } }
@PUT @Path("/edit/{id}") @Consumes(MediaType.APPLICATION_JSON) public void editNote(String inputData, @PathParam("id") int id) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { Note newNote = resolveNoteData(inputJSON); if (inputJSON.has("noteID")) inputJSON.remove("noteID"); // na wszelki wypadek - ID podano hibernate.controllers.NotesController notesController = new hibernate.controllers.NotesController(); if (newNote != null) notesController.updateNote(newNote, id); } }
@POST @Path("/create") @Consumes(MediaType.APPLICATION_JSON) public void createNote(String inputData) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { if (inputJSON.has("noteID")) inputJSON.remove("noteID"); // na wszelki wypadek - chcemy generowaƦ ID automatycznie Note newNote = resolveNoteData(inputJSON); hibernate.controllers.NotesController notesController = new hibernate.controllers.NotesController(); if (newNote != null) notesController.addNote(newNote); } }
@Path("/verifyUser") @POST public javax.ws.rs.core.Response verifyUser(String user_information) throws JSONException { System.out.println(user_information); rds api = rds.getInstance(); JSONObject[] result_set = api.retrieve("twitter_user", user_information); System.out.println("returned result_set length: " + result_set.length); for (int i = 0; i < result_set.length; i++) { System.out.println(result_set[i].getString("password")); } String name = null; Double id = null; // verify password boolean success_value = false; if (result_set.length != 0) { String password_db = result_set[0].getString("password"); JSONObject obj = new JSONObject(user_information); String password_fr = obj.getString("password"); success_value = (password_db.equals(password_fr)); name = obj.getString("name"); // get id of user System.out.println(result_set[0]); id = (double) result_set[0].get("user_id"); } // encode JSONObject data = new JSONObject(); data.put("success", success_value); data.put("name", name); data.put("user_id", id); return javax.ws.rs.core.Response.ok().entity(data).build(); }
@POST @Path("/view/{id}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public String viewNote(String inputData, @PathParam("id") int id) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { hibernate.controllers.NotesController notesController = new hibernate.controllers.NotesController(); Note note = notesController.readNote(id); if (note != null) { JSONObject jsonNote = getJSONNote(note); return jsonNote.toString(); } else { return "No subject of ID == " + id + "!"; } } else return "Invalid session!"; }
@DELETE @Path("/{id}") @Consumes(MediaType.APPLICATION_JSON) @Produces("text/plain") public String deleteNote(String inputData, @PathParam("id") int id) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { try { hibernate.controllers.NotesController notesController = new hibernate.controllers.NotesController(); notesController.deleteNote(id); return "OK"; } catch (Exception ex) { return ex.getMessage(); } } else return "Invalid session!"; }
@POST @Path("/view") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public String viewAllNotes(String inputData) { JSONObject inputJSON = new JSONObject(inputData); if ((inputJSON.has("sessionID")) && UsersController.checkLogin(inputJSON.getString("sessionID"))) { hibernate.controllers.NotesController notesController = new hibernate.controllers.NotesController(); List<Note> notes = notesController.readAllNotes(); JSONArray results = new JSONArray(); JSONObject jsonNote; for (Note note : notes) { jsonNote = getJSONNote(note); results.put(jsonNote); } return results.toString(); } else return "Invalid session!"; }
@Path("/verifyRetailer") @POST public javax.ws.rs.core.Response verifyRetailer(String retailer_information) throws JSONException { System.out.println(retailer_information); rds api = rds.getInstance(); JSONObject[] result_set = api.retrieve("retailer", retailer_information); System.out.println("returned result_set length: " + result_set.length); for (int i = 0; i < result_set.length; i++) { System.out.println(result_set[i].getString("password")); } // verify password boolean success_value = false; if (result_set.length != 0) { String password_db = result_set[0].getString("password"); JSONObject obj = new JSONObject(retailer_information); String password_fr = obj.getString("password"); success_value = (password_db.equals(password_fr)); } // encode JSONObject data = new JSONObject(); data.put("success", success_value); return javax.ws.rs.core.Response.ok().entity(data).build(); }
private JSONObject getJSONNote(Note note) { JSONObject jsonNote = new JSONObject(); jsonNote.put("noteID", note.getNoteID()); jsonNote.put("noteTitle", note.getNoteTitle()); jsonNote.put("noteURL", note.getNoteURL()); JSONObject jsonSubject = api.login.SubjectsController.getJSONsubject(note.getSubject()); jsonNote.put("subject", jsonSubject); JSONObject jsonUser = api.login.UsersController.getJSONuser(note.getUser()); jsonNote.put("user", jsonUser); return jsonNote; }
private Note resolveNoteData(JSONObject inputJSON) { if (!((inputJSON.has("noteTitle")) && (inputJSON.has("noteURL")) && (inputJSON.has("subjectID")) && (inputJSON.has("userID")))) { return null; } else { try { hibernate.controllers.SubjectsController subjectsController = new hibernate.controllers.SubjectsController(); Subject subject = subjectsController.readSubject(Integer.parseInt(inputJSON.get("subjectID").toString())); hibernate.controllers.UsersController usersController = new hibernate.controllers.UsersController(); User user = usersController.readUser(Integer.parseInt(inputJSON.get("userID").toString())); return new Note( inputJSON.getString("noteTitle"), inputJSON.getString("noteURL"), subject, user); } catch (NullPointerException ex) { return null; } } }