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