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