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 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);
 }