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 cimitero.rest; import cimitero.dto.CemetryGroundDto; import cimitero.dto.CoordinateDto; import cimitero.dto.ItemWrapper; import cimitero.dto.ResponseDto; import cimitero.dto.TombCoordinateDto; import cimitero.dto.TombRequestDto; import cimitero.entities.TBody; import cimitero.entities.TCemetry; import cimitero.entities.TCemetryGround; import cimitero.entities.TCoordinate; import cimitero.entities.TTomb; import cimitero.entities.TTombRequest; import cimitero.persistence.HibernateUtil; import java.util.ArrayList; import java.util.List; import javax.ejb.Stateless; import javax.ws.rs.Consumes; import javax.ws.rs.Path; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.DELETE; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import org.hibernate.Session; import org.hibernate.criterion.Restrictions; import org.jboss.logging.Logger; /** * REST Web Service * * @author Markus */ @Stateless @Path("/cemetryground") @Consumes({ MediaType.APPLICATION_JSON }) @Produces({ MediaType.APPLICATION_JSON }) public class CemetryGroundRESTService { Logger log = Logger.getLogger(CemetryGroundRESTService.class); public CemetryGroundRESTService() { } // get all cemetry ground entries @GET public ResponseDto getAllCemetryGrounds() { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TCemetryGround> results = session.createCriteria(TCemetryGround.class).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "no entries found"); } else { response.setItems(new ItemWrapper<TCemetryGround>(results)); } session.getTransaction().commit(); return response; } // get cemetry ground by id @GET @Path("{id}") public ResponseDto getCemetryGroundById(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround result = (TCemetryGround) session.get(TCemetryGround.class, id); ResponseDto response = new ResponseDto(true); if (result == null) { response.setOk(false); response.addError(1, "cemetry ground with id " + id + " not found"); } else { List<TCemetryGround> results = new ArrayList<TCemetryGround>(); results.add(result); response.setItems(new ItemWrapper(results)); } session.getTransaction().commit(); return response; } // save or update cemetry ground + set cemetry @POST public ResponseDto updateCemetryGround(CemetryGroundDto cemetryGroundDto) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = new TCemetryGround(cemetryGroundDto.getCemetryGroundId(), cemetryGroundDto.getName(), cemetryGroundDto.getAddress()); TCemetry tmpCemetry = (TCemetry) session.get(TCemetry.class, cemetryGroundDto.getCemetryId()); tmpCemetryGround.setCemetry(tmpCemetry); if (cemetryGroundDto.getCemetryGroundId() == -1) tmpCemetryGround.setCemetryGroundId(null); session.saveOrUpdate(tmpCemetryGround); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } // delete cemetry ground @DELETE @Path("{id}") public ResponseDto removeCemetryGround(@PathParam("id") Integer id) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround result = (TCemetryGround) session.get(TCemetryGround.class, id); session.delete(result); ResponseDto response = new ResponseDto(true); session.getTransaction().commit(); return response; } @GET @Path("/{cemetryGroundId}/tomb") public ResponseDto getAllTombsOfCemetryGround(@PathParam("cemetryGroundId") Integer cemetryGroundId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TTomb> results = session.createCriteria(TTomb.class) .add(Restrictions.eq("cemetryGround.cemetryGroundId", cemetryGroundId)).list(); ResponseDto response = new ResponseDto(true); if (results.isEmpty()) { response.setOk(false); response.addError(1, "cemetry ground with id " + cemetryGroundId + " has no tombs"); } else { response.setItems(new ItemWrapper<TTomb>(results)); } session.getTransaction().commit(); return response; } @GET @Path("/{cemetryGroundId}/body") public ResponseDto getAllBodiesOfCemetryGround(@PathParam("cemetryGroundId") Integer cemetryGroundId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TTomb> tombs = session.createCriteria(TTomb.class) .add(Restrictions.eq("cemetryGround.cemetryGroundId", cemetryGroundId)).list(); ResponseDto response = new ResponseDto(true); if (tombs.isEmpty()) { response.setOk(false); response.addError(1, "cemetry ground with id " + cemetryGroundId + " has no bodies"); } else { List<TBody> bodies = new ArrayList<TBody>(); for (TTomb tomb : tombs) { bodies.addAll(tomb.getBodies()); } response.setItems(new ItemWrapper(bodies)); } session.getTransaction().commit(); return response; } @GET @Path("/{cemetryGroundId}/tombrequest") public ResponseDto getAllTombRequestsOfCemetryGround(@PathParam("cemetryGroundId") Integer cemetryGroundId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); List<TTomb> tombs = session.createCriteria(TTomb.class) .add(Restrictions.eq("cemetryGround.cemetryGroundId", cemetryGroundId)).list(); ResponseDto response = new ResponseDto(true); if (tombs.isEmpty()) { response.setOk(false); response.addError(1, "cemetry ground with id " + cemetryGroundId + " has no tomb requests"); } else { List<TombRequestDto> tombRequests = new ArrayList<TombRequestDto>(); for (TTomb tomb : tombs) { for (TTombRequest request : tomb.getTombRequests()) { tombRequests.add(new TombRequestDto(request.getTombRequestId(), request.getRequestDate(), request.getRequestText(), request.getTomb().getTombId(), request.getTomb().getTombNo(), request.getCustomer().getPersonId())); } } response.setItems(new ItemWrapper(tombRequests)); } session.getTransaction().commit(); return response; } @POST @Path("/{cemetryGroundId}/borders") public ResponseDto setCemetryGroundBorders(@PathParam("cemetryGroundId") Integer cemetryGroundId, List<CoordinateDto> coordinates) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class, cemetryGroundId); ResponseDto response = new ResponseDto(true); if (tmpCemetryGround == null) { response.setOk(false); response.addError(1, "there is no cemetry ground with id " + cemetryGroundId); } else { for (TCoordinate coordToDelete : tmpCemetryGround.getBoundaryCoordinates()) { session.delete(coordToDelete); } List<TCoordinate> coordinatesList = new ArrayList<TCoordinate>(); for (CoordinateDto coordinate : coordinates) { TCoordinate coord = new TCoordinate(coordinate.getX(), coordinate.getY()); coord.setCemetryGroundBoundary(tmpCemetryGround); session.save(coord); coordinatesList.add(coord); } tmpCemetryGround.setBoundaryCoordinates(coordinatesList); } session.getTransaction().commit(); return response; } @GET @Path("/{cemetryGroundId}/borders") public ResponseDto getCemetryGroundBorders(@PathParam("cemetryGroundId") Integer cemetryGroundId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class, cemetryGroundId); ResponseDto response = new ResponseDto(true); if (tmpCemetryGround == null) { response.setOk(false); response.addError(1, "there is no cemetry ground with id " + cemetryGroundId); } else { List<CoordinateDto> coordinatesList = new ArrayList<CoordinateDto>(); for (TCoordinate coordinate : tmpCemetryGround.getBoundaryCoordinates()) { coordinatesList.add(new CoordinateDto(coordinate.getX(), coordinate.getY())); } response.setItems(new ItemWrapper<CoordinateDto>(coordinatesList)); } session.getTransaction().commit(); return response; } @POST @Path("/{cemetryGroundId}/track") public ResponseDto setCemetryGroundTrack(@PathParam("cemetryGroundId") Integer cemetryGroundId, List<CoordinateDto> coordinates) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class, cemetryGroundId); ResponseDto response = new ResponseDto(true); if (tmpCemetryGround == null) { response.setOk(false); response.addError(1, "there is no cemetry ground with id " + cemetryGroundId); } else { for (TCoordinate coordToDelete : tmpCemetryGround.getTrackCoordinates()) { session.delete(coordToDelete); } List<TCoordinate> coordinatesList = new ArrayList<TCoordinate>(); for (CoordinateDto coordinate : coordinates) { TCoordinate coord = new TCoordinate(coordinate.getX(), coordinate.getY()); coord.setCemetryGroundTrack(tmpCemetryGround); session.save(coord); coordinatesList.add(coord); } tmpCemetryGround.setTrackCoordinates(coordinatesList); } session.getTransaction().commit(); return response; } @GET @Path("/{cemetryGroundId}/track") public ResponseDto getCemetryGroundTrack(@PathParam("cemetryGroundId") Integer cemetryGroundId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class, cemetryGroundId); ResponseDto response = new ResponseDto(true); if (tmpCemetryGround == null) { response.setOk(false); response.addError(1, "there is no cemetry ground with id " + cemetryGroundId); } else { List<CoordinateDto> coordinatesList = new ArrayList<CoordinateDto>(); for (TCoordinate coordinate : tmpCemetryGround.getTrackCoordinates()) { coordinatesList.add(new CoordinateDto(coordinate.getX(), coordinate.getY())); } response.setItems(new ItemWrapper<CoordinateDto>(coordinatesList)); } session.getTransaction().commit(); return response; } @POST @Path("/{cemetryGroundId}/tombcoords") public ResponseDto setCemetryGroundTombCoords(@PathParam("cemetryGroundId") Integer cemetryGroundId, List<TombCoordinateDto> coordinates) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class, cemetryGroundId); ResponseDto response = new ResponseDto(true); if (tmpCemetryGround == null) { response.setOk(false); response.addError(1, "there is no cemetry ground with id " + cemetryGroundId); } else { List<TTomb> tombs = tmpCemetryGround.getTombs(); if (!coordinates.isEmpty()) { for (TombCoordinateDto tomb : coordinates) { TTomb found = null; for (TTomb savedTomb : tombs) { if (savedTomb.getTombNo().equals(tomb.getTombNo())) found = savedTomb; } if (found != null) { if (found.getCoordinate() != null) { found.getCoordinate().setX(tomb.getX()); found.getCoordinate().setY(tomb.getY()); found.getCoordinate().setIsDouble(tomb.isIsSupersized()); } else { TCoordinate newCoord = new TCoordinate(tomb.getX(), tomb.getY()); newCoord.setIsDouble(tomb.isIsSupersized()); session.save(newCoord); found.setCoordinate(newCoord); } } else { TCoordinate tmpCoord = new TCoordinate(tomb.getX(), tomb.getY()); tmpCoord.setIsDouble(tomb.isIsSupersized()); session.save(tmpCoord); TTomb newTomb = new TTomb(tomb.getTombNo()); newTomb.setCoordinate(tmpCoord); newTomb.setCemetryGround(tmpCemetryGround); session.save(newTomb); } } } else { if (!tombs.isEmpty()) { for (TTomb tomb : tombs) { if (tomb.getCoordinate() != null) { tomb.getCoordinate().setTomb(null); } } } } } session.getTransaction().commit(); return response; } @GET @Path("/{cemetryGroundId}/tombcoords") public ResponseDto getCemetryGroundTombCoords(@PathParam("cemetryGroundId") Integer cemetryGroundId) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); TCemetryGround tmpCemetryGround = (TCemetryGround) session.get(TCemetryGround.class, cemetryGroundId); ResponseDto response = new ResponseDto(true); if (tmpCemetryGround == null) { response.setOk(false); response.addError(1, "there is no cemetry ground with id " + cemetryGroundId); } else { List<TombCoordinateDto> coordinatesList = new ArrayList<TombCoordinateDto>(); for (TTomb tomb : tmpCemetryGround.getTombs()) { if (tomb.getCoordinate() != null) { coordinatesList.add(new TombCoordinateDto(tomb.getCoordinate().getX(), tomb.getCoordinate().getY(), tomb.getTombNo(), tomb.getCoordinate().isIsDouble())); } } response.setItems(new ItemWrapper<TombCoordinateDto>(coordinatesList)); } session.getTransaction().commit(); return response; } }