View Javadoc

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