Project

General

Profile

1
package eu.dnetlib.clients.enabling;
2

    
3
import java.util.*;
4
import java.util.stream.Collectors;
5

    
6
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
7
import eu.dnetlib.rmi.enabling.ISLookUpException;
8
import eu.dnetlib.rmi.enabling.ISLookUpService;
9
import org.apache.commons.lang3.StringUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.PropertyAccessorFactory;
13
import org.springframework.beans.factory.annotation.Autowired;
14

    
15
/**
16
 * Created by sandro on 3/21/16.
17
 */
18
public class ISLookUpClient {
19

    
20
	private static final Log log = LogFactory.getLog(ISLookUpClient.class);
21

    
22
	@Autowired
23
	private UniqueServiceLocator serviceLocator;
24

    
25
	private ISLookUpService lookUpService;
26

    
27
	public void initialize() {
28
		log.debug("Initializing the ISLookUpService");
29
		setLookUpService(serviceLocator.getService(ISLookUpService.class));
30
	}
31

    
32
	public String getResourceProfileById(final String id) throws ISLookUpException {
33
		return getLookUpService().getResourceProfile(id);
34
	}
35

    
36
	public String getResourceProfileByQuery(final String xquery) throws ISLookUpException {
37
		return getLookUpService().getResourceProfileByQuery(xquery);
38
	}
39

    
40
	public List<String> search(final String xquery) throws ISLookUpException {
41
		return getLookUpService().quickSearchProfile(xquery);
42
	}
43

    
44
	/**
45
	 * This method return a List of object of generic class
46
	 * obtained by splitting the query using the param separator.
47
	 * <p>
48
	 * Each query MUST return a sequence like key1 separator value1 separator key2 separator value2 ....
49
	 * Each key should match a name of the attribute of the class
50
	 *
51
	 * @param query
52
	 * @param clazz
53
	 * @param separator
54
	 * @return
55
	 * @throws ISLookUpException
56
	 */
57
	public <T> List<T> search(final String query, final Class<T> clazz, final String separator) throws ISLookUpException {
58
		try {
59
			return getLookUpService().quickSearchProfile(query)
60
					.stream()
61
					.map(it -> convertResult(it, clazz, separator))
62
					.collect(Collectors.toList());
63
		} catch (Throwable e) {
64
			throw new ISLookUpException(e);
65
		}
66
	}
67

    
68
	/**
69
	 * This method return a List of object of generic class
70
	 * obtained by splitting the query using the param separator.
71
	 * <p>
72
	 * Each query MUST return a sequence values1 separator value1 separator value3 ....
73
	 *
74
	 * @param query
75
	 * @param clazz
76
	 * @param separator
77
	 * @return
78
	 * @throws ISLookUpException
79
	 */
80
	public <T> List<T> searchAndMapToClassByConstructor(final String query, final Class<T> clazz, final String separator) throws ISLookUpException {
81
		final List<String> results = getLookUpService().quickSearchProfile(query);
82
		try {
83
			return results.stream().map(result -> constructObjectByResult(clazz, separator, result)).collect(Collectors.toList());
84
		} catch (Throwable e) {
85
			throw new ISLookUpException(e);
86
		}
87
	}
88

    
89
	public <T> T constructObjectByResult(final Class<T> clazz, final String separator, final String result) {
90
		try {
91
			if (result == null)
92
				throw new RuntimeException("the result of query is null");
93
			List<String> splittedValues = Arrays.asList(result.split(separator));
94
			if (StringUtils.endsWith(result, separator)) {
95
				splittedValues.add("");
96
			}
97
			List<Class> types = new ArrayList<>();
98
			splittedValues.forEach(it -> types.add(String.class));
99

    
100
			return clazz.getDeclaredConstructor(types.toArray(new Class[] {})).newInstance(splittedValues.toArray(new String[] {}));
101
		} catch (Exception e) {
102
			throw new RuntimeException(e);
103
		}
104
	}
105

    
106
	private <T> T convertResult(final String result, final Class<T> clazz, final String separator) {
107
		if (result == null)
108
			throw new RuntimeException("the result of query is null");
109

    
110
		List<String> splittedValues = Arrays.asList(result.split(separator));
111
		if (StringUtils.endsWith(result, separator)) {
112
			splittedValues.add("");
113
		}
114
		if (splittedValues.size() % 2 != 0)
115
			throw new RuntimeException("the number of argument of result must be key" + separator + "value in  " + result);
116
		try {
117
			Map<String, String> keyValuesMap = new HashMap<>();
118
			for (int i = 0; i < splittedValues.size() - 1; i += 2) {
119
				keyValuesMap.put(splittedValues.get(i), splittedValues.get(i + 1));
120
			}
121
			T object = clazz.newInstance();
122
			PropertyAccessorFactory.forBeanPropertyAccess(object).setPropertyValues(keyValuesMap);
123
			return object;
124
		} catch (Exception e) {
125
			throw new RuntimeException("Error on creating object of class: " + clazz.getCanonicalName());
126
		}
127
	}
128

    
129
	public ISLookUpService getLookUpService() {
130
		if (lookUpService == null)
131
			initialize();
132
		return lookUpService;
133
	}
134

    
135
	public void setLookUpService(final ISLookUpService lookUpService) {
136
		this.lookUpService = lookUpService;
137
	}
138

    
139
}
    (1-1/1)