Coverage Report - org.kuali.student.common.assembly.dictionary.MetadataServiceImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
MetadataServiceImpl
72%
226/312
59%
138/233
5.548
MetadataServiceImpl$1
100%
2/2
N/A
5.548
MetadataServiceImpl$RecursionCounter
92%
12/13
50%
2/4
5.548
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the Educational Community License, Version 2.0 (the "License"); you may
 3  
  * not use this file except in compliance with the License. You may obtain a copy of the License at
 4  
  * http://www.osedu.org/licenses/ECL-2.0 Unless required by applicable law or agreed to in writing, software distributed
 5  
  * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
 6  
  * implied. See the License for the specific language governing permissions and limitations under the License.
 7  
  */
 8  
 
 9  
 package org.kuali.student.common.assembly.dictionary;
 10  
 
 11  
 import java.text.DateFormat;
 12  
 import java.text.ParseException;
 13  
 import java.text.SimpleDateFormat;
 14  
 import java.util.ArrayList;
 15  
 import java.util.HashMap;
 16  
 import java.util.List;
 17  
 import java.util.Map;
 18  
 
 19  
 import org.apache.log4j.Logger;
 20  
 import org.kuali.student.common.assembly.data.ConstraintMetadata;
 21  
 import org.kuali.student.common.assembly.data.Data;
 22  
 import org.kuali.student.common.assembly.data.LookupMetadata;
 23  
 import org.kuali.student.common.assembly.data.LookupParamMetadata;
 24  
 import org.kuali.student.common.assembly.data.Metadata;
 25  
 import org.kuali.student.common.assembly.data.UILookupConfig;
 26  
 import org.kuali.student.common.assembly.data.UILookupData;
 27  
 import org.kuali.student.common.assembly.data.Data.DataType;
 28  
 import org.kuali.student.common.assembly.data.Data.Value;
 29  
 import org.kuali.student.common.assembly.data.Metadata.WriteAccess;
 30  
 import org.kuali.student.common.dictionary.dto.CaseConstraint;
 31  
 import org.kuali.student.common.dictionary.dto.CommonLookupParam;
 32  
 import org.kuali.student.common.dictionary.dto.Constraint;
 33  
 import org.kuali.student.common.dictionary.dto.FieldDefinition;
 34  
 import org.kuali.student.common.dictionary.dto.ObjectStructureDefinition;
 35  
 import org.kuali.student.common.dictionary.dto.WhenConstraint;
 36  
 import org.kuali.student.common.dictionary.service.DictionaryService;
 37  
 import org.kuali.student.common.dto.DtoConstants;
 38  
 import org.kuali.student.common.dto.DtoConstants.DtoState;
 39  
 import org.kuali.student.common.validation.dto.ValidationResultInfo.ErrorLevel;
 40  
 import org.springframework.beans.BeanUtils;
 41  
 import org.springframework.context.ApplicationContext;
 42  
 import org.springframework.context.support.ClassPathXmlApplicationContext;
 43  
 
 44  
 /**
 45  
  * This class provides metadata lookup for service dto objects.
 46  
  * 
 47  
  * @author Kuali Student Team
 48  
  */
 49  
 public class MetadataServiceImpl {
 50  4
     final Logger LOG = Logger.getLogger(MetadataServiceImpl.class);
 51  
 
 52  
     private Map<String, DictionaryService> dictionaryServiceMap;
 53  
     private List<UILookupConfig> lookupObjectStructures;
 54  
     private String uiLookupContext;
 55  
 
 56  36
     private static class RecursionCounter {
 57  
         public static final int MAX_DEPTH = 4;
 58  
 
 59  18
         private Map<String, Integer> recursions = new HashMap<String, Integer>();
 60  
 
 61  
         public int increment(String objectName) {
 62  18
             Integer hits = recursions.get(objectName);
 63  
 
 64  18
             if (hits == null) {
 65  18
                 hits = new Integer(1);
 66  
             } else {
 67  0
                 hits++;
 68  
             }
 69  18
             recursions.put(objectName, hits);
 70  18
             return hits;
 71  
         }
 72  
 
 73  
         public int decrement(String objectName) {
 74  18
             Integer hits = recursions.get(objectName);
 75  18
             if (hits >= 1) {
 76  18
                 hits--;
 77  
             }
 78  
 
 79  18
             recursions.put(objectName, hits);
 80  18
             return hits;
 81  
         }
 82  
     }
 83  
 
 84  
     /**
 85  
      * Create a metadata service initializing it with all known dictionary services
 86  
      * 
 87  
      * @param dictionaryServices
 88  
      */
 89  4
     public MetadataServiceImpl(DictionaryService... dictionaryServices) {
 90  4
         if (dictionaryServices != null) {
 91  4
             this.dictionaryServiceMap = new HashMap<String, DictionaryService>();
 92  8
             for (DictionaryService d : dictionaryServices) {
 93  4
                 List<String> objectTypes = d.getObjectTypes();
 94  4
                 for (String objectType : objectTypes) {
 95  12
                     dictionaryServiceMap.put(objectType, d);
 96  
                 }
 97  
             }
 98  
         }
 99  4
     }
 100  
 
 101  
     /**
 102  
      * This method gets the metadata for the given object key, type, state and nextState
 103  
      * 
 104  
      * @param objectKey
 105  
      * @param type The type of the object (value can be null)
 106  
      * @param state The state for which to retrieve object constraints (value can be null)
 107  
      * @param nextState The state to to check requiredForNextState indicators (value can be null)
 108  
      * @return
 109  
      */
 110  
     public Metadata getMetadata(String objectKey, String type, String state, String nextState) {
 111  15
             nextState = (nextState == null || nextState.length() <=0 ? DtoState.getNextStateAsString(state):nextState);
 112  15
             state = state==null?null:state.toUpperCase();
 113  15
             nextState = nextState==null?null:nextState.toUpperCase();
 114  15
             return getMetadataFromDictionaryService(objectKey, type, state, nextState, null);
 115  
     }
 116  
 
 117  
     /**
 118  
      * This method gets the metadata for the given object id key and workflowNode
 119  
      * 
 120  
      * @return
 121  
      */
 122  
     public Metadata getMetadataByWorkflowNode(String objectKey, String workflowNode) {
 123  3
             return getMetadataFromDictionaryService(objectKey, null, DtoState.DRAFT.toString(), null, workflowNode);
 124  
     }
 125  
 
 126  
     /**
 127  
      * This method gets the metadata for the given object key, type and state
 128  
      * 
 129  
      * @param objectKey
 130  
      * @param type The type of the object (value can be null)
 131  
      * @param state The state for which to retrieve object constraints (value can be null)
 132  
      * @return
 133  
      */
 134  
     public Metadata getMetadata(String objectKey, String type, String state) {
 135  15
             state = (state == null ? DtoState.DRAFT.toString():state);
 136  15
             String nextState = DtoState.getNextStateAsString(state);
 137  
             
 138  15
             return getMetadata(objectKey, type, state.toUpperCase(), nextState);
 139  
     }
 140  
 
 141  
 
 142  
     /**
 143  
      * This method gets the metadata for the given object key and state
 144  
      * 
 145  
      * @param objectKey
 146  
      * @param type The type of the object (value can be null)
 147  
      */
 148  
     public Metadata getMetadata(String objectKey, String state) {
 149  1
             return getMetadata(objectKey, null, state);
 150  
     }
 151  
 
 152  
     /**
 153  
      * This method gets the metadata for the given object key for state DRAFT.
 154  
      * 
 155  
      * @see MetadataServiceImpl#getMetadata(String, String)
 156  
      * @param objectKey
 157  
      * @return
 158  
      */
 159  
     public Metadata getMetadata(String objectKey) {
 160  9
         return getMetadata(objectKey, null, null);
 161  
     }
 162  
 
 163  
     /**
 164  
      * This invokes the appropriate dictionary service to get the object structure and then converts it to a metadata
 165  
      * structure.
 166  
      * 
 167  
      * @param objectKey
 168  
      * @param type
 169  
      * @param state
 170  
      * @return
 171  
      */
 172  
     protected Metadata getMetadataFromDictionaryService(String objectKey, String type, String state, String nextState, String workflowNode) {
 173  18
         Metadata metadata = new Metadata();
 174  
 
 175  18
         ObjectStructureDefinition objectStructure = getObjectStructure(objectKey);
 176  
 
 177  18
         metadata.setProperties(getProperties(objectStructure, type, state, nextState, workflowNode, new RecursionCounter()));
 178  
 
 179  18
         metadata.setWriteAccess(WriteAccess.ALWAYS);
 180  18
         metadata.setDataType(DataType.DATA);
 181  18
         addLookupstoMetadata(objectKey, metadata, type);
 182  18
         return metadata;
 183  
     }
 184  
 
 185  
     /**
 186  
      * This method is used to convert a list of dictionary fields into metadata properties
 187  
      * 
 188  
      * @param fields
 189  
      * @param type
 190  
      * @param state
 191  
      * @return
 192  
      */
 193  
     private Map<String, Metadata> getProperties(ObjectStructureDefinition objectStructure, String type, String state, String nextState, String workflowNode, RecursionCounter counter) {
 194  18
         String objectName = objectStructure.getName();
 195  18
         int hits = counter.increment(objectName);
 196  
 
 197  18
         Map<String, Metadata> properties = null;
 198  
 
 199  18
         if (hits < RecursionCounter.MAX_DEPTH) {
 200  18
             properties = new HashMap<String, Metadata>();
 201  
 
 202  18
             List<FieldDefinition> attributes = objectStructure.getAttributes();
 203  18
             for (FieldDefinition fd : attributes) {
 204  
 
 205  93
                 Metadata metadata = new Metadata();
 206  
 
 207  
                 // Set constraints, authz flags, default value
 208  93
                 metadata.setWriteAccess(WriteAccess.ALWAYS);
 209  93
                 metadata.setDataType(convertDictionaryDataType(fd.getDataType()));
 210  93
                 metadata.setConstraints(getConstraints(fd, type, state, nextState, workflowNode));
 211  93
                 metadata.setCanEdit(!fd.isReadOnly());
 212  93
                 metadata.setCanUnmask(!fd.isMask());
 213  93
                 metadata.setCanView(!fd.isHide());
 214  93
                 metadata.setDynamic(fd.isDynamic());
 215  93
                 metadata.setLabelKey(fd.getLabelKey());
 216  93
                 metadata.setDefaultValue(convertDefaultValue(metadata.getDataType(), fd.getDefaultValue()));
 217  93
                 metadata.setDefaultValuePath(fd.getDefaultValuePath());
 218  
                 
 219  93
                            if (fd.isPartialMask()){
 220  11
                                    metadata.setPartialMaskFormatter(fd.getPartialMaskFormatter());
 221  
                            }
 222  
                            
 223  93
                            if (fd.isMask()){
 224  11
                                    metadata.setMaskFormatter(fd.getMaskFormatter());
 225  
                            }
 226  
 
 227  
                 // Get properties for nested object structure
 228  93
                 Map<String, Metadata> nestedProperties = null;
 229  93
                 if (fd.getDataType() == org.kuali.student.common.dictionary.dto.DataType.COMPLEX && fd.getDataObjectStructure() != null) {
 230  0
                     nestedProperties = getProperties(fd.getDataObjectStructure(), type, state, nextState, workflowNode, counter);
 231  
                 }
 232  
 
 233  
                 // For repeating field, create a LIST with wildcard in metadata structure
 234  93
                 if (isRepeating(fd)) {
 235  0
                     Metadata repeatingMetadata = new Metadata();
 236  0
                     metadata.setDataType(DataType.LIST);
 237  
 
 238  0
                     repeatingMetadata.setWriteAccess(WriteAccess.ALWAYS);
 239  0
                     repeatingMetadata.setOnChangeRefreshMetadata(false);
 240  0
                     repeatingMetadata.setDataType(convertDictionaryDataType(fd.getDataType()));
 241  
 
 242  0
                     if (nestedProperties != null) {
 243  0
                         repeatingMetadata.setProperties(nestedProperties);
 244  
                     }
 245  
 
 246  0
                     Map<String, Metadata> repeatingProperty = new HashMap<String, Metadata>();
 247  0
                     repeatingProperty.put("*", repeatingMetadata);
 248  0
                     metadata.setProperties(repeatingProperty);
 249  0
                 } else if (nestedProperties != null) {
 250  0
                     metadata.setProperties(nestedProperties);
 251  
                 }
 252  
 
 253  93
                 properties.put(fd.getName(), metadata);
 254  
 
 255  93
             }
 256  
         }
 257  
 
 258  18
         counter.decrement(objectName);
 259  18
         return properties;
 260  
     }
 261  
 
 262  
     /**
 263  
      * This method determines if a field is repeating
 264  
      * 
 265  
      * @param fd
 266  
      * @return
 267  
      */
 268  
     protected boolean isRepeating(FieldDefinition fd) {
 269  93
         boolean isRepeating = false;
 270  
         try {
 271  93
             int maxOccurs = Integer.parseInt(fd.getMaxOccurs());
 272  28
             isRepeating = maxOccurs > 1;
 273  65
         } catch (NumberFormatException nfe) {
 274  65
             isRepeating = FieldDefinition.UNBOUNDED.equals(fd.getMaxOccurs());
 275  28
         }
 276  
 
 277  93
         return isRepeating;
 278  
     }
 279  
 
 280  
     /**
 281  
      * This method gets the object structure for given objectKey from a dictionaryService
 282  
      * 
 283  
      * @param objectKey
 284  
      * @return
 285  
      */
 286  
     protected ObjectStructureDefinition getObjectStructure(String objectKey) {
 287  18
         DictionaryService dictionaryService = dictionaryServiceMap.get(objectKey);
 288  
 
 289  18
         if (dictionaryService == null) {
 290  0
             throw new RuntimeException("Dictionary service not provided for objectKey=[" + objectKey + "].");
 291  
         }
 292  
 
 293  18
         return dictionaryService.getObjectStructure(objectKey);
 294  
     }
 295  
 
 296  
     protected List<ConstraintMetadata> getConstraints(FieldDefinition fd, String type, String state, String nextState, String workflowNode) {
 297  93
         List<ConstraintMetadata> constraints = new ArrayList<ConstraintMetadata>();
 298  
 
 299  93
         ConstraintMetadata constraintMetadata = new ConstraintMetadata();
 300  
 
 301  93
         updateConstraintMetadata(constraintMetadata, (Constraint) fd, type, state, nextState, workflowNode);
 302  93
         constraints.add(constraintMetadata);
 303  
 
 304  93
         return constraints;
 305  
     }
 306  
 
 307  
     /**
 308  
      * This updates the constraintMetadata with defintions from the dictionary constraint field.
 309  
      * 
 310  
      * @param constraintMetadata
 311  
      * @param constraint
 312  
      */
 313  
     protected void updateConstraintMetadata(ConstraintMetadata constraintMetadata, Constraint constraint, String type, String state, String nextState, String workflowNode) {
 314  
         // For now ignoring the serverSide flag and making determination of which constraints
 315  
         // should be passed up to the UI via metadata.
 316  
 
 317  
         // Min Length
 318  104
         if (constraint.getMinLength() != null) {
 319  60
             constraintMetadata.setMinLength(constraint.getMinLength());
 320  
         }
 321  
 
 322  
         // Max Length
 323  
         try {
 324  104
             if (constraint.getMaxLength() != null) {
 325  60
                 constraintMetadata.setMaxLength(Integer.parseInt(constraint.getMaxLength()));
 326  
             }
 327  
             // Do we need to add another constraint and label it required if minOccurs = 1
 328  0
         } catch (NumberFormatException nfe) {
 329  
             // Ignoring an unbounded length, cannot be handled in metadata structure, maybe change Metadata to string or set
 330  
             // to -1
 331  0
             constraintMetadata.setMaxLength(9999);
 332  104
         }
 333  
 
 334  
         // Min Occurs
 335  104
         if (constraint.getMinOccurs() != null) {
 336  83
             constraintMetadata.setMinOccurs(constraint.getMinOccurs());
 337  
         }
 338  
 
 339  
         // Max Occurs
 340  104
         String maxOccurs = constraint.getMaxOccurs();
 341  104
         if (maxOccurs != null) {
 342  
             try {
 343  28
                 constraintMetadata.setMaxOccurs(Integer.parseInt(maxOccurs));
 344  28
                 if (!FieldDefinition.SINGLE.equals(maxOccurs)) {
 345  0
                     constraintMetadata.setId("repeating");
 346  
                 }
 347  0
             } catch (NumberFormatException nfe) {
 348  
                 // Setting unbounded to a value of 9999, since unbounded not handled by metadata
 349  0
                 if (FieldDefinition.UNBOUNDED.equals(maxOccurs)) {
 350  0
                     constraintMetadata.setId("repeating");
 351  0
                     constraintMetadata.setMaxOccurs(9999);
 352  
                 }
 353  28
             }
 354  
         }
 355  
 
 356  
         // Min Value
 357  104
         if (constraint.getExclusiveMin() != null) {
 358  22
             constraintMetadata.setMinValue(constraint.getExclusiveMin());
 359  
         }
 360  
 
 361  
         // Max Value
 362  104
         if (constraint.getInclusiveMax() != null) {
 363  11
             constraintMetadata.setMaxValue(constraint.getInclusiveMax());
 364  
         }
 365  
 
 366  104
         if (constraint.getValidChars() != null) {
 367  25
             constraintMetadata.setValidChars(constraint.getValidChars().getValue());
 368  25
             constraintMetadata.setValidCharsMessageId(constraint.getValidChars().getLabelKey());
 369  
         }
 370  
 
 371  
         // Case constraints
 372  104
         if (constraint.getCaseConstraint() != null) {
 373  35
             processCaseConstraint(constraintMetadata, constraint.getCaseConstraint(), type, state, nextState, workflowNode);
 374  
         }
 375  104
     }
 376  
 
 377  
     /**
 378  
      * Currently this only handles requiredness indicators for case constraints with the following field paths:
 379  
      * 
 380  
      *  type, state, and proposal/workflowNode
 381  
      */
 382  
     protected void processCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint, String type, String state, String nextState, String workflowNode) {
 383  35
         String fieldPath = caseConstraint.getFieldPath();
 384  35
         fieldPath = (fieldPath != null ? fieldPath.toUpperCase() : fieldPath);
 385  
         
 386  35
         if (workflowNode != null && fieldPath != null && fieldPath.startsWith("PROPOSAL/WORKFLOWNODE")){
 387  3
                 processRequiredByNodeCaseConstraint(constraintMetadata, caseConstraint, workflowNode);                
 388  32
         } else if ("STATE".equals(fieldPath)) {
 389  11
                 processStateCaseConstraint(constraintMetadata, caseConstraint, type, state, nextState, workflowNode);
 390  21
         } else if ("TYPE".equals(fieldPath)) {
 391  0
                 processTypeCaseConstraint(constraintMetadata, caseConstraint, type, state, nextState, workflowNode);
 392  
         }
 393  35
     }
 394  
         
 395  
     /**
 396  
      * Modifies the constraintMetadata to add required to save or required to approve constraints based on the 
 397  
      * workflow route node the proposal is currently in.
 398  
      * 
 399  
      * @param constraintMetadata The fields constraintMetadata to be modified
 400  
      * @param caseConstraint The caseConstraint defined in dictionary for field
 401  
      * @param workflowNode The current node in workflow process
 402  
      */
 403  
     private void processRequiredByNodeCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint,  String workflowNode) {
 404  3
         List<WhenConstraint> whenConstraints = caseConstraint.getWhenConstraint();
 405  
         
 406  3
             if ("EQUALS".equals(caseConstraint.getOperator()) && whenConstraints != null) {
 407  3
             for (WhenConstraint whenConstraint : whenConstraints) {
 408  3
                 List<Object> values = whenConstraint.getValues();
 409  3
                 Constraint constraint = whenConstraint.getConstraint();
 410  
 
 411  3
                 if (constraint.getErrorLevel() == ErrorLevel.ERROR && constraint.getMinOccurs() != null && constraint.getMinOccurs() > 0){
 412  
                     //This is a required field, so need to determine if it is required to save or required to approve based on the
 413  
                         //workflowNode parameter. The order of workflow nodes defined in the case constraint on this field is important in 
 414  
                         //determining if required to approve or required to save. If the workflowNode parameter equals is the first  
 415  
                         //node defined in the constraint, then it's required to approve, otherwise it's required to save.
 416  
                                
 417  3
                         if (isWorkflowNodeFirstConstraintValue(workflowNode, values)) {
 418  
                                 //Field is required to approve. Indicated this by setting the required for next state flag in metadata.
 419  
                                 //If node is PreRoute, then the next state will be set to "SUBMIT" to indicate submit action, otherwise
 420  
                                 //will be set to "APPROVED" to indicate approval action for node transition.
 421  1
                                        constraintMetadata.setRequiredForNextState(true);
 422  1
                                        if (DtoConstants.WORKFLOW_NODE_PRE_ROUTE.equals(workflowNode)){
 423  0
                                                constraintMetadata.setNextState(DtoState.SUBMITTED.toString());
 424  
                                        } else {
 425  1
                                                constraintMetadata.setNextState(DtoState.APPROVED.toString());
 426  
                                        }
 427  1
                                        constraintMetadata.setMinOccurs(0);
 428  2
                     } else if (values.contains(workflowNode)){
 429  
                             //Field is required only for save
 430  1
                                        constraintMetadata.setRequiredForNextState(false);
 431  1
                                        constraintMetadata.setNextState(null);
 432  1
                                        constraintMetadata.setMinOccurs(1);
 433  
                     }
 434  
                 }
 435  3
             }
 436  
         }
 437  3
     }
 438  
     
 439  
     /** 
 440  
      * @param values
 441  
      * @param workflowNode
 442  
      * @return true if workflowNode is first item in values, otherwise returns false
 443  
      */
 444  
     private boolean isWorkflowNodeFirstConstraintValue(String workflowNode, List<Object> values){
 445  3
             if (values != null && !values.isEmpty()){
 446  3
                     return values.get(0).equals(workflowNode);
 447  
             } else {
 448  0
                     return false;
 449  
             }
 450  
     }
 451  
 
 452  
         /**
 453  
      * Processes a case constraint with field path of state. 
 454  
      */
 455  
     private void processStateCaseConstraint(ConstraintMetadata constraintMetadata,        CaseConstraint caseConstraint, String type, String state, String nextState, String workflowNode) {
 456  11
         List<WhenConstraint> whenConstraints = caseConstraint.getWhenConstraint();
 457  
         
 458  11
         if ("EQUALS".equals(caseConstraint.getOperator()) && whenConstraints != null) {
 459  11
             for (WhenConstraint whenConstraint : whenConstraints) {
 460  22
                 List<Object> values = whenConstraint.getValues();
 461  22
                 if (values != null) {
 462  22
                     Constraint constraint = whenConstraint.getConstraint();
 463  
 
 464  22
                     if (constraint.getErrorLevel() == ErrorLevel.ERROR){
 465  
                             //NOTE: if the constraint has a nested constraint with fieldPath="lookup:proposal...", 
 466  
                             //the required, requiredForNextState, and nextState values will be reset based on workflow node                   
 467  
                             
 468  
                             // Set the required for next state flag. 
 469  22
                         if (values.contains(nextState)) {
 470  7
                             if (constraint.getMinOccurs() != null && constraint.getMinOccurs() > 0) {
 471  7
                                 constraintMetadata.setRequiredForNextState(true);
 472  7
                                 constraintMetadata.setNextState(nextState);
 473  
                             }
 474  
                         }
 475  
 
 476  
                         // Update constraints based on state constraints
 477  22
                         if (values.contains(state)) {
 478  11
                             updateConstraintMetadata(constraintMetadata, constraint, type, state, nextState, workflowNode);
 479  
                         }
 480  
                     }
 481  
                 }
 482  22
             }
 483  
         }                
 484  11
         }
 485  
     
 486  
     /**
 487  
      * Process a case constraint with fieldPath of type 
 488  
      */
 489  
     private void processTypeCaseConstraint(ConstraintMetadata constraintMetadata, CaseConstraint caseConstraint, String type, String state,        String nextState, String workflowNode) {
 490  0
         List<WhenConstraint> whenConstraints = caseConstraint.getWhenConstraint();
 491  
             
 492  0
         if ("EQUALS".equals(caseConstraint.getOperator()) && whenConstraints != null) {
 493  0
             for (WhenConstraint whenConstraint : whenConstraints) {
 494  0
                 List<Object> values = whenConstraint.getValues();
 495  0
                 if (values != null && values.contains(type)) {
 496  0
                     Constraint constraint = whenConstraint.getConstraint();
 497  0
                     updateConstraintMetadata(constraintMetadata, constraint, type, state, nextState, workflowNode);
 498  
                 }
 499  0
             }
 500  
         }                
 501  0
         }
 502  
     
 503  
 
 504  
         /**
 505  
      * Convert Object value to respective DataType. Method return null for object Value.
 506  
      * 
 507  
      * @param dataType
 508  
      * @param value
 509  
      * @return
 510  
      */
 511  
     protected Value convertDefaultValue(DataType dataType, Object value) {
 512  93
         Value v = null;
 513  93
         if (value instanceof String) {
 514  0
             String s = (String) value;
 515  1
             switch (dataType) {
 516  
                 case STRING:
 517  0
                     v = new Data.StringValue(s);
 518  0
                     break;
 519  
                 case BOOLEAN:
 520  0
                     v = new Data.BooleanValue(Boolean.valueOf(s));
 521  0
                     break;
 522  
                 case FLOAT:
 523  0
                     v = new Data.FloatValue(Float.valueOf(s));
 524  0
                     break;
 525  
                 case DATE:
 526  0
                     DateFormat format = new SimpleDateFormat("yyyy-MM-dd");
 527  
                     try {
 528  0
                         v = new Data.DateValue(format.parse(s));
 529  0
                     } catch (ParseException e) {
 530  0
                         LOG.error("Unable to get default date value from metadata definition");
 531  0
                     }
 532  0
                     break;
 533  
                 case LONG:
 534  0
                     if (!s.isEmpty()) {
 535  0
                         v = new Data.LongValue(Long.valueOf(s));
 536  
                     }
 537  
                     break;
 538  
                 case DOUBLE:
 539  0
                     v = new Data.DoubleValue(Double.valueOf(s));
 540  0
                     break;
 541  
                 case INTEGER:
 542  0
                     v = new Data.IntegerValue(Integer.valueOf(s));
 543  
                     break;
 544  
             }
 545  0
         } else {
 546  93
             v = convertDefaultValue(value);
 547  
         }
 548  
 
 549  93
         return v;
 550  
     }
 551  
 
 552  
     protected Value convertDefaultValue(Object value) {
 553  93
         Value v = null;
 554  
 
 555  93
         if (value instanceof String) {
 556  0
             v = new Data.StringValue((String) value);
 557  93
         } else if (value instanceof Boolean) {
 558  0
             v = new Data.BooleanValue((Boolean) value);
 559  93
         } else if (value instanceof Integer) {
 560  0
             v = new Data.IntegerValue((Integer) value);
 561  93
         } else if (value instanceof Double) {
 562  0
             v = new Data.DoubleValue((Double) value);
 563  93
         } else if (value instanceof Long) {
 564  0
             v = new Data.LongValue((Long) value);
 565  93
         } else if (value instanceof Short) {
 566  0
             v = new Data.ShortValue((Short) value);
 567  93
         } else if (value instanceof Float) {
 568  0
             v = new Data.FloatValue((Float) value);
 569  
         }
 570  
 
 571  93
         return v;
 572  
     }
 573  
 
 574  
     protected DataType convertDictionaryDataType(org.kuali.student.common.dictionary.dto.DataType dataType) {
 575  1
         switch (dataType) {
 576  
             case STRING:
 577  71
                 return DataType.STRING;
 578  
             case BOOLEAN:
 579  0
                 return DataType.BOOLEAN;
 580  
             case INTEGER:
 581  0
                 return DataType.INTEGER;
 582  
             case FLOAT:
 583  0
                 return DataType.FLOAT;
 584  
             case COMPLEX:
 585  0
                 return DataType.DATA;
 586  
             case DATE:
 587  11
                 return DataType.DATE;
 588  
             case DOUBLE:
 589  11
                 return DataType.DOUBLE;
 590  
             case LONG:
 591  0
                 return DataType.LONG;
 592  
         }
 593  
 
 594  0
         return null;
 595  
     }
 596  
 
 597  
     public void setUiLookupContext(String uiLookupContext) {
 598  2
         this.uiLookupContext = uiLookupContext;
 599  2
         init();
 600  
 
 601  2
     }
 602  
 
 603  
     @SuppressWarnings("unchecked")
 604  
     private void init() {
 605  2
         ApplicationContext ac = new ClassPathXmlApplicationContext(uiLookupContext);
 606  
 
 607  2
         Map<String, UILookupConfig> beansOfType = (Map<String, UILookupConfig>) ac.getBeansOfType(UILookupConfig.class);
 608  2
         lookupObjectStructures = new ArrayList<UILookupConfig>();
 609  2
         for (UILookupConfig objStr : beansOfType.values()) {
 610  4
             lookupObjectStructures.add(objStr);
 611  
         }
 612  2
         System.out.println("UILookup loaded");
 613  2
     }
 614  
 
 615  
     private String calcSimpleName(String objectKey) {
 616  26
         int lastDot = objectKey.lastIndexOf(".");
 617  26
         if (lastDot == -1) {
 618  26
             return objectKey;
 619  
         }
 620  0
         return objectKey.substring(lastDot + 1);
 621  
 
 622  
     }
 623  
 
 624  
     private boolean matchesObjectKey(String objectKey, String path) {
 625  26
         String simpleName = calcSimpleName(objectKey);
 626  26
         if (path.toLowerCase().startsWith(simpleName.toLowerCase())) {
 627  
             // System.out.println ("matchesObjectKey: is TRUE for " + objectKey + " and " + path);
 628  14
             return true;
 629  
         }
 630  
         // System.out.println ("matchesObjectKey: is FALSE for " + objectKey + " and " + path);
 631  12
         return false;
 632  
     }
 633  
 
 634  
     private boolean matchesType(String paramType, String lookupType) {
 635  
         // both null
 636  14
         if (paramType == null && lookupType == null) {
 637  0
             return true;
 638  
         }
 639  
         // not asking for type specific but the lookup defnition is type specific then
 640  
         // no match
 641  14
         if (paramType == null && lookupType != null) {
 642  4
             return false;
 643  
         }
 644  
         // if looking for type specific but the lookup is not specific then
 645  
         // take as default
 646  
         // If configuration has both a null type (i.e. default) AND has a type
 647  
         // specific one the type specific one has to be entered into the configuration
 648  
         // file first so it is found first
 649  10
         if (paramType != null && lookupType == null) {
 650  0
             return true;
 651  
         }
 652  10
         if (paramType.equalsIgnoreCase(lookupType)) {
 653  
             // System.out.println ("matchesType: is TRUE for " + paramType + " and " + lookupType);
 654  5
             return true;
 655  
         }
 656  
         // System.out.println ("matchesType: is FALSE for " + paramType + " and " + lookupType);
 657  5
         return false;
 658  
     }
 659  
 
 660  
     private void addLookupstoMetadata(String objectKey, Metadata metadata, String type) {
 661  18
         if (lookupObjectStructures != null) {
 662  13
             for (UILookupConfig lookup : lookupObjectStructures) {
 663  26
                 if (!matchesObjectKey(objectKey, lookup.getPath())) {
 664  12
                     continue;
 665  
                 }
 666  14
                 if (!matchesType(type, lookup.getType())) {
 667  9
                     continue;
 668  
                 }
 669  
                 // TODO: figure out why path=courseInfo.creditOptions.type matches any structure that has a type on it so
 670  
                 // that lookup gets returned for all types
 671  5
                 Map<String, Metadata> parsedMetadataMap = metadata.getProperties();
 672  5
                 Metadata parsedMetadata = null;
 673  5
                 String parsedMetadataKey = "";
 674  5
                 String lookupFieldPath = lookup.getPath();
 675  5
                 String[] lookupPathTokens = getPathTokens(lookupFieldPath);
 676  10
                 for (int i = 1; i < lookupPathTokens.length; i++) {
 677  5
                     if (parsedMetadataMap == null) {
 678  0
                         break;
 679  
                     }
 680  5
                     if (i == lookupPathTokens.length - 1) {
 681  
                         // get the metadata on the last path key token
 682  5
                         parsedMetadata = parsedMetadataMap.get(lookupPathTokens[i]);
 683  5
                         parsedMetadataKey = parsedMetadataKey + "." + lookupPathTokens[i];
 684  
                     }
 685  5
                     if (parsedMetadataMap.get(lookupPathTokens[i]) != null) {
 686  5
                         parsedMetadataMap = parsedMetadataMap.get(lookupPathTokens[i]).getProperties();
 687  0
                     } else if (parsedMetadataMap.get("*") != null) {
 688  
                         // Lookup wildcard in case of unbounded elements in metadata.
 689  0
                         parsedMetadataMap = parsedMetadataMap.get("*").getProperties();
 690  0
                         i--;
 691  
                     }
 692  
 
 693  
                 }
 694  5
                 if (parsedMetadata != null) {
 695  
                     // System.out.println ("addLookupstoMetadata:" + parsedMetadataKey + " was found as a match for " +
 696  
                     // lookup.getPath ());
 697  5
                     UILookupData initialLookup = lookup.getInitialLookup();
 698  5
                     if (initialLookup != null) {
 699  5
                         mapLookupDatatoMeta(initialLookup);
 700  5
                         parsedMetadata.setInitialLookup(mapLookupDatatoMeta(lookup.getInitialLookup()));
 701  
                     }
 702  5
                     List<LookupMetadata> additionalLookupMetadata = null;
 703  5
                     if (lookup.getAdditionalLookups() != null) {
 704  0
                         additionalLookupMetadata = new ArrayList<LookupMetadata>();
 705  0
                         for (UILookupData additionallookup : lookup.getAdditionalLookups()) {
 706  0
                             additionalLookupMetadata.add(mapLookupDatatoMeta(additionallookup));
 707  
                         }
 708  0
                         parsedMetadata.setAdditionalLookups(additionalLookupMetadata);
 709  
                     }
 710  
                 }
 711  5
             }
 712  
         }
 713  18
     }
 714  
 
 715  
     private LookupMetadata mapLookupDatatoMeta(UILookupData lookupData) {
 716  10
         LookupMetadata lookupMetadata = new LookupMetadata();
 717  
         List<LookupParamMetadata> paramsMetadata;
 718  10
         BeanUtils.copyProperties(lookupData, lookupMetadata, new String[]{"widget", "usage", "widgetOptions", "params"});
 719  10
         if (lookupData.getWidget() != null) {
 720  10
             lookupMetadata.setWidget(org.kuali.student.common.assembly.data.LookupMetadata.Widget.valueOf(lookupData.getWidget().toString()));
 721  
         }
 722  10
         if (lookupData.getUsage() != null) {
 723  10
             lookupMetadata.setUsage(org.kuali.student.common.assembly.data.LookupMetadata.Usage.valueOf(lookupData.getUsage().toString()));
 724  
         }
 725  10
         if (lookupData.getWidgetOptions () != null) {
 726  0
          lookupMetadata.setWidgetOptions (new HashMap ());
 727  0
          for (UILookupData.WidgetOption wo: lookupData.getWidgetOptions ().keySet ()) {
 728  0
           String value = lookupData.getWidgetOptions ().get (wo);
 729  0
           LookupMetadata.WidgetOption key = LookupMetadata.WidgetOption.valueOf(wo.toString());
 730  0
           lookupMetadata.getWidgetOptions ().put (key, value);
 731  0
          }
 732  
         }
 733  10
         if (lookupData.getParams() != null) {
 734  10
             paramsMetadata = new ArrayList<LookupParamMetadata>();
 735  10
             for (CommonLookupParam param : lookupData.getParams()) {
 736  50
                 paramsMetadata.add(mapLookupParamMetadata(param));
 737  
             }
 738  10
             lookupMetadata.setParams(paramsMetadata);
 739  
         }
 740  
         // WidgetOptions is not used as of now. So not setting it into metadata.
 741  10
         return lookupMetadata;
 742  
     }
 743  
 
 744  
     private LookupParamMetadata mapLookupParamMetadata(CommonLookupParam param) {
 745  50
         LookupParamMetadata paramMetadata = new LookupParamMetadata();
 746  50
         BeanUtils.copyProperties(param, paramMetadata, new String[]{"childLookup", "dataType", "writeAccess", "usage", "widget"});
 747  50
         if (param.getChildLookup() != null) {
 748  0
             paramMetadata.setChildLookup(mapLookupDatatoMeta((UILookupData) param.getChildLookup()));
 749  
         }
 750  50
         if (param.getDataType() != null) {
 751  50
             paramMetadata.setDataType(org.kuali.student.common.assembly.data.Data.DataType.valueOf(param.getDataType().toString()));
 752  
         }
 753  50
         if (param.getWriteAccess() != null) {
 754  40
             paramMetadata.setWriteAccess(org.kuali.student.common.assembly.data.Metadata.WriteAccess.valueOf(param.getWriteAccess().toString()));
 755  
         }
 756  50
         if (param.getUsage() != null) {
 757  0
             paramMetadata.setUsage(org.kuali.student.common.assembly.data.LookupMetadata.Usage.valueOf(param.getUsage().toString()));
 758  
         }
 759  50
         if (param.getWidget() != null) {
 760  0
             paramMetadata.setWidget(org.kuali.student.common.assembly.data.LookupParamMetadata.Widget.valueOf(param.getWidget().toString()));
 761  
         }
 762  
 
 763  50
         return paramMetadata;
 764  
     }
 765  
 
 766  
     private static String[] getPathTokens(String fieldPath) {
 767  5
         return (fieldPath != null && fieldPath.contains(".") ? fieldPath.split("\\.") : new String[]{fieldPath});
 768  
     }
 769  
 }