1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.impl.responsibility;
17
18 import org.kuali.rice.core.api.criteria.Predicate;
19 import org.kuali.rice.core.api.criteria.QueryByCriteria;
20 import org.kuali.rice.kew.api.KewApiConstants;
21 import org.kuali.rice.kim.api.responsibility.Responsibility;
22 import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults;
23 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
24 import org.kuali.rice.kns.document.MaintenanceDocument;
25 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
26 import org.kuali.rice.krad.util.GlobalVariables;
27 import org.apache.commons.lang.StringUtils;
28
29 import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
30 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
31
32
33
34
35
36
37
38 public class ReviewResponsibilityMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
39
40 protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.reviewresponsibility.";
41 protected static final String ERROR_DUPLICATE_RESPONSIBILITY = ERROR_MESSAGE_PREFIX + "duplicateresponsibility";
42 protected static final String ERROR_NAMESPACE_AND_NAME_VALIDATION = ERROR_MESSAGE_PREFIX + "namespaceandnamevalidation";
43 protected static final String NAMESPACE_CODE_PROPERTY = "namespaceCode";
44
45
46
47
48 @Override
49 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
50 boolean rulesPassed = true;
51 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
52 try {
53 ReviewResponsibilityBo resp = (ReviewResponsibilityBo)document.getNewMaintainableObject().getDataObject();
54
55 if ( resp.getDocumentTypeName() != null
56 && resp.getRouteNodeName() != null
57 && !checkForDuplicateResponsibility( resp ) ) {
58 GlobalVariables.getMessageMap().putError( "documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY );
59 rulesPassed &= false;
60 }
61 if(StringUtils.isNotBlank(resp.getNamespaceCode()) && StringUtils.isNotBlank(resp.getName()) && StringUtils.isBlank(resp.getId())){
62 rulesPassed &=validateNamespaceCodeAndName(resp.getNamespaceCode(),resp.getName());
63 }
64 } finally {
65 GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
66 }
67 return rulesPassed;
68 }
69
70 protected boolean checkForDuplicateResponsibility( ReviewResponsibilityBo resp ) {
71 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
72 Predicate p = and(
73 equal("template.namespaceCode", KewApiConstants.KEW_NAMESPACE ),
74 equal("template.name", KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME),
75 equal("attributes[documentTypeName]", resp.getDocumentTypeName()),
76 equal("attributes[routeNodeName]", resp.getRouteNodeName())
77 );
78 builder.setPredicates(p);
79 ResponsibilityQueryResults results = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build());
80 return results.getResults().isEmpty();
81 }
82
83 protected boolean validateNamespaceCodeAndName(String namespaceCode,String name){
84 Responsibility responsibility = KimApiServiceLocator.getResponsibilityService().findRespByNamespaceCodeAndName(namespaceCode,name);
85
86 if(null != responsibility){
87 GlobalVariables.getMessageMap().putError(NAMESPACE_CODE_PROPERTY,ERROR_NAMESPACE_AND_NAME_VALIDATION,namespaceCode,name);
88 return false;
89 } else {
90 return true;
91 }
92
93 }
94 }