Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package restservice; import java.net.URISyntaxException; import java.util.ArrayList; import java.util.Date; import javax.ejb.Stateless; import javax.inject.Inject; import javax.ws.rs.GET; import javax.ws.rs.NotAuthorizedException; import javax.ws.rs.POST; import javax.ws.rs.PUT; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.QueryParam; import javax.ws.rs.core.MediaType; import javax.ws.rs.core.Response; import nl.fontys.pts61a.vps.model.Cartracker; import nl.fontys.pts61a.vps.model.Movement; import nl.fontys.pts61a.vps.service.MovementService; import org.json.simple.JSONArray; import org.json.simple.JSONObject; /** * * @author Jesper */ @Stateless @Path("cartrackers") public class CartrackerFacadeREST { @Inject MovementService service; public CartrackerFacadeREST() { } @GET @Path("{id}") @Produces({ MediaType.APPLICATION_JSON }) public String getAllMovementsByCartracker(@PathParam("id") Long id, @QueryParam("name") String name, @QueryParam("key") String key) throws Exception { if (service.getUser(name, key) == null) { throw new NotAuthorizedException("Not authorized"); } JSONObject returnJson = new JSONObject(); returnJson.put("cartrackerId", id); // TODO: currentID JSONArray jsonarray = new JSONArray(); for (Movement m : service.getMovementsByCartracker(id)) { JSONObject tempObject = new JSONObject(); tempObject.put("movementId", m.getMovementId()); tempObject.put("latitude", m.getLatitude()); tempObject.put("longitude", m.getLongitude()); tempObject.put("distance", m.getDistance()); tempObject.put("date", m.getRegistrationDate().toString()); jsonarray.add(tempObject); } returnJson.put("verplaatsingen", jsonarray); System.out.println(returnJson); return returnJson.toString(); } @GET @Path("upload") public Response toUpload() throws URISyntaxException { java.net.URI location = new java.net.URI("../upload.jsp"); return Response.temporaryRedirect(location).build(); } @GET @Path("create/{amount}") public String createCartrackers(@PathParam("amount") int amount, @QueryParam("name") String name, @QueryParam("key") String key) throws Exception { if (service.getUser(name, key) == null) { throw new NotAuthorizedException("Not authorized"); } for (int i = 0; i < amount; i++) { Cartracker cartracker = new Cartracker(); cartracker.setCartrackId(Integer.toUnsignedLong(i + 1)); cartracker.setLastId(0l); cartracker.setIdCode("hrgrjgf" + i); cartracker.setActive(true); ArrayList<Movement> movements = new ArrayList<>(); Movement m = new Movement(); m.setMovementId(Integer.toUnsignedLong(i + 1)); m.setLatitude(50.0000); m.setLongitude(50.0000); m.setDistance(i); m.setRegistrationDate(new Date()); m.setCartracker(cartracker); movements.add(m); cartracker.setMovements(movements); service.createCartracker(cartracker); service.addMovement(m); } return "Created" + amount; } @GET @Path("getstatus") @Produces({ MediaType.APPLICATION_JSON }) public String getAllCartrackersWithStatus(@QueryParam("name") String name, @QueryParam("key") String key) { if (service.getUser(name, key) == null) { throw new NotAuthorizedException("Not authorized"); } JSONObject returnJson = new JSONObject(); JSONArray jsonarray = new JSONArray(); for (Cartracker c : service.getAllCartrackers()) { JSONObject tempObject = new JSONObject(); tempObject.put("cartrackerId", c.getCartrackId()); if (c.isActive() == true) { tempObject.put("status", "active"); } else { tempObject.put("status", "inactive"); } jsonarray.add(tempObject); } System.out.println(returnJson); return jsonarray.toString(); } //change active/deactive state @GET @Path("/changestate/{id}/{state}") @Produces("text/plain") public String changeState(@PathParam("id") Long id, @PathParam("state") String state, @QueryParam("name") String name, @QueryParam("key") String key) throws Exception { if (service.getUser(name, key) == null) { throw new NotAuthorizedException("Not authorized"); } if (service.changeState(id, state)) { return "cartracker change succesfull"; } else { return "cartracker change failed"; } } }