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