Project

General

Profile

1
package eu.dnetlib.functionality.index.client.solr;
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.functionality.index.client.AbstractIndexClientFactory;
9
import eu.dnetlib.functionality.index.client.IndexClient;
10
import eu.dnetlib.functionality.index.client.IndexClientException;
11
import eu.dnetlib.functionality.index.client.IndexClientFactory;
12
import eu.dnetlib.functionality.index.query.SolrIndexQueryFactory;
13
import eu.dnetlib.functionality.index.query.SolrIndexQueryResponseFactory;
14
import eu.dnetlib.functionality.index.solr.cql.SolrTypeBasedCqlValueTransformerMapFactory;
15
import eu.dnetlib.functionality.index.utils.MetadataReference;
16
import eu.dnetlib.functionality.index.utils.MetadataReferenceFactory;
17
import org.springframework.beans.factory.annotation.Autowired;
18
import org.springframework.beans.factory.annotation.Required;
19

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

    
25
	/** The id. */
26
	private static String ID = "id";
27

    
28
	/** The configuration. */
29
	private String configuration;
30

    
31
	/** The type token. */
32
	protected Type typeToken = new TypeToken<Map<String, String>>() {}.getType();
33

    
34
	/** The service properties. */
35
	private Map<String, String> serviceProperties;
36

    
37
	@Autowired
38
	private SolrIndexQueryFactory solrIndexQueryFactory;
39

    
40
	@Autowired
41
	private SolrIndexQueryResponseFactory solrIndexQueryResponseFactory;
42

    
43
	@Autowired
44
	private SolrTypeBasedCqlValueTransformerMapFactory tMapFactory;
45

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

    
61
	/**
62
	 * {@inheritDoc}
63
	 *
64
	 * @see IndexClientFactory#getBackendId()
65
	 */
66
	@Override
67
	public String getBackendId() {
68
		return serviceProperties.get(SolrIndexClientFactory.ID);
69
	}
70

    
71
	/**
72
	 * {@inheritDoc}
73
	 *
74
	 * @throws IndexClientException
75
	 *
76
	 * @see IndexClientFactory#getClient(String, String, String)
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
	 * {@inheritDoc}
86
	 *
87
	 * @throws IndexClientException
88
	 *
89
	 * @see IndexClientFactory#getClient(MetadataReference)
90
	 */
91
	@Override
92
	public IndexClient getClient(final MetadataReference mdRef) throws IndexClientException {
93
		return getClient(mdRef.getFormat(), mdRef.getLayout(), mdRef.getInterpretation());
94
	}
95

    
96
	@Override
97
	public IndexClient getClient(final String collection) throws IndexClientException {
98
		return getClient(MetadataReferenceFactory.decodeMetadata(collection));
99
	}
100

    
101
	/**
102
	 * Gets the configuration.
103
	 *
104
	 * @return the configuration
105
	 */
106
	public String getConfiguration() {
107
		return configuration;
108
	}
109

    
110
	/**
111
	 * Sets the configuration.
112
	 *
113
	 * @param configuration
114
	 *            the configuration
115
	 */
116
	@Required
117
	public void setConfiguration(final String configuration) {
118
		this.configuration = configuration;
119
	}
120

    
121
}
(2-2/2)