Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.registryKit.user; import com.registryKit.client.engagements; import java.util.ArrayList; import java.util.List; import org.hibernate.Criteria; import org.hibernate.Query; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import org.hibernate.transform.Transformers; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Repository; /** * * @author chadmccue */ @Repository public class userDAO { @Autowired private SessionFactory sessionFactory; /** * The 'emailAddress' function will return a single user object based on a email address passed in. * * @param emailAddress This will used to query the email field of the users table * * @return The function will return a user object */ public User getUserByEmail(String emailAddress) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class); criteria.add(Restrictions.eq("email", emailAddress)); return (User) criteria.uniqueResult(); } /** * The 'getUserById' function will return a single user object based on the userId passed in. * * @param userId This will used to query the id field of the users table * * @return The function will return a user object */ public User getUserById(Integer userId) { Criteria criteria = sessionFactory.getCurrentSession().createCriteria(User.class); criteria.add(Restrictions.eq("id", userId)); return (User) criteria.uniqueResult(); } /** * The 'getUserByIdentifier' function will try to location a user based on the identifier passed in. * * @param identifier The value that will be used to find a user. * * @return The function will return a user object */ public Integer getUserByIdentifier(String identifier) { String sql = ("select id from users where status = 1 and (lower(email) = '" + identifier + "' or lower(concat(concat(firstName,' '),lastName)) = '" + identifier + "')"); Query findUser = sessionFactory.getCurrentSession().createSQLQuery(sql); if (findUser.list().size() > 1) { return null; } else { if (findUser.uniqueResult() == null) { return null; } else { return (Integer) findUser.uniqueResult(); } } } /** * The 'setLastLogin' function will be called upon a successful login. It will save the entry into the rel_userLogins table. * * @param username This will be the username of the person logging in. * */ public void setLastLogin(String emailAddress) { Query q1 = sessionFactory.getCurrentSession().createQuery( "insert into userLogin (systemUserId)" + "select id from User where email = :emailAddress"); q1.setParameter("emailAddress", emailAddress); q1.executeUpdate(); /* Update the users last logged in value */ Query q2 = sessionFactory.getCurrentSession().createQuery( "from userLogin where systemUserId = (select id from User where email = :emailAddress) order by id desc"); q2.setParameter("emailAddress", emailAddress); userLogin lastLogin = (userLogin) q2.list().get(0); User user = getUserByEmail(emailAddress); user.setLastloggedIn(lastLogin.getDateCreated()); updateUser(user); } /** * The 'updateUser' function will update the selected user with the changes entered into the form. * * @param user This will hold the user object from the user form * * @return the function does not return anything */ public void updateUser(User user) { sessionFactory.getCurrentSession().update(user); } /** * The 'getUsedModulesByUser' function will return a list of used modules for a user and program. * * @param programId The selected program id. * @param userId The selected user id. * @return * @throws Exception */ public List<userProgramModules> getUsedModulesByUser(Integer programId, Integer userId, Integer roleId) throws Exception { /* If program admin get access to all modules for the program */ if (roleId == 2) { String sql = "select moduleId from program_modules where programId = " + programId; Query query = sessionFactory.getCurrentSession().createSQLQuery(sql); List<Integer> modules = query.list(); List<userProgramModules> moduleList = new ArrayList<userProgramModules>(); if (query.list().size() > 0) { for (Integer module : modules) { userProgramModules newModule = new userProgramModules(); newModule.setSystemUserId(userId); newModule.setModuleId(module); newModule.setProgramId(programId); moduleList.add(newModule); } } return moduleList; } else { Query query = sessionFactory.getCurrentSession() .createQuery("from userProgramModules where programId = :programId and systemUserId = :userId"); query.setParameter("programId", programId); query.setParameter("userId", userId); List<userProgramModules> moduleList = query.list(); return moduleList; } } @SuppressWarnings("unchecked") public List<String> getUserRoles(User user) { try { String sql = ("select r.role as authority from users u inner join " + " user_roles r on u.roleId = r.id where u.status = 1 and u.email = :email"); Query query = sessionFactory.getCurrentSession().createSQLQuery(sql); query.setParameter("email", user.getEmail()); List<String> roles = query.list(); return roles; } catch (Exception ex) { System.err.println("getUserRoles " + ex.getCause()); ex.printStackTrace(); return null; } } /** * The 'getUserByResetCode' function will try to location a user based on the a reset code * * @param resetCode The value that will be used to find a user. * * @return The function will return a user object */ public User getUserByResetCode(String resetCode) { Query query = sessionFactory.getCurrentSession().createQuery("from User where resetCode = :resetCode"); query.setParameter("resetCode", resetCode); if (query.list().size() > 1) { return null; } else { if (query.uniqueResult() == null) { return null; } else { return (User) query.uniqueResult(); } } } }