@Override public Photo addPhoto(Long complaintId, Photo photo) throws Exception { final EntityManager em = createEntityManager(); try { em.getTransaction().begin(); Complaint c = em.find(Complaint.class, complaintId); photo.setId(0); if (c.getPhotos() == null) c.setPhotos(new Complaint.Photos()); c.getPhotos().getPhotos().add(photo); em.getTransaction().commit(); } catch (Exception e) { // TODO e.printStackTrace(); } finally { // em.close(); } return photo; }
@Override public Complaint updateComplaint(Complaint complaint) throws Exception { final EntityManager em = createEntityManager(); Complaint updateComplaint; try { em.getTransaction().begin(); updateComplaint = em.find(Complaint.class, complaint.getId()); if (updateComplaint != null) { // updating updateComplaint.setAddress(complaint.getAddress()); updateComplaint.setAverageRating(complaint.getAverageRating()); updateComplaint.setCategory(complaint.getCategory()); updateComplaint.setComments(complaint.getComments()); updateComplaint.setCreationTime(complaint.getCreationTime()); updateComplaint.setDescription(complaint.getDescription()); updateComplaint.setGeolocation(complaint.getGeolocation()); updateComplaint.setPhotos(complaint.getPhotos()); updateComplaint.setRatings(complaint.getRatings()); updateComplaint.setStatus(complaint.getStatus()); updateComplaint.setTags(complaint.getTags()); updateComplaint.setTitle(complaint.getTitle()); updateComplaint.setUserId(complaint.getUserId()); em.merge(updateComplaint); logger.debug("complaint [" + complaint.getId() + "] updated"); // $NON-NLS-1$ //$NON-NLS-2$ } else { throw new Exception( "complaint [" + complaint.getId() // $NON-NLS-1$ + "] not found"); //$NON-NLS-1$ } em.getTransaction().commit(); } finally { em.close(); } return refactorComplaint(updateComplaint); }
/** * sets properties comments, ratings and photos to null * * @param c the complete complaint * @return the complaint without comments, ratings and photos */ private Complaint refactorComplaint(Complaint c) { c.setComments(null); c.setRatings(null); c.setPhotos(null); return c; }