View Javadoc

1   /**
2    * Copyright 2005-2014 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.kns.workflow.attribute;
17  
18  import org.apache.commons.lang.StringUtils;
19  import org.kuali.rice.kew.engine.RouteContext;
20  import org.kuali.rice.kew.api.exception.WorkflowException;
21  import org.kuali.rice.kew.role.QualifierResolver;
22  import org.kuali.rice.kim.api.KimConstants;
23  import org.kuali.rice.krad.document.Document;
24  import org.kuali.rice.krad.service.DocumentService;
25  import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
26  
27  import java.util.List;
28  import java.util.Map;
29  
30  /**
31   * @author Kuali Rice Team (rice.collab@kuali.org)
32   */
33  public abstract class QualifierResolverBase implements QualifierResolver {
34      private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QualifierResolverBase.class);
35  
36      protected static final String KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME = KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME;
37      protected static final String KIM_ATTRIBUTE_DOCUMENT_NUMBER = KimConstants.AttributeConstants.DOCUMENT_NUMBER;
38      protected static final String KIM_ATTRIBUTE_ROUTE_LEVEL_NAME = KimConstants.AttributeConstants.ROUTE_NODE_NAME;
39  
40      private static DocumentService documentService;
41      
42      /**
43       * Retrieves the document that the current route context is operating on
44       * @param context the current route context
45       * @return the document
46       */
47      protected Document getDocument(RouteContext context) {
48          String documentID = getDocumentId(context);
49          
50          if (documentID != null) {
51              try {
52                  return getDocumentService().getByDocumentHeaderIdSessionless(documentID);
53              }
54              catch (WorkflowException e) {
55                  LOG.error("Unable to retrieve document with system user.", e);
56                  return null;
57              }
58          }
59          return null;
60      }
61  
62      
63      /**
64       * Retrieves the id of the current document from the RouteContext
65       * @param context the current route context
66       * @return the id of the document
67       */
68      protected String getDocumentId(RouteContext context) {
69          final String documentID = context.getNodeInstance().getDocumentId();
70          return documentID != null ? documentID.toString() : null;
71      }
72  
73  
74  	public DocumentService getDocumentService() {
75  		if ( documentService == null ) {
76  			documentService = KRADServiceLocatorWeb.getDocumentService();
77  		}
78  		return documentService;
79  	}
80  	
81      /**
82       * Add common qualifiers to every Map<String, String> in the given List of Map<String, String>
83       * @param qualifiers a List of Map<String, String>s to add common qualifiers to
84       * @param document the document currently being routed
85       * @param documentEntry the data dictionary entry of the type of document currently being routed
86       * @param routeLevel the document's current route level
87       */
88      protected void decorateWithCommonQualifiers(List<Map<String, String>> qualifiers, RouteContext context, String customDocTypeName) {
89          for (Map<String, String> qualifier : qualifiers) {
90              addCommonQualifiersToMap(qualifier, context, customDocTypeName);
91          }
92      }
93      
94      /**
95       * Adds common qualifiers to a given Map<String, String>
96       * @param qualifier an Map<String, String> to add common qualifiers to
97       * @param document the document currently being routed
98       * @param documentEntry the data dictionary entry of the type of document currently being routed
99       * @param routeLevel the document's current route level
100      */
101     protected void addCommonQualifiersToMap(Map<String, String> qualifier, RouteContext context, String customDocTypeName) {
102         qualifier.put(KIM_ATTRIBUTE_DOCUMENT_NUMBER, context.getDocument().getDocumentId() );
103         if ( !qualifier.containsKey(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME) ) {
104 	        if ( StringUtils.isBlank(customDocTypeName)) {
105 	        	qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, 
106 	        			context.getDocument().getDocumentType().getName() );
107 	        } else {
108 	        	qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, customDocTypeName );        	
109 	        }
110         }
111         qualifier.put(KIM_ATTRIBUTE_ROUTE_LEVEL_NAME, context.getNodeInstance().getName());
112     }
113 	
114 }