Coverage Report - org.kuali.student.common.ui.client.application.ApplicationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationContext
0%
0/58
0%
0/12
1.35
ApplicationContext$1
0%
0/4
N/A
1.35
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.application;
 17  
 
 18  
 import java.util.ArrayList;
 19  
 import java.util.HashMap;
 20  
 import java.util.List;
 21  
 import java.util.Map;
 22  
 
 23  
 import org.kuali.student.common.ui.client.security.SecurityContext;
 24  
 import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService;
 25  
 import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync;
 26  
 import org.kuali.student.core.messages.dto.Message;
 27  
 
 28  
 import com.google.gwt.core.client.GWT;
 29  
 import com.google.gwt.user.client.rpc.AsyncCallback;
 30  
 
 31  
 /**
 32  
  * The application contains information about who is currently logged in, the security context, and access
 33  
  * to messages loaded from the message service in the app.  It provides and a static way to obtain this
 34  
  * information across the entire app.
 35  
  * 
 36  
  * @author Kuali Student
 37  
  *
 38  
  */
 39  0
 public class ApplicationContext {
 40  0
         private ServerPropertiesRpcServiceAsync serverPropertiesRpcService = GWT.create(ServerPropertiesRpcService.class);
 41  
         
 42  0
         private boolean loggedIn = true;
 43  0
         private String userId = "testuser";
 44  0
         private String version = "KS";
 45  0
         private List<String> roles = new ArrayList<String>();
 46  
         
 47  0
         private Map<String, Map<String, String>> messages = new HashMap<String, Map<String,String>>();
 48  0
         private Map<String, String> flatMessages = new HashMap<String, String>();
 49  0
         private List<Message> messagesList = new ArrayList<Message>();
 50  
         
 51  
         private SecurityContext securityContext;
 52  
         private String applicationContextUrl;
 53  
         
 54  
         /**
 55  
          * This constructor should only be visible to the common application package. If ApplicationContext is 
 56  
          * required outside this package do Application.getApplicationContext();
 57  
          */
 58  0
         protected ApplicationContext() {
 59  0
                 roles.add("role1");
 60  0
                 roles.add("role2");
 61  
                 
 62  0
                 serverPropertiesRpcService.getContextPath(new AsyncCallback<String>(){
 63  
 
 64  
                         @Override
 65  
                         public void onFailure(Throwable caught) {
 66  0
                                 throw new RuntimeException("Fatal - Unable to initialze application context");
 67  
                         }
 68  
 
 69  
                         @Override
 70  
                         public void onSuccess(String result) {
 71  0
                                 applicationContextUrl = result;
 72  0
                         }                        
 73  
                 });
 74  0
         }
 75  
 
 76  
         public void setLoggedIn(boolean loggedIn) {
 77  0
                 this.loggedIn = loggedIn;
 78  0
         }
 79  
 
 80  
         public void setUserId(String userId) {
 81  0
                 this.userId = userId;
 82  0
         }
 83  
 
 84  
         public void setRoles(List<String> roles) {
 85  0
                 this.roles = roles;
 86  0
         }
 87  
 
 88  
         public boolean isLoggedIn() {
 89  0
                 return loggedIn;
 90  
         }
 91  
 
 92  
         public String getUserId() {
 93  0
                 return userId;
 94  
         }
 95  
 
 96  
         public List<String> getRoles() {
 97  0
                 return roles;
 98  
         }
 99  
 
 100  
     /**
 101  
      * Adds the messages in the list of messages to the map of the messages
 102  
      * @param messages
 103  
      */
 104  
     public void addMessages(List<Message> messages) {
 105  0
                 messagesList.addAll(messages);
 106  0
             for (Message m : messages) {
 107  0
                 String groupName = m.getGroupName();
 108  0
                 Map<String, String> group = this.messages.get(groupName);
 109  0
                 if (group == null) {
 110  0
                     group = new HashMap<String, String>();
 111  0
                     this.messages.put(groupName, group);
 112  
                 }
 113  0
                 group.put(m.getId(), m.getValue());
 114  0
                 flatMessages.put(m.getId(), m.getValue());
 115  0
             }
 116  0
         }
 117  
         
 118  
         /**
 119  
          * Get a message by a unique id
 120  
          */
 121  
         public String getMessage(String messageId) {
 122  0
             return flatMessages.get(messageId);
 123  
     }
 124  
     
 125  
         /**
 126  
          * Returns all the messages in the ApplicationContext
 127  
          */
 128  
         public List<Message> getMessages() {
 129  0
             return messagesList;
 130  
     }
 131  
     
 132  
         
 133  
         /**
 134  
          * Get message by the group it is in and its unique id within that group
 135  
          */
 136  
         public String getMessage(String groupName, String messageId) {
 137  
                         
 138  0
             String result = null;
 139  
             
 140  0
             Map<String, String> group = this.messages.get(groupName);
 141  0
             if (group != null) {
 142  0
                 result = group.get(messageId);
 143  
             }
 144  
             
 145  0
             return result;
 146  
         }
 147  
         
 148  
     /**
 149  
      * 
 150  
      * This method looks up a UI Label in the messages cache.  
 151  
      * First looks for a label specific to the type and state of the field.
 152  
      * If none found try for a generalized label.
 153  
      * Otherwise return the supplied fieldId
 154  
      * Groups provide namespace for same label ids within different LUs
 155  
      * 
 156  
      * @param groupName - for example 'course' or 'program'
 157  
      * @param type
 158  
      * @param state
 159  
      * @param fieldId
 160  
      * @return
 161  
      */
 162  
          public String getUILabel(String groupName, String type, String state, String fieldId) {
 163  
 
 164  0
         String label = getMessage(groupName, type + ":" + state + ":" + fieldId);
 165  
         
 166  0
         if (label == null)
 167  0
             label = getMessage(groupName, fieldId);
 168  
         
 169  0
         if (label == null)
 170  0
             label =  fieldId;
 171  
         
 172  0
         return label;
 173  
         
 174  
     }
 175  
          
 176  
         /**
 177  
          * Same as getUILabel(String groupName, String type, String state, String fieldId) with no
 178  
          * type and state needed
 179  
          */
 180  
         public String getUILabel(String groupName, String fieldId) {
 181  
 
 182  0
                 String label = getMessage(groupName, fieldId);
 183  
                 
 184  0
                 if (label == null)
 185  0
                     label =  fieldId;
 186  
                 
 187  0
                 return label;
 188  
                 
 189  
         }
 190  
 
 191  
     /**
 192  
      * Get the security context for the app
 193  
      * @return SecurityContext
 194  
      */
 195  
     public SecurityContext getSecurityContext() {
 196  0
         return securityContext;
 197  
     }
 198  
 
 199  
     public void setSecurityContext(SecurityContext securityContext) {
 200  0
         this.securityContext = securityContext;
 201  0
     }
 202  
         
 203  
         /**
 204  
          * Application URL based on the serverPropertiesRPC service result
 205  
          */
 206  
         public String getApplicationContextUrl() {
 207  0
                 return applicationContextUrl;
 208  
         }
 209  
 
 210  
         public void setVersion(String version) {
 211  0
                 this.version = version;
 212  0
         }
 213  
 
 214  
         public String getVersion() {
 215  0
                 return version;
 216  
         }
 217  
         
 218  
 }