View Javadoc

1   /**
2    * Copyright 2005-2012 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.kew.document;
17  
18  import org.kuali.rice.kew.api.KEWPropertyConstants;
19  import org.kuali.rice.kew.api.KewApiServiceLocator;
20  import org.kuali.rice.kew.api.rule.RuleTemplate;
21  import org.kuali.rice.kew.api.rule.RuleTemplateAttribute;
22  import org.kuali.rice.kew.api.validation.ValidationResults;
23  import org.kuali.rice.kew.doctype.service.DocumentTypeService;
24  import org.kuali.rice.kew.framework.KewFrameworkServiceLocator;
25  import org.kuali.rice.kew.framework.rule.attribute.WorkflowRuleAttributeHandlerService;
26  import org.kuali.rice.kew.rule.GroupRuleResponsibility;
27  import org.kuali.rice.kew.rule.PersonRuleResponsibility;
28  import org.kuali.rice.kew.rule.RuleBaseValues;
29  import org.kuali.rice.kew.rule.RuleResponsibilityBo;
30  import org.kuali.rice.kew.rule.web.WebRuleUtils;
31  import org.kuali.rice.kew.service.KEWServiceLocator;
32  import org.kuali.rice.kew.api.KewApiConstants;
33  import org.kuali.rice.kns.document.MaintenanceDocument;
34  import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
35  import org.kuali.rice.krad.bo.PersistableBusinessObject;
36  
37  import java.util.Map;
38  
39  /**
40   * This is a description of what this class does - Garey don't forget to fill this in.
41   *
42   * @author Kuali Rice Team (rice.collab@kuali.org)
43   *
44   */
45  public class RoutingRuleMaintainableBusRule extends MaintenanceDocumentRuleBase {
46  
47  	/**
48  	 * This overridden method ...
49  	 *
50  	 * @see org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase#processCustomSaveDocumentBusinessRules(org.kuali.rice.krad.maintenance.MaintenanceDocument)
51  	 */
52  	@Override
53  	protected boolean processCustomSaveDocumentBusinessRules(
54  			MaintenanceDocument document) {
55  
56  		boolean isValid = true;
57  
58  		RuleBaseValues ruleBaseValues = this.getRuleBaseValues(document);
59  		RuleBaseValues oldRuleBaseValues = this.getOldRuleBaseValues(document);
60  		
61  		isValid &= this.populateErrorMap(ruleBaseValues);
62  
63  
64  		return isValid;
65  	}
66  
67  	protected RuleBaseValues getRuleBaseValues(MaintenanceDocument document){
68  		return (RuleBaseValues)document.getNewMaintainableObject().getBusinessObject();
69  	}
70  	
71  	protected RuleBaseValues getOldRuleBaseValues(MaintenanceDocument document){
72  		return (RuleBaseValues)document.getOldMaintainableObject().getBusinessObject();
73  	}
74  	
75  
76  	protected void populateErrorMap(Map<String,String> errorMap){
77  		for(Map.Entry<String, String> entry : errorMap.entrySet()){
78  			this.putFieldError(entry.getKey(), entry.getValue());
79  		}
80  	}
81  
82  	/**
83  	 * This overridden method ...
84  	 *
85  	 * @see org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase#processCustomAddCollectionLineBusinessRules(org.kuali.rice.krad.maintenance.MaintenanceDocument, java.lang.String, org.kuali.rice.krad.bo.PersistableBusinessObject)
86  	 */
87  	@Override
88  	public boolean processCustomAddCollectionLineBusinessRules(
89  			MaintenanceDocument document, String collectionName,
90  			PersistableBusinessObject line) {
91  
92  		boolean isValid = true;
93  
94  		if(getPersonSectionName().equals(collectionName)){
95  			PersonRuleResponsibility pr = (PersonRuleResponsibility)line;
96  			String name = pr.getPrincipalName();
97  
98  			if(!personExists(name)){
99  				isValid &= false;
100 				this.putFieldError(getPersonSectionName(), "error.document.personResponsibilities.principleDoesNotExist");
101 			}
102 		}else if(getGroupSectionName().equals(collectionName)){
103 			GroupRuleResponsibility gr = (GroupRuleResponsibility)line;
104 			if(!groupExists(gr.getNamespaceCode(), gr.getName())){
105 				isValid &= false;
106 				this.putFieldError(getGroupSectionName(), "error.document.personResponsibilities.groupDoesNotExist");
107 			}
108 		}
109 
110 		return isValid;
111 	}
112 
113 	protected String getPersonSectionName(){
114 		return KEWPropertyConstants.PERSON_RESP_SECTION;
115 	}
116 	protected String getGroupSectionName(){
117 		return KEWPropertyConstants.GROUP_RESP_SECTION;
118 	}
119 
120 	protected boolean personExists(String principalName){
121 		boolean bRet = false;
122 		try{
123 			KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(principalName);
124 			bRet = true;
125 		}catch(Exception ex){
126 			bRet = false;
127 			//ex.printStackTrace();
128 		}
129 
130 		return bRet;
131 	}
132 
133 	protected boolean groupExists(String namespaceCode, String groupName){
134 		boolean bRet = false;
135 		try{
136 			KEWServiceLocator.getIdentityHelperService().getGroupByName(namespaceCode, groupName);
137 			bRet = true;
138 		}catch(Exception ex){
139 			bRet = false;
140 			//ex.printStackTrace();
141 		}
142 		return bRet;
143 	}
144 
145 	protected boolean populateErrorMap(RuleBaseValues ruleBaseValues){
146 
147 		boolean isValid = true;
148 
149 		if (getDocumentTypeService().findByName(ruleBaseValues.getDocTypeName()) == null) {
150             this.putFieldError("docTypeName", "doctype.documenttypeservice.doctypename.required");
151             isValid &= false;
152         }
153         if(ruleBaseValues.getName() != null){
154         	if(ruleExists(ruleBaseValues)){
155         		this.putFieldError("name", "routetemplate.ruleservice.name.unique");
156             	isValid &= false;
157         	}
158         }
159 
160         /*
161          * Logic: If both from and to dates exist, make sure toDate is after fromDate
162          */
163         if(ruleBaseValues.getToDateValue() != null && ruleBaseValues.getFromDateValue() != null){
164         	if (ruleBaseValues.getToDateValue().before(ruleBaseValues.getFromDateValue())) {
165     			this.putFieldError("toDate", "error.document.maintainableItems.toDate");
166     			isValid &= false;
167             }
168         }
169 
170 		if(!setRuleAttributeErrors(ruleBaseValues)){
171 			isValid &= false;
172 		}
173 
174 		// This doesn't map directly to a single field. It's either the person or the group tab
175         if (ruleBaseValues.getRuleResponsibilities().isEmpty()) {
176         	this.putFieldError("Responsibilities", "error.document.responsibility.required");
177         	isValid &= false;
178         } else {
179             for (RuleResponsibilityBo responsibility : ruleBaseValues.getRuleResponsibilities()) {
180                 if (responsibility.getRuleResponsibilityName() != null && KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(responsibility.getRuleResponsibilityType())) {
181                     if (getGroupService().getGroup(responsibility.getRuleResponsibilityName()) == null) {
182                     	this.putFieldError("Groups", "routetemplate.ruleservice.workgroup.invalid");
183                     	isValid &= false;
184                     }
185                 } else if (responsibility.getPrincipal() == null && responsibility.getRole() == null) {
186                 	this.putFieldError("Persons", "routetemplate.ruleservice.user.invalid");
187                 	isValid &= false;
188                 }
189             }
190         }
191 
192         return isValid;
193 	}
194 
195 	protected boolean ruleExists(RuleBaseValues rule){
196 		boolean bRet = false;
197 
198 		RuleBaseValues tmp = KEWServiceLocator.getRuleService().getRuleByName(rule.getName());
199 
200 		if(tmp != null) {
201 		    if ((rule.getPreviousRuleId() == null)
202 		         || (rule.getPreviousRuleId() != null
203 		            && !rule.getPreviousRuleId().equals(tmp.getId()))) {
204 			    bRet = true;
205 		    }
206 		}
207 
208 		return bRet;
209 	}
210 
211 	protected DocumentTypeService getDocumentTypeService() {
212         return (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
213     }
214 
215 
216 	protected boolean setRuleAttributeErrors(RuleBaseValues rule){
217 
218 		boolean isValid = true;
219 
220 		RuleTemplate ruleTemplate = KewApiServiceLocator.getRuleService().getRuleTemplate(rule.getRuleTemplateId());
221 
222 		/** Populate rule extension values * */
223 		for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes()) {
224             WorkflowRuleAttributeHandlerService wrahs = KewFrameworkServiceLocator.getWorkflowRuleAttributeHandlerService();
225 
226 			Map<String, String> parameterMap = WebRuleUtils.getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
227 
228     		ValidationResults validationResults = wrahs.validateRuleData(ruleTemplateAttribute.getRuleAttribute().getName(), parameterMap);
229 
230             if (!validationResults.getErrors().isEmpty()) {
231 				isValid = false;
232 				this.putFieldError("RuleAttributes", "routetemplate.xmlattribute.required.error");
233 			}
234 			// TODO hook validation of rule data into PreRules
235 			if (validationResults.getErrors().isEmpty()) {
236 				isValid = false;
237 				for(Map.Entry<String, String> error: validationResults.getErrors().entrySet()){
238 				    this.putFieldError("RuleAttributes", error.getValue(), error.getKey());
239 				}
240 			}
241 		}
242 		return isValid;
243 
244 	}
245 
246 }