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