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