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
	 * @param dao        the dao
70
	 * @return the index query
71
	 */
72
	protected abstract IndexQuery setQueryOptions(final IndexQuery indexQuery, final AbstractIndexClient client);
73

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

    
86
		String myquery = query;
87

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

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

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

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

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

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

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

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

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

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

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

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

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

    
193
}
(3-3/9)