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.krad;
017
018import org.kuali.rice.kew.api.KewApiConstants;
019import org.kuali.rice.kew.api.WorkflowDocument;
020
021import java.util.Map;
022import java.util.concurrent.ConcurrentHashMap;
023import java.util.concurrent.ConcurrentMap;
024
025/**
026 * Utility class for working with the UserSession.
027 */
028public final class UserSessionUtils {
029
030    private UserSessionUtils() {
031        throw new IllegalStateException("this class should not be instantiated");
032    }
033
034    /**
035     * Adds the given {@link org.kuali.rice.kew.api.WorkflowDocument} to the {@link org.kuali.rice.krad.UserSession}.
036     * @param userSession the session to add the workflow document to
037     * @param workflowDocument the workflow doc to add to the session
038     */
039    public static void addWorkflowDocument(UserSession userSession, WorkflowDocument workflowDocument) {
040        Map<String, WorkflowDocument> workflowDocMap = getWorkflowDocumentMap(userSession);
041
042        workflowDocMap.put(workflowDocument.getDocumentId(), workflowDocument);
043    }
044
045    /**
046     * Returns the {@link org.kuali.rice.kew.api.WorkflowDocument} with the given ID from the
047     * {@link org.kuali.rice.krad.UserSession}.  If there is not one cached in the session with
048     * that ID, then null is returned.
049     * @param userSession the user session from which to retrieve the workflow document
050     * @param workflowDocumentId the ID of the workflow document to get
051     * @return the cached workflow document, or null if a document with that ID is not cached in the user session
052     */
053    public static WorkflowDocument getWorkflowDocument(UserSession userSession, String workflowDocumentId) {
054        Map<String, WorkflowDocument> workflowDocMap = getWorkflowDocumentMap(userSession);
055
056        return workflowDocMap.get(workflowDocumentId);
057    }
058
059    /**
060     * Returns the map of workflow document IDs to {@link org.kuali.rice.kew.api.WorkflowDocument}, making sure to
061     * initialize in a thread-safe way if the map does not exist.
062     *
063     * <p>
064     * We assume the {@link org.kuali.rice.krad.UserSession} is not null here.
065     * </p>
066     * @param userSession the user session from which to retrieve the workflow document
067     * @return the map of workflow document IDs to workflow documents
068     */
069    private static Map<String, WorkflowDocument> getWorkflowDocumentMap(UserSession userSession) {
070        synchronized (userSession) {
071            @SuppressWarnings("unchecked") ConcurrentMap<String, WorkflowDocument> workflowDocMap =
072                    (ConcurrentMap<String, WorkflowDocument>) userSession
073                            .retrieveObject(KewApiConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME);
074
075            if (workflowDocMap == null) {
076                workflowDocMap = new ConcurrentHashMap<String, WorkflowDocument>();
077                userSession.addObject(KewApiConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, workflowDocMap);
078            }
079
080            return workflowDocMap;
081        }
082    }
083}