View Javadoc

1   package org.kuali.student.r2.common.util;
2   
3   import org.kuali.rice.krad.UserSession;
4   import org.kuali.rice.krad.util.GlobalVariables;
5   import org.kuali.student.r2.common.dto.ContextInfo;
6   import org.kuali.student.r2.common.dto.LocaleInfo;
7   
8   import java.util.Date;
9   import java.util.Locale;
10  
11  /**
12   * 
13   *  This is a library of utility methods that can be used when working with the context info. 
14   * 
15   * @author Kuali Rice Team (kuali-rice@googlegroups.com)
16   *
17   */
18  public class ContextUtils {
19      
20      /**
21       * 
22       * Create a new instance of ContextInfo.
23       * 
24       * @return
25       */
26      public static ContextInfo getContextInfo(){
27          ContextInfo contextInfo = new ContextInfo();
28          UserSession userSession = GlobalVariables.getUserSession();
29          if (userSession != null) {
30              contextInfo.setPrincipalId(userSession.getPrincipalId());
31          }
32  
33          return contextInfo;
34      }
35  
36      /**
37       * Creates a default context with the current user and date and default locale
38       * @return a default context
39       */
40      public static ContextInfo createDefaultContextInfo(){
41          ContextInfo contextInfo = new ContextInfo();
42          UserSession userSession = GlobalVariables.getUserSession();
43          if (userSession != null) {
44              contextInfo.setAuthenticatedPrincipalId(userSession.getPrincipalId());
45              contextInfo.setPrincipalId(userSession.getPrincipalId());
46          }
47          contextInfo.setCurrentDate(new Date());
48          LocaleInfo localeInfo = new LocaleInfo();
49          localeInfo.setLocaleLanguage(Locale.getDefault().getLanguage());
50          localeInfo.setLocaleRegion(Locale.getDefault().getCountry());
51          contextInfo.setLocale(localeInfo);
52          return contextInfo;
53      }
54  }