Project

General

Profile

1
package eu.dnetlib.clients.index.query;
2

    
3
import java.util.List;
4
import java.util.Map;
5

    
6
import com.google.common.collect.BiMap;
7
import com.google.common.collect.Lists;
8
import com.google.common.collect.Maps;
9
import eu.dnetlib.clients.index.client.AbstractIndexClient;
10
import eu.dnetlib.clients.index.client.IndexClientException;
11
import eu.dnetlib.clients.index.query.Pruner.Result;
12
import eu.dnetlib.clients.index.utils.MetadataReference;
13
import eu.dnetlib.index.cql.CqlTranslator;
14
import eu.dnetlib.index.cql.TranslatedQuery;
15
import org.springframework.beans.factory.annotation.Autowired;
16

    
17
/**
18
 * A factory for creating IndexQuery objects.
19
 */
20
public abstract class IndexQueryFactory {
21

    
22
	/**
23
	 * Query tree pruner.
24
	 */
25
	private Pruner pruner;
26

    
27
	/**
28
	 * Query tree pruner. Collects parameters which affect the semantic of the cql parser.
29
	 */
30
	private Pruner cqlPruner;
31

    
32
	/**
33
	 * The default query params.
34
	 */
35
	private Map<String, List<String>> defaultQueryParams;
36

    
37
	/**
38
	 * CqlTranslator.
39
	 */
40
	@Autowired
41
	private CqlTranslator translator;
42

    
43
	/**
44
	 * The browse aliases.
45
	 */
46
	@Autowired
47
	private BrowseAliases browseAliases;
48

    
49
	/**
50
	 * The weights.
51
	 */
52
	@Autowired
53
	private Weights weights;
54

    
55
	/**
56
	 * New instance.
57
	 *
58
	 * @param cql           the cql
59
	 * @param res           the res
60
	 * @param queryLanguage the query language
61
	 * @return the index query
62
	 */
63
	protected abstract IndexQuery newInstance(final TranslatedQuery cql, final Result res, final QueryLanguage queryLanguage);
64

    
65
	/**
66
	 * Sets the query options.
67
	 *
68
	 * @param indexQuery the index query
69
	 * @return the index query
70
	 */
71
	protected abstract IndexQuery setQueryOptions(final IndexQuery indexQuery, final AbstractIndexClient client);
72

    
73
	/**
74
	 * Gets the index query.
75
	 *
76
	 * @param lang  the lang
77
	 * @param query the query
78
	 * @param mdRef the md ref
79
	 * @return the index query
80
	 * @throws IndexClientException the index service exception
81
	 */
82
	public IndexQuery getIndexQuery(final QueryLanguage lang, final String query, final AbstractIndexClient client, final MetadataReference mdRef)
83
			throws IndexClientException {
84

    
85
		String myquery = query;
86

    
87
		if ((myquery == null) || myquery.isEmpty()) throw new IndexClientException("query cannot be empty or null");
88

    
89
		try {
90
			final Result cqlRes = getCqlPruner().prune(getCqlPruner().parse(myquery));
91
			final Result res = getPruner().prune(cqlRes.getNode());
92

    
93
			final TranslatedQuery tQuery = translator.getTranslatedQuery(res.getNode(), client.getCqlValueTransformerMap(mdRef),
94
					overrideCqlParams(cqlRes.getOptionMap()), browseAliases.get(mdRef), weights.get(mdRef));
95

    
96
			return setQueryOptions(newInstance(tQuery, res, lang), client);
97
		} catch (Exception e) {
98
			throw new IndexClientException(e);
99
		}
100
	}
101

    
102
	/**
103
	 * Method overrides the default values in the defaultQueryParams with the given override map.
104
	 *
105
	 * @param override the map containing the override values.
106
	 * @return the overridden parameter map
107
	 */
108
	private Map<String, List<String>> overrideCqlParams(final Map<String, List<String>> override) {
109
		Map<String, List<String>> cqlParams = Maps.newHashMap();
110
		cqlParams.putAll(getDefaultQueryParams());
111
		cqlParams.putAll(override);
112
		return cqlParams;
113
	}
114

    
115
	public List<String> getBrowsableFields(final List<String> fields, final MetadataReference mdRef) throws IndexClientException {
116
		return getBrowsableFields(fields, browseAliases.get(mdRef));
117
	}
118

    
119
	/**
120
	 * Gets the list of aliases available for browse
121
	 *
122
	 * @param fields  list of input fields
123
	 * @param aliases key= non-browasbale-field-name, value=browsable-field-name
124
	 * @return the list of browasable field names
125
	 */
126
	public List<String> getBrowsableFields(final List<String> fields, final BiMap<String, String> aliases) {
127
		List<String> browsables = Lists.newArrayListWithExpectedSize(fields.size());
128
		for (String f : fields) {
129
			if (aliases.containsKey(f)) {
130
				browsables.add(aliases.get(f));
131
			} else {
132
				browsables.add(f);
133
			}
134
		}
135
		return browsables;
136
	}
137

    
138
	/**
139
	 * Gets the pruner.
140
	 *
141
	 * @return the pruner
142
	 */
143
	public Pruner getPruner() {
144
		return pruner;
145
	}
146

    
147
	/**
148
	 * Sets the pruner.
149
	 *
150
	 * @param pruner the new pruner
151
	 */
152
	public void setPruner(final Pruner pruner) {
153
		this.pruner = pruner;
154
	}
155

    
156
	/**
157
	 * Gets the cql pruner.
158
	 *
159
	 * @return the cql pruner
160
	 */
161
	public Pruner getCqlPruner() {
162
		return cqlPruner;
163
	}
164

    
165
	/**
166
	 * Sets the cql pruner.
167
	 *
168
	 * @param cqlPruner the new cql pruner
169
	 */
170
	public void setCqlPruner(final Pruner cqlPruner) {
171
		this.cqlPruner = cqlPruner;
172
	}
173

    
174
	/**
175
	 * Gets the default query params.
176
	 *
177
	 * @return the default query params
178
	 */
179
	public Map<String, List<String>> getDefaultQueryParams() {
180
		return defaultQueryParams;
181
	}
182

    
183
	/**
184
	 * Sets the default query params.
185
	 *
186
	 * @param defaultQueryParams the default query params
187
	 */
188
	public void setDefaultQueryParams(final Map<String, List<String>> defaultQueryParams) {
189
		this.defaultQueryParams = defaultQueryParams;
190
	}
191

    
192
}
(3-3/9)