/src/java/service/IrrigationEventResource.java

https://github.com/agmip/agmip_ws · Java · 191 lines · 128 code · 17 blank · 46 comment · 10 complexity · 695e3801f27300a00b3e30cc0b3a14d2 MD5 · raw file

  1. package service;
  2. import beans.IrrigationEvent;
  3. import javax.ws.rs.Path;
  4. import javax.ws.rs.GET;
  5. import javax.ws.rs.PUT;
  6. import javax.ws.rs.DELETE;
  7. import javax.ws.rs.Produces;
  8. import javax.ws.rs.Consumes;
  9. import javax.ws.rs.QueryParam;
  10. import javax.ws.rs.DefaultValue;
  11. import javax.ws.rs.core.Context;
  12. import javax.ws.rs.core.UriInfo;
  13. import javax.ws.rs.WebApplicationException;
  14. import javax.persistence.NoResultException;
  15. import javax.persistence.EntityManager;
  16. import beans.IrrigationLevel;
  17. import converter.IrrigationEventConverter;
  18. import com.sun.jersey.api.core.ResourceContext;
  19. /**
  20. *
  21. * @author fonini
  22. */
  23. public class IrrigationEventResource {
  24. @Context
  25. protected ResourceContext resourceContext;
  26. @Context
  27. protected UriInfo uriInfo;
  28. protected Integer id3;
  29. protected Integer id2;
  30. protected Integer id1;
  31. /** Creates a new instance of IrrigationEventResource */
  32. public IrrigationEventResource() {
  33. }
  34. public void setId1(Integer id1) {
  35. this.id1 = id1;
  36. }
  37. public void setId2(Integer id2) {
  38. this.id2 = id2;
  39. }
  40. public void setId3(Integer id3) {
  41. this.id3 = id3;
  42. }
  43. /**
  44. * Get method for retrieving an instance of IrrigationEvents identified by id in XML format.
  45. *
  46. * @param id identifier for the entity
  47. * @return an instance of IrrigationEventsConverter
  48. */
  49. @GET
  50. @Produces({"application/xml", "application/json"})
  51. public IrrigationEventConverter get(@QueryParam("expandLevel")
  52. @DefaultValue("1") int expandLevel) {
  53. PersistenceService persistenceSvc = PersistenceService.getInstance();
  54. try {
  55. persistenceSvc.beginTx();
  56. return new IrrigationEventConverter(getEntity(), uriInfo.getAbsolutePath(), expandLevel);
  57. }
  58. finally {
  59. PersistenceService.getInstance().close();
  60. }
  61. }
  62. /**
  63. * Put method for updating an instance of IrrigationEvents identified by id using XML as the input format.
  64. *
  65. * @param id identifier for the entity
  66. * @param data an IrrigationEventsConverter entity that is deserialized from a XML stream
  67. */
  68. @PUT
  69. @Consumes({"application/xml", "application/json"})
  70. public void put(IrrigationEventConverter data) {
  71. PersistenceService persistenceSvc = PersistenceService.getInstance();
  72. try {
  73. persistenceSvc.beginTx();
  74. EntityManager em = persistenceSvc.getEntityManager();
  75. updateEntity(getEntity(), data.resolveEntity(em));
  76. persistenceSvc.commitTx();
  77. }
  78. finally {
  79. persistenceSvc.close();
  80. }
  81. }
  82. /**
  83. * Delete method for deleting an instance of IrrigationEvents identified by id.
  84. *
  85. * @param id identifier for the entity
  86. */
  87. @DELETE
  88. public void delete() {
  89. PersistenceService persistenceSvc = PersistenceService.getInstance();
  90. try {
  91. persistenceSvc.beginTx();
  92. deleteEntity(getEntity());
  93. persistenceSvc.commitTx();
  94. }
  95. finally {
  96. persistenceSvc.close();
  97. }
  98. }
  99. /**
  100. * Returns an instance of IrrigationEvents identified by id.
  101. *
  102. * @param id identifier for the entity
  103. * @return an instance of IrrigationEvents
  104. */
  105. protected IrrigationEvent getEntity() {
  106. EntityManager em = PersistenceService.getInstance().getEntityManager();
  107. try {
  108. beans.IrrigationEventPK id = new beans.IrrigationEventPK(id1, id2, id3);
  109. return (IrrigationEvent) em.createQuery("SELECT e FROM IrrigationEvent e where e.irrigationEventPK = :irrigationEventPK").setParameter("irrigationEventPK", id).getSingleResult();
  110. }
  111. catch (NoResultException ex) {
  112. throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
  113. }
  114. }
  115. /**
  116. * Updates entity using data from newEntity.
  117. *
  118. * @param entity the entity to update
  119. * @param newEntity the entity containing the new data
  120. * @return the updated entity
  121. */
  122. private IrrigationEvent updateEntity(IrrigationEvent entity, IrrigationEvent newEntity) {
  123. EntityManager em = PersistenceService.getInstance().getEntityManager();
  124. IrrigationLevel irrigationLevels = entity.getIrrigationLevel();
  125. IrrigationLevel irrigationLevelsNew = newEntity.getIrrigationLevel();
  126. entity = em.merge(newEntity);
  127. if (irrigationLevels != null && !irrigationLevels.equals(irrigationLevelsNew)) {
  128. irrigationLevels.getIrrigationEventsCollection().remove(entity);
  129. }
  130. if (irrigationLevelsNew != null && !irrigationLevelsNew.equals(irrigationLevels)) {
  131. irrigationLevelsNew.getIrrigationEventsCollection().add(entity);
  132. }
  133. return entity;
  134. }
  135. /**
  136. * Deletes the entity.
  137. *
  138. * @param entity the entity to deletle
  139. */
  140. private void deleteEntity(IrrigationEvent entity) {
  141. EntityManager em = PersistenceService.getInstance().getEntityManager();
  142. IrrigationLevel irrigationLevels = entity.getIrrigationLevel();
  143. if (irrigationLevels != null) {
  144. irrigationLevels.getIrrigationEventsCollection().remove(entity);
  145. }
  146. em.remove(entity);
  147. }
  148. /**
  149. * Returns a dynamic instance of IrrigationLevelResource used for entity navigation.
  150. *
  151. * @param id identifier for the parent entity
  152. * @return an instance of IrrigationLevelResource
  153. */
  154. @Path("irrigationLevels/")
  155. public IrrigationLevelResource getIrrigationLevelsResource() {
  156. IrrigationLevelsResourceSub irrigationLevelsResourceSub = resourceContext.getResource(IrrigationLevelsResourceSub.class);
  157. irrigationLevelsResourceSub.setParent(getEntity());
  158. return irrigationLevelsResourceSub;
  159. }
  160. public static class IrrigationLevelsResourceSub extends IrrigationLevelResource {
  161. private IrrigationEvent parent;
  162. public void setParent(IrrigationEvent parent) {
  163. this.parent = parent;
  164. }
  165. @Override
  166. protected IrrigationLevel getEntity() {
  167. IrrigationLevel entity = parent.getIrrigationLevel();
  168. if (entity == null) {
  169. throw new WebApplicationException(new Throwable("Resource for " + uriInfo.getAbsolutePath() + " does not exist."), 404);
  170. }
  171. return entity;
  172. }
  173. }
  174. }