1
2
3
4
5
6
7
8
9
10
11
12
13
14
15 package org.kuali.mobility.security.group.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.springframework.stereotype.Repository;
21
22 import javax.persistence.EntityManager;
23 import javax.persistence.PersistenceContext;
24 import javax.persistence.Query;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.Map;
28
29
30
31
32 @Repository
33 public class GroupDaoImpl implements GroupDao {
34 private static final Logger LOG = Logger.getLogger(GroupDaoImpl.class);
35
36 @PersistenceContext
37 private EntityManager entityManager;
38
39 private Map<String,Group> groupMap;
40
41 public void init() {
42 getGroups();
43 }
44
45
46 @Override
47 public List<Group> getGroups() {
48 Query query = getEntityManager().createNamedQuery("Group.loadAllGroups");
49 List<Group> groups = query.getResultList();
50 if( groups != null && !groups.isEmpty() ) {
51 Map<String,Group> newGroupMap = new HashMap<String,Group>();
52 for( Group g : groups ) {
53 newGroupMap.put(g.getName(),g);
54 }
55 setGroupMap(newGroupMap);
56 }
57 return groups;
58 }
59
60 @Override
61 public Group getGroup(Long id) {
62 Query query = getEntityManager().createNamedQuery("Group.loadGroupById");
63 query.setParameter("id",id);
64 Group group = (Group)query.getSingleResult();
65 if( getGroupMap() != null ) {
66 getGroupMap().put(group.getName(),group);
67 } else {
68 getGroups();
69 }
70 return group;
71 }
72
73 @Override
74 public Group getGroup(String name) {
75 Group group = null;
76 if( getGroupMap() != null && getGroupMap().containsKey(name) ) {
77 group = getGroupMap().get(name);
78 }
79 return group;
80 }
81
82 @Override
83 public Long saveGroup(Group group) {
84 Long id;
85 if( null == group ) {
86 LOG.error("Attempting to save null group. Why is this happening?");
87 id = null;
88 } else {
89 if( null == group.getId() ) {
90 getEntityManager().persist(group);
91 } else {
92 getEntityManager().merge(group);
93 }
94 id = group.getId();
95 }
96 return id;
97 }
98
99 public EntityManager getEntityManager() {
100 return entityManager;
101 }
102
103 public void setEntityManager(EntityManager entityManager) {
104 this.entityManager = entityManager;
105 }
106
107 @Override
108 public Map<String, Group> getGroupMap() {
109 if( groupMap == null ) {
110 getGroups();
111 }
112 return groupMap;
113 }
114
115 @Override
116 public void setGroupMap(Map<String, Group> groupMap) {
117 this.groupMap = groupMap;
118 }
119 }