Project

General

Profile

1
package eu.dnetlib.functionality.index.query;
2

    
3
import java.util.List;
4
import java.util.Map;
5

    
6
import org.apache.commons.logging.Log;
7
import org.apache.commons.logging.LogFactory;
8
import org.dom4j.Document;
9
import org.dom4j.Element;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.beans.factory.annotation.Required;
12

    
13
import com.google.common.collect.BiMap;
14
import com.google.common.collect.HashBiMap;
15
import com.google.common.collect.Maps;
16

    
17
import eu.dnetlib.data.provision.index.rmi.IndexServiceException;
18
import eu.dnetlib.functionality.index.client.IndexClientException;
19
import eu.dnetlib.functionality.index.utils.IndexFieldUtility;
20
import eu.dnetlib.functionality.index.utils.MDFormatReader;
21
import eu.dnetlib.functionality.index.utils.MetadataReference;
22
import eu.dnetlib.functionality.index.utils.ServiceTools;
23

    
24
/**
25
 * The Class BrowseAliases.
26
 */
27
public class BrowseAliases {
28

    
29
	/**
30
	 * logger.
31
	 */
32
	private static final Log log = LogFactory.getLog(BrowseAliases.class); // NOPMD
33

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

    
37
	private ServiceTools serviceTools;
38

    
39
	@Autowired
40
	private MDFormatReader mdFormatReader;
41

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

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

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

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

    
101
		final BiMap<String, String> aliases = HashBiMap.create();
102

    
103
		@SuppressWarnings("unchecked")
104
		final List<Element> fieldList = fields.getRootElement().selectNodes(IndexFieldUtility.XPATH_BROWSING_ALIAS_FOR);
105
		for (final Element e : fieldList) {
106
			final String name = e.attribute(IndexFieldUtility.FIELD_BROWSING_ALIAS_FOR).getValue().toLowerCase();
107
			final String alias = e.attribute(IndexFieldUtility.FIELD_NAME).getValue().toLowerCase();
108
			aliases.put(name, alias);
109
		}
110

    
111
		if (aliases.isEmpty()) {
112
			log.warn("couldn'f find alias fields for browsing");
113
		}
114
		return aliases;
115
	}
116

    
117
	/**
118
	 * @return the serviceTools
119
	 */
120
	public ServiceTools getServiceTools() {
121
		return serviceTools;
122
	}
123

    
124
	/**
125
	 * @param serviceTools
126
	 *            the serviceTools to set
127
	 */
128
	@Required
129
	public void setServiceTools(final ServiceTools serviceTools) {
130
		this.serviceTools = serviceTools;
131
	}
132

    
133
}
(1-1/9)