@Override public Rating addRating(Long complaintId, Rating rating) throws Exception { final EntityManager em = createEntityManager(); try { em.getTransaction().begin(); Complaint c = em.find(Complaint.class, complaintId); rating.setId(0); if (c.getRatings() == null) c.setRatings(new Complaint.Ratings()); c.getRatings().getRatings().add(rating); setAvgRating(c); em.getTransaction().commit(); } catch (Exception e) { // TODO e.printStackTrace(); } finally { // em.close(); } return rating; }
@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; }