Coverage Report - org.kuali.student.common.ui.client.application.ApplicationContext
 
Classes in this File Line Coverage Branch Coverage Complexity
ApplicationContext
0%
0/137
0%
0/64
2.167
ApplicationContext$1
0%
0/4
N/A
2.167
 
 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.HashSet;
 21  
 import java.util.Iterator;
 22  
 import java.util.List;
 23  
 import java.util.Map;
 24  
 import java.util.Map.Entry;
 25  
 
 26  
 import org.kuali.student.common.messages.dto.Message;
 27  
 import org.kuali.student.common.ui.client.configurable.mvc.FieldDescriptor;
 28  
 import org.kuali.student.common.ui.client.mvc.HasCrossConstraints;
 29  
 import org.kuali.student.common.ui.client.security.SecurityContext;
 30  
 import org.kuali.student.common.ui.client.service.ServerPropertiesRpcService;
 31  
 import org.kuali.student.common.ui.client.service.ServerPropertiesRpcServiceAsync;
 32  
 
 33  
 import com.google.gwt.core.client.GWT;
 34  
 import com.google.gwt.user.client.rpc.AsyncCallback;
 35  
 
 36  
 /**
 37  
  * The application contains information about who is currently logged in, the security context, and access
 38  
  * to messages loaded from the message service in the app.  It provides and a static way to obtain this
 39  
  * information across the entire app.
 40  
  * 
 41  
  * @author Kuali Student
 42  
  *
 43  
  */
 44  0
 public class ApplicationContext {
 45  0
         private ServerPropertiesRpcServiceAsync serverPropertiesRpcService = GWT.create(ServerPropertiesRpcService.class);
 46  
         
 47  0
         private boolean loggedIn = true;
 48  0
         private String userId = "testuser";
 49  0
         private String version = "KS";
 50  0
         private List<String> roles = new ArrayList<String>();
 51  
         
 52  0
         private Map<String, Map<String, String>> messages = new HashMap<String, Map<String,String>>();
 53  0
         private Map<String, String> flatMessages = new HashMap<String, String>();
 54  0
         private List<Message> messagesList = new ArrayList<Message>();
 55  
         
 56  
         private SecurityContext securityContext;
 57  
         private String applicationContextUrl;
 58  
         
 59  
         //These maps are used to store query paths to their corresponding fieldDefinitions, and also whcih fields have cross constraints
 60  0
         private String parentPath = "";
 61  0
         private HashMap<String,HashMap<String,FieldDescriptor>> pathToFieldMapping = new HashMap<String,HashMap<String,FieldDescriptor>>();
 62  0
         private HashMap<String,HashMap<String,HashSet<HasCrossConstraints>>> crossConstraints = new HashMap<String,HashMap<String,HashSet<HasCrossConstraints>>>();
 63  
 //        private HashMap<String,HashMap<FieldDescriptor, String>> defaultValueMapping = new HashMap<String,HashMap<FieldDescriptor, String>>();
 64  
         /**
 65  
          * This constructor should only be visible to the common application package. If ApplicationContext is 
 66  
          * required outside this package do Application.getApplicationContext();
 67  
          */
 68  0
         protected ApplicationContext() {
 69  0
                 roles.add("role1");
 70  0
                 roles.add("role2");
 71  
                 
 72  0
                 serverPropertiesRpcService.getContextPath(new AsyncCallback<String>(){
 73  
 
 74  
                         @Override
 75  
                         public void onFailure(Throwable caught) {
 76  0
                                 throw new RuntimeException("Fatal - Unable to initialze application context");
 77  
                         }
 78  
 
 79  
                         @Override
 80  
                         public void onSuccess(String result) {
 81  0
                                 applicationContextUrl = result;
 82  0
                         }                        
 83  
                 });
 84  0
         }
 85  
 
 86  
         public void setLoggedIn(boolean loggedIn) {
 87  0
                 this.loggedIn = loggedIn;
 88  0
         }
 89  
 
 90  
         public void setUserId(String userId) {
 91  0
                 this.userId = userId;
 92  0
         }
 93  
 
 94  
         public void setRoles(List<String> roles) {
 95  0
                 this.roles = roles;
 96  0
         }
 97  
 
 98  
         public boolean isLoggedIn() {
 99  0
                 return loggedIn;
 100  
         }
 101  
 
 102  
         public String getUserId() {
 103  0
                 return userId;
 104  
         }
 105  
 
 106  
         public List<String> getRoles() {
 107  0
                 return roles;
 108  
         }
 109  
 
 110  
     /**
 111  
      * Adds the messages in the list of messages to the map of the messages
 112  
      * @param messages
 113  
      */
 114  
     public void addMessages(List<Message> messages) {
 115  0
                 messagesList.addAll(messages);
 116  0
             for (Message m : messages) {
 117  0
                 String groupName = m.getGroupName();
 118  0
                 Map<String, String> group = this.messages.get(groupName);
 119  0
                 if (group == null) {
 120  0
                     group = new HashMap<String, String>();
 121  0
                     this.messages.put(groupName, group);
 122  
                 }
 123  0
                 group.put(m.getId(), m.getValue());
 124  0
                 flatMessages.put(m.getId(), m.getValue());
 125  0
             }
 126  0
         }
 127  
         
 128  
         /**
 129  
          * Get a message by a unique id
 130  
          */
 131  
         public String getMessage(String messageId) {
 132  0
             return flatMessages.get(messageId);
 133  
     }
 134  
     
 135  
         /**
 136  
          * Returns all the messages in the ApplicationContext
 137  
          */
 138  
         public List<Message> getMessages() {
 139  0
             return messagesList;
 140  
     }
 141  
     
 142  
         
 143  
         /**
 144  
          * Get message by the group it is in and its unique id within that group
 145  
          */
 146  
         public String getMessage(String groupName, String messageId) {
 147  
                         
 148  0
             String result = null;
 149  
             
 150  0
             Map<String, String> group = this.messages.get(groupName);
 151  0
             if (group != null) {
 152  0
                 result = group.get(messageId);
 153  
             }
 154  
             
 155  0
             return result;
 156  
         }
 157  
         
 158  
     /**
 159  
      * 
 160  
      * This method looks up a UI Label in the messages cache.  
 161  
      * First looks for a label specific to the type and state of the field.
 162  
      * If none found try for a generalized label.
 163  
      * Otherwise return the supplied fieldId
 164  
      * Groups provide namespace for same label ids within different LUs
 165  
      * 
 166  
      * @param groupName - for example 'course' or 'program'
 167  
      * @param type
 168  
      * @param state
 169  
      * @param fieldId
 170  
      * @return
 171  
      */
 172  
          public String getUILabel(String groupName, String type, String state, String fieldId) {
 173  
 
 174  0
         String label = getMessage(groupName, type + ":" + state + ":" + fieldId);
 175  
         
 176  0
         if (label == null)
 177  0
             label = getMessage(groupName, fieldId);
 178  
         
 179  0
         if (label == null)
 180  0
             label =  fieldId;
 181  
         
 182  0
         return label;
 183  
         
 184  
     }
 185  
          
 186  
         /**
 187  
          * Same as getUILabel(String groupName, String type, String state, String fieldId) with no
 188  
          * type and state needed
 189  
          */
 190  
         public String getUILabel(String groupName, String fieldId) {
 191  
 
 192  0
                 String label = getMessage(groupName, fieldId);
 193  
                 
 194  0
                 if (label == null)
 195  0
                     label =  fieldId;
 196  
                 
 197  0
                 return label;
 198  
                 
 199  
         }
 200  
 
 201  
     /**
 202  
      * Get the security context for the app
 203  
      * @return SecurityContext
 204  
      */
 205  
     public SecurityContext getSecurityContext() {
 206  0
         return securityContext;
 207  
     }
 208  
 
 209  
     public void setSecurityContext(SecurityContext securityContext) {
 210  0
         this.securityContext = securityContext;
 211  0
     }
 212  
         
 213  
         /**
 214  
          * Application URL based on the serverPropertiesRPC service result
 215  
          */
 216  
         public String getApplicationContextUrl() {
 217  0
                 return applicationContextUrl;
 218  
         }
 219  
 
 220  
         public void setVersion(String version) {
 221  0
                 this.version = version;
 222  0
         }
 223  
 
 224  
         public String getVersion() {
 225  0
                 return version;
 226  
         }
 227  
         
 228  
         /**
 229  
          * Adds a mapping from path to a list of field descriptors for a given namespace
 230  
          * namespace defaults to _default if null
 231  
          * @param path
 232  
          * @param fd
 233  
          */
 234  
         public void putCrossConstraint(String namespace, String path, HasCrossConstraints fd){
 235  0
                 if(namespace==null){
 236  0
                         namespace="_default";
 237  
                 }
 238  
                 
 239  0
                 HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace);
 240  0
                 if(crossConstraintMap==null){
 241  0
                         crossConstraintMap = new HashMap<String,HashSet<HasCrossConstraints>>();
 242  0
                         crossConstraints.put(namespace, crossConstraintMap);
 243  
                 }
 244  0
                 HashSet<HasCrossConstraints> fieldDescriptors = crossConstraintMap.get(path);
 245  0
                 if(fieldDescriptors == null){
 246  0
                         fieldDescriptors = new HashSet<HasCrossConstraints>();
 247  0
                         crossConstraintMap.put(path, fieldDescriptors);
 248  
                 }
 249  0
                 fieldDescriptors.add(fd);
 250  0
         }
 251  
 
 252  
         
 253  
         
 254  
         public HashSet<HasCrossConstraints> getCrossConstraint(String namespace, String path){
 255  0
                 if(namespace==null){
 256  0
                         namespace="_default";
 257  
                 }
 258  0
                 HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace);
 259  0
                 if(crossConstraintMap!=null){
 260  0
                         return crossConstraintMap.get(path);
 261  
                 }
 262  0
                 return null;
 263  
         }
 264  
         public void clearCrossConstraintMap(String namespace){
 265  0
                 if(namespace==null){
 266  0
                         namespace="_default";
 267  
                 }
 268  0
                 crossConstraints.remove(namespace);
 269  0
         }
 270  
         public void putPathToFieldMapping(String namespace, String path, FieldDescriptor fd){
 271  0
                 if(namespace==null){
 272  0
                         namespace="_default";
 273  
                 }
 274  
                 
 275  0
                 HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace);
 276  0
                 if(pathToField==null){
 277  0
                         pathToField = new HashMap<String,FieldDescriptor>();
 278  0
                         pathToFieldMapping.put(namespace, pathToField);
 279  
                 }
 280  0
                 pathToField.put(path, fd);
 281  0
         }
 282  
 
 283  
         public FieldDescriptor getPathToFieldMapping(String namespace, String path){
 284  0
                 if(namespace==null){
 285  0
                         namespace="_default";
 286  
                 }
 287  
                 
 288  0
                 HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace);
 289  0
                 if(pathToField!=null){
 290  0
                         return pathToField.get(path);
 291  
                 }
 292  0
                 return null;
 293  
         }
 294  
         public void clearPathToFieldMapping(String namespace){
 295  0
                 if(namespace==null){
 296  0
                         namespace="_default";
 297  
                 }
 298  0
                 pathToFieldMapping.remove(namespace);
 299  0
         }
 300  
 
 301  
         /**
 302  
          * Removes the bidirectional mapping for all paths that start with the path prefix
 303  
          * This means if Field A had a dependency on Field B, and you cleared A, first all mappings with
 304  
          * dependencies to A would be removed, then all mappings with dependencies to A would be removed. 
 305  
          * @param namespace
 306  
          * @param pathPrefix
 307  
          */
 308  
         public void clearCrossConstraintsWithStartingPath(String namespace, String pathPrefix){
 309  0
                 if(namespace==null){
 310  0
                         namespace="_default";
 311  
                 }
 312  
                 //First delete any cross constraint mappings based on this field
 313  0
                 HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace);
 314  0
                 if(crossConstraintMap!=null){
 315  0
                         Iterator<Map.Entry<String,HashSet<HasCrossConstraints>>> constraintMapIter = crossConstraintMap.entrySet().iterator();
 316  0
                         while(constraintMapIter.hasNext()){
 317  0
                                 Map.Entry<String,HashSet<HasCrossConstraints>> entry = constraintMapIter.next();
 318  0
                                 if(entry.getKey().startsWith(pathPrefix)){
 319  0
                                         constraintMapIter.remove();
 320  
                                 }
 321  0
                         }
 322  
 
 323  
                         //Find all the fieldDescriptors that start with the prefix and remove the cross constraint mapping to them 
 324  0
                         HashMap<String,FieldDescriptor> pathToField = pathToFieldMapping.get(namespace);
 325  0
                         if(pathToField!=null){
 326  0
                                 Iterator<Entry<String, FieldDescriptor>> pathMapIter = pathToField.entrySet().iterator();
 327  0
                                 while(pathMapIter.hasNext()){
 328  0
                                         Entry<String, FieldDescriptor> entry = pathMapIter.next();
 329  0
                                         if(entry.getKey().startsWith(pathPrefix)){
 330  0
                                                 FieldDescriptor fd = entry.getValue();
 331  0
                                                 if(fd.getFieldWidget()!=null && fd.getFieldWidget() instanceof HasCrossConstraints && ((HasCrossConstraints)fd.getFieldWidget()).getCrossConstraints()!=null){
 332  
                                                         //Loop through the constraint paths and remove any mapping to the existing field descriptor
 333  0
                                                         for(String path:((HasCrossConstraints)fd.getFieldWidget()).getCrossConstraints()){
 334  0
                                                                 HashSet<HasCrossConstraints> set = crossConstraintMap.get(path);
 335  0
                                                                 if(set!=null){
 336  0
                                                                         set.remove(fd.getFieldWidget());
 337  
                                                                 }
 338  0
                                                         }
 339  
                                                 }
 340  
                                         }
 341  0
                                 }
 342  
                         }
 343  
                 }
 344  0
         }
 345  
 
 346  
         
 347  
         public HashSet<HasCrossConstraints> getCrossConstraints(String namespace) {
 348  0
                 if(namespace==null){
 349  0
                         namespace="_default";
 350  
                 }
 351  0
                 HashSet<HasCrossConstraints> results = new HashSet<HasCrossConstraints>();
 352  0
                 HashMap<String,HashSet<HasCrossConstraints>> crossConstraintMap = crossConstraints.get(namespace);
 353  0
                 if(crossConstraintMap!=null){
 354  0
                         for(HashSet<HasCrossConstraints> fds: crossConstraintMap.values()){
 355  0
                                 results.addAll(fds);
 356  
                         }
 357  
                 }
 358  0
                 return results;
 359  
         }
 360  
 
 361  
         public String getParentPath() {
 362  0
                 return parentPath;
 363  
         }
 364  
 
 365  
         public void setParentPath(String parentPath) {
 366  0
                 this.parentPath = parentPath;
 367  0
         }
 368  
 
 369  
 //        public void putDefaultValueMapping(String namespace,
 370  
 //                        FieldDescriptor fieldDescriptor, String defaultValuePath) {
 371  
 //                if(namespace==null){
 372  
 //                        namespace="_default";
 373  
 //                }
 374  
 //                HashMap<FieldDescriptor, String> defaultValueMap = defaultValueMapping.get(namespace);
 375  
 //                if(defaultValueMap==null){
 376  
 //                        defaultValueMap = new HashMap<FieldDescriptor, String>();
 377  
 //                        defaultValueMapping.put(namespace, defaultValueMap);
 378  
 //                }
 379  
 //                defaultValueMap.put(fieldDescriptor, defaultValuePath);
 380  
 //        }
 381  
 //
 382  
 //        public HashMap<FieldDescriptor, String> getDefaultValueMapping(String namespace) {
 383  
 //                if(namespace==null){
 384  
 //                        namespace="_default";
 385  
 //                }
 386  
 //                HashMap<FieldDescriptor, String> result = defaultValueMapping.get(namespace);
 387  
 //                if(result==null){
 388  
 //                        result = new HashMap<FieldDescriptor, String>();
 389  
 //                }
 390  
 //                return result;
 391  
 //        }
 392  
 //        public void clearDefaultValueMapping(String namespace){
 393  
 //                if(namespace==null){
 394  
 //                        namespace="_default";
 395  
 //                }
 396  
 //                defaultValueMapping.remove(namespace);
 397  
 //        }
 398  
 
 399  
 }