Coverage Report - org.kuali.rice.kim.document.rule.ReviewResponsibilityMaintenanceDocumentRule
 
Classes in this File Line Coverage Branch Coverage Complexity
ReviewResponsibilityMaintenanceDocumentRule
0%
0/38
0%
0/16
2.8
 
 1  
 /*
 2  
  * Copyright 2007-2009 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.document.rule;
 17  
 
 18  
 import java.util.Collection;
 19  
 import java.util.HashMap;
 20  
 import java.util.HashSet;
 21  
 import java.util.List;
 22  
 
 23  
 import org.kuali.rice.kew.doctype.bo.DocumentType;
 24  
 import org.kuali.rice.kew.engine.node.RouteNode;
 25  
 import org.kuali.rice.kew.service.KEWServiceLocator;
 26  
 import org.kuali.rice.kew.util.KEWConstants;
 27  
 import org.kuali.rice.kim.bo.impl.ReviewResponsibility;
 28  
 import org.kuali.rice.kim.bo.role.dto.KimResponsibilityInfo;
 29  
 import org.kuali.rice.kim.service.KIMServiceLocator;
 30  
 import org.kuali.rice.kns.document.MaintenanceDocument;
 31  
 import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase;
 32  
 import org.kuali.rice.kns.util.GlobalVariables;
 33  
 
 34  
 /**
 35  
  * This is a description of what this class does - kellerj don't forget to fill this in. 
 36  
  * 
 37  
  * @author Kuali Rice Team (rice.collab@kuali.org)
 38  
  *
 39  
  */
 40  0
 public class ReviewResponsibilityMaintenanceDocumentRule extends
 41  
                 MaintenanceDocumentRuleBase {
 42  
 
 43  
         protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.reviewresponsibility.";
 44  
         protected static final String ERROR_INVALID_ROUTE_NODE = ERROR_MESSAGE_PREFIX + "invalidroutenode";
 45  
         protected static final String ERROR_DUPLICATE_RESPONSIBILITY = ERROR_MESSAGE_PREFIX + "duplicateresponsibility";
 46  
 
 47  
         /**
 48  
          * This overridden method ...
 49  
          * 
 50  
          * @see org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase#processCustomRouteDocumentBusinessRules(org.kuali.rice.kns.document.MaintenanceDocument)
 51  
          */
 52  
         @Override
 53  
         protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) {
 54  0
                 boolean rulesPassed = true;
 55  0
                 GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH );
 56  
                 try {
 57  0
                         ReviewResponsibility resp = (ReviewResponsibility)document.getNewMaintainableObject().getBusinessObject();
 58  
                         // check the route level exists on the document or a child
 59  0
                         HashSet<String> routeNodeNames = getAllPossibleRouteNodeNames( resp.getDocumentTypeName() );
 60  0
                         if ( !routeNodeNames.contains( resp.getRouteNodeName() ) ) {
 61  0
                                 GlobalVariables.getMessageMap().putError( "routeNodeName", ERROR_INVALID_ROUTE_NODE, resp.getRouteNodeName() );
 62  0
                                 rulesPassed = false;
 63  
                         }
 64  
                         // check for creation of a duplicate node
 65  0
                         if ( !checkForDuplicateResponsibility( resp ) ) {
 66  0
                                 GlobalVariables.getMessageMap().putError( "documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY );
 67  0
                                 rulesPassed = false;
 68  
                         }
 69  0
                 } catch ( RuntimeException ex ) {
 70  0
                         LOG.error( "Error in processCustomRouteDocumentBusinessRules()", ex );
 71  0
                         throw ex;
 72  
                 } finally {
 73  0
                         GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH );
 74  0
                 }
 75  0
                 return rulesPassed;
 76  
         }
 77  
         
 78  
         protected HashSet<String> getAllPossibleRouteNodeNames( String documentTypeName ) {
 79  0
                 DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName( documentTypeName );
 80  0
                 HashSet<String> routeNodeNames = new HashSet<String>();
 81  0
                 if ( docType != null ) {
 82  0
                         addNodesForDocType( docType, routeNodeNames );
 83  0
                         addNodesForChildDocTypes( docType, routeNodeNames );
 84  
                 }
 85  0
                 return routeNodeNames;
 86  
         }
 87  
         
 88  
         @SuppressWarnings("unchecked")
 89  
         protected void addNodesForDocType( DocumentType docType, HashSet<String> routeNodeNames ) {
 90  0
                 List<RouteNode> routeNodes = KEWServiceLocator.getRouteNodeService().getFlattenedNodes( docType, true );
 91  0
                 for ( RouteNode node : routeNodes ) {
 92  
                         // only add request nodes (not split or join nodes)
 93  0
                         if ( node.isRoleNode() ) {
 94  0
                                 routeNodeNames.add( node.getRouteNodeName() );
 95  
                         }
 96  
                 }
 97  0
         }
 98  
         @SuppressWarnings("unchecked")
 99  
         protected void addNodesForChildDocTypes( DocumentType docType, HashSet<String> routeNodeNames ) {
 100  0
                 for ( DocumentType childDocType : (Collection<DocumentType>)docType.getChildrenDocTypes() ) {
 101  0
                         addNodesForDocType( childDocType, routeNodeNames );
 102  0
                         addNodesForChildDocTypes( childDocType, routeNodeNames );
 103  
                 }
 104  0
         }
 105  
         protected boolean checkForDuplicateResponsibility( ReviewResponsibility resp ) {
 106  0
                 HashMap<String,String> criteria = new HashMap<String,String>();
 107  0
                 criteria.put( "template.namespaceCode", KEWConstants.KEW_NAMESPACE );
 108  0
                 criteria.put( "template.name", KEWConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME );
 109  0
                 criteria.put( "detailCriteria", "documentTypeName="+resp.getDocumentTypeName()+",routeNodeName="+resp.getRouteNodeName() );
 110  0
                 List<? extends KimResponsibilityInfo> results = KIMServiceLocator.getResponsibilityService().lookupResponsibilityInfo( criteria, true );
 111  0
                 return results.isEmpty() || results.get(0).getResponsibilityId().equals( resp.getResponsibilityId() );
 112  
         }
 113  
 }