public List<SchoolReview> getSchoolReviews(int id) { HibernateUtil hibernateUtil = new HibernateUtil(); Session session = hibernateUtil.getSessionFactory().openSession(); String hql = "FROM SchoolReview where school.id = :id"; Query query = session.createQuery(hql); query.setParameter("id", id); List<SchoolReview> reviews = query.list(); session.close(); List<SchoolReview> schoolReviews = new ArrayList<SchoolReview>(); for (int i = 0; i < reviews.size(); i++) { UserRegistrationInfo userRegistrationInfo = new UserRegistrationInfo(); userRegistrationInfo.setId(reviews.get(i).getUserRegistrationInfo().getId()); userRegistrationInfo.setFirstName(reviews.get(i).getUserRegistrationInfo().getFirstName()); userRegistrationInfo.setLastName(reviews.get(i).getUserRegistrationInfo().getLastName()); userRegistrationInfo.setImage(reviews.get(i).getUserRegistrationInfo().getImage()); SchoolReview schoolReview = new SchoolReview(); schoolReview.setId(reviews.get(i).getId()); schoolReview.setReview(reviews.get(i).getReview()); schoolReview.setDate(reviews.get(i).getDate()); schoolReview.setTime(reviews.get(i).getTime()); schoolReview.setTitle(reviews.get(i).getTitle()); schoolReview.setUserRegistrationInfo(userRegistrationInfo); schoolReviews.add(schoolReview); } return schoolReviews; }
public ResponseMessage shortlistSchool(Integer schoolId, Integer userId) { ResponseMessage responseMessage = new ResponseMessage(); ArrayList<String> errors = new ArrayList<String>(); if (schoolId != null && userId != null) { String hql = " FROM ShortListedSchool sls " + " WHERE sls.school.id = :schoolId AND sls.userRegistrationInfo.id = :userId"; HibernateUtil hibernateUtil = new HibernateUtil(); Session session = hibernateUtil.openSession(); Query query = session .createQuery(hql) .setParameter("schoolId", schoolId) .setParameter("userId", userId); ShortListedSchool shortListedSchool = (ShortListedSchool) query.uniqueResult(); session.close(); try { Session newSession = hibernateUtil.openSession(); newSession.beginTransaction(); if (shortListedSchool != null) { newSession.delete(shortListedSchool); responseMessage.setStatus(1); responseMessage.setMessage("School delisted successfully."); } else { shortListedSchool = new ShortListedSchool(); School school = new School(); school.setId(schoolId); UserRegistrationInfo userRegistrationInfo = new UserRegistrationInfo(); userRegistrationInfo.setId(userId); shortListedSchool.setSchool(school); shortListedSchool.setUserRegistrationInfo(userRegistrationInfo); newSession.save(shortListedSchool); responseMessage.setStatus(1); responseMessage.setMessage("School shortlisted successfully."); } newSession.getTransaction().commit(); newSession.flush(); newSession.close(); } catch (Exception e) { errors.add(e.getMessage()); responseMessage.setErrors(errors); responseMessage.setStatus(0); responseMessage.setMessage("Failed to shortlist/delist school."); } } else { errors.add("School id and user id required."); responseMessage.setErrors(errors); responseMessage.setStatus(0); responseMessage.setMessage("Failed to shortlist/delist school."); } return responseMessage; }
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; }