public Zipcode findZipcode(String zipcode) {
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO dao = new UserDAO();
     return (Zipcode) dao.searchForZipcode(zipcode).get(0);
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public PageList loadUsersForUser(User user, int startingRow, int listSize) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO dao = new UserDAO();
     return dao.searchBasedOnUser(user, startingRow, listSize);
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public User login(String userName, String password) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO dao = new UserDAO();
     return dao.login(userName, PasswordEncryptor.md5(password));
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public PageList findProfiles(User user, int startingRow, int listSize) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO dao = new UserDAO();
     return dao.findProfiles(user.getUserId(), startingRow, listSize);
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public List<TUsermessages> getUserMessages(User user) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     MessageDAO dao = new MessageDAO();
     return dao.getMessagesByUserId(user);
   } catch (Exception e) {
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public int getNewMessageCount(User user) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     MessageDAO dao = new MessageDAO();
     return dao.getNewMessagesCount(user);
   } catch (Exception e) {
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public boolean checkLoginNameAlreadyUsed(String loginName) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO userDao = new UserDAO();
     return userDao.checkLoginNameAlreadyUsed(loginName);
   } catch (Exception e) {
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public User findUserById(int id) throws Exception {
   User user = null;
   Session session = HibernateSessionFactory.getSession();
   try {
     UserDAO dao = new UserDAO();
     user = dao.getUserByUserId(id);
     return user;
   } catch (Exception e) {
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void changeUserPassword(User user, String password) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     UserDAO dao = new UserDAO();
     dao.changeUserPassword(user, password);
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void viewMessage(int messageId) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     MessageDAO dao = new MessageDAO();
     dao.viewMessage(messageId);
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void saveUser(User user) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     UserDAO dao = new UserDAO();
     dao.addUserOrUpdate(user);
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void removeAllUsers() throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     UserDAO dao = new UserDAO();
     dao.removeAllUsers();
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void saveProfile(User user, User targetUser) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     UserDAO dao = new UserDAO();
     if (dao.testForExistingProfiles(user, targetUser).size() == 0) {
       dao.addProfile(user, targetUser);
     }
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void deleteMessages(String[] messageIds) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     MessageDAO dao = new MessageDAO();
     for (int i = 0; i < messageIds.length; i++) {
       dao.deleteMessage(messageIds[i]);
     }
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public PasswordInfo resetUserPassword(User user, String password) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     UserDAO dao = new UserDAO();
     String newPassword = dao.resetUserPassword(user);
     transaction.commit();
     PasswordInfo pi = new PasswordInfo(user.getEmail(), newPassword);
     return pi;
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public Zipcode getZipcodeById(Integer id) throws Exception {
   log.debug("getting Zipcode instance with id: " + id);
   Zipcode instance =
       (Zipcode)
           HibernateSessionFactory.getSession()
               .get("us.ttyl.chance.domain.Zipcode", new Integer(id));
   return instance;
 }
 public void deletePicture(Integer pictureId) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     PictureDAO pictureDao = new PictureDAO();
     List pictures = pictureDao.getPicturesByPictureId(pictureId);
     if (pictures.size() > 0) {
       TPicture picture = (TPicture) pictures.get(0);
       picture.setDeleted(1);
       pictureDao.savePicture(picture);
     }
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public Integer addPicture(User user, String path) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     PictureDAO dao = new PictureDAO();
     TPicture picture = new TPicture();
     picture.setFilenamePath(path);
     picture.setUser(user);
     picture.setMainPagePic(0);
     picture.setDeleted(0);
     picture = dao.savePicture(picture);
     transaction.commit();
     return picture.getPictureId();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public PageList baseSearch(
     int gender,
     int ageStart,
     int ageEnd,
     String zipcode,
     int distance,
     int startIndex,
     int pageSize,
     int userId)
     throws Exception {
   Session session = HibernateSessionFactory.getSession();
   List<Distance> zipcodes = null;
   try {
     UserDAO dao = new UserDAO();
     zipcodes = dao.searchZipcodeDistance(zipcode, distance);
     return dao.searchUserGeneral(
         zipcodes, ageStart, ageEnd, gender, startIndex, pageSize, userId);
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 public void setMainPicture(User user, int pictureId) throws Exception {
   Session session = HibernateSessionFactory.getSession();
   Transaction transaction = session.beginTransaction();
   try {
     // update pictures
     Iterator pictures = user.getPictures().iterator();
     while (pictures.hasNext()) {
       TPicture picture = (TPicture) pictures.next();
       if (picture.getPictureId() == pictureId) {
         picture.setMainPagePic(1);
       } else {
         picture.setMainPagePic(0);
       }
       PictureDAO pictureDao = new PictureDAO();
       pictureDao.savePicture(picture);
     }
     transaction.commit();
   } catch (Exception e) {
     transaction.rollback();
     throw e;
   } finally {
     HibernateSessionFactory.closeSession();
   }
 }
 private List findByProperty(String sql, String propertyName, Object value) throws Exception {
   String queryString = sql + propertyName + "=?";
   Query queryObject = HibernateSessionFactory.getSession().createQuery(queryString);
   queryObject.setParameter(0, value);
   return queryObject.list();
 }
 public List getAllZipcodes() throws Exception {
   log.debug("getting all zipcode instances");
   Session session = HibernateSessionFactory.getSession();
   Query query = session.createQuery("from Zipcode");
   return query.list();
 }
 public State getStateById(Integer id) throws Exception {
   log.debug("getting State instance with id: " + id);
   State instance =
       (State) HibernateSessionFactory.getSession().get("us.ttyl.chance.domain.State", id);
   return instance;
 }
 public Country getCountryById(Integer id) throws Exception {
   log.debug("getting Country instance with id: " + id);
   Country instance =
       (Country) HibernateSessionFactory.getSession().get("us.ttyl.chance.domain.Country", id);
   return instance;
 }
  public Integer createUser(
      Date birthdate,
      TEthnicity ethnicity,
      TSex sex,
      TSex sexLookingFor,
      Integer height,
      TColor eyeColor,
      TColor hairColor,
      TChoice smoke,
      TChoice drink,
      THasChildren haveChildren,
      TWantsChildren wantsMoreChildren,
      TMaritalStatus maritalStatus,
      TBodytype userBodyType,
      String catchphrase,
      String aboutme,
      Integer idealHeightStart,
      Integer idealHeightEnd,
      TChoice idealSmokes,
      TChoice idealDrinks,
      THasChildren idealHasChildren,
      TWantsChildren idealWantsChildren,
      TMaritalStatus idealMaritalStatus,
      Integer termserviceagreement,
      Integer profileStatus,
      Zipcode zipcode,
      String password,
      String username,
      String email,
      List idealEthnicities,
      List idealBodyTypes,
      List idealLookingfor,
      Zipcode idealZipcode,
      Integer idealAgeStart,
      Integer idealAgeEnd,
      Integer idealDistance)
      throws Exception {
    Session session = HibernateSessionFactory.getSession();
    Transaction transaction = session.beginTransaction();
    User user = new User();
    try {
      user.setEthnicity(ethnicity);
      user.setBirthdate(birthdate);
      user.setSex(sex);
      user.setSexLookingFor(sexLookingFor);
      user.setEyeColor(eyeColor);
      user.setHairColor(hairColor);
      user.setHeight(height);
      user.setSmoke(smoke);
      user.setDrink(drink);
      user.setHaveChildern(haveChildren);
      user.setWantChildern(wantsMoreChildren);
      user.setMaritalStatus(maritalStatus);
      user.setBodyType(userBodyType);
      user.setCatchphrase(catchphrase);
      user.setAboutme(aboutme);
      user.setIdealHeightStart(idealHeightStart);
      user.setIdealHeightEnd(idealHeightEnd);
      user.setIdealSmokes(idealSmokes);
      user.setIdealDrinks(idealDrinks);
      user.setIdealHasChildern(idealHasChildren);
      user.setIdealWantsChildern(idealWantsChildren);
      user.setIdealMaritalStatus(idealMaritalStatus);
      user.setTermserviceagreement(termserviceagreement);
      user.setProfileStatus(profileStatus);
      user.setZipcode(zipcode);
      user.setPassword(PasswordEncryptor.md5(password));
      user.setUserName(username);
      user.setEmail(email);
      user.setIdealAgeStart(idealAgeStart);
      user.setIdealAgeEnd(idealAgeEnd);
      user.setIdealDistance(idealDistance);
      user.setIdealZipcode(idealZipcode);
      user.setIdealEthnicities(idealEthnicities);
      user.setIdealBodyTypes(idealBodyTypes);
      user.setIdealLookingfor(idealLookingfor);

      UserDAO dao = new UserDAO();
      dao.addUserOrUpdate(user);

      transaction.commit();
      return user.getUserId();
    } catch (Exception e) {
      transaction.rollback();
      throw e;
    } finally {
      HibernateSessionFactory.closeSession();
    }
  }