@Override public Comment addComment(Long complaintId, Comment comment) throws Exception { final EntityManager em = createEntityManager(); try { em.getTransaction().begin(); Complaint c = em.find(Complaint.class, complaintId); comment.setId(0); if (c.getComments() == null) c.setComments(new Complaint.Comments()); c.getComments().getComments().add(comment); em.getTransaction().commit(); } catch (Exception e) { // TODO e.printStackTrace(); } finally { // em.close(); } return comment; }
@Override public Comment deleteComplaintComment(Long complaintId, Long objectId) throws Exception { final EntityManager em = createEntityManager(); Complaint c; Comment o = null; try { c = em.find(Complaint.class, complaintId); o = em.find(Comment.class, objectId); c.getComments().getComments().remove(o); em.remove(o); return o; } finally { // //em.close(); } }
/** * @throws Exception * @see de.fhg.fokus.cm.service.ComplaintService#deleteComplaint(java.lang.String) * @return null */ @Override public Complaint deleteComplaint(Long complaintId) throws Exception { EntityManager em = createEntityManager(); Complaint complaint; try { EntityTransaction tx = em.getTransaction(); tx.begin(); // TODO try to cascade the delete to comments, photos and ratings complaint = em.find(Complaint.class, complaintId); complaint.getComments().getComments().clear(); // // try { // List<Comment> list = complaint.getComments().getComments(); // for (Comment object : list) // this.deleteComplaintComment(complaintId, object.getId()); // } catch (Exception e) { // // no Comments, nothing to do // } // try { // List<Photo> list = complaint.getPhotos().getPhotos(); // for (Photo object : list) // this.deleteComplaintPhoto(complaintId, object.getId()); // } catch (Exception e) { // // no Photos, nothing to do // } // try { // List<Rating> list = complaint.getRatings().getRatings(); // for (Rating object : list) // this.deleteComplaintRating(complaintId, object.getId()); // } catch (Exception e) { // // no Ratings, nothing to do // } String deleteComplaint = "DELETE " + Complaint.class.getName() + " c WHERE c.id = :complaintId"; em.createQuery(deleteComplaint).setParameter("complaintId", complaintId).executeUpdate(); logger.info("complaint [" + complaintId + "] deleted"); // $NON-NLS-1$ //$NON-NLS-2$ tx.commit(); } finally { // em.close(); } return null; }
private Comment findComment(Complaint c, Long objectId) throws Exception { String objectName = "object"; // $NON-NLS-1$ for (Comment o : c.getComments().getComments()) { objectName = o.getClass().getSimpleName(); if (o.getId() == objectId) { logger.debug( objectName + " [" + objectId //$NON-NLS-1$ + "] is attached to complaint [" + c.getId() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ return o; } } throw new Exception( objectName + " [" + objectId //$NON-NLS-1$ + "] is NOT attached to complaint [" + c.getId() + "]"); //$NON-NLS-1$ //$NON-NLS-2$ }
@Override public Comments getComments(Long complaintId, Integer limit, Integer offset, String userId) throws Exception { offset = ServiceHelper.checkOffset(offset); Comments comments = new Comments(); Complaint complaint = getComplaint(complaintId); List<Comment> allComments = complaint.getComments().getComments(); List<Comment> rl = allComments; if (userId != null) { rl = new ArrayList<Comment>(); for (Comment comment : allComments) { if (userId.equals(comment.getUserId())) { rl.add(comment); } } } Integer newLimit = ServiceHelper.calcLimit(offset, limit, rl.size()); comments.setComments(rl.subList(offset, offset + newLimit)); comments.setOffset(offset); comments.setResult(newLimit); comments.setTotal(rl.size()); return comments; }
@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); }