1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.kuali.rice.kim.service.impl; |
17 | |
|
18 | |
import org.apache.commons.collections.CollectionUtils; |
19 | |
import org.apache.commons.collections.Predicate; |
20 | |
import org.apache.log4j.Logger; |
21 | |
import org.kuali.rice.core.exception.RiceRuntimeException; |
22 | |
import org.kuali.rice.kim.bo.group.dto.GroupInfo; |
23 | |
import org.kuali.rice.kim.bo.group.impl.GroupAttributeDataImpl; |
24 | |
import org.kuali.rice.kim.bo.group.impl.GroupMemberImpl; |
25 | |
import org.kuali.rice.kim.bo.impl.GroupImpl; |
26 | |
import org.kuali.rice.kim.service.GroupUpdateService; |
27 | |
import org.kuali.rice.kim.service.KIMServiceLocator; |
28 | |
import org.kuali.rice.kim.util.KIMPropertyConstants; |
29 | |
import org.kuali.rice.kim.util.KIMWebServiceConstants; |
30 | |
import org.kuali.rice.kim.util.KimCommonUtils; |
31 | |
import org.kuali.rice.kim.util.KimConstants.KimGroupMemberTypes; |
32 | |
import org.mortbay.log.Log; |
33 | |
|
34 | |
import javax.jws.WebService; |
35 | |
import java.util.*; |
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | |
@WebService(endpointInterface = KIMWebServiceConstants.GroupUpdateService.INTERFACE_CLASS, serviceName = KIMWebServiceConstants.GroupUpdateService.WEB_SERVICE_NAME, portName = KIMWebServiceConstants.GroupUpdateService.WEB_SERVICE_PORT, targetNamespace = KIMWebServiceConstants.MODULE_TARGET_NAMESPACE) |
44 | 0 | public class GroupUpdateServiceImpl extends GroupServiceBase implements GroupUpdateService { |
45 | |
|
46 | 0 | private static final Logger LOG = Logger.getLogger(GroupUpdateServiceImpl.class); |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
public boolean addGroupToGroup(String childId, String parentId) { |
52 | 0 | if(childId.equals(parentId)) { |
53 | 0 | throw new IllegalArgumentException("Can't add group to itself."); |
54 | |
} |
55 | |
|
56 | 0 | if(isGroupMemberOfGroup(parentId, childId)) { |
57 | 0 | throw new IllegalArgumentException("Circular group reference."); |
58 | |
} |
59 | |
|
60 | 0 | GroupMemberImpl groupMember = new GroupMemberImpl(); |
61 | 0 | groupMember.setGroupId(parentId); |
62 | 0 | groupMember.setMemberTypeCode( KimGroupMemberTypes.GROUP_MEMBER_TYPE ); |
63 | 0 | groupMember.setMemberId(childId); |
64 | |
|
65 | 0 | getBusinessObjectService().save(groupMember); |
66 | 0 | getIdentityManagementNotificationService().groupUpdated(); |
67 | |
|
68 | 0 | return true; |
69 | |
} |
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
public boolean addPrincipalToGroup(String principalId, String groupId) { |
75 | 0 | GroupMemberImpl groupMember = new GroupMemberImpl(); |
76 | 0 | groupMember.setGroupId(groupId); |
77 | 0 | groupMember.setMemberTypeCode( KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE ); |
78 | 0 | groupMember.setMemberId(principalId); |
79 | |
|
80 | 0 | getBusinessObjectService().save(groupMember); |
81 | 0 | KIMServiceLocator.getGroupInternalService().updateForUserAddedToGroup(groupMember.getMemberId(), groupMember.getGroupId()); |
82 | 0 | getIdentityManagementNotificationService().groupUpdated(); |
83 | 0 | return true; |
84 | |
} |
85 | |
|
86 | |
public GroupInfo createGroup(GroupInfo groupInfo) { |
87 | 0 | GroupImpl group = new GroupImpl(); |
88 | |
|
89 | 0 | group = KimCommonUtils.copyInfoToGroup(groupInfo, group); |
90 | |
|
91 | 0 | saveGroup(group); |
92 | |
|
93 | 0 | GroupInfo newGroupInfo = getGroupInfoByName(groupInfo.getNamespaceCode(), groupInfo.getGroupName()); |
94 | |
|
95 | 0 | if(groupInfo.getAttributes() != null && groupInfo.getAttributes().size() > 0) { |
96 | 0 | List<GroupAttributeDataImpl> groupAttributes = |
97 | |
KimCommonUtils.copyInfoAttributesToGroupAttributes(groupInfo.getAttributes(), newGroupInfo.getGroupId(), newGroupInfo.getKimTypeId()); |
98 | 0 | saveGroupAttributes(groupAttributes); |
99 | |
} |
100 | 0 | return getGroupInfo(newGroupInfo.getGroupId()); |
101 | |
} |
102 | |
|
103 | |
|
104 | |
|
105 | |
|
106 | |
|
107 | |
public void removeAllGroupMembers(String groupId) { |
108 | 0 | List<String> memberPrincipalsBefore = KIMServiceLocator.getGroupService().getMemberPrincipalIds(groupId); |
109 | |
|
110 | 0 | Collection<GroupMemberImpl> toDeactivate = getActiveGroupMembers(groupId, null, null); |
111 | 0 | java.sql.Timestamp today = new java.sql.Timestamp(System.currentTimeMillis()); |
112 | |
|
113 | |
|
114 | 0 | for (GroupMemberImpl aToDeactivate : toDeactivate) { |
115 | 0 | aToDeactivate.setActiveToDate(today); |
116 | |
} |
117 | |
|
118 | |
|
119 | 0 | getBusinessObjectService().save(new ArrayList<GroupMemberImpl>(toDeactivate)); |
120 | 0 | List<String> memberPrincipalsAfter = KIMServiceLocator.getGroupService().getMemberPrincipalIds(groupId); |
121 | |
|
122 | 0 | if (!CollectionUtils.isEmpty(memberPrincipalsAfter)) { |
123 | |
|
124 | 0 | Log.warn("after attempting removal of all members, group with id '" + groupId + "' still has principal members"); |
125 | |
} |
126 | |
|
127 | |
|
128 | 0 | KIMServiceLocator.getGroupInternalService().updateForWorkgroupChange(groupId, memberPrincipalsBefore, memberPrincipalsAfter); |
129 | 0 | getIdentityManagementNotificationService().groupUpdated(); |
130 | 0 | } |
131 | |
|
132 | |
|
133 | |
|
134 | |
|
135 | |
public boolean removeGroupFromGroup(String childId, String parentId) { |
136 | 0 | java.sql.Timestamp today = new java.sql.Timestamp(System.currentTimeMillis()); |
137 | |
|
138 | 0 | List<GroupMemberImpl> groupMembers = |
139 | |
getActiveGroupMembers(parentId, childId, KimGroupMemberTypes.GROUP_MEMBER_TYPE); |
140 | |
|
141 | 0 | if(groupMembers.size() == 1) { |
142 | 0 | GroupMemberImpl groupMember = groupMembers.get(0); |
143 | 0 | groupMember.setActiveToDate(today); |
144 | 0 | getBusinessObjectService().save(groupMember); |
145 | 0 | getIdentityManagementNotificationService().groupUpdated(); |
146 | 0 | return true; |
147 | |
} |
148 | |
|
149 | 0 | return false; |
150 | |
} |
151 | |
|
152 | |
|
153 | |
|
154 | |
|
155 | |
@SuppressWarnings("unchecked") |
156 | |
public boolean removePrincipalFromGroup(String principalId, String groupId) { |
157 | 0 | List<GroupMemberImpl> groupMembers = |
158 | |
getActiveGroupMembers(groupId, principalId, KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE); |
159 | |
|
160 | 0 | if(groupMembers.size() == 1) { |
161 | 0 | GroupMemberImpl member = groupMembers.iterator().next(); |
162 | 0 | member.setActiveToDate(new java.sql.Timestamp(System.currentTimeMillis())); |
163 | 0 | getBusinessObjectService().save(member); |
164 | 0 | KIMServiceLocator.getGroupInternalService().updateForUserRemovedFromGroup(member.getMemberId(), member.getGroupId()); |
165 | 0 | getIdentityManagementNotificationService().groupUpdated(); |
166 | 0 | return true; |
167 | |
} |
168 | |
|
169 | 0 | return false; |
170 | |
} |
171 | |
|
172 | |
|
173 | |
|
174 | |
|
175 | |
|
176 | |
|
177 | |
public GroupInfo updateGroup(String groupId, GroupInfo groupInfo) { |
178 | |
|
179 | 0 | GroupImpl group = getGroupImpl(groupId); |
180 | |
|
181 | 0 | if (group == null) { |
182 | 0 | throw new IllegalArgumentException("Group not found for update."); |
183 | |
} |
184 | |
|
185 | 0 | group = KimCommonUtils.copyInfoToGroup(groupInfo, group); |
186 | |
|
187 | |
|
188 | 0 | Map<String,String> criteria = new HashMap<String,String>(); |
189 | 0 | criteria.put(KIMPropertyConstants.Group.GROUP_ID, group.getGroupId()); |
190 | 0 | getBusinessObjectService().deleteMatching(GroupAttributeDataImpl.class, criteria); |
191 | |
|
192 | |
|
193 | 0 | saveGroup(group); |
194 | |
|
195 | |
|
196 | 0 | if(groupInfo.getAttributes() != null && groupInfo.getAttributes().size() > 0) { |
197 | 0 | List<GroupAttributeDataImpl> groupAttributes = |
198 | |
KimCommonUtils.copyInfoAttributesToGroupAttributes(groupInfo.getAttributes(), group.getGroupId(), group.getKimTypeId()); |
199 | 0 | saveGroupAttributes(groupAttributes); |
200 | |
} |
201 | |
|
202 | 0 | return getGroupInfo(groupInfo.getGroupId()); |
203 | |
} |
204 | |
|
205 | |
protected void saveGroup(GroupImpl group) { |
206 | 0 | if ( group == null ) { |
207 | 0 | return; |
208 | 0 | } else if (group.getGroupId() != null) { |
209 | |
|
210 | 0 | GroupImpl oldGroup = getGroupImpl(group.getGroupId()); |
211 | |
|
212 | 0 | if (oldGroup != null) { |
213 | |
|
214 | 0 | java.sql.Timestamp activeTo = new java.sql.Timestamp(System.currentTimeMillis()); |
215 | 0 | List<GroupMemberImpl> toReAdd = null; |
216 | |
|
217 | 0 | if (oldGroup.getMembers() != null) for (GroupMemberImpl member : oldGroup.getMembers()) { |
218 | |
|
219 | 0 | if (group.getMembers() == null || !group.getMembers().contains(member)) { |
220 | |
|
221 | 0 | member.setActiveToDate(activeTo); |
222 | 0 | if (toReAdd == null) { toReAdd = new ArrayList<GroupMemberImpl>(); } |
223 | |
|
224 | 0 | toReAdd.add(member); |
225 | |
} |
226 | |
} |
227 | |
|
228 | |
|
229 | 0 | if (toReAdd != null) { |
230 | 0 | List<GroupMemberImpl> groupMembers = group.getMembers(); |
231 | 0 | if (groupMembers == null) { groupMembers = new ArrayList<GroupMemberImpl>(toReAdd.size()); } |
232 | 0 | group.setMembers(groupMembers); |
233 | |
} |
234 | |
} |
235 | |
} |
236 | |
|
237 | |
|
238 | 0 | KIMServiceLocator.getGroupInternalService().saveWorkgroup(group); |
239 | 0 | getIdentityManagementNotificationService().groupUpdated(); |
240 | 0 | } |
241 | |
|
242 | |
protected void saveGroupAttributes(List<GroupAttributeDataImpl> groupAttributes) { |
243 | 0 | if ( groupAttributes == null ) { |
244 | 0 | return; |
245 | |
} |
246 | 0 | getBusinessObjectService().save( groupAttributes ); |
247 | 0 | } |
248 | |
|
249 | |
protected void deleteGroupAttribute(GroupAttributeDataImpl groupAttribute) { |
250 | 0 | if ( groupAttribute == null ) { |
251 | 0 | return; |
252 | |
} |
253 | 0 | getBusinessObjectService().delete( groupAttribute ); |
254 | 0 | } |
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
|
260 | |
|
261 | |
|
262 | |
|
263 | |
|
264 | |
|
265 | |
|
266 | |
private List<GroupMemberImpl> getActiveGroupMembers(String parentId, |
267 | |
String childId, String memberType) { |
268 | 0 | final java.sql.Date today = new java.sql.Date(System.currentTimeMillis()); |
269 | |
|
270 | 0 | if (childId != null && memberType == null) throw new RiceRuntimeException("memberType must be non-null if childId is non-null"); |
271 | |
|
272 | 0 | Map<String,Object> criteria = new HashMap<String,Object>(4); |
273 | 0 | criteria.put(KIMPropertyConstants.GroupMember.GROUP_ID, parentId); |
274 | |
|
275 | 0 | if (childId != null) { |
276 | 0 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_ID, childId); |
277 | 0 | criteria.put(KIMPropertyConstants.GroupMember.MEMBER_TYPE_CODE, memberType); |
278 | |
} |
279 | |
|
280 | 0 | Collection<GroupMemberImpl> groupMembers = getBusinessObjectService().findMatching(GroupMemberImpl.class, criteria); |
281 | |
|
282 | 0 | CollectionUtils.filter(groupMembers, new Predicate() { |
283 | |
public boolean evaluate(Object object) { |
284 | 0 | GroupMemberImpl member = (GroupMemberImpl) object; |
285 | |
|
286 | 0 | return member.getActiveToDate() == null || today.before(member.getActiveToDate()); |
287 | |
} |
288 | |
}); |
289 | |
|
290 | 0 | return new ArrayList<GroupMemberImpl>(groupMembers); |
291 | |
} |
292 | |
|
293 | |
} |