Project

General

Profile

1
package eu.dnetlib.index.solr.client;
2

    
3
import java.lang.reflect.Type;
4
import java.util.Map;
5

    
6
import com.google.common.reflect.TypeToken;
7
import com.google.gson.Gson;
8
import eu.dnetlib.clients.index.client.AbstractIndexClientFactory;
9
import eu.dnetlib.clients.index.client.IndexClient;
10
import eu.dnetlib.clients.index.client.IndexClientException;
11
import eu.dnetlib.clients.index.client.IndexClientFactory;
12
import eu.dnetlib.index.query.SolrIndexQueryFactory;
13
import eu.dnetlib.index.query.SolrIndexQueryResponseFactory;
14
import eu.dnetlib.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory;
15
import org.springframework.beans.factory.annotation.Autowired;
16
import org.springframework.beans.factory.annotation.Required;
17

    
18
/**
19
 * The Class SolrIndexClientFactory.
20
 */
21
public class SolrIndexClientFactory extends AbstractIndexClientFactory implements IndexClientFactory {
22

    
23
	/**
24
	 * The id.
25
	 */
26
	private static String ID = "id";
27
	/**
28
	 * The type token.
29
	 */
30
	@SuppressWarnings("serial")
31
	protected Type typeToken = new TypeToken<Map<String, String>>() {
32
	}.getType();
33
	/**
34
	 * The configuration.
35
	 */
36
	private String configuration;
37
	/**
38
	 * The service properties.
39
	 */
40
	private Map<String, String> serviceProperties;
41

    
42
	@Autowired
43
	private SolrIndexQueryFactory solrIndexQueryFactory;
44

    
45
	@Autowired
46
	private SolrIndexQueryResponseFactory solrIndexQueryResponseFactory;
47

    
48
	@Autowired
49
	private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory;
50

    
51
	/**
52
	 * Inits the class.
53
	 *
54
	 * @throws IndexClientException the index client exception
55
	 */
56
	@Override
57
	public void init() throws IndexClientException {
58
		try {
59
			serviceProperties = new Gson().fromJson(getConfiguration(), typeToken);
60
		} catch (Throwable e) {
61
			throw new IndexClientException("unable to parse configuration: " + getConfiguration(), e);
62
		}
63
	}
64

    
65
	/**
66
	 * {@inheritDoc}
67
	 */
68
	@Override
69
	public String getBackendId() {
70
		return serviceProperties.get(SolrIndexClientFactory.ID);
71
	}
72

    
73
	/**
74
	 * {@inheritDoc}
75
	 *
76
	 * @throws IndexClientException
77
	 */
78
	@Override
79
	public IndexClient getClient(final String format, final String layout, final String interpretation) throws IndexClientException {
80
		return new SolrIndexClient(format, layout, interpretation, isQueryTools.getIndexProperties(getBackendId()), solrIndexQueryFactory,
81
				solrIndexQueryResponseFactory, tMapFactory);
82
	}
83

    
84
	/**
85
	 * Gets the configuration.
86
	 *
87
	 * @return the configuration
88
	 */
89
	public String getConfiguration() {
90
		return configuration;
91
	}
92

    
93
	/**
94
	 * Sets the configuration.
95
	 *
96
	 * @param configuration the configuration
97
	 */
98
	@Required
99
	public void setConfiguration(final String configuration) {
100
		this.configuration = configuration;
101
	}
102

    
103
}
(2-2/2)