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