1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
41
42
43
44
45 public class RoutingRuleMaintainableBusRule extends MaintenanceDocumentRuleBase {
46
47
48
49
50
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 if (oldRuleBaseValues != null) {
62 ruleBaseValues.setPreviousRuleId(oldRuleBaseValues.getId());
63 }
64 isValid &= this.populateErrorMap(ruleBaseValues);
65
66
67 return isValid;
68 }
69
70 protected RuleBaseValues getRuleBaseValues(MaintenanceDocument document){
71 return (RuleBaseValues)document.getNewMaintainableObject().getBusinessObject();
72 }
73
74 protected RuleBaseValues getOldRuleBaseValues(MaintenanceDocument document){
75 return (RuleBaseValues)document.getOldMaintainableObject().getBusinessObject();
76 }
77
78
79 protected void populateErrorMap(Map<String,String> errorMap){
80 for(Map.Entry<String, String> entry : errorMap.entrySet()){
81 this.putFieldError(entry.getKey(), entry.getValue());
82 }
83 }
84
85
86
87
88
89
90 @Override
91 public boolean processCustomAddCollectionLineBusinessRules(
92 MaintenanceDocument document, String collectionName,
93 PersistableBusinessObject line) {
94
95 boolean isValid = true;
96
97 if(getPersonSectionName().equals(collectionName)){
98 PersonRuleResponsibility pr = (PersonRuleResponsibility)line;
99 String name = pr.getPrincipalName();
100
101 if(!personExists(name)){
102 isValid &= false;
103 this.putFieldError(getPersonSectionName(), "error.document.personResponsibilities.principleDoesNotExist");
104 }
105 }else if(getGroupSectionName().equals(collectionName)){
106 GroupRuleResponsibility gr = (GroupRuleResponsibility)line;
107 if(!groupExists(gr.getNamespaceCode(), gr.getName())){
108 isValid &= false;
109 this.putFieldError(getGroupSectionName(), "error.document.personResponsibilities.groupDoesNotExist");
110 }
111 }
112
113 return isValid;
114 }
115
116 protected String getPersonSectionName(){
117 return KEWPropertyConstants.PERSON_RESP_SECTION;
118 }
119 protected String getGroupSectionName(){
120 return KEWPropertyConstants.GROUP_RESP_SECTION;
121 }
122
123 protected boolean personExists(String principalName){
124 boolean bRet = false;
125 try{
126 KEWServiceLocator.getIdentityHelperService().getIdForPrincipalName(principalName);
127 bRet = true;
128 }catch(Exception ex){
129 bRet = false;
130
131 }
132
133 return bRet;
134 }
135
136 protected boolean groupExists(String namespaceCode, String groupName){
137 boolean bRet = false;
138 try{
139 KEWServiceLocator.getIdentityHelperService().getGroupByName(namespaceCode, groupName);
140 bRet = true;
141 }catch(Exception ex){
142 bRet = false;
143
144 }
145 return bRet;
146 }
147
148 protected boolean populateErrorMap(RuleBaseValues ruleBaseValues){
149
150 boolean isValid = true;
151
152 if (getDocumentTypeService().findByName(ruleBaseValues.getDocTypeName()) == null) {
153 this.putFieldError("docTypeName", "doctype.documenttypeservice.doctypename.required");
154 isValid &= false;
155 }
156 if(ruleBaseValues.getName() != null){
157 if(ruleExists(ruleBaseValues)){
158 this.putFieldError("name", "routetemplate.ruleservice.name.unique");
159 isValid &= false;
160 }
161 }
162
163
164
165
166 if(ruleBaseValues.getToDateValue() != null && ruleBaseValues.getFromDateValue() != null){
167 if (ruleBaseValues.getToDateValue().before(ruleBaseValues.getFromDateValue())) {
168 this.putFieldError("toDate", "error.document.maintainableItems.toDate");
169 isValid &= false;
170 }
171 }
172
173 if(!setRuleAttributeErrors(ruleBaseValues)){
174 isValid &= false;
175 }
176
177
178 if (ruleBaseValues.getRuleResponsibilities().isEmpty()) {
179 this.putFieldError("Responsibilities", "error.document.responsibility.required");
180 isValid &= false;
181 } else {
182 for (RuleResponsibilityBo responsibility : ruleBaseValues.getRuleResponsibilities()) {
183 if (responsibility.getRuleResponsibilityName() != null && KewApiConstants.RULE_RESPONSIBILITY_GROUP_ID.equals(responsibility.getRuleResponsibilityType())) {
184 if (getGroupService().getGroup(responsibility.getRuleResponsibilityName()) == null) {
185 this.putFieldError("Groups", "routetemplate.ruleservice.workgroup.invalid");
186 isValid &= false;
187 }
188 } else if (responsibility.getPrincipal() == null && responsibility.getRole() == null) {
189 this.putFieldError("Persons", "routetemplate.ruleservice.user.invalid");
190 isValid &= false;
191 }
192 }
193 }
194
195 return isValid;
196 }
197
198 protected boolean ruleExists(RuleBaseValues rule){
199 boolean bRet = false;
200
201 RuleBaseValues tmp = KEWServiceLocator.getRuleService().getRuleByName(rule.getName());
202
203 if(tmp != null) {
204 if ((rule.getPreviousRuleId() == null)
205 || (rule.getPreviousRuleId() != null
206 && !rule.getPreviousRuleId().equals(tmp.getId()))) {
207 bRet = true;
208 }
209 }
210
211 return bRet;
212 }
213
214 protected DocumentTypeService getDocumentTypeService() {
215 return (DocumentTypeService) KEWServiceLocator.getService(KEWServiceLocator.DOCUMENT_TYPE_SERVICE);
216 }
217
218
219 protected boolean setRuleAttributeErrors(RuleBaseValues rule){
220
221 boolean isValid = true;
222
223 RuleTemplate ruleTemplate = KewApiServiceLocator.getRuleService().getRuleTemplate(rule.getRuleTemplateId());
224
225
226 for (RuleTemplateAttribute ruleTemplateAttribute : ruleTemplate.getActiveRuleTemplateAttributes()) {
227 WorkflowRuleAttributeHandlerService wrahs = KewFrameworkServiceLocator.getWorkflowRuleAttributeHandlerService();
228
229 Map<String, String> parameterMap = WebRuleUtils.getFieldMapForRuleTemplateAttribute(rule, ruleTemplateAttribute);
230
231 ValidationResults validationResults = wrahs.validateRuleData(ruleTemplateAttribute.getRuleAttribute().getName(), parameterMap);
232
233 if (!validationResults.getErrors().isEmpty()) {
234 isValid = false;
235 this.putFieldError("RuleAttributes", "routetemplate.xmlattribute.required.error");
236 }
237
238 if (validationResults.getErrors().isEmpty()) {
239 isValid = false;
240 for(Map.Entry<String, String> error: validationResults.getErrors().entrySet()){
241 this.putFieldError("RuleAttributes", error.getValue(), error.getKey());
242 }
243 }
244 }
245 return isValid;
246
247 }
248
249 }