示例#1
0
 public UsersVO getUsers(String userId) throws Exception {
   UsersVO objUsersVO = null;
   Criteria objCriteria = null;
   Session objSession = null;
   try {
     logger.info("GET USER DETAILS OF " + userId);
     objSession = HibernateUtil.getSession();
     objCriteria = objSession.createCriteria(UsersVO.class);
     objCriteria.add(Restrictions.idEq(userId));
     List lstCust = objCriteria.list();
     if (lstCust.size() > 0) {
       objUsersVO = (UsersVO) lstCust.get(0);
       objUsersVO.setPassword(this.decrypt(objUsersVO.getPassword()));
     }
     logger.info("GET USERS ENDS");
   } catch (HibernateException e) {
     logger.error("HIBERNATE EXCEPTION DURING GET A USER", e);
     e.printStackTrace();
     throw new HibernateException(e.toString());
   } catch (Exception e) {
     logger.error("GENERAL EXCEPTION DURING GET A USER", e);
     e.printStackTrace();
     throw new Exception(e.toString());
   } finally {
     if (objSession != null) {
       objSession.close();
     }
   }
   return objUsersVO;
 }
示例#2
0
 public boolean Authentication(String userId, String password) throws Exception {
   if (BuildConfig.DMODE) System.out.println("USER AUTHENTICATION - STARTS");
   boolean result = false;
   Session objSession = null;
   try {
     logger.info("AUTHENTICATING USER " + userId);
     objSession = HibernateUtil.getSession();
     UsersVO objUsersVO = this.getUsers(userId);
     if (objUsersVO != null && objUsersVO.getPassword().equals(password)) {
       result = true;
       logger.info("USER IS AUTHENTICATED");
     } else {
       logger.info("USER IS NOT AUTHENTICATED");
     }
   } catch (Exception sqle) {
     logger.error("GENERAL EXCEPTION DURING AUTHENTICATE THE USER", sqle);
     sqle.printStackTrace();
     throw new Exception(sqle.toString());
   } finally {
     if (objSession != null) {
       objSession.close();
     }
   }
   return result;
 }
示例#3
0
  public boolean addUsers(UsersVO usersVO, RolesVO objRolesVO)
      throws ConstraintViolationException, HibernateException {
    boolean addResult = false;
    Session objSession = null;
    try {
      logger.info("ADDING A NEW USER");
      objSession = HibernateUtil.getSession();
      // To save the user
      objSession.beginTransaction();
      usersVO.setPassword(this.encrypt(usersVO.getPassword()));
      objSession.save(usersVO);
      objSession.getTransaction().commit();

      // To save the Role for that user
      objSession.beginTransaction();
      UserGroupVO objUserGroupVO = new UserGroupVO();
      objUserGroupVO.setUserVo(usersVO);
      objUserGroupVO.setRole(objRolesVO);
      objSession.save(objUserGroupVO);
      objSession.getTransaction().commit();
      addResult = true;
      logger.info("NEW USER ADDED");
    } catch (ConstraintViolationException e) {
      logger.error("CONSTRAINT VIOLATION EXCEPTION DURING ADD A NEW USER", e);
      objSession.getTransaction().rollback();
      throw new ConstraintViolationException(
          "User Id already exist", new SQLException(), usersVO.getUserId());
    } catch (HibernateException e) {
      logger.error("HIBERNATE EXCEPTION DURING ADD A NEW USER", e);
      objSession.getTransaction().rollback();
      e.printStackTrace();
      throw new HibernateException("User does not saved.");
    } finally {
      if (objSession != null) {
        objSession.close();
      }
    }
    return addResult;
  }
示例#4
0
  public boolean updateUser(UsersVO userVO, RolesVO objRolesVO) {
    boolean updateResult = false;
    Session objSession = null;
    try {
      logger.info("UPDATING THE USER");
      objSession = HibernateUtil.getSession();
      objSession.beginTransaction();
      if (BuildConfig.DMODE) System.out.println("userVO:" + userVO.getUserId());
      userVO.setPassword(this.encrypt(userVO.getPassword()));
      objSession.saveOrUpdate(userVO);
      updateResult = true;
      objSession.getTransaction().commit();

      if (objRolesVO != null) {
        // To update the Role for that user
        objSession.beginTransaction();
        objSession
            .createQuery(
                "delete from UserGroupVO as userGroupVo where userGroupVo.userVo.userId = ?")
            .setString(0, userVO.getUserId())
            .executeUpdate();
        UserGroupVO objUserGroupVO = new UserGroupVO();
        objUserGroupVO.setUserVo(userVO);
        objUserGroupVO.setRole(objRolesVO);
        objSession.save(objUserGroupVO);
        objSession.getTransaction().commit();
      }
      logger.info("USER IS UPDATED");
    } catch (HibernateException e) {
      logger.error("HIBERNATE EXCEPTION DURING UPDATE THE USER", e);
      objSession.getTransaction().rollback();
      e.printStackTrace();
    } finally {
      if (objSession != null) {
        objSession.close();
      }
    }
    return updateResult;
  }