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