View Javadoc
1   /**
2    * Copyright 2005-2015 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.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   * This is a description of what this class does - kellerj don't forget to fill this in. 
38   * 
39   * @author Kuali Rice Team (rice.collab@kuali.org)
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       * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.krad.maintenance.MaintenanceDocument)
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              // check for duplicates if the responsibility is being copied or created
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                  // check for duplicates if particular fields of the responsibility are being edited
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             // KULRICE-8538 -- Check the route node by looping through the results below.  If it is added
106             // into the predicate, no rows are ever returned.
107             //equal("attributes[routeNodeName]", resp.getRouteNodeName())
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 }