View Javadoc

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.r1.common.assembly.data.Data;
21  import org.kuali.student.r1.common.assembly.data.Metadata;
22  import org.kuali.student.r1.common.assembly.data.ModelDefinition;
23  import org.kuali.student.r1.common.assembly.data.QueryPath;
24  import org.kuali.student.r1.common.assembly.data.Data.DataType;
25  import org.kuali.student.r1.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      public DataModelDefinition() {
35  
36      }
37  
38      public DataModelDefinition(Metadata metadata) {
39          this.metadata = metadata;
40      }
41  
42  
43      @Override
44      public void ensurePath(Data root, QueryPath path, boolean includeLeafNode) {
45          try {
46              _ensurePath(root, metadata, path.iterator(), includeLeafNode);
47          } catch (RuntimeException e) {
48              throw new IllegalArgumentException("Invalid property path: " + path.toString(), e);
49          }
50      }
51  
52      private void _ensurePath(Data data, Metadata meta, Iterator<Data.Key> itr, boolean includeLeafNode) {
53          Data.Key key = itr.next();
54  
55          if (key.toString().startsWith("_run")) {
56              if (!data.containsKey(key)) {
57                  data.set(key, new Data.DataValue(new Data()));
58              }
59          } else {
60              Metadata currentMeta = meta.getProperties().get(key.toString());
61              if (currentMeta == null) {
62                  currentMeta = meta.getProperties().get(QueryPath.getWildCard());
63              }
64              if (currentMeta == null) {
65                  throw new IllegalArgumentException("Invalid property path element: " + key.toString());
66              }
67  
68              if (itr.hasNext()) {
69                  // not at leaf node yet
70                  if (!data.containsKey(key)) {
71                      // branch doesn't exist yet
72                      Data.DataType type = currentMeta.getDataType();
73                      if (type != DataType.DATA && type != DataType.LIST) {
74                          throw new RuntimeException("Non-leaf nodes cannot be a simple type: " + key.toString() + " " + (type == null ? "null" : type.toString()));
75                      } else {
76                          Data.Value value = currentMeta.getDefaultValue();
77                          if (value == null) {
78                              value = new Data.DataValue(new Data());
79                          }
80                          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                  _ensurePath((Data) data.get(key), currentMeta, itr, includeLeafNode);
87                  //}
88              } else {
89                  // we're at the leaf node
90                  if (includeLeafNode) {
91                      data.set(key, currentMeta.getDefaultValue());
92                  }
93              }
94          }
95  
96      }
97  
98      @Override
99      public DataType getType(QueryPath path) {
100         Metadata meta = getMetadata(path);
101         if (meta == null) {
102             return null;
103         } else {
104             return meta.getDataType();
105         }
106     }
107 
108     @Override
109     public Metadata getMetadata(QueryPath path) {
110         Metadata meta = this.metadata;
111         for (Key key : path) {
112             Metadata m = meta.getProperties().get(key.toString());
113             if (m == null) {
114                 m = meta.getProperties().get(QueryPath.getWildCard());
115             }
116             if (m == null) {
117                 return null;
118             } else {
119                 meta = m;
120             }
121         }
122         return meta;
123     }
124 
125     @Override
126     public Metadata getMetadata(String path) {
127         QueryPath queryPath = QueryPath.concat(path);
128         return getMetadata(queryPath);
129     }
130 
131     public Metadata getMetadata() {
132         return metadata;
133     }
134 
135     public void setMetadata(Metadata root) {
136         this.metadata = root;
137     }
138 
139 }