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.KimConstants;
22 import org.kuali.rice.kim.api.responsibility.Responsibility;
23 import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults;
24 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
25 import org.kuali.rice.kns.document.MaintenanceDocument;
26 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
27 import org.kuali.rice.krad.util.GlobalVariables;
28 import org.apache.commons.lang.StringUtils;
29
30 import java.util.ArrayList;
31 import java.util.List;
32
33 import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
34 import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
35
36
37
38
39
40
41
42 public class ReviewResponsibilityMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
43
44 protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.reviewresponsibility.";
45 protected static final String ERROR_DUPLICATE_RESPONSIBILITY = ERROR_MESSAGE_PREFIX + "duplicateresponsibility";
46 protected static final String ERROR_NAMESPACE_AND_NAME_VALIDATION = ERROR_MESSAGE_PREFIX + "namespaceandnamevalidation";
47 protected static final String NAMESPACE_CODE_PROPERTY = "namespaceCode";
48
49
50
51
52 @Override
53 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
54 boolean rulesPassed = true;
55 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
56 try {
57 ReviewResponsibilityBo resp = (ReviewResponsibilityBo)document.getNewMaintainableObject().getDataObject();
58
59 if ( resp.getDocumentTypeName() != null
60 && resp.getRouteNodeName() != null
61 && !checkForDuplicateResponsibility( resp ) ) {
62 GlobalVariables.getMessageMap().putError( "documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY );
63 rulesPassed &= false;
64 }
65 if(StringUtils.isNotBlank(resp.getNamespaceCode()) && StringUtils.isNotBlank(resp.getName()) && StringUtils.isBlank(resp.getId())){
66 rulesPassed &=validateNamespaceCodeAndName(resp.getNamespaceCode(),resp.getName());
67 }
68 } finally {
69 GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
70 }
71 return rulesPassed;
72 }
73
74 protected boolean checkForDuplicateResponsibility( ReviewResponsibilityBo resp ) {
75 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
76 Predicate p = and(
77 equal("template.namespaceCode", KewApiConstants.KEW_NAMESPACE ),
78 equal("template.name", KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME),
79 equal("attributes[documentTypeName]", resp.getDocumentTypeName())
80
81
82
83 );
84 builder.setPredicates(p);
85 ResponsibilityQueryResults results = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build());
86 List<Responsibility> responsibilities = new ArrayList<Responsibility>();
87
88 if ( !results.getResults().isEmpty() ) {
89 for ( Responsibility responsibility : results.getResults() ) {
90 String routeNodeName = responsibility.getAttributes().get( KimConstants.AttributeConstants.ROUTE_NODE_NAME);
91 if (StringUtils.isNotEmpty(routeNodeName) && StringUtils.equals(routeNodeName, resp.getRouteNodeName())){
92 responsibilities.add(responsibility);
93 }
94 }
95 }
96
97 return responsibilities.isEmpty();
98 }
99
100 protected boolean validateNamespaceCodeAndName(String namespaceCode,String name){
101 Responsibility responsibility = KimApiServiceLocator.getResponsibilityService().findRespByNamespaceCodeAndName(namespaceCode,name);
102
103 if(null != responsibility){
104 GlobalVariables.getMessageMap().putError(NAMESPACE_CODE_PROPERTY,ERROR_NAMESPACE_AND_NAME_VALIDATION,namespaceCode,name);
105 return false;
106 } else {
107 return true;
108 }
109
110 }
111 }