Project

General

Profile

1
package eu.dnetlib.index.utils;
2

    
3
import java.util.Iterator;
4
import java.util.Map.Entry;
5

    
6
import org.apache.solr.common.SolrDocumentList;
7
import org.apache.solr.common.util.NamedList;
8

    
9
import eu.dnetlib.clients.index.model.AnyMap;
10
import eu.dnetlib.clients.index.model.DataFactory;
11
import eu.dnetlib.clients.index.model.InvalidValueTypeException;
12
import eu.dnetlib.clients.index.model.Value;
13
import eu.dnetlib.clients.index.model.impl.DefaultDataFactoryImpl;
14
import eu.dnetlib.clients.index.model.util.AnyUtil;
15

    
16
/**
17
 * The Class AnySolrUtil.
18
 */
19
public class AnySolrUtil extends AnyUtil {
20

    
21
	/**
22
	 * Convert named list to any map.
23
	 * 
24
	 * @param list
25
	 *            the list
26
	 * @param map
27
	 *            the map
28
	 * @return the any map
29
	 */
30
	@SuppressWarnings("unchecked")
31
	public static AnyMap convertNamedListToAnyMap(final NamedList<Object> list, final AnyMap map) {
32
		final Iterator<Entry<String, Object>> it = list.iterator();
33
		while (it.hasNext()) {
34
			Entry<String, Object> entry = it.next();
35
			final String key = entry.getKey();
36
			final Object obj = entry.getValue();
37
			if (obj instanceof NamedList<?>) {
38
				final AnyMap subMap = map.getMap(key, true);
39
				convertNamedListToAnyMap((NamedList<Object>) obj, subMap);
40
			} else if (obj instanceof SolrDocumentList) {
41
				SolrDocumentList docList = (SolrDocumentList) obj;
42
				AnyMap response = DataFactory.DEFAULT.createAnyMap();
43
				response.put("numFound", docList.getNumFound());
44
				response.put("start", docList.getStart());
45
				response.put("maxScore", docList.getMaxScore());
46
				response.put("docs", objectToAny(obj));
47
				map.put("response", response);
48
			} else {
49
				try {
50
					final Value value = DataFactory.DEFAULT.autoConvertValue(obj);
51
					map.put(key, value);
52
				} catch (InvalidValueTypeException exception) {
53
					; // skip
54
				}
55
			}
56
		}
57
		return map;
58
	}
59

    
60
	/**
61
	 * Convert named list to any map.
62
	 * 
63
	 * @param list
64
	 *            the list
65
	 * @return the any map
66
	 */
67
	public static AnyMap convertNamedListToAnyMap(final NamedList<Object> list) {
68
		return convertNamedListToAnyMap(list, DefaultDataFactoryImpl.INSTANCE.createAnyMap());
69
	}
70

    
71
}
(1-1/9)