Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 7e845740325347c772aaf2d396befdc6e4d7acf4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
 * Copyright (c) 2011 Mia-Software.
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 * 	Nicolas Guyomar (Mia-Software) - Bug 333553 - The user has not to deal with two files to create a facet
 *  Emmanuelle Rouillé (Mia-Software) - Bug 352618 - To be able to use non derived facet structural features and save them values.
 *  Nicolas Bros (Mia-Software) - Bug 361612 - New core for new version of the Facet metamodel
 *  Gregoire Dupe (Mia-Software) - Bug 362087 - [Deprecated] org.eclipse.papyrus.emf.facet.util.emf.core.ICatalogSetManager
 *  Nicolas Bros (Mia-Software) - Bug 362191 - [Restructuring] Query mechanism for eFacet2
 *  Gregoire Dupe (Mia-Software) - Bug 369987 - [Restructuring][Table] Switch to the new customization and facet framework
 */
package org.eclipse.papyrus.emf.facet.efacet.core.internal.query;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.papyrus.emf.facet.efacet.core.IDerivedTypedElementManager;
import org.eclipse.papyrus.emf.facet.efacet.core.exception.DerivedTypedElementException;
import org.eclipse.papyrus.emf.facet.efacet.core.internal.Activator;
import org.eclipse.papyrus.emf.facet.efacet.core.query.IDerivedTypedElementImplementation;
import org.eclipse.papyrus.emf.facet.efacet.core.query.IDerivedTypedElementImplementationFactory;
import org.eclipse.papyrus.emf.facet.efacet.metamodel.v0_2_0.efacet.extensible.Query;
import org.eclipse.papyrus.emf.facet.util.core.Logger;
import org.eclipse.papyrus.emf.facet.util.emf.core.ICatalogSetManagerFactory;
import org.osgi.framework.Bundle;

/** @deprecated replaced by {@link QueryImplementationFactoryRegistry}. No tracking bug because this API has not been released yet. */
@Deprecated
public class DerivedTypedElementImplementationFactoryRegistry {
	private static final String DERIVED_TYPED_ELEMENT_IMPLEMENTATION_REGISTRATION_ID = "org.eclipse.papyrus.emf.facet.efacet.core.derivedTypedElementImplementationRegistration"; //$NON-NLS-1$
	private static final String CLASS = "class"; //$NON-NLS-1$
	public static final DerivedTypedElementImplementationFactoryRegistry INSTANCE = new DerivedTypedElementImplementationFactoryRegistry();

	private final List<IDerivedTypedElementImplementationFactory> derivedTypedElementImplementationFactories = new ArrayList<IDerivedTypedElementImplementationFactory>();

	public DerivedTypedElementImplementationFactoryRegistry() {
		initRegisteredEntries();
	}

	private void initRegisteredEntries() {
		IExtensionRegistry registry = Platform.getExtensionRegistry();
		IExtensionPoint extPoint = registry
				.getExtensionPoint(DerivedTypedElementImplementationFactoryRegistry.DERIVED_TYPED_ELEMENT_IMPLEMENTATION_REGISTRATION_ID);
		if (extPoint != null) {
			for (IExtension ext : extPoint.getExtensions()) {
				for (IConfigurationElement configElt : ext.getConfigurationElements()) {

					IDerivedTypedElementImplementationFactory derivedTypedElementImplementationFactory;
					try {
						derivedTypedElementImplementationFactory = (IDerivedTypedElementImplementationFactory) configElt
								.createExecutableExtension(DerivedTypedElementImplementationFactoryRegistry.CLASS);
						this.derivedTypedElementImplementationFactories.add(derivedTypedElementImplementationFactory);
					} catch (CoreException e) {
						Logger.logError(e, Activator.getDefault());
					}
				}
			}
		}
	}

	/**
	 * Returns the derived typed element evaluator that handles the given type of query
	 *
	 * @param query
	 *            the type of query for which an {@link IDerivedTypedElementImplementation} is searched
	 * @param derivedTEManager 
	 * @return the {@link IDerivedTypedElementImplementation}
	 * @throws DerivedTypedElementException
	 *             if no factory implementation was registered for the type of the given query
	 */
	public IDerivedTypedElementImplementation getEvaluatorFor(final Query query, final IDerivedTypedElementManager derivedTEManager)
			throws DerivedTypedElementException {
		if (query == null) {
			throw new IllegalArgumentException("query cannot be null"); //$NON-NLS-1$
		}
		IDerivedTypedElementImplementation derivedTypedElementImplementation = null;
		boolean factoryFound = false;
		for (IDerivedTypedElementImplementationFactory derivedTypedElementImplementationFactory : this.derivedTypedElementImplementationFactories) {
			factoryFound = derivedTypedElementImplementationFactory.getManagedQueryType() == query.eClass();
			if (factoryFound) {
				Resource queryResource = query.eResource();
				Bundle bundle;
				if (queryResource != null) {
					bundle = ICatalogSetManagerFactory.DEFAULT.createICatalogSetManager(query.eResource().getResourceSet()).getBundleByResource(queryResource);
				} else {
					bundle = null;
				}
				derivedTypedElementImplementation = derivedTypedElementImplementationFactory.create(query, bundle, derivedTEManager);
				break;
			}
		}
		if (!factoryFound) {
			StringBuffer sb = new StringBuffer();
			sb.append("No factory implementation found for "); //$NON-NLS-1$
			sb.append(QueryUtils.getQueryDescription(query));
			sb.append(".\nA factory should be registered through the " + DerivedTypedElementImplementationFactoryRegistry.DERIVED_TYPED_ELEMENT_IMPLEMENTATION_REGISTRATION_ID + " extension point."); //$NON-NLS-1$ //$NON-NLS-2$
			sb.append("\nAvailable Query types are: "); //$NON-NLS-1$
			for (int i = 0; i < this.derivedTypedElementImplementationFactories.size(); i++) {
				if (i > 0) {
					sb.append(", "); //$NON-NLS-1$
				}
				IDerivedTypedElementImplementationFactory queryEvaluatorFactory = this.derivedTypedElementImplementationFactories.get(i);
				sb.append(queryEvaluatorFactory.getManagedQueryType().getName());
			}
			throw new DerivedTypedElementException(sb.toString());
		}
		return derivedTypedElementImplementation;
	}
}

Back to the top