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.apache.commons.lang.StringUtils;
19 import org.kuali.rice.core.api.criteria.Predicate;
20 import org.kuali.rice.core.api.criteria.QueryByCriteria;
21 import org.kuali.rice.kew.api.KewApiConstants;
22 import org.kuali.rice.kim.api.KimConstants;
23 import org.kuali.rice.kim.api.responsibility.Responsibility;
24 import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults;
25 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
26 import org.kuali.rice.krad.maintenance.MaintenanceDocument;
27 import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase;
28 import org.kuali.rice.krad.util.GlobalVariables;
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 = super.processCustomRouteDocumentBusinessRules(document);
55
56 GlobalVariables.getMessageMap().addToErrorPath(MAINTAINABLE_ERROR_PATH);
57 try {
58 ReviewResponsibilityBo newResp =
59 (ReviewResponsibilityBo) document.getNewMaintainableObject().getDataObject();
60 ReviewResponsibilityBo oldResp =
61 (ReviewResponsibilityBo) document.getOldMaintainableObject().getDataObject();
62
63
64 if (!newResp.getId().equals(oldResp.getId())) {
65 if (newResp.getDocumentTypeName() != null
66 && newResp.getRouteNodeName() != null
67 && !checkForDuplicateResponsibility(newResp)) {
68 GlobalVariables.getMessageMap().putError("documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY);
69 rulesPassed &= false;
70 }
71
72 if (StringUtils.isNotBlank(newResp.getNamespaceCode()) && StringUtils.isNotBlank(newResp.getName())) {
73 rulesPassed &= validateNamespaceCodeAndName(newResp.getNamespaceCode(), newResp.getName());
74 }
75 } else {
76
77 if (newResp.getDocumentTypeName() != null && newResp.getRouteNodeName() != null
78 && (!StringUtils.equals(oldResp.getDocumentTypeName(), newResp.getDocumentTypeName())
79 || !StringUtils.equals(oldResp.getRouteNodeName(), newResp.getRouteNodeName()))
80 && !checkForDuplicateResponsibility(newResp)) {
81 GlobalVariables.getMessageMap().putError("documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY);
82 rulesPassed &= false;
83 }
84
85 if (StringUtils.isNotBlank(newResp.getNamespaceCode()) && StringUtils.isNotBlank(newResp.getName()) && (
86 !StringUtils.equals(oldResp.getNamespaceCode(), newResp.getNamespaceCode())
87 || !StringUtils.equals(oldResp.getName(), newResp.getName()))) {
88 rulesPassed &= validateNamespaceCodeAndName(newResp.getNamespaceCode(), newResp.getName());
89 }
90 }
91
92 } finally {
93 GlobalVariables.getMessageMap().removeFromErrorPath(MAINTAINABLE_ERROR_PATH);
94 }
95 return rulesPassed;
96 }
97
98
99 protected boolean checkForDuplicateResponsibility( ReviewResponsibilityBo resp ) {
100 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
101 Predicate p = and(
102 equal("template.namespaceCode", KewApiConstants.KEW_NAMESPACE ),
103 equal("template.name", KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME),
104 equal("attributes[documentTypeName]", resp.getDocumentTypeName())
105
106
107
108 );
109 builder.setPredicates(p);
110 ResponsibilityQueryResults results = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build());
111 List<Responsibility> responsibilities = new ArrayList<Responsibility>();
112
113 if ( !results.getResults().isEmpty() ) {
114 for ( Responsibility responsibility : results.getResults() ) {
115 String routeNodeName = responsibility.getAttributes().get( KimConstants.AttributeConstants.ROUTE_NODE_NAME);
116 if (StringUtils.isNotEmpty(routeNodeName) && StringUtils.equals(routeNodeName, resp.getRouteNodeName())){
117 responsibilities.add(responsibility);
118 }
119 }
120 }
121
122 return responsibilities.isEmpty();
123 }
124
125 protected boolean validateNamespaceCodeAndName(String namespaceCode,String name){
126 Responsibility responsibility = KimApiServiceLocator.getResponsibilityService().findRespByNamespaceCodeAndName(namespaceCode,name);
127
128 if(null != responsibility){
129 GlobalVariables.getMessageMap().putError(NAMESPACE_CODE_PROPERTY,ERROR_NAMESPACE_AND_NAME_VALIDATION,namespaceCode,name);
130 return false;
131 } else {
132 return true;
133 }
134
135 }
136 }