1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.security.user.dao;
16
17 import org.apache.log4j.Logger;
18 import org.kuali.mobility.security.group.api.Group;
19 import org.kuali.mobility.security.group.api.GroupDao;
20 import org.kuali.mobility.security.user.api.User;
21 import org.kuali.mobility.security.user.api.UserDao;
22 import org.kuali.mobility.security.user.entity.GroupMembership;
23 import org.kuali.mobility.security.user.entity.UserImpl;
24 import org.springframework.beans.factory.annotation.Autowired;
25 import org.springframework.beans.factory.annotation.Qualifier;
26 import org.springframework.stereotype.Repository;
27 import org.springframework.transaction.annotation.Transactional;
28
29 import javax.persistence.EntityManager;
30 import javax.persistence.PersistenceContext;
31 import javax.persistence.Query;
32 import java.util.ArrayList;
33 import java.util.List;
34 import javax.persistence.EntityNotFoundException;
35 import javax.persistence.NonUniqueResultException;
36
37
38
39
40 @Repository
41 public class UserDaoImpl implements UserDao {
42
43 private static final Logger LOG = Logger.getLogger(UserDaoImpl.class);
44
45 @PersistenceContext
46 private EntityManager entityManager;
47
48 @Autowired
49 @Qualifier("kmeGroupDao")
50 private GroupDao groupDao;
51
52 @Override
53 public User loadUserById(Long id) {
54 Query query = getEntityManager().createNamedQuery("User.lookupById");
55 query.setParameter("id", id);
56 UserImpl user = null;
57
58 try {
59 user = (UserImpl) query.getSingleResult();
60 } catch (EntityNotFoundException e) {
61 LOG.error(e.getLocalizedMessage());
62 } catch (NonUniqueResultException e) {
63 LOG.error(e.getLocalizedMessage());
64 } catch (Exception e) {
65 LOG.error(e.getLocalizedMessage());
66 }
67
68 if (null != user) {
69 loadGroupMembership(user);
70 }
71 return user;
72 }
73
74 @Override
75 public User loadUserByLoginName(String loginName) {
76 Query query = getEntityManager().createNamedQuery("User.lookupByLoginName");
77 query.setParameter("loginName", loginName);
78 UserImpl user = null;
79
80 try {
81 user = (UserImpl) query.getSingleResult();
82 } catch (EntityNotFoundException e) {
83 LOG.error(e.getLocalizedMessage());
84 } catch (NonUniqueResultException e) {
85 LOG.error(e.getLocalizedMessage());
86 } catch (Exception e) {
87 LOG.error(e.getLocalizedMessage());
88 }
89
90 if (null != user) {
91 loadGroupMembership(user);
92 }
93 return user;
94 }
95
96 @Override
97 @Transactional
98 public Long saveUser(User user) {
99 Long id;
100 if (null == user) {
101 id = null;
102 } else {
103 if (null == user.getId()) {
104 getEntityManager().persist(user);
105 } else {
106 getEntityManager().merge(user);
107 }
108 id = user.getId();
109 Query query = getEntityManager().createNamedQuery("GroupMembership.deleteMembershipByUserId");
110 query.setParameter("userId", id);
111 query.executeUpdate();
112 for (Group g : user.getGroups()) {
113 GroupMembership groupMembership = new GroupMembership();
114 groupMembership.setGroupId(g.getId());
115 groupMembership.setUserId(id);
116 getEntityManager().persist(groupMembership);
117 }
118 }
119 return id;
120 }
121
122 private void loadGroupMembership(UserImpl user) {
123 if (user != null && user.getId() != null) {
124 List<Group> groups = new ArrayList<Group>();
125 Query query = getEntityManager().createNamedQuery("GroupMembership.loadMembershipByUserId");
126 query.setParameter("userId", user.getId());
127 List<GroupMembership> groupMemberships = query.getResultList();
128 for (GroupMembership gm : groupMemberships) {
129 Group g = getGroupDao().getGroup(gm.getGroupId());
130 groups.add(g);
131 }
132 user.setGroups(groups);
133 }
134 }
135
136 private void loadGroupMembership(List<UserImpl> users) {
137 for (UserImpl u : users) {
138 loadGroupMembership(u);
139 }
140 }
141
142 public EntityManager getEntityManager() {
143 return entityManager;
144 }
145
146 public void setEntityManager(EntityManager entityManager) {
147 this.entityManager = entityManager;
148 }
149
150 public GroupDao getGroupDao() {
151 return groupDao;
152 }
153
154 public void setGroupDao(GroupDao groupDao) {
155 this.groupDao = groupDao;
156 }
157 }