/**
   * @param complaintAbbr
   * @return
   * @throws Exception
   */
  private String getOrderByClause(final String complaintAbbr, String orderBy, Boolean orderDesc)
      throws Exception {
    String ASC = "DESC";
    if (orderDesc == null || orderDesc == false) ASC = "ASC";

    if (orderBy == null || orderBy == "") orderBy = "id";
    Complaint c = new Complaint();
    c.getAverageRating();
    if (orderBy.equals("id")
        || orderBy.equals("averageRating")
        || orderBy.equals("title")
        || orderBy.equals("description")
        || orderBy.equals("userId")
        || orderBy.equals("tags"))
      return " ORDER BY " + complaintAbbr + "." + orderBy + " " + ASC; // $NON-NLS-1$ //$NON-NLS-2$
    else throw new Exception("orderBy=" + orderBy + " is not allowed");
  }
 @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);
 }