1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package pl.edu.agh.cast.model;
19
20 import java.util.ArrayList;
21 import java.util.Collection;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28 import org.apache.log4j.Logger;
29 import org.eclipse.core.runtime.CoreException;
30 import org.eclipse.core.runtime.IConfigurationElement;
31 import org.eclipse.core.runtime.Platform;
32
33 import pl.edu.agh.cast.model.base.BasePlugin;
34 import pl.edu.agh.cast.model.base.IDataLoader;
35 import pl.edu.agh.cast.model.base.IDataSet;
36
37
38
39
40
41
42 public class ModelUtil {
43
44 private final Logger log = BasePlugin.getLogger();
45
46 private IModelPluginRegistryProvider pluginRegistryProvider;
47
48
49
50
51 public ModelUtil() {
52 this.pluginRegistryProvider = new DefaultModelPluginRegistryProvider();
53 }
54
55
56
57
58
59
60
61
62 public ModelUtil(IModelPluginRegistryProvider provider) {
63 if (provider != null) {
64 this.pluginRegistryProvider = provider;
65 } else {
66 this.pluginRegistryProvider = new DefaultModelPluginRegistryProvider();
67 }
68 }
69
70
71
72
73
74
75
76
77 public IConfigurationElement getModelConfiguration(String modelExtensionId) {
78 if (extensionRegistryAbsent()) {
79 return null;
80 }
81 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
82 BasePlugin.MODEL_EXTENSION_ID);
83 for (IConfigurationElement element : elements) {
84 if (element.getAttribute(BasePlugin.MODEL_ID).equals(modelExtensionId)) {
85 return element;
86 }
87 }
88 return null;
89 }
90
91 private boolean extensionRegistryAbsent() {
92 return Platform.getExtensionRegistry() == null;
93 }
94
95
96
97
98
99
100
101
102
103 public IDataLoader getDataLoader(String modelExtensionId) throws CoreException {
104 IConfigurationElement modelConf = getModelConfiguration(modelExtensionId);
105
106 IDataLoader loader = (IDataLoader)modelConf.createExecutableExtension(BasePlugin.LOADER);
107 return loader;
108 }
109
110
111
112
113
114
115
116
117
118
119
120
121
122 public String getAttributeName(String modelExtensionId, String typeName, String attributeId) {
123 for (IConfigurationElement type : getModelConfiguration(modelExtensionId).getChildren(BasePlugin.TYPE)) {
124 if (type.getAttribute(BasePlugin.TYPE_ID).equals(typeName)) {
125 for (IConfigurationElement attribute : type.getChildren(BasePlugin.ATTRIBUTE)) {
126 if (attribute.getAttribute(BasePlugin.ATTRIBUTE_ID).equals(attributeId)) {
127 return attribute.getAttribute(BasePlugin.ATTRIBUTE_NAME);
128 }
129 }
130 }
131 }
132 throw new RuntimeException("The localized name for attribute '" +
133 attributeId + "' of type '" + typeName + "' was not found");
134 }
135
136
137
138
139
140
141
142
143 public ModelColumn getColumnInfo(IConfigurationElement column) {
144 String columnName = column.getAttribute(BasePlugin.COLUMN_NAME);
145 int columnModelIndex = -1;
146 try {
147 columnModelIndex = Integer.parseInt(column.getAttribute(BasePlugin.COLUMN_MODEL_INDEX));
148 } catch (NumberFormatException e) {
149 log.debug("Number format exception while retrieving column model index.", e);
150 }
151 String columnId = column.getAttribute(BasePlugin.COLUMN_ID);
152 String columntType = column.getAttribute(BasePlugin.COLUMN_TYPE);
153 String columnUseString = column.getAttribute(BasePlugin.COLUMN_USE);
154 String columnDefault = column.getAttribute(BasePlugin.COLUMN_DEFAULTVALUE);
155
156 boolean columnRequired = false;
157 columnRequired = columnUseString.equals(BasePlugin.COLUMN_REQUIRED);
158
159 ModelColumn mc = new ModelColumn(columnId, columnName, columnModelIndex, columntType, columnRequired,
160 columnDefault);
161
162 ModelColumnParameter[] parameters = getColumnParameters(column);
163 mc.setParameters(parameters);
164
165 return mc;
166 }
167
168 private ModelColumnParameter[] getColumnParameters(IConfigurationElement column) {
169 IConfigurationElement[] parametersConf = column.getChildren(BasePlugin.PARAMETER);
170
171 if (parametersConf.length == 0) {
172 return null;
173 }
174
175 ModelColumnParameter[] parameters = new ModelColumnParameter[parametersConf.length];
176
177 int i = 0;
178 for (IConfigurationElement parameterConf : parametersConf) {
179 ModelColumnParameter parameter = getColumnParameterInfo(parameterConf);
180 parameters[i++] = parameter;
181 }
182
183 return parameters;
184 }
185
186 private ModelColumnParameter getColumnParameterInfo(IConfigurationElement parameter) {
187 String parameterId = parameter.getAttribute(BasePlugin.PARAMETER_ID);
188 String parameterName = parameter.getAttribute(BasePlugin.PARAMETER_NAME);
189
190 ModelColumnParameter param = new ModelColumnParameter(parameterId, parameterName);
191 return param;
192 }
193
194
195
196
197
198
199
200
201
202 public Map<String, Map<String, String>> getAttributeMap(Collection<IDataSet> dataSets) {
203 Set<String> modelExtensions = new HashSet<String>();
204 Map<String, Map<String, String>> map = new HashMap<String, Map<String, String>>();
205 if (extensionRegistryAbsent()) {
206 return null;
207 }
208 Map<String, String> attributeMap;
209 for (IDataSet ds : dataSets) {
210 modelExtensions.add(ds.getModelExtensionId());
211 }
212 for (String extensionId : modelExtensions) {
213 for (IConfigurationElement type : getModelConfiguration(extensionId).getChildren(BasePlugin.TYPE)) {
214 attributeMap = new HashMap<String, String>();
215 map.put(type.getAttribute(BasePlugin.TYPE_ID), attributeMap);
216 for (IConfigurationElement attribute : type.getChildren(BasePlugin.ATTRIBUTE)) {
217 attributeMap.put(attribute.getAttribute(BasePlugin.ATTRIBUTE_ID), extensionId);
218 }
219 }
220 }
221 return map;
222 }
223
224
225
226
227
228
229
230
231
232
233 public static String getColumnDefaultValue(String modelId, String columnName) {
234 IConfigurationElement[] elements = Platform.getExtensionRegistry().getConfigurationElementsFor(
235 BasePlugin.MODEL_EXTENSION_ID);
236
237 IConfigurationElement[] columns = null;
238 for (IConfigurationElement element : elements) {
239 if (element.getAttribute(BasePlugin.MODEL_ID).equals(modelId)) {
240 columns = element.getChildren(BasePlugin.COLUMN);
241 break;
242 }
243 }
244
245 for (IConfigurationElement column : columns) {
246 String actualAttrbute = column.getAttribute(BasePlugin.COLUMN_ID);
247 if (columnName.equals(actualAttrbute)) {
248 return column.getAttribute(BasePlugin.COLUMN_DEFAULTVALUE);
249 }
250 }
251
252 return null;
253 }
254
255
256
257
258
259
260
261
262 public List<ModelInfo> getModelInfos() throws CoreException {
263 return this.pluginRegistryProvider.getModelInfos();
264 }
265
266
267
268
269
270
271
272
273 public ModelColumn[] getModelColumns(String modelExtensionId) {
274 return this.pluginRegistryProvider.getModelColumns(modelExtensionId);
275 }
276
277
278
279
280
281
282
283
284
285
286
287
288 private class DefaultModelPluginRegistryProvider implements IModelPluginRegistryProvider {
289
290
291
292
293
294
295 public ModelColumn[] getModelColumns(String modelExtensionId) {
296 IConfigurationElement[] columnsConf = getColumnsConfiguration(modelExtensionId);
297 return getModelColumns(columnsConf);
298 }
299
300 private IConfigurationElement[] getColumnsConfiguration(String modelExtensionId) {
301 IConfigurationElement modelConf = getModelConfiguration(modelExtensionId);
302 IConfigurationElement[] columns = modelConf.getChildren(BasePlugin.COLUMN);
303 return columns;
304 }
305
306 private ModelColumn[] getModelColumns(IConfigurationElement[] columnsConf) {
307 ModelColumn[] modelColumns = new ModelColumn[columnsConf.length];
308
309 for (IConfigurationElement columnConf : columnsConf) {
310 ModelColumn modelColumn = getColumnInfo(columnConf);
311
312 int modelIndex = modelColumn.getModelIndex();
313 if (modelIndex < 0 || modelIndex >= columnsConf.length) {
314 log.warn("Omitting column '" + modelColumn.getName()
315 + "' with wrong model index value: " + modelIndex);
316 continue;
317 }
318 modelColumns[modelIndex] = modelColumn;
319 }
320 return modelColumns;
321
322 }
323
324
325
326
327
328
329 public String getModelName(String modelId) throws CoreException {
330 IConfigurationElement conf = getModelConfiguration(modelId);
331 return conf.getAttribute(BasePlugin.MODEL_NAME);
332 }
333
334
335
336
337
338
339 public List<ModelInfo> getModelInfos() throws CoreException {
340 List<ModelInfo> modelInfos = new ArrayList<ModelInfo>();
341 IConfigurationElement[] confs = BasePlugin.getModelConfigurations();
342
343 for (IConfigurationElement confElement : confs) {
344 String modelId = confElement.getAttribute(BasePlugin.MODEL_ID);
345 modelInfos.add(new ModelInfo(modelId, getModelName(modelId)));
346 }
347
348 return modelInfos;
349 }
350 }
351
352 }