001/**
002 * Copyright 2011-2013 The Kuali Foundation Licensed under the
003 * Educational Community License, Version 2.0 (the "License"); you may
004 * not use this file except in compliance with the License. You may
005 * obtain a copy of the License at
006 *
007 * http://www.osedu.org/licenses/ECL-2.0
008 *
009 * Unless required by applicable law or agreed to in writing,
010 * software distributed under the License is distributed on an "AS IS"
011 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
012 * or implied. See the License for the specific language governing
013 * permissions and limitations under the License.
014 */
015package org.kuali.mobility.security.group.dao;
016
017import org.kuali.mobility.security.group.api.Group;
018import org.kuali.mobility.security.group.api.GroupDao;
019import org.kuali.mobility.security.user.api.User;
020import org.kuali.mobility.security.user.api.UserDao;
021import org.kuali.mobility.security.user.entity.GroupMembership;
022import org.slf4j.Logger;
023import org.slf4j.LoggerFactory;
024import org.springframework.beans.factory.annotation.Autowired;
025import org.springframework.beans.factory.annotation.Qualifier;
026import org.springframework.stereotype.Repository;
027import org.springframework.transaction.annotation.Transactional;
028
029import javax.persistence.EntityManager;
030import javax.persistence.PersistenceContext;
031import javax.persistence.Query;
032import java.util.HashMap;
033import java.util.List;
034import java.util.Map;
035
036/**
037 * @author Kuali Mobility Team (mobility.collab@kuali.org)
038 */
039@Repository
040public class GroupDaoImpl implements GroupDao {
041        private static final Logger LOG = LoggerFactory.getLogger(GroupDaoImpl.class);
042
043        @PersistenceContext
044        private EntityManager entityManager;
045        
046        @Autowired
047        @Qualifier("kmeUserDao")
048        private UserDao userDao;
049
050        private Map<String,Group> groupMap;
051
052        public void init() {
053                getGroups();
054        }
055
056        // This method can be used to refresh the cached group data.
057        @Override
058        public List<Group> getGroups() {
059                Query query = getEntityManager().createNamedQuery("Group.loadAllGroups");
060                List<Group> groups = query.getResultList();
061                if( groups != null && !groups.isEmpty() ) {
062                        Map<String,Group> newGroupMap = new HashMap<String,Group>();
063                        for( Group g : groups ) {
064                                newGroupMap.put(g.getName(),g);
065                        }
066                        setGroupMap(newGroupMap);
067                }
068                return groups;
069        }
070
071        @Override
072        public Group getGroup(Long id) {
073                Query query = getEntityManager().createNamedQuery("Group.loadGroupById");
074                query.setParameter("id",id);
075                Group group = (Group)query.getSingleResult();
076                if( getGroupMap() != null ) {
077                        getGroupMap().put(group.getName(),group);
078                } else {
079                        getGroups();
080                }
081                return group;
082        }
083
084        @Override
085        public Group getGroup(String name) {
086                Group group = null;
087                if( getGroupMap() != null && getGroupMap().containsKey(name) ) {
088                        group = getGroupMap().get(name);
089                }
090                return group;
091        }
092
093        @Override
094        @Transactional
095        public Long saveGroup(Group group) {
096                Long id;
097                if( null == group ) {
098                        LOG.error("Attempting to save null group. Why is this happening?");
099                        id = null;
100                } else {
101                        if( null == group.getId() ) {
102                                getEntityManager().persist(group);
103                        } else {
104                                getEntityManager().merge(group);
105                        }
106                        id = group.getId();
107                }
108                return id;
109        }
110        
111        @Override
112        @Transactional
113        public void removeGroup(Group group){
114                if( null == group || group.getId() == null) {
115                        LOG.error("Attempting to remove null group.");
116                } else {
117                        Query query = getEntityManager().createNamedQuery("GroupMembership.loadMembershipByGroupId");
118                        query.setParameter("groupId", group.getId());
119                        List<GroupMembership> groupMemberships = query.getResultList();
120                        if(groupMemberships != null && groupMemberships.size() !=0){
121                                Query updateQuery = getEntityManager().createNamedQuery("GroupMembership.deleteMembershipByGroupId");
122                                updateQuery.setParameter("groupId", group.getId());
123                                updateQuery.executeUpdate();
124                        }
125                        getEntityManager().remove(getGroup(group.getId()));
126                }
127        }
128        
129        @Override
130        public void removeFromGroup(List<User> users, Long groupId) {
131                if( null == users || users.size() == 0) {
132                        LOG.error("Attempting to remove null users.");
133                } else {
134                        for(User user : users){
135                                Query query = getEntityManager().createNamedQuery("GroupMembership.deleteMembershipByUserId");
136                                query.setParameter("userId", user.getId());
137                                query.executeUpdate();
138                        }
139                }
140        }
141
142        @Override
143        public void addToGroup(List<User> users, Long groupId) {
144                if( null == users || users.size() == 0) {
145                        LOG.error("Attempting to add null users.");
146                } else {
147                        for(User user : users){
148                                GroupMembership groupMembership = new GroupMembership();
149                                groupMembership.setGroupId(groupId);
150                                groupMembership.setUserId(user.getId());
151                                getEntityManager().persist(groupMembership);
152                        }
153                }
154                
155        }
156
157        public EntityManager getEntityManager() {
158                return entityManager;
159        }
160
161        public void setEntityManager(EntityManager entityManager) {
162                this.entityManager = entityManager;
163        }
164
165        public UserDao getUserDao() {
166                return userDao;
167        }
168
169        public void setUserDao(UserDao userDao) {
170                this.userDao = userDao;
171        }
172
173        @Override
174        public Map<String, Group> getGroupMap() {
175                if( groupMap == null ) {
176                        getGroups();
177                }
178                return groupMap;
179        }
180
181        @Override
182        public void setGroupMap(Map<String, Group> groupMap) {
183                this.groupMap = groupMap;
184        }
185}