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.HashBiMap;
8
import com.google.common.collect.Maps;
9
import eu.dnetlib.clients.index.client.IndexClientException;
10
import eu.dnetlib.clients.index.utils.IndexFieldUtility;
11
import eu.dnetlib.clients.index.utils.MDFormatReader;
12
import eu.dnetlib.clients.index.utils.MetadataReference;
13
import eu.dnetlib.clients.index.utils.ServiceTools;
14
import org.apache.commons.logging.Log;
15
import org.apache.commons.logging.LogFactory;
16
import org.dom4j.Document;
17
import org.dom4j.Element;
18
import org.springframework.beans.factory.annotation.Autowired;
19
import org.springframework.beans.factory.annotation.Required;
20

    
21
/**
22
 * The Class BrowseAliases.
23
 */
24
public class BrowseAliases {
25

    
26
	/**
27
	 * logger.
28
	 */
29
	private static final Log log = LogFactory.getLog(BrowseAliases.class); // NOPMD
30

    
31
	/**
32
	 * The aliases.
33
	 */
34
	private Map<MetadataReference, BiMap<String, String>> aliases = Maps.newConcurrentMap();
35

    
36
	private ServiceTools serviceTools;
37

    
38
	@Autowired
39
	private MDFormatReader mdFormatReader;
40

    
41
	/**
42
	 * Initialize.
43
	 * <p>
44
	 * the index service exception
45
	 */
46
	public void initialize() throws IndexClientException {
47
		log.info("initializing browse aliases");
48
		for (MetadataReference mdRef : getServiceTools().listMDRefs()) {
49
			log.debug("inside foreach");
50
			put(mdRef);
51
		}
52
		log.info("browse aliases initialization completed");
53
	}
54

    
55
	/**
56
	 * Put.
57
	 *
58
	 * @param mdRef the metadata refeference
59
	 */
60
	public void put(final MetadataReference mdRef) {
61
		final Document fields = mdFormatReader.getFields(mdRef);
62
		if (fields != null) {
63
			aliases.put(mdRef, extractBrowsingAliases(fields));
64
		} else {
65
			// log.info("couldn't find any");
66
			BiMap<String, String> m = HashBiMap.create();
67
			aliases.put(mdRef, m);
68
		}
69
	}
70

    
71
	/**
72
	 * Gets the.
73
	 *
74
	 * @param mdRef the md ref
75
	 * @return browsing aliases for given mdRef.
76
	 * @throws IndexClientException
77
	 */
78
	public BiMap<String, String> get(final MetadataReference mdRef) throws IndexClientException {
79
		if ((aliases == null) || (aliases.size() == 0)) {
80
			initialize();
81
		}
82
		return aliases.get(mdRef);
83
	}
84

    
85
	/**
86
	 * Method extract aliases field names from the given fields.
87
	 *
88
	 * @param fields the fields
89
	 * @return aliases map. Keys are "normal" field names, values are names of the non-tokenized version of the field
90
	 */
91
	protected BiMap<String, String> extractBrowsingAliases(final Document fields) {
92
		// default tokenizer splits field names, this would cause to
93
		// have too many browsing results, so we use an untokenized
94
		// alias in place of it.
95

    
96
		final BiMap<String, String> aliases = HashBiMap.create();
97

    
98
		@SuppressWarnings("unchecked")
99
		final List<Element> fieldList = fields.getRootElement().selectNodes(IndexFieldUtility.XPATH_BROWSING_ALIAS_FOR);
100
		for (final Element e : fieldList) {
101
			final String name = e.attribute(IndexFieldUtility.FIELD_BROWSING_ALIAS_FOR).getValue().toLowerCase();
102
			final String alias = e.attribute(IndexFieldUtility.FIELD_NAME).getValue().toLowerCase();
103
			aliases.put(name, alias);
104
		}
105

    
106
		if (aliases.isEmpty()) {
107
			log.warn("couldn'f find alias fields for browsing");
108
		}
109
		return aliases;
110
	}
111

    
112
	/**
113
	 * @return the serviceTools
114
	 */
115
	public ServiceTools getServiceTools() {
116
		return serviceTools;
117
	}
118

    
119
	/**
120
	 * @param serviceTools the serviceTools to set
121
	 */
122
	@Required
123
	public void setServiceTools(final ServiceTools serviceTools) {
124
		this.serviceTools = serviceTools;
125
	}
126

    
127
}
(1-1/9)