Project

General

Profile

1
package eu.dnetlib.clients.is;
2

    
3
import java.util.Arrays;
4
import java.util.Collections;
5
import java.util.HashMap;
6
import java.util.List;
7
import java.util.Map;
8

    
9
import org.apache.commons.lang3.math.NumberUtils;
10
import org.apache.commons.logging.Log;
11
import org.apache.commons.logging.LogFactory;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.stereotype.Component;
14
import org.springframework.util.LinkedMultiValueMap;
15
import org.springframework.web.client.RestTemplate;
16

    
17
import com.google.common.collect.Lists;
18

    
19
import eu.dnetlib.clients.BaseServiceClient;
20
import eu.dnetlib.conf.DnetGenericApplicationProperties;
21
import eu.dnetlib.enabling.annotations.DnetServiceClient;
22
import eu.dnetlib.enabling.annotations.DnetServiceType;
23
import eu.dnetlib.exceptions.DnetGenericRuntimeException;
24
import eu.dnetlib.exceptions.InformationServiceException;
25

    
26
@Component
27
@DnetServiceClient(DnetServiceType.is)
28
public class InformationServiceClient extends BaseServiceClient {
29

    
30
	private static final Log log = LogFactory.getLog(InformationServiceClient.class);
31

    
32
	@Autowired
33
	private DnetGenericApplicationProperties props;
34

    
35
	public List<CollectionDesc> listCollections() throws Exception {
36
		final String xquery = "for $kind in xmldb:get-child-collections('/db/DRIVER') " +
37
				"for $type in xmldb:get-child-collections(concat('/db/DRIVER/', $kind)) " +
38
				"return concat ($kind, ' @@@ ', $type, ' @@@ ', count(xmldb:get-child-resources(concat('/db/DRIVER/', $kind, '/', $type))))";
39

    
40
		final Map<String, CollectionDesc> map = new HashMap<>();
41
		for (final String s : find(xquery)) {
42
			final String[] arr = s.split("@@@");
43
			final String kind = arr[0].trim();
44
			final String type = arr[1].trim();
45
			final int size = NumberUtils.toInt(arr[2].trim(), 0);
46
			if (!map.containsKey(kind)) {
47
				map.put(kind, new CollectionDesc(kind));
48
			}
49
			map.get(kind).addType(type, size);
50
		}
51

    
52
		final List<CollectionDesc> res = Lists.newArrayList(map.values());
53
		for (final CollectionDesc d : res) {
54
			Collections.sort(d.getTypes());
55
		}
56
		Collections.sort(res);
57

    
58
		return res;
59
	}
60

    
61
	public List<String> listSchemas() {
62
		final RestTemplate restTemplate = new RestTemplate();
63
		final String url = props.getInformationServiceUrl() + "/is/schemas";
64
		return Arrays.asList(restTemplate.getForObject(url, String[].class));
65
	}
66

    
67
	public String getSchema(final String name) throws InformationServiceException {
68
		final RestTemplate restTemplate = new RestTemplate();
69
		final String url = props.getInformationServiceUrl() + "/is/schema/" + name;
70
		return restTemplate.getForObject(url, String.class);
71
	}
72

    
73
	public void registerSchema(final String name, final String xsd) {
74
		final RestTemplate restTemplate = new RestTemplate();
75
		final String url = props.getInformationServiceUrl() + "/is/schema/" + name;
76
		restTemplate.postForObject(url, xsd, Boolean.class);
77
	}
78

    
79
	public void deleteSchema(final String name) {
80
		final RestTemplate restTemplate = new RestTemplate();
81
		final String url = props.getInformationServiceUrl() + "/is/schema/" + name;
82
		restTemplate.delete(url);
83
	}
84

    
85
	public String register(final String profile) {
86
		final RestTemplate restTemplate = new RestTemplate();
87
		final String url = props.getInformationServiceUrl() + "/is/profile";
88
		return restTemplate.postForObject(url, profile, String.class);
89
	}
90

    
91
	public String bulkRegister(final String profile) {
92
		final RestTemplate restTemplate = new RestTemplate();
93
		final String url = props.getInformationServiceUrl() + "/is/profile?bulk=true";
94
		return restTemplate.postForObject(url, profile, String.class);
95
	}
96

    
97
	public void deleteProfile(final String id) {
98
		final RestTemplate restTemplate = new RestTemplate();
99
		final String url = props.getInformationServiceUrl() + "/is/profile/" + id;
100
		restTemplate.delete(url);
101
	}
102

    
103
	public void updateProfile(final String id, final String profile) {
104
		final RestTemplate restTemplate = new RestTemplate();
105
		final String url = props.getInformationServiceUrl() + "/is/profile/" + id;
106
		restTemplate.postForObject(url, profile, String.class);
107
	}
108

    
109
	public void xupdate(final String query) {
110
		final RestTemplate restTemplate = new RestTemplate();
111
		final String url = props.getInformationServiceUrl() + "/is/find";
112
		final LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
113
		params.add("q", query);
114
		restTemplate.postForObject(url, params, String.class);
115
	}
116

    
117
	public String getProfile(final String id) throws InformationServiceException {
118
		final RestTemplate restTemplate = new RestTemplate();
119
		final String url = props.getInformationServiceUrl() + "/is/profile/" + id;
120
		return restTemplate.getForObject(url, String.class);
121
	}
122

    
123
	public List<String> find(final String query) throws InformationServiceException {
124
		final RestTemplate restTemplate = new RestTemplate();
125
		final String url = props.getInformationServiceUrl() + "/is/find";
126
		final LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
127
		params.add("q", query);
128
		return Arrays.asList(restTemplate.postForObject(url, params, String[].class));
129
	}
130

    
131
	public String findOne(final String query) throws InformationServiceException {
132
		final List<String> list = find(query);
133
		if (list.size() == 1) {
134
			return list.get(0);
135
		} else if (list.isEmpty()) {
136
			throw new InformationServiceException("Document not found, query: " + query);
137
		} else {
138
			throw new InformationServiceException("Too many results (" + list.size() + "), query: " + query);
139
		}
140
	}
141

    
142
	public void updateProfileNode(final String profileId, final String string, final String string2) throws InformationServiceException {
143
		// TODO Auto-generated method stub
144
		throw new DnetGenericRuntimeException("to be implemented");
145
	}
146

    
147
}
(3-3/3)