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 *
033 * @deprecated Only used by KNS classes, no replacement.
034 */
035@Deprecated
036public abstract class QualifierResolverBase implements QualifierResolver {
037    private static org.apache.log4j.Logger LOG = org.apache.log4j.Logger.getLogger(QualifierResolverBase.class);
038
039    protected static final String KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME = KimConstants.AttributeConstants.DOCUMENT_TYPE_NAME;
040    protected static final String KIM_ATTRIBUTE_DOCUMENT_NUMBER = KimConstants.AttributeConstants.DOCUMENT_NUMBER;
041    protected static final String KIM_ATTRIBUTE_ROUTE_LEVEL_NAME = KimConstants.AttributeConstants.ROUTE_NODE_NAME;
042
043    private static DocumentService documentService;
044    
045    /**
046     * Retrieves the document that the current route context is operating on
047     * @param context the current route context
048     * @return the document
049     */
050    protected Document getDocument(RouteContext context) {
051        String documentID = getDocumentId(context);
052        
053        if (documentID != null) {
054            try {
055                return getDocumentService().getByDocumentHeaderIdSessionless(documentID);
056            }
057            catch (WorkflowException e) {
058                LOG.error("Unable to retrieve document with system user.", e);
059                return null;
060            }
061        }
062        return null;
063    }
064
065    
066    /**
067     * Retrieves the id of the current document from the RouteContext
068     * @param context the current route context
069     * @return the id of the document
070     */
071    protected String getDocumentId(RouteContext context) {
072        final String documentID = context.getNodeInstance().getDocumentId();
073        return documentID != null ? documentID.toString() : null;
074    }
075
076
077        public DocumentService getDocumentService() {
078                if ( documentService == null ) {
079                        documentService = KRADServiceLocatorWeb.getDocumentService();
080                }
081                return documentService;
082        }
083        
084    /**
085     * Add common qualifiers to every Map<String, String> in the given List of Map<String, String>
086     * @param qualifiers a List of Map<String, String>s to add common qualifiers to
087     * @param document the document currently being routed
088     * @param documentEntry the data dictionary entry of the type of document currently being routed
089     * @param routeLevel the document's current route level
090     */
091    protected void decorateWithCommonQualifiers(List<Map<String, String>> qualifiers, RouteContext context, String customDocTypeName) {
092        for (Map<String, String> qualifier : qualifiers) {
093            addCommonQualifiersToMap(qualifier, context, customDocTypeName);
094        }
095    }
096    
097    /**
098     * Adds common qualifiers to a given Map<String, String>
099     * @param qualifier an Map<String, String> to add common qualifiers to
100     * @param document the document currently being routed
101     * @param documentEntry the data dictionary entry of the type of document currently being routed
102     * @param routeLevel the document's current route level
103     */
104    protected void addCommonQualifiersToMap(Map<String, String> qualifier, RouteContext context, String customDocTypeName) {
105        qualifier.put(KIM_ATTRIBUTE_DOCUMENT_NUMBER, context.getDocument().getDocumentId() );
106        if ( !qualifier.containsKey(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME) ) {
107                if ( StringUtils.isBlank(customDocTypeName)) {
108                        qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, 
109                                        context.getDocument().getDocumentType().getName() );
110                } else {
111                        qualifier.put(KIM_ATTRIBUTE_DOCUMENT_TYPE_NAME, customDocTypeName );            
112                }
113        }
114        qualifier.put(KIM_ATTRIBUTE_ROUTE_LEVEL_NAME, context.getNodeInstance().getName());
115    }
116        
117}