001/**
002 * Copyright 2005-2016 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 */
016package org.kuali.rice.kns.workflow.attribute;
017
018import org.apache.commons.lang.StringUtils;
019import org.kuali.rice.kew.engine.RouteContext;
020import org.kuali.rice.kew.api.exception.WorkflowException;
021import org.kuali.rice.kew.role.QualifierResolver;
022import org.kuali.rice.kim.api.KimConstants;
023import org.kuali.rice.krad.document.Document;
024import org.kuali.rice.krad.service.DocumentService;
025import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
026
027import java.util.List;
028import java.util.Map;
029
030/**
031 * @author Kuali Rice Team (rice.collab@kuali.org)
032 */
033public abstract class QualifierResolverBase implements QualifierResolver {
034    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QualifierResolverBase.class);
035
036    protected static final String KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME = KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME;
037    protected static final String KIM_ATTRIBUTE_DOCUMENT_NUMBER = KimConstants.AttributeConstants.DOCUMENT_NUMBER;
038    protected static final String KIM_ATTRIBUTE_ROUTE_LEVEL_NAME = KimConstants.AttributeConstants.ROUTE_NODE_NAME;
039
040    private static DocumentService documentService;
041    
042    /**
043     * Retrieves the document that the current route context is operating on
044     * @param context the current route context
045     * @return the document
046     */
047    protected Document getDocument(RouteContext context) {
048        String documentID = getDocumentId(context);
049        
050        if (documentID != null) {
051            try {
052                return getDocumentService().getByDocumentHeaderIdSessionless(documentID);
053            }
054            catch (WorkflowException e) {
055                LOG.error("Unable to retrieve document with system user.", e);
056                return null;
057            }
058        }
059        return null;
060    }
061
062    
063    /**
064     * Retrieves the id of the current document from the RouteContext
065     * @param context the current route context
066     * @return the id of the document
067     */
068    protected String getDocumentId(RouteContext context) {
069        final String documentID = context.getNodeInstance().getDocumentId();
070        return documentID != null ? documentID.toString() : null;
071    }
072
073
074        public DocumentService getDocumentService() {
075                if ( documentService == null ) {
076                        documentService = KRADServiceLocatorWeb.getDocumentService();
077                }
078                return documentService;
079        }
080        
081    /**
082     * Add common qualifiers to every Map<String, String> in the given List of Map<String, String>
083     * @param qualifiers a List of Map<String, String>s to add common qualifiers to
084     * @param document the document currently being routed
085     * @param documentEntry the data dictionary entry of the type of document currently being routed
086     * @param routeLevel the document's current route level
087     */
088    protected void decorateWithCommonQualifiers(List<Map<String, String>> qualifiers, RouteContext context, String customDocTypeName) {
089        for (Map<String, String> qualifier : qualifiers) {
090            addCommonQualifiersToMap(qualifier, context, customDocTypeName);
091        }
092    }
093    
094    /**
095     * Adds common qualifiers to a given Map<String, String>
096     * @param qualifier an Map<String, String> to add common qualifiers to
097     * @param document the document currently being routed
098     * @param documentEntry the data dictionary entry of the type of document currently being routed
099     * @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}