Coverage Report - org.kuali.student.common.ui.client.mvc.DataModelDefinition
 
Classes in this File Line Coverage Branch Coverage Complexity
DataModelDefinition
49%
25/51
43%
13/30
3.333
 
 1  
 /**
 2  
  * Copyright 2010 The Kuali Foundation Licensed under the
 3  
  * Educational Community License, Version 2.0 (the "License"); you may
 4  
  * not use this file except in compliance with the License. You may
 5  
  * obtain a copy of the License at
 6  
  *
 7  
  * http://www.osedu.org/licenses/ECL-2.0
 8  
  *
 9  
  * Unless required by applicable law or agreed to in writing,
 10  
  * software distributed under the License is distributed on an "AS IS"
 11  
  * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 12  
  * or implied. See the License for the specific language governing
 13  
  * permissions and limitations under the License.
 14  
  */
 15  
 
 16  
 package org.kuali.student.common.ui.client.mvc;
 17  
 
 18  
 import java.util.Iterator;
 19  
 
 20  
 import org.kuali.student.common.assembly.data.Data;
 21  
 import org.kuali.student.common.assembly.data.Metadata;
 22  
 import org.kuali.student.common.assembly.data.ModelDefinition;
 23  
 import org.kuali.student.common.assembly.data.QueryPath;
 24  
 import org.kuali.student.common.assembly.data.Data.DataType;
 25  
 import org.kuali.student.common.assembly.data.Data.Key;
 26  
 
 27  
 public class DataModelDefinition implements ModelDefinition {
 28  
     private Metadata metadata;
 29  
     /**
 30  
      *
 31  
      */
 32  
     private static final long serialVersionUID = 1L;
 33  
 
 34  4
     public DataModelDefinition() {
 35  
 
 36  4
     }
 37  
 
 38  0
     public DataModelDefinition(Metadata metadata) {
 39  0
         this.metadata = metadata;
 40  0
     }
 41  
 
 42  
 
 43  
     @Override
 44  
     public void ensurePath(Data root, QueryPath path, boolean includeLeafNode) {
 45  
         try {
 46  5
             _ensurePath(root, metadata, path.iterator(), includeLeafNode);
 47  0
         } catch (RuntimeException e) {
 48  0
             throw new IllegalArgumentException("Invalid property path: " + path.toString(), e);
 49  5
         }
 50  5
     }
 51  
 
 52  
     private void _ensurePath(Data data, Metadata meta, Iterator<Data.Key> itr, boolean includeLeafNode) {
 53  11
         Data.Key key = itr.next();
 54  
 
 55  11
         if (key.toString().startsWith("_run")) {
 56  0
             if (!data.containsKey(key)) {
 57  0
                 data.set(key, new Data.DataValue(new Data()));
 58  
             }
 59  
         } else {
 60  11
             Metadata currentMeta = meta.getProperties().get(key.toString());
 61  11
             if (currentMeta == null) {
 62  3
                 currentMeta = meta.getProperties().get(QueryPath.getWildCard());
 63  
             }
 64  11
             if (currentMeta == null) {
 65  0
                 throw new IllegalArgumentException("Invalid property path element: " + key.toString());
 66  
             }
 67  
 
 68  11
             if (itr.hasNext()) {
 69  
                 // not at leaf node yet
 70  6
                 if (!data.containsKey(key)) {
 71  
                     // branch doesn't exist yet
 72  5
                     Data.DataType type = currentMeta.getDataType();
 73  5
                     if (type != DataType.DATA && type != DataType.LIST) {
 74  0
                         throw new RuntimeException("Non-leaf nodes cannot be a simple type: " + key.toString() + " " + (type == null ? "null" : type.toString()));
 75  
                     } else {
 76  5
                         Data.Value value = currentMeta.getDefaultValue();
 77  5
                         if (value == null) {
 78  5
                             value = new Data.DataValue(new Data());
 79  
                         }
 80  5
                         data.set(key, value);
 81  
                     }
 82  
 
 83  
                 }
 84  
                 //commented out: this is a workaround for some reason runtime metadata was disappearing from time to time???
 85  
                 //if(!(key.toString().equals("_runtimeData"))){
 86  6
                 _ensurePath((Data) data.get(key), currentMeta, itr, includeLeafNode);
 87  
                 //}
 88  
             } else {
 89  
                 // we're at the leaf node
 90  5
                 if (includeLeafNode) {
 91  0
                     data.set(key, currentMeta.getDefaultValue());
 92  
                 }
 93  
             }
 94  
         }
 95  
 
 96  11
     }
 97  
 
 98  
     @Override
 99  
     public DataType getType(QueryPath path) {
 100  0
         Metadata meta = getMetadata(path);
 101  0
         if (meta == null) {
 102  0
             return null;
 103  
         } else {
 104  0
             return meta.getDataType();
 105  
         }
 106  
     }
 107  
 
 108  
     @Override
 109  
     public Metadata getMetadata(QueryPath path) {
 110  0
         Metadata meta = this.metadata;
 111  0
         for (Key key : path) {
 112  0
             Metadata m = meta.getProperties().get(key.toString());
 113  0
             if (m == null) {
 114  0
                 m = meta.getProperties().get(QueryPath.getWildCard());
 115  
             }
 116  0
             if (m == null) {
 117  0
                 return null;
 118  
             } else {
 119  0
                 meta = m;
 120  
             }
 121  0
         }
 122  0
         return meta;
 123  
     }
 124  
 
 125  
     @Override
 126  
     public Metadata getMetadata(String path) {
 127  0
         QueryPath queryPath = QueryPath.concat(path);
 128  0
         return getMetadata(queryPath);
 129  
     }
 130  
 
 131  
     public Metadata getMetadata() {
 132  4
         return metadata;
 133  
     }
 134  
 
 135  
     public void setMetadata(Metadata root) {
 136  4
         this.metadata = root;
 137  4
     }
 138  
 
 139  
 }