Skip to main content
summaryrefslogtreecommitdiffstats
blob: fed4d51e1e6d06a43fc426a46f0a8396b77f6a3e (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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
/*****************************************************************************
 * Copyright (c) 2010 CEA LIST.
 *    
 * 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:
 *  Remi Schnekenburger (CEA LIST) remi.schnekenburger@cea.fr - Initial API and implementation
 *****************************************************************************/
package org.eclipse.papyrus.infra.queries.core.converter;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.eclipse.core.databinding.conversion.IConverter;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.papyrus.infra.queries.core.Activator;
import org.eclipse.papyrus.infra.queries.core.configuration.ConstantParameterValue;
import org.eclipse.papyrus.infra.queries.core.configuration.ListParameterValue;
import org.eclipse.papyrus.infra.queries.core.configuration.ModiscoQueryConfiguration;
import org.eclipse.papyrus.infra.queries.core.configuration.ParameterValue;
import org.eclipse.papyrus.infra.queries.core.configuration.QueryConfiguration;
import org.eclipse.papyrus.infra.queries.core.configuration.QueryExecutionValue;
import org.eclipse.papyrus.infra.queries.core.configuration.util.ConfigurationSwitch;
import org.eclipse.papyrus.infra.queries.core.modisco.QueryUtil;
import org.eclipse.uml2.uml.ValueSpecification;

/**
 * Registry to store converters.
 */
public class ConverterRegistry {

	/** singleton instance */
	private static ConverterRegistry instance;

	/** map to store converters, identified by their class */
	private Map<Class<?>, IConverter> converterMaps = new HashMap<Class<?>, IConverter>();

	/**
	 * Constructor (not visible: singleton pattern)
	 */
	private ConverterRegistry() {
	}

	/**
	 * Returns the single instance of this registry
	 * 
	 * @return the single instance of this registry
	 */
	public static synchronized ConverterRegistry getSingleton() {
		if(instance == null) {
			instance = new ConverterRegistry();
			instance.initializeRegistry();
		}
		return instance;
	}

	/**
	 * Initialize the registry
	 */
	protected void initializeRegistry() {
		converterMaps.put(String.class, new ValueSpecificationToStringConverter());
		converterMaps.put(int.class, new ValueSpecificationToIntegerConverter());
		converterMaps.put(boolean.class, new ValueSpecificationToBooleanConverter());
	}

	/**
	 * Returns the converter for the convenient type, from an {@link Object}
	 * 
	 * @param toType
	 *        the class into which the element should be transformed
	 * 
	 * @return the converter for the convenient type
	 */
	public IConverter getConverter(Class<?> toType) {
		return converterMaps.get(toType);
	}

	public Object convert(Class<?> parameterType, ValueSpecification valueInstance) throws ConverterNotfoundException {
		// retrieve the converter
		IConverter converter = getConverter(parameterType);

		if(converter != null) {
			return converter.convert(valueInstance);
		} else {
			throw new ConverterNotfoundException(parameterType, valueInstance);
		}
	}

	/**
	 * Converts a parameter value into a list of values
	 * 
	 * @param context
	 *        the context of the evaluation of the query
	 * @param parameterType
	 *        the type of the list
	 * @param parameterValue
	 *        the parameter value to transform into a list
	 * @return the list of values contained by the parameter value
	 */
	public List<?> convertToList(final EObject context, final Class<?> parameterType, final ParameterValue parameterValue) {
		return new ConfigurationSwitch<List<?>>() {
			
			/**
			 * {@inheritDoc}
			 */
			@Override
			public java.util.List<?> caseConstantParameterValue(ConstantParameterValue constantParameterValue) {
				IConverter converter = getConverter(parameterType);

				if(converter != null) {
					return Arrays.asList(converter.convert(constantParameterValue.getValueInstance()));
				} else {
					Activator.log.error("Impossible to find a converter for type: " + parameterType, null);
					return Collections.emptyList();
				}
			};

			/**
			 * {@inheritDoc}
			 */
			@Override
			public java.util.List<?> caseQueryExecutionValue(QueryExecutionValue queryExecutionValue) {
				// if query returns a list, returns it, else creates an array of one element
				QueryConfiguration queryConfiguration = queryExecutionValue.getConfiguration();
				if(!(queryConfiguration instanceof ModiscoQueryConfiguration)) {
					throw new RuntimeException("List conversion not implemented for this type of configuration. " + queryConfiguration.eClass().getName());
				}
				Object executionResult = null;
//				try {
//					executionResult = QueryUtil.evaluateBooleanQuery(context, queryConfiguration);
//				} catch (Exception e) {
//					Activator.log.error(e);
//				}
//				int upperBound = ((ModiscoQueryConfiguration)queryConfiguration).getQuery().getUpperBound();
//				if(upperBound == 1) {
//					return Arrays.asList(executionResult);
//				} else {
					return (List<?>)executionResult;
//				}
			};

			/**
			 * {@inheritDoc}
			 */
			@Override
			public java.util.List<?> caseListParameterValue(ListParameterValue listParameterValue) {
				List<Object> values = new ArrayList<Object>();
				for(ParameterValue parameterValue : listParameterValue.getValues()) {
					values.addAll(convertToList(context, parameterType, parameterValue));
				}
				return values;
			};

		}.doSwitch(parameterValue);
	}
}

Back to the top