View Javadoc

1   /*
2    * Copyright 2007-2009 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.rice.kim.document.rule;
17  
18  import org.kuali.rice.core.util.RiceKeyConstants;
19  import org.kuali.rice.kim.api.group.Group;
20  import org.kuali.rice.kim.api.services.IdentityService;
21  import org.kuali.rice.kim.api.services.KimApiServiceLocator;
22  import org.kuali.rice.kim.api.type.KimType;
23  import org.kuali.rice.kim.api.type.KimTypeService;
24  import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
25  import org.kuali.rice.kim.bo.ui.GroupDocumentQualifier;
26  import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
27  import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent;
28  import org.kuali.rice.kim.rule.ui.AddGroupMemberRule;
29  import org.kuali.rice.kim.rules.ui.GroupDocumentMemberRule;
30  import org.kuali.rice.kim.service.KIMServiceLocatorWeb;
31  import org.kuali.rice.kim.util.KimConstants;
32  import org.kuali.rice.krad.document.Document;
33  import org.kuali.rice.krad.rules.TransactionalDocumentRuleBase;
34  import org.kuali.rice.krad.service.BusinessObjectService;
35  import org.kuali.rice.krad.service.KRADServiceLocator;
36  import org.kuali.rice.krad.util.GlobalVariables;
37  import org.kuali.rice.krad.util.KRADConstants;
38  import org.kuali.rice.krad.util.MessageMap;
39  
40  import java.sql.Timestamp;
41  import java.util.HashMap;
42  import java.util.List;
43  import java.util.Map;
44  
45  /**
46   * @author Kuali Rice Team (rice.collab@kuali.org)
47   */
48  public class IdentityManagementGroupDocumentRule extends TransactionalDocumentRuleBase implements AddGroupMemberRule {
49  
50  	protected AddGroupMemberRule addGroupMemberRule;
51  	protected AttributeValidationHelper attributeValidationHelper = new AttributeValidationHelper();
52  	
53  	protected BusinessObjectService businessObjectService;
54  	protected Class<? extends GroupDocumentMemberRule> addGroupMemberRuleClass = GroupDocumentMemberRule.class;
55  
56  	protected IdentityService identityService; 
57  	
58      public IdentityService getIdentityService() {
59          if ( identityService == null) {
60              identityService = KimApiServiceLocator.getIdentityService();
61          }
62          return identityService;
63      }
64  
65      @Override
66      protected boolean processCustomSaveDocumentBusinessRules(Document document) {
67          if (!(document instanceof IdentityManagementGroupDocument)) {
68              return false;
69          }
70  
71          IdentityManagementGroupDocument groupDoc = (IdentityManagementGroupDocument)document;
72  
73          boolean valid = true;
74          GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
75          valid &= validAssignGroup(groupDoc);
76          valid &= validDuplicateGroupName(groupDoc);
77          getDictionaryValidationService().validateDocumentAndUpdatableReferencesRecursively(document, getMaxDictionaryValidationDepth(), true, false);
78          valid &= validateGroupQualifier(groupDoc.getQualifiers(), groupDoc.getKimType());
79          valid &= validGroupMemberActiveDates(groupDoc.getMembers());
80          GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
81  
82          return valid;
83      }
84      
85  	protected boolean validAssignGroup(IdentityManagementGroupDocument document){
86          boolean rulePassed = true;
87          Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
88          additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace());
89          additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName());
90  		if(document.getMembers()!=null && document.getMembers().size()>0){
91  			if(!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
92  					document, KimConstants.NAMESPACE_CODE, KimConstants.PermissionTemplateNames.POPULATE_GROUP, 
93  					GlobalVariables.getUserSession().getPrincipalId(), additionalPermissionDetails, null)){
94  	    		GlobalVariables.getMessageMap().putError("document.groupName", 
95  	    				RiceKeyConstants.ERROR_ASSIGN_GROUP, 
96  	    				new String[] {document.getGroupNamespace(), document.getGroupName()});
97  	            rulePassed = false;
98  			}
99  		}
100 		return rulePassed;
101 	}
102 
103     @SuppressWarnings("unchecked")
104 	protected boolean validDuplicateGroupName(IdentityManagementGroupDocument groupDoc){
105         Group group = KimApiServiceLocator.getGroupService().getGroupByName(groupDoc.getGroupNamespace(), groupDoc.getGroupName());
106         boolean rulePassed = true;
107     	if(group!=null){
108     		if(group.getId().equals(groupDoc.getGroupId())) {
109     			rulePassed = true;
110             }
111     		else{
112 	    		GlobalVariables.getMessageMap().putError("document.groupName", 
113 	    				RiceKeyConstants.ERROR_DUPLICATE_ENTRY, new String[] {"Group Name"});
114 	    		rulePassed = false;
115     		}
116     	}
117     	return rulePassed;
118     }
119     
120     protected boolean validGroupMemberActiveDates(List<GroupDocumentMember> groupMembers) {
121     	boolean valid = true;
122 		int i = 0;
123     	for(GroupDocumentMember groupMember: groupMembers) {
124    			valid &= validateActiveDate("document.members["+i+"].activeToDate", groupMember.getActiveFromDate(), groupMember.getActiveToDate());
125     		i++;
126     	}
127     	return valid;
128     }
129 
130     protected boolean validateGroupQualifier(List<GroupDocumentQualifier> groupQualifiers, KimType kimType){
131 		Map<String, String> validationErrors = new HashMap<String, String>();
132 
133 		Map<String, String> errorsTemp;
134 		Map<String, String> mapToValidate;
135         KimTypeService kimTypeService = KIMServiceLocatorWeb.getKimTypeService(kimType);
136         GlobalVariables.getMessageMap().removeFromErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
137 		mapToValidate = attributeValidationHelper.convertQualifiersToMap(groupQualifiers);
138 		errorsTemp = kimTypeService.validateAttributes(kimType.getId(), mapToValidate);
139 		validationErrors.putAll( attributeValidationHelper.convertErrors("",
140                 attributeValidationHelper.convertQualifiersToAttrIdxMap(groupQualifiers), errorsTemp));
141 		GlobalVariables.getMessageMap().addToErrorPath(KRADConstants.DOCUMENT_PROPERTY_NAME);
142 		
143     	if (validationErrors.isEmpty()) {
144     		return true;
145     	} 
146     	attributeValidationHelper.moveValidationErrorsToErrorMap(validationErrors);
147     	return false;
148     }
149     
150 	protected boolean validateActiveDate(String errorPath, Timestamp activeFromDate, Timestamp activeToDate) {
151 		// TODO : do not have detail bus rule yet, so just check this for now.
152 		boolean valid = true;
153 		if (activeFromDate != null && activeToDate !=null && activeToDate.before(activeFromDate)) {
154 	        MessageMap errorMap = GlobalVariables.getMessageMap();
155             errorMap.putError(errorPath, RiceKeyConstants.ERROR_ACTIVE_TO_DATE_BEFORE_FROM_DATE);
156             valid = false;
157 			
158 		}
159 		return valid;
160 	}
161 	
162 	/**
163 	 * @return the addGroupMemberRule
164 	 */
165 	public AddGroupMemberRule getAddGroupMemberRule() {
166 		if(addGroupMemberRule == null){
167 			try {
168 				addGroupMemberRule = addGroupMemberRuleClass.newInstance();
169 			} catch ( Exception ex ) {
170 				throw new RuntimeException( "Unable to create AddMemberRule instance using class: " + addGroupMemberRuleClass, ex );
171 			}
172 		}
173 		return addGroupMemberRule;
174 	}
175 
176     public boolean processAddGroupMember(AddGroupMemberEvent addGroupMemberEvent) {
177         return new GroupDocumentMemberRule().processAddGroupMember(addGroupMemberEvent);    
178     }
179 
180     /**
181 	 * @return the businessObjectService
182 	 */
183 	public BusinessObjectService getBusinessObjectService() {
184 		if(businessObjectService == null){
185 			businessObjectService = KRADServiceLocator.getBusinessObjectService();
186 		}
187 		return businessObjectService;
188 	}
189 
190 }