Project

General

Profile

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

    
3
import java.util.Map;
4
import java.util.function.Function;
5

    
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.apache.solr.common.SolrException;
9

    
10
import eu.dnetlib.clients.index.model.Any.ValueType;
11
import eu.dnetlib.cql.CqlValueTransformerMap;
12

    
13
/**
14
 * This class maps the fields in the given index schema with a transformation rule.
15
 * 
16
 * @author marko
17
 * 
18
 */
19
public class SolrTypeBasedCqlValueTransformerMap implements CqlValueTransformerMap {
20

    
21
	/**
22
	 * logger.
23
	 */
24
	private static final Log log = LogFactory.getLog(SolrTypeBasedCqlValueTransformerMap.class); // NOPMD by marko on 11/24/08 5:02 PM
25

    
26
	/**
27
	 * Index schema.
28
	 */
29
	private final Map<String, ValueType> schema;
30

    
31
	/**
32
	 * Map of functions.
33
	 */
34
	private final Map<String, Function<String, String>> transformerMap;
35

    
36
	/**
37
	 * Create value transformer map bound to a specific schema
38
	 * @param schema
39
	 * @param transformerMap
40
	 */
41
	public SolrTypeBasedCqlValueTransformerMap(final Map<String, ValueType> schema, final Map<String, Function<String, String>> transformerMap) {
42
		this.schema = schema;
43
		this.transformerMap = transformerMap;
44
	}
45

    
46
	/**
47
	 * {@inheritDoc}
48
	 * 
49
	 * @see CqlValueTransformerMap#transformerFor(String)
50
	 */
51
	@Override
52
	public Function<String, String> transformerFor(final String fieldName) {
53
		try {
54
			final ValueType field = schema.get(fieldName);
55

    
56
			if (field != null) {
57
				Function<String, String> res = transformerMap.get(field.name());
58
				if (res != null) {
59
					return res;
60
				}
61
			}
62
		} catch (SolrException e) {
63
			log.debug("cannot find field", e);
64
		}
65
		Function<String,String> identityFunction= (String s)->s;
66
		return identityFunction;
67
	}
68

    
69
}
(2-2/3)