View Javadoc

1   /**
2    * Copyright 2004-2012 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.hr.time.util;
17  
18  import java.util.Collections;
19  import java.util.HashMap;
20  import java.util.Map;
21  
22  import javax.servlet.http.HttpServletRequest;
23  
24  import org.kuali.hr.time.timesheet.TimesheetDocument;
25  import org.kuali.rice.krad.util.GlobalVariables;
26  
27  public class TKContext {
28  
29      private static final String TDOC_OBJ_KEY = "_TDOC_O_KEY";
30      private static final String TDOC_KEY = "_TDOC_ID_KEY"; // Timesheet Document ID Key
31  	//private static final String USER_KEY = "_USER_KEY";
32  
33  	private static final ThreadLocal<Map<String, Object>> STORAGE_MAP = new ThreadLocal<Map<String, Object>>() {
34  		@Override
35  		protected Map<String, Object> initialValue() {
36  			return Collections.synchronizedMap(new HashMap<String, Object>());
37  		}
38  	};
39  
40      public static TimesheetDocument getCurrentTimesheetDocument() {
41          return (TimesheetDocument)TKContext.getStorageMap().get(TDOC_OBJ_KEY);
42      }
43  
44      public static void setCurrentTimesheetDocument(TimesheetDocument tdoc) {
45          TKContext.getStorageMap().put(TDOC_OBJ_KEY, tdoc);
46      }
47  
48      /**
49       * @return The current timesheet document id, as set by the detail/clock
50       * pages.
51       */
52      public static String getCurrentTimesheetDocumentId() {
53          return (String)TKContext.getStorageMap().get(TDOC_KEY);
54      }
55  
56      /**
57       * Set the current timesheet document id.
58       * @param timesheetDocumentId The ID we are setting this value to.
59       */
60      public static void setCurrentTimesheetDocumentId(String timesheetDocumentId) {
61          TKContext.getStorageMap().put(TDOC_KEY, timesheetDocumentId);
62      }
63  
64  	/**
65  	 * TKUser has the internal concept of Backdoor User vs.Actual User.
66  	 * @return
67  	 */
68  	public static TKUser getUser() {
69          //TODO, this method isn't needed if everything in TKUser is accessed in a static fashion...
70          return new TKUser();
71  		//return (TKUser) GlobalVariables.getUserSession().retrieveObject(USER_KEY);
72  	}
73  
74  	//public static void setUser(TKUser user) {
75  	//	GlobalVariables.getUserSession().addObject(USER_KEY, user);
76  	//}
77  
78  	public static String getPrincipalId(){
79  		return GlobalVariables.getUserSession().getPrincipalId();
80  	}
81  
82      public static String getTargetPrincipalId() {
83          return TKUser.getCurrentTargetPerson().getPrincipalId();
84      }
85  
86  	public static HttpServletRequest getHttpServletRequest() {
87  		return (HttpServletRequest) getStorageMap().get("REQUEST");
88  	}
89  
90  	public static void setHttpServletRequest(HttpServletRequest request) {
91  		getStorageMap().put("REQUEST", request);
92  	}
93  
94  	public static Map<String, Object> getStorageMap() {
95  		return STORAGE_MAP.get();
96  	}
97  
98  	public static void resetStorageMap() {
99  		STORAGE_MAP.remove();
100 	}
101 
102 	public static void clear() {
103 		resetStorageMap();
104 	}
105 }