1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.student.common.assembly.util;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import org.kuali.student.common.assembly.data.Data;
22 import org.kuali.student.common.assembly.data.Metadata;
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 import org.kuali.student.common.assembly.helper.PropertyEnum;
27 import org.kuali.student.common.assembly.helper.RuntimeDataHelper;
28
29 public class AssemblerUtils {
30 public enum VersionProperties implements PropertyEnum {
31 TYPENAME("typeName"), ID("id"), VERSION_INDICATOR("versionIndicator");
32
33 private final String key;
34
35 private VersionProperties(final String key) {
36 this.key = key;
37 }
38
39 @Override
40 public String getKey() {
41 return this.key;
42 }
43 }
44
45 public static String getVersionIndicator(Data data) {
46 String result = null;
47 Data versions = getVersions(data);
48 if (versions == null) {
49 return null;
50 }
51 if (versions.size() > 0) {
52 Data v = versions.get(0);
53 if (v != null) {
54 result = v.get(VersionProperties.VERSION_INDICATOR.getKey());
55 }
56 }
57 return result;
58 }
59
60 public static String getVersionIndicator(Data data, String typeName) {
61 return getVersionIndicator(data, typeName, null);
62 }
63
64 public static String getVersionIndicator(Data data, String typeName, String id) {
65 Data version = getVersionData(data, typeName, id);
66 if (version == null) {
67 return null;
68 } else {
69 return version.get(VersionProperties.VERSION_INDICATOR.getKey());
70 }
71 }
72
73 public static Data getVersionData(Data data, String typeName, String id) {
74 Data result = null;
75 Data versions = getVersions(data);
76 if (versions == null) {
77 return null;
78 }
79 for (Data.Property p : versions) {
80 Data v = p.getValue();
81 if (typeName.equals((String) v.get(VersionProperties.TYPENAME.getKey())) && (id == null || id.equals((String) v.get(VersionProperties.ID.getKey())))) {
82 result = v;
83 break;
84 }
85 }
86 return result;
87
88 }
89
90 public static Data getVersions(Data data) {
91 Data result = null;
92
93 if (data != null) {
94
95 Data runtime = data.get("_runtimeData");
96 if (runtime != null) {
97 result = runtime.get(RuntimeDataHelper.Properties.VERSIONS.getKey());
98 }
99 }
100
101 return result;
102 }
103
104 public static void addVersionIndicator(Data data, String typeName, String id, String version) {
105 Data existing = getVersionData(data, typeName, id);
106 if (existing == null) {
107 Data d = new Data();
108 d.set(VersionProperties.TYPENAME.getKey(), typeName);
109 d.set(VersionProperties.ID.getKey(), id);
110 d.set(VersionProperties.VERSION_INDICATOR.getKey(), version);
111 Data versions = getVersions(data);
112 if (versions == null) {
113 versions = new Data();
114 setVersions(data, versions);
115 }
116 versions.add(d);
117 } else {
118 existing.set(VersionProperties.VERSION_INDICATOR.getKey(), version);
119 }
120 }
121 public static void setVersions(Data data, Data versions) {
122 if (data != null) {
123
124 Data runtime = data.get("_runtimeData");
125 if (runtime == null) {
126 runtime = new Data();
127 data.set("_runtimeData", runtime);
128 }
129 runtime.set(RuntimeDataHelper.Properties.VERSIONS.getKey(), versions);
130 }
131 }
132
133 public static boolean isCreated(Data data) {
134 return isFlagSet(data, RuntimeDataHelper.Properties.CREATED.getKey());
135 }
136
137 public static boolean isDeleted(Data data) {
138 return isFlagSet(data, RuntimeDataHelper.Properties.DELETED.getKey());
139 }
140
141 public static boolean isUpdated(Data data) {
142 return isFlagSet(data, RuntimeDataHelper.Properties.UPDATED.getKey());
143 }
144
145 public static boolean isModified(Data data) {
146 return isCreated(data) || isUpdated(data) || isDeleted(data);
147 }
148
149 public static void setCreated(Data data, boolean flag) {
150 setFlag(data, RuntimeDataHelper.Properties.CREATED.getKey(), flag);
151 }
152
153 public static void setDeleted(Data data, boolean flag) {
154 setFlag(data, RuntimeDataHelper.Properties.DELETED.getKey(), flag);
155 }
156
157 public static void setUpdated(Data data, boolean flag) {
158 setFlag(data, RuntimeDataHelper.Properties.UPDATED.getKey(), flag);
159 }
160
161 private static void setFlag(Data data, String key, Boolean flag) {
162 if (data != null) {
163 Data runtime = data.get("_runtimeData");
164 if (runtime == null) {
165 runtime = new Data();
166 data.set("_runtimeData", runtime);
167 }
168 runtime.set(key, flag);
169 }
170 }
171 private static boolean isFlagSet(Data data, String key) {
172 boolean result = false;
173
174 if (data != null) {
175 Data runtime = data.get("_runtimeData");
176 if (runtime != null) {
177 result = runtime.get(key) != null && (Boolean) runtime.get(key);
178 }
179 }
180
181 return result;
182 }
183
184
185 public static List<QueryPath> findDirtyElements(Data data) {
186 List<QueryPath> results = new ArrayList<QueryPath>();
187 _findDirtyElements(data, results, new QueryPath());
188 return results;
189 }
190 private static void _findDirtyElements(Data data, List<QueryPath> results, QueryPath currentFrame) {
191 if (data == null) {
192 return;
193 }
194
195 Data flags = getDirtyFlags(data);
196 if (flags != null && flags.size() > 0) {
197 for (Data.Property p : flags) {
198 QueryPath q = new QueryPath();
199 q.addAll(currentFrame);
200 Key key = p.getWrappedKey();
201 q.add(key);
202 results.add(q);
203 }
204 }
205
206 for (Data.Property p : data) {
207 if (p.getValueType().equals(Data.class) && p.getValue() != null) {
208 QueryPath q = new QueryPath();
209 q.addAll(currentFrame);
210 Key key = p.getWrappedKey();
211 q.add(key);
212
213 _findDirtyElements((Data) p.getValue(), results, q);
214 }
215 }
216 }
217 public static Data getDirtyFlags(Data data) {
218 Data result = null;
219 Data runtime = data.get("_runtimeData");
220 if (runtime != null) {
221 result = runtime.get("dirty");
222
223 }
224 return result;
225 }
226
227
228
229 public static Metadata get(Metadata metadata, QueryPath frame) {
230 if(frame.size() == 1) {
231 return metadata.getProperties().get(frame.get(0).get());
232 } else {
233 if (metadata.getDataType() == DataType.LIST){
234 return get(metadata, frame, DataType.LIST);
235 }
236 else{
237 return get(metadata.getProperties().get(frame.get(0).get()), frame.subPath(1, frame.size()));
238 }
239 }
240 }
241
242 private static Metadata get(Metadata metadata, QueryPath frame, DataType type){
243 if(type == DataType.LIST){
244 return get(metadata.getProperties().get(QueryPath.getWildCard()), frame.subPath(1, frame.size()));
245 }
246 else{
247 return get(metadata.getProperties().get(frame.get(0).get()), frame.subPath(1, frame.size()));
248 }
249 }
250 }