1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.document.rule;
17
18 import org.kuali.rice.core.api.uif.RemotableAttributeError;
19 import org.kuali.rice.core.api.util.RiceKeyConstants;
20 import org.kuali.rice.kim.api.KimConstants;
21 import org.kuali.rice.kim.api.group.Group;
22 import org.kuali.rice.kim.api.identity.IdentityService;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.kim.api.type.KimType;
25 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
26 import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
27 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
28 import org.kuali.rice.kim.framework.services.KimFrameworkServiceLocator;
29 import org.kuali.rice.kim.framework.type.KimTypeService;
30 import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent;
31 import org.kuali.rice.kim.rule.ui.AddGroupMemberRule;
32 import org.kuali.rice.kim.rules.ui.GroupDocumentMemberRule;
33 import org.kuali.rice.krad.document.Document;
34 import org.kuali.rice.krad.rules.TransactionalDocumentRuleBase;
35 import org.kuali.rice.krad.service.BusinessObjectService;
36 import org.kuali.rice.krad.service.KRADServiceLocator;
37 import org.kuali.rice.krad.util.GlobalVariables;
38 import org.kuali.rice.krad.util.KRADConstants;
39 import org.kuali.rice.krad.util.MessageMap;
40
41 import java.sql.Timestamp;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46
47
48
49
50 public class IdentityManagementGroupDocumentRule extends TransactionalDocumentRuleBase implements AddGroupMemberRule {
51
52 protected AddGroupMemberRule addGroupMemberRule;
53 protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
54
55 protected BusinessObjectService businessObjectService;
56 protected Class<? extends GroupDocumentMemberRule> addGroupMemberRuleClass = GroupDocumentMemberRule.class;
57
58 protected IdentityService identityService;
59
60 public IdentityService getIdentityService() {
61 if ( identityService == null) {
62 identityService = KimApiServiceLocator.getIdentityService();
63 }
64 return identityService;
65 }
66
67 @Override
68 protected boolean processCustomSaveDocumentBusinessRules(Document document) {
69 if (!(document instanceof IdentityManagementGroupDocument)) {
70 return false;
71 }
72
73 IdentityManagementGroupDocument groupDoc = (IdentityManagementGroupDocument)document;
74
75 boolean valid = true;
76 GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
77 valid &= validAssignGroup(groupDoc);
78 valid &= validDuplicateGroupName(groupDoc);
79 getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(document, getMaxDictionaryValidationDepth(), true, false);
80 valid &= validateGroupQualifier(groupDoc.getQualifiers(), groupDoc.getKimType());
81 valid &= validGroupMemberActiveDates(groupDoc.getMembers());
82 GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
83
84 return valid;
85 }
86
87 protected boolean validAssignGroup(IdentityManagementGroupDocument document){
88 boolean rulePassed = true;
89 Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
90 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace());
91 additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName());
92 if(document.getMembers()!=null && document.getMembers().size()>0){
93 if(!getDocumentDictionaryService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
94 document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP,
95 GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){
96 GlobalVariables.getMessageMap().putError("document.groupName",
97 RiceKeyConstants.ERROR_ASSIGN_GROUP,
98 new String[] {document.getGroupNamespace(), document.getGroupName()});
99 rulePassed = false;
100 }
101 }
102 return rulePassed;
103 }
104
105 @SuppressWarnings("unchecked")
106 protected boolean validDuplicateGroupName(IdentityManagementGroupDocument groupDoc){
107 Group group = KimApiServiceLocator.getGroupService().getGroupByNameAndNamespaceCode(
108 groupDoc.getGroupNamespace(), groupDoc.getGroupName());
109 boolean rulePassed = true;
110 if(group!=null){
111 if(group.getId().equals(groupDoc.getGroupId())) {
112 rulePassed = true;
113 }
114 else{
115 GlobalVariables.getMessageMap().putError("document.groupName",
116 RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group Name"});
117 rulePassed = false;
118 }
119 }
120 return rulePassed;
121 }
122
123 protected boolean validGroupMemberActiveDates(List<GroupDocumentMember> groupMembers) {
124 boolean valid = true;
125 int i = 0;
126 for(GroupDocumentMember groupMember: groupMembers) {
127 valid &= validateActiveDate("document.members["+i+"].activeToDate", groupMember.getActiveFromDate(), groupMember.getActiveToDate());
128 i++;
129 }
130 return valid;
131 }
132
133 protected boolean validateGroupQualifier(List<GroupDocumentQualifier> groupQualifiers, KimType kimType){
134 List<RemotableAttributeError> validationErrors = new ArrayList<RemotableAttributeError>();
135
136 List<RemotableAttributeError> errorsTemp;
137 Map<String, String> mapToValidate;
138 KimTypeService kimTypeService = KimFrameworkServiceLocator.getKimTypeService(kimType);
139 GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
140 mapToValidate = attributeValidationHelper.convertQualifiersToMap(groupQualifiers);
141 errorsTemp = kimTypeService.validateAttributes(kimType.getId(), mapToValidate);
142 validationErrors.addAll(attributeValidationHelper.convertErrors("",
143 attributeValidationHelper.convertQualifiersToAttrIdxMap(groupQualifiers), errorsTemp));
144 GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
145
146 if (validationErrors.isEmpty()) {
147 return true;
148 }
149 attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
150 return false;
151 }
152
153 protected boolean validateActiveDate(String errorPath, Timestamp activeFromDate, Timestamp activeToDate) {
154
155 boolean valid = true;
156 if (activeFromDate != null && activeToDate !=null && activeToDate.before(activeFromDate)) {
157 MessageMap errorMap = GlobalVariables.getMessageMap();
158 errorMap.putError(errorPath, RiceKeyConstants.ERROR_ACTIVE_TO_DATE_BEFORE_FROM_DATE);
159 valid = false;
160
161 }
162 return valid;
163 }
164
165
166
167
168 public AddGroupMemberRule getAddGroupMemberRule() {
169 if(addGroupMemberRule == null){
170 try {
171 addGroupMemberRule = addGroupMemberRuleClass.newInstance();
172 } catch ( Exception ex ) {
173 throw new RuntimeException( "Unable to create AddMemberRule instance using class: " + addGroupMemberRuleClass, ex );
174 }
175 }
176 return addGroupMemberRule;
177 }
178
179 public boolean processAddGroupMember(AddGroupMemberEvent addGroupMemberEvent) {
180 return new GroupDocumentMemberRule().processAddGroupMember(addGroupMemberEvent);
181 }
182
183
184
185
186 public BusinessObjectService getBusinessObjectService() {
187 if(businessObjectService == null){
188 businessObjectService = KRADServiceLocator.getBusinessObjectService();
189 }
190 return businessObjectService;
191 }
192
193 }