1 /** 2 * Copyright 2005-2015 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.krad; 17 18 import org.kuali.rice.kew.api.KewApiConstants; 19 import org.kuali.rice.kew.api.WorkflowDocument; 20 21 import java.util.Map; 22 import java.util.concurrent.ConcurrentHashMap; 23 import java.util.concurrent.ConcurrentMap; 24 25 /** 26 * Utility class for working with the UserSession. 27 */ 28 public final class UserSessionUtils { 29 30 private UserSessionUtils() { 31 throw new IllegalStateException("this class should not be instantiated"); 32 } 33 34 /** 35 * Adds the given {@link org.kuali.rice.kew.api.WorkflowDocument} to the {@link org.kuali.rice.krad.UserSession}. 36 * @param userSession the session to add the workflow document to 37 * @param workflowDocument the workflow doc to add to the session 38 */ 39 public static void addWorkflowDocument(UserSession userSession, WorkflowDocument workflowDocument) { 40 Map<String, WorkflowDocument> workflowDocMap = getWorkflowDocumentMap(userSession); 41 42 workflowDocMap.put(workflowDocument.getDocumentId(), workflowDocument); 43 } 44 45 /** 46 * Returns the {@link org.kuali.rice.kew.api.WorkflowDocument} with the given ID from the 47 * {@link org.kuali.rice.krad.UserSession}. If there is not one cached in the session with 48 * that ID, then null is returned. 49 * @param userSession the user session from which to retrieve the workflow document 50 * @param workflowDocumentId the ID of the workflow document to get 51 * @return the cached workflow document, or null if a document with that ID is not cached in the user session 52 */ 53 public static WorkflowDocument getWorkflowDocument(UserSession userSession, String workflowDocumentId) { 54 Map<String, WorkflowDocument> workflowDocMap = getWorkflowDocumentMap(userSession); 55 56 return workflowDocMap.get(workflowDocumentId); 57 } 58 59 /** 60 * Returns the map of workflow document IDs to {@link org.kuali.rice.kew.api.WorkflowDocument}, making sure to 61 * initialize in a thread-safe way if the map does not exist. 62 * 63 * <p> 64 * We assume the {@link org.kuali.rice.krad.UserSession} is not null here. 65 * </p> 66 * @param userSession the user session from which to retrieve the workflow document 67 * @return the map of workflow document IDs to workflow documents 68 */ 69 @SuppressWarnings("unchecked") 70 private static Map<String, WorkflowDocument> getWorkflowDocumentMap(UserSession userSession) { 71 userSession.addObjectIfAbsent( 72 KewApiConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME, new ConcurrentHashMap<String, WorkflowDocument>()); 73 74 return (ConcurrentMap<String, WorkflowDocument>) userSession.retrieveObject( 75 KewApiConstants.WORKFLOW_DOCUMENT_MAP_ATTR_NAME); 76 } 77 }