View Javadoc

1   /**
2    * Copyright 2005-2012 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.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   * This is a description of what this class does - kellerj don't forget to fill this in. 
34   * 
35   * @author Kuali Rice Team (rice.collab@kuali.org)
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  	 * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.krad.maintenance.MaintenanceDocument)
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  			// check for creation of a duplicate node
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  }