| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
package org.kuali.rice.kim.impl.responsibility; |
| 17 | |
|
| 18 | |
import org.kuali.rice.core.api.criteria.Predicate; |
| 19 | |
import org.kuali.rice.core.api.criteria.QueryByCriteria; |
| 20 | |
import org.kuali.rice.kew.doctype.bo.DocumentType; |
| 21 | |
import org.kuali.rice.kew.engine.node.RouteNode; |
| 22 | |
import org.kuali.rice.kew.service.KEWServiceLocator; |
| 23 | |
import org.kuali.rice.kew.util.KEWConstants; |
| 24 | |
import org.kuali.rice.kim.api.responsibility.ResponsibilityQueryResults; |
| 25 | |
import org.kuali.rice.kim.api.services.KimApiServiceLocator; |
| 26 | |
import org.kuali.rice.kns.document.MaintenanceDocument; |
| 27 | |
import org.kuali.rice.kns.maintenance.rules.MaintenanceDocumentRuleBase; |
| 28 | |
import org.kuali.rice.kns.util.GlobalVariables; |
| 29 | |
|
| 30 | |
import java.util.Collection; |
| 31 | |
import java.util.HashSet; |
| 32 | |
import java.util.List; |
| 33 | |
|
| 34 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.and; |
| 35 | |
import static org.kuali.rice.core.api.criteria.PredicateFactory.equal; |
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | 0 | public class ReviewResponsibilityMaintenanceDocumentRule extends |
| 44 | |
MaintenanceDocumentRuleBase { |
| 45 | |
|
| 46 | |
protected static final String ERROR_MESSAGE_PREFIX = "error.document.kim.reviewresponsibility."; |
| 47 | |
protected static final String ERROR_INVALID_ROUTE_NODE = ERROR_MESSAGE_PREFIX + "invalidroutenode"; |
| 48 | |
protected static final String ERROR_DUPLICATE_RESPONSIBILITY = ERROR_MESSAGE_PREFIX + "duplicateresponsibility"; |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
@Override |
| 56 | |
protected boolean processCustomRouteDocumentBusinessRules(MaintenanceDocument document) { |
| 57 | 0 | boolean rulesPassed = true; |
| 58 | 0 | GlobalVariables.getMessageMap().addToErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 59 | |
try { |
| 60 | 0 | ReviewResponsibilityBo resp = (ReviewResponsibilityBo)document.getNewMaintainableObject().getBusinessObject(); |
| 61 | |
|
| 62 | 0 | HashSet<String> routeNodeNames = getAllPossibleRouteNodeNames( resp.getDocumentTypeName() ); |
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | 0 | if ( !checkForDuplicateResponsibility( resp ) ) { |
| 69 | 0 | GlobalVariables.getMessageMap().putError( "documentTypeName", ERROR_DUPLICATE_RESPONSIBILITY ); |
| 70 | 0 | rulesPassed = false; |
| 71 | |
} |
| 72 | 0 | } catch ( RuntimeException ex ) { |
| 73 | 0 | LOG.error( "Error in processCustomRouteDocumentBusinessRules()", ex ); |
| 74 | 0 | throw ex; |
| 75 | |
} finally { |
| 76 | 0 | GlobalVariables.getMessageMap().removeFromErrorPath( MAINTAINABLE_ERROR_PATH ); |
| 77 | 0 | } |
| 78 | 0 | return rulesPassed; |
| 79 | |
} |
| 80 | |
|
| 81 | |
protected HashSet<String> getAllPossibleRouteNodeNames( String documentTypeName ) { |
| 82 | 0 | DocumentType docType = KEWServiceLocator.getDocumentTypeService().findByName( documentTypeName ); |
| 83 | 0 | HashSet<String> routeNodeNames = new HashSet<String>(); |
| 84 | 0 | if ( docType != null ) { |
| 85 | 0 | addNodesForDocType( docType, routeNodeNames ); |
| 86 | 0 | addNodesForChildDocTypes( docType, routeNodeNames ); |
| 87 | |
} |
| 88 | 0 | return routeNodeNames; |
| 89 | |
} |
| 90 | |
|
| 91 | |
@SuppressWarnings("unchecked") |
| 92 | |
protected void addNodesForDocType( DocumentType docType, HashSet<String> routeNodeNames ) { |
| 93 | 0 | List<RouteNode> routeNodes = KEWServiceLocator.getRouteNodeService().getFlattenedNodes( docType, true ); |
| 94 | 0 | for ( RouteNode node : routeNodes ) { |
| 95 | |
|
| 96 | 0 | if ( node.isRoleNode() ) { |
| 97 | 0 | routeNodeNames.add( node.getRouteNodeName() ); |
| 98 | |
} |
| 99 | |
} |
| 100 | 0 | } |
| 101 | |
@SuppressWarnings("unchecked") |
| 102 | |
protected void addNodesForChildDocTypes( DocumentType docType, HashSet<String> routeNodeNames ) { |
| 103 | 0 | for ( DocumentType childDocType : (Collection<DocumentType>)docType.getChildrenDocTypes() ) { |
| 104 | 0 | addNodesForDocType( childDocType, routeNodeNames ); |
| 105 | 0 | addNodesForChildDocTypes( childDocType, routeNodeNames ); |
| 106 | |
} |
| 107 | 0 | } |
| 108 | |
protected boolean checkForDuplicateResponsibility( ReviewResponsibilityBo resp ) { |
| 109 | 0 | QueryByCriteria.Builder builder = QueryByCriteria.Builder.create(); |
| 110 | 0 | Predicate p = and( |
| 111 | |
equal("template.namespaceCode", KEWConstants.KEW_NAMESPACE ), |
| 112 | |
equal("template.name", KEWConstants.DEFAULT_RESPONSIBILITY_TEMPLATE_NAME), |
| 113 | |
equal("attributes[documentTypeName]", resp.getDocumentTypeName()), |
| 114 | |
equal("attributes[routeNodeName]", resp.getRouteNodeName()) |
| 115 | |
); |
| 116 | 0 | builder.setPredicates(p); |
| 117 | 0 | ResponsibilityQueryResults results = KimApiServiceLocator.getResponsibilityService().findResponsibilities(builder.build()); |
| 118 | 0 | return results.getResults().isEmpty() || results.getResults().get(0).getId().equals( resp.getResponsibilityId() ); |
| 119 | |
} |
| 120 | |
} |