001    /**
002     * Copyright 2005-2012 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     */
016    package org.kuali.rice.kim.impl.responsibility;
017    
018    import org.kuali.rice.core.api.criteria.Predicate;
019    import org.kuali.rice.core.api.criteria.QueryByCriteria;
020    import org.kuali.rice.kew.api.KewApiConstants;
021    import org.kuali.rice.kim.api.responsibility.Responsibility;
022    import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults;
023    import org.kuali.rice.kim.api.services.KimApiServiceLocator;
024    import org.kuali.rice.kns.document.MaintenanceDocument;
025    import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
026    import org.kuali.rice.krad.util.GlobalVariables;
027    import org.apache.commons.lang.StringUtils;
028    
029    import static org.kuali.rice.core.api.criteria.PredicateFactory.and;
030    import static org.kuali.rice.core.api.criteria.PredicateFactory.equal;
031    
032    /**
033     * This is a description of what this class does - kellerj don't forget to fill this in. 
034     * 
035     * @author Kuali Rice Team (rice.collab@kuali.org)
036     *
037     */
038    public class ReviewResponsibilityMaintenanceDocumentRule extends MaintenanceDocumentRuleBase {
039    
040            protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.reviewresponsibility.";
041            protected static final String ERROR_DUPLICATE_RESPONSIBILITY = ERROR_MESSAGE_PREFIX + "duplicateresponsibility";
042        protected static final String ERROR_NAMESPACE_AND_NAME_VALIDATION = ERROR_MESSAGE_PREFIX + "namespaceandnamevalidation";
043        protected static final String NAMESPACE_CODE_PROPERTY = "namespaceCode";
044    
045            /**
046             * @see org.kuali.rice.krad.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.krad.maintenance.MaintenanceDocument)
047             */
048            @Override
049            protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
050                    boolean rulesPassed = true;
051                    GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
052                    try {
053                            ReviewResponsibilityBo resp = (ReviewResponsibilityBo)document.getNewMaintainableObject().getDataObject();
054                            // check for creation of a duplicate node
055                            if ( resp.getDocumentTypeName() != null
056                        && resp.getRouteNodeName() != null
057                        && !checkForDuplicateResponsibility( resp ) ) {
058                                    GlobalVariables.getMessageMap().putError( "documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY );
059                                    rulesPassed &= false;
060                            }
061                 if(StringUtils.isNotBlank(resp.getNamespaceCode()) && StringUtils.isNotBlank(resp.getName()) && StringUtils.isBlank(resp.getId())){
062                    rulesPassed &=validateNamespaceCodeAndName(resp.getNamespaceCode(),resp.getName());
063                 }
064            } finally {
065                            GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
066                    }
067                    return rulesPassed;
068            }
069    
070            protected boolean checkForDuplicateResponsibility( ReviewResponsibilityBo resp ) {
071            QueryByCriteria.Builder builder = QueryByCriteria.Builder.create();
072            Predicate p = and(
073                equal("template.namespaceCode", KewApiConstants.KEW_NAMESPACE ),
074                equal("template.name", KewApiConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME),
075                equal("attributes[documentTypeName]", resp.getDocumentTypeName()),
076                equal("attributes[routeNodeName]", resp.getRouteNodeName())
077            );
078            builder.setPredicates(p);
079            ResponsibilityQueryResults results = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build());
080                    return results.getResults().isEmpty();
081            }
082    
083        protected boolean validateNamespaceCodeAndName(String namespaceCode,String name){
084            Responsibility responsibility = KimApiServiceLocator.getResponsibilityService().findRespByNamespaceCodeAndName(namespaceCode,name);
085    
086            if(null != responsibility){
087               GlobalVariables.getMessageMap().putError(NAMESPACE_CODE_PROPERTY,ERROR_NAMESPACE_AND_NAME_VALIDATION,namespaceCode,name);
088               return false;
089            } else {
090                return true;
091            }
092    
093        }
094    }