View Javadoc

1   /**
2    * Copyright 2011-2013 The Kuali Foundation Licensed under the
3    * Educational Community License, Version 2.0 (the "License"); you may
4    * not use this file except in compliance with the License. You may
5    * obtain a copy of the License at
6    *
7    * http://www.osedu.org/licenses/ECL-2.0
8    *
9    * Unless required by applicable law or agreed to in writing,
10   * software distributed under the License is distributed on an "AS IS"
11   * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
12   * or implied. See the License for the specific language governing
13   * permissions and limitations under the License.
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   * @author Kuali Mobility Team (mobility.collab@kuali.org)
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  	// This method can be used to refresh the cached group data.
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 }