1
2
3
4
5
6
7
8
9
10
11
12
13
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
70 if (!data.containsKey(key)) {
71
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
85
86 _ensurePath((Data) data.get(key), currentMeta, itr, includeLeafNode);
87
88 } else {
89
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 }