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.apache.commons.lang.StringUtils; 019import org.kuali.rice.core.api.criteria.Predicate; 020import org.kuali.rice.core.api.criteria.QueryByCriteria; 021import org.kuali.rice.kew.api.KewApiConstants; 022import org.kuali.rice.kim.api.KimConstants; 023import org.kuali.rice.kim.api.responsibility.Responsibility; 024import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults; 025import org.kuali.rice.kim.api.services.KimApiServiceLocator; 026import org.kuali.rice.krad.maintenance.MaintenanceDocument; 027import org.kuali.rice.krad.rules.MaintenanceDocumentRuleBase; 028import org.kuali.rice.krad.util.GlobalVariables; 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 = super.processCustomRouteDocumentBusinessRules(document); 055 056 GlobalVariables.getMessageMap().addToErrorPath(MAINTAINABLE_ERROR_PATH); 057 try { 058 ReviewResponsibilityBo newResp = 059 (ReviewResponsibilityBo) document.getNewMaintainableObject().getDataObject(); 060 ReviewResponsibilityBo oldResp = 061 (ReviewResponsibilityBo) document.getOldMaintainableObject().getDataObject(); 062 063 // check for duplicates if the responsibility is being copied or created 064 if (!newResp.getId().equals(oldResp.getId())) { 065 if (newResp.getDocumentTypeName() != null 066 && newResp.getRouteNodeName() != null 067 && !checkForDuplicateResponsibility(newResp)) { 068 GlobalVariables.getMessageMap().putError("documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY); 069 rulesPassed &= false; 070 } 071 072 if (StringUtils.isNotBlank(newResp.getNamespaceCode()) && StringUtils.isNotBlank(newResp.getName())) { 073 rulesPassed &= validateNamespaceCodeAndName(newResp.getNamespaceCode(), newResp.getName()); 074 } 075 } else { 076 // check for duplicates if particular fields of the responsibility are being edited 077 if (newResp.getDocumentTypeName() != null && newResp.getRouteNodeName() != null 078 && (!StringUtils.equals(oldResp.getDocumentTypeName(), newResp.getDocumentTypeName()) 079 || !StringUtils.equals(oldResp.getRouteNodeName(), newResp.getRouteNodeName())) 080 && !checkForDuplicateResponsibility(newResp)) { 081 GlobalVariables.getMessageMap().putError("documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY); 082 rulesPassed &= false; 083 } 084 085 if (StringUtils.isNotBlank(newResp.getNamespaceCode()) && StringUtils.isNotBlank(newResp.getName()) && ( 086 !StringUtils.equals(oldResp.getNamespaceCode(), newResp.getNamespaceCode()) 087 || !StringUtils.equals(oldResp.getName(), newResp.getName()))) { 088 rulesPassed &= validateNamespaceCodeAndName(newResp.getNamespaceCode(), newResp.getName()); 089 } 090 } 091 092 } finally { 093 GlobalVariables.getMessageMap().removeFromErrorPath(MAINTAINABLE_ERROR_PATH); 094 } 095 return rulesPassed; 096 } 097 098 099 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}