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.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   * 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 = true;
55  		GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
56  		try {
57  			ReviewResponsibilityBo resp = (ReviewResponsibilityBo)document.getNewMaintainableObject().getDataObject();
58  			// check for creation of a duplicate node
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              // KULRICE-8538 -- Check the route node by looping through the results below.  If it is added
81              // into the predicate, no rows are ever returned.
82              //equal("attributes[routeNodeName]", resp.getRouteNodeName())
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 }