View Javadoc

1   /*
2    * This file is a part of CAST project.
3    * (c) Copyright 2007, AGH University of Science & Technology
4    * https://caribou.iisg.agh.edu.pl/trac/cast
5    *
6    * Licensed under the Eclipse Public License, Version 1.0 (the "License").
7    * You may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    * http://www.eclipse.org/legal/epl-v10.html
10   */
11  /*
12   * File: ModelUtil.java
13   * Created: 2008-06-28
14   * Author: apohllo, entrop, bmilos
15   * $Id: ModelUtil.java 2708 2009-04-03 14:51:27Z bmilos $
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   * The ModeluUtil class is a helper for accessing data retrieved from extension points defined by 'Base' plug-in.
39   *
40   * @author AGH CAST Team
41   */
42  public class ModelUtil {
43  
44  	private final Logger log = BasePlugin.getLogger();
45  
46  	private IModelPluginRegistryProvider pluginRegistryProvider;
47  
48  	/**
49  	 * The default constructor. Creates a new instance of {@link DefaultModelPluginRegistryProvider}.
50  	 */
51  	public ModelUtil() {
52  		this.pluginRegistryProvider = new DefaultModelPluginRegistryProvider();
53  	}
54  
55  	/**
56  	 * Constructor which allows to set custom {@link IModelPluginRegistryProvider} instance. If null is passed, the
57  	 * {@link DefaultModelPluginRegistryProvider} is used. This constructor should be used in tests.
58  	 *
59  	 * @param provider
60  	 *            custom provider instance
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  	 * Returns the model configuration for model with given extension identifier.
72  	 *
73  	 * @param modelExtensionId
74  	 *            the id of the model extension
75  	 * @return the model configuration
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  	 * Returns the data loader for model with given extension identifier.
97  	 *
98  	 * @param modelExtensionId
99  	 *            the id of the model extension
100 	 * @return The data loader for given model
101 	 * @throws CoreException
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 	 * Returns the localized name of the attribute with given id defined in the type with given type name of the model
112 	 * with given model extension ID.
113 	 *
114 	 * @param modelExtensionId
115 	 *            The ID of the model of the type which defines the attribute
116 	 * @param typeName
117 	 *            The name of the type which defines the attribute
118 	 * @param attributeId
119 	 *            The ID of the attribute
120 	 * @return The human readable name of the attribute.
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 '" + //$NON-NLS-1$
133 		        attributeId + "' of type '" + typeName + "' was not found"); //$NON-NLS-1$//$NON-NLS-2$
134 	}
135 
136 	/**
137 	 * Returns ModelColumn constructed of given configuration element (extension) describing it.
138 	 *
139 	 * @param column
140 	 *            The configuration element describing the model column
141 	 * @return Model column description as defined in the extension.
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); //$NON-NLS-1$
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 	 * Constructs map which allows quick look-up of model extension ID for given pair of type name and attribute name.
196 	 * The map contains only these model extension IDs which are referenced by data sets given as method parameter.
197 	 *
198 	 * @param dataSets
199 	 *            The list of data sets.
200 	 * @return Second order map - for given type name and attribute name returns the model extension ID.
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 	 * Returns the default value of the column with given name defined in model with given id.
226 	 *
227 	 * @param modelId
228 	 *            The id of the model
229 	 * @param columnName
230 	 *            The name of the column
231 	 * @return The default value of the column
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 	 * Delegated method.
257 	 *
258 	 * {@inheritDoc}
259 	 *
260 	 * @see pl.edu.agh.cast.model.IModelPluginRegistryProvider#getModelInfos()
261 	 */
262 	public List<ModelInfo> getModelInfos() throws CoreException {
263 		return this.pluginRegistryProvider.getModelInfos();
264 	}
265 
266 	/**
267 	 * Delegated method.
268 	 *
269 	 * {@inheritDoc}
270 	 *
271 	 * @see pl.edu.agh.cast.model.IModelPluginRegistryProvider#getModelColumns(java.lang.String)
272 	 */
273 	public ModelColumn[] getModelColumns(String modelExtensionId) {
274 		return this.pluginRegistryProvider.getModelColumns(modelExtensionId);
275 	}
276 
277 	/*
278 	 * -----------------------------------------------------------------------------------------------------------
279 	 * --------------------------PRIVATE CLASSES------------------------------------------------------------
280 	 * -----------------------------------------------------------------------------------------------------------
281 	 */
282 
283 	/**
284 	 * Default implementation of the {@link IModelPluginRegistryProvider}.
285 	 *
286 	 * @author AGH CAST Team
287 	 */
288 	private class DefaultModelPluginRegistryProvider implements IModelPluginRegistryProvider {
289 
290 		/**
291 		 * {@inheritDoc}
292 		 *
293 		 * @see pl.edu.agh.cast.model.IModelPluginRegistryProvider#getModelColumns(java.lang.String)
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() //$NON-NLS-1$
315 					        + "' with wrong model index value: " + modelIndex); //$NON-NLS-1$
316 					continue;
317 				}
318 				modelColumns[modelIndex] = modelColumn;
319 			}
320 			return modelColumns;
321 
322 		}
323 
324 		/**
325 		 * {@inheritDoc}
326 		 *
327 		 * @see pl.edu.agh.cast.model.IModelPluginRegistryProvider#getModelName(java.lang.String)
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 		 * {@inheritDoc}
336 		 *
337 		 * @see pl.edu.agh.cast.model.IModelPluginRegistryProvider#getModelInfos()
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 }