001/** 002 * Copyright 2005-2016 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.rice.kim.impl.responsibility; 017 018import org.kuali.rice.core.api.criteria.Predicate; 019import org.kuali.rice.core.api.criteria.QueryByCriteria; 020import org.kuali.rice.kew.api.KewApiConstants; 021import org.kuali.rice.kim.api.KimConstants; 022import org.kuali.rice.kim.api.responsibility.Responsibility; 023import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults; 024import org.kuali.rice.kim.api.services.KimApiServiceLocator; 025import org.kuali.rice.kns.document.MaintenanceDocument; 026import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase; 027import org.kuali.rice.krad.util.GlobalVariables; 028import org.apache.commons.lang.StringUtils; 029 030import java.util.ArrayList; 031import java.util.List; 032 033import static org.kuali.rice.core.api.criteria.PredicateFactory.and; 034import static org.kuali.rice.core.api.criteria.PredicateFactory.equal; 035 036/** 037 * This is a description of what this class does - kellerj don't forget to fill this in. 038 * 039 * @author Kuali Rice Team (rice.collab@kuali.org) 040 * 041 */ 042public class ReviewResponsibilityMaintenanceDocumentRule extends MaintenanceDocumentRuleBase { 043 044 protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.reviewresponsibility."; 045 protected static final String ERROR_DUPLICATE_RESPONSIBILITY = ERROR_MESSAGE_PREFIX + "duplicateresponsibility"; 046 protected static final String ERROR_NAMESPACE_AND_NAME_VALIDATION = ERROR_MESSAGE_PREFIX + "namespaceandnamevalidation"; 047 protected static final String NAMESPACE_CODE_PROPERTY = "namespaceCode"; 048 049 /** 050 * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.krad.maintenance.MaintenanceDocument) 051 */ 052 @Override 053 protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { 054 boolean rulesPassed = true; 055 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH ); 056 try { 057 ReviewResponsibilityBo resp = (ReviewResponsibilityBo)document.getNewMaintainableObject().getDataObject(); 058 // check for creation of a duplicate node 059 if ( resp.getDocumentTypeName() != null 060 && resp.getRouteNodeName() != null 061 && !checkForDuplicateResponsibility( resp ) ) { 062 GlobalVariables.getMessageMap().putError( "documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY ); 063 rulesPassed &= false; 064 } 065 if(StringUtils.isNotBlank(resp.getNamespaceCode()) && StringUtils.isNotBlank(resp.getName()) && StringUtils.isBlank(resp.getId())){ 066 rulesPassed &=validateNamespaceCodeAndName(resp.getNamespaceCode(),resp.getName()); 067 } 068 } finally { 069 GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH ); 070 } 071 return rulesPassed; 072 } 073 074 protected boolean checkForDuplicateResponsibility( ReviewResponsibilityBo resp ) { 075 QueryByCriteria.Builder builder = QueryByCriteria.Builder.create(); 076 Predicate p = and( 077 equal("template.namespaceCode", KewApiConstants.KEW_NAMESPACE ), 078 equal("template.name", KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME), 079 equal("attributes[documentTypeName]", resp.getDocumentTypeName()) 080 // KULRICE-8538 -- Check the route node by looping through the results below. If it is added 081 // into the predicate, no rows are ever returned. 082 //equal("attributes[routeNodeName]", resp.getRouteNodeName()) 083 ); 084 builder.setPredicates(p); 085 ResponsibilityQueryResults results = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build()); 086 List<Responsibility> responsibilities = new ArrayList<Responsibility>(); 087 088 if ( !results.getResults().isEmpty() ) { 089 for ( Responsibility responsibility : results.getResults() ) { 090 String routeNodeName = responsibility.getAttributes().get( KimConstants.AttributeConstants.ROUTE_NODE_NAME); 091 if (StringUtils.isNotEmpty(routeNodeName) && StringUtils.equals(routeNodeName, resp.getRouteNodeName())){ 092 responsibilities.add(responsibility); 093 } 094 } 095 } 096 097 return responsibilities.isEmpty(); 098 } 099 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}