| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 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 | |
|
| 36 | |
|
| 37 | |
|
| 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 | |
|
| 49 | |
|
| 50 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
} |