private void setAvgRating(Complaint c) {
   Float avgRating = 0.0f;
   for (Rating r : c.getRatings().getRatings()) {
     avgRating += r.getValue();
     c.setAverageRating(avgRating / c.getRatings().getRatings().size());
   }
 }
  @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 Rating deleteComplaintRating(Long complaintId, Long objectId) throws Exception {
    final EntityManager em = createEntityManager();
    em.getTransaction().begin();
    Complaint c;
    Rating o = null;
    try {
      c = em.find(Complaint.class, complaintId);
      o = em.find(Rating.class, objectId);
      c.getRatings().getRatings().remove(o);
      em.remove(o);
      setAvgRating(c);
      em.getTransaction().commit();

      return o;
    } finally {
      // em.close();
    }
  }
 private Rating findRating(Complaint c, Long objectId) throws Exception {
   String objectName = "object"; // $NON-NLS-1$
   for (Rating o : c.getRatings().getRatings()) {
     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 Ratings getRatings(Long complaintId, Integer limit, Integer offset, String userId)
     throws Exception {
   offset = ServiceHelper.checkOffset(offset);
   Ratings ratings = new Ratings();
   Complaint complaint = getComplaint(complaintId);
   List<Rating> allRatings = complaint.getRatings().getRatings();
   List<Rating> rl = allRatings;
   if (userId != null) {
     rl = new ArrayList<Rating>();
     for (Rating rating : allRatings) {
       if (userId.equals(rating.getUserId())) {
         rl.add(rating);
       }
     }
   }
   Integer newLimit = ServiceHelper.calcLimit(offset, limit, rl.size());
   ratings.setRatings(rl.subList(offset, offset + newLimit));
   ratings.setOffset(offset);
   ratings.setResult(newLimit);
   ratings.setTotal(rl.size());
   return ratings;
 }
 @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);
 }