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
	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.info("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
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
		// PairHashMap aliases = new PairHashMap();
104

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

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

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

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

    
135
}
(1-1/9)