예제 #1
0
 public void updateSchoolFinalRating(SchoolRating schoolRating) {
   String HQL =
       "SELECT COUNT(id) as userCount, SUM(rating) as totalRating from UserRating where school.id = :schoolId";
   HibernateUtil hibernateUtil = new HibernateUtil();
   Session session = hibernateUtil.openSession();
   Query query =
       session.createQuery(HQL).setResultTransformer(Transformers.aliasToBean(TotalRating.class));
   query.setParameter("schoolId", schoolRating.getSchool().getId());
   List<TotalRating> totalRatings = query.list();
   session.flush();
   System.out.println(
       "total rating:"
           + totalRatings.get(0).getTotalRating()
           + " count"
           + totalRatings.get(0).getUserCount());
   if (totalRatings.size() > 0) {
     String sql = "from SchoolRating where school.id =" + schoolRating.getSchool().getId();
     Session sqlseSession = hibernateUtil.openSession();
     Query sqlQuery = sqlseSession.createQuery(sql);
     boolean newRating = sqlQuery.list().size() > 0 ? true : false;
     sqlseSession.flush();
     /*String newhql = "SELECT id from RatingCategoryType";
     Session newsession1 = hibernateUtil.openSession();
     Query newquery1 = newsession1.createQuery(newhql);
     int catSize = newquery1.list().size() > 0 ?newquery1.list().size():0;
     newsession1.flush();*/
     if (newRating) {
       long calculated_rating =
           Math.round(
               totalRatings.get(0).getTotalRating() / totalRatings.get(0).getUserCount() * 100);
       float final_rating = (float) calculated_rating / 100;
       System.out.println("Rating:" + final_rating);
       String updatehql =
           "UPDATE SchoolRating set rating="
               + final_rating
               + " where school.id="
               + schoolRating.getSchool().getId();
       Session newsession = hibernateUtil.openSession();
       newsession.beginTransaction();
       Query newquery = newsession.createQuery(updatehql);
       newquery.executeUpdate();
       newsession.getTransaction().commit();
       newsession.flush();
     } else {
       long calculated_rating =
           Math.round(
               totalRatings.get(0).getTotalRating() / totalRatings.get(0).getUserCount() * 100);
       float final_rating = (float) calculated_rating / 100;
       System.out.println("Rating:" + final_rating);
       schoolRating.setRating(final_rating);
       Session newsession = hibernateUtil.openSession();
       newsession.beginTransaction();
       newsession.save(schoolRating);
       newsession.getTransaction().commit();
       newsession.flush();
     }
   }
 }
예제 #2
0
  public ResponseMessage addSchoolRating(RatingData ratingData) {
    ResponseMessage msg = new ResponseMessage();
    ArrayList<String> errors = new ArrayList<String>();
    if (ratingData.getSchoolId() > 0
        && ratingData.getUserId() > 0
        && ratingData.getRatings().size() > 0) {
      try {
        String HQL =
            "SELECT ur.id as id, ur.ratingCategoryType.id as catid "
                + " FROM UserRating ur WHERE ur.school.id = :schoolId AND ur.userRegistrationInfo.id = :userId ";
        HibernateUtil hibernateUtil = new HibernateUtil();
        Session sess = hibernateUtil.openSession();
        Query query =
            sess.createQuery(HQL)
                .setResultTransformer(Transformers.aliasToBean(Rating.class))
                .setParameter("schoolId", ratingData.getSchoolId())
                .setParameter("userId", ratingData.getUserId());
        List<Rating> ratings = query.list();
        sess.close();

        School school = new School();
        school.setId(ratingData.getSchoolId());
        UserRegistrationInfo userRegistrationInfo = new UserRegistrationInfo();
        userRegistrationInfo.setId(ratingData.getUserId());
        SchoolRating schoolRating = new SchoolRating();
        schoolRating.setSchool(school);
        Session session = hibernateUtil.openSession();
        session.beginTransaction();
        for (int i = 0; i < ratingData.getRatings().size(); i++) {
          UserRating userRating = new UserRating();
          if (ratings.isEmpty() == false) {
            for (int j = 0; j < ratings.size(); j++) {
              if (ratingData.getRatings().get(i).getCatid() == ratings.get(j).getCatid()) {
                userRating.setId(ratings.get(j).getId());
              }
            }
          }
          userRating.setSchool(school);
          userRating.setUserRegistrationInfo(userRegistrationInfo);
          userRating.setRating((float) ratingData.getRatings().get(i).getRating());
          userRating.setAddedDate(new Date());
          RatingCategoryType ratingCategoryType = new RatingCategoryType();
          ratingCategoryType.setId(ratingData.getRatings().get(i).getCatid());
          userRating.setRatingCategoryType(ratingCategoryType);
          session.saveOrUpdate(userRating);
        }
        session.getTransaction().commit();
        session.flush();
        updateSchoolFinalRating(schoolRating);
        msg.setStatus(1);
        msg.setMessage("Rating saved successfully.");
      } catch (Exception e) {
        errors.add(e.getMessage());
        msg.setStatus(0);
        msg.setErrors(errors);
      }
    } else {
      if (ratingData.getSchoolId() <= 0) {
        errors.add("School Id Can not be empty");
      }
      if (ratingData.getUserId() <= 0) {
        errors.add("User Id Can not be empty");
      }
      if (ratingData.getRatings().size() <= 0) {
        errors.add("Rating data can not be empty");
      }
      msg.setStatus(0);
      msg.setErrors(errors);
    }
    return msg;
  }