1
|
package eu.dnetlib.clients.dsManager;
|
2
|
|
3
|
import java.util.Arrays;
|
4
|
import java.util.Date;
|
5
|
import java.util.HashMap;
|
6
|
import java.util.List;
|
7
|
import java.util.Map;
|
8
|
|
9
|
import org.apache.commons.lang3.StringUtils;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.cache.annotation.CacheEvict;
|
12
|
import org.springframework.cache.annotation.Cacheable;
|
13
|
import org.springframework.context.annotation.Scope;
|
14
|
import org.springframework.stereotype.Component;
|
15
|
import org.springframework.web.client.RestTemplate;
|
16
|
|
17
|
import com.google.common.collect.ImmutableMap;
|
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
|
|
24
|
@Component
|
25
|
@Scope("prototype")
|
26
|
@DnetServiceClient(DnetServiceType.dsManager)
|
27
|
public class DsManagerClient extends BaseServiceClient {
|
28
|
|
29
|
@Autowired
|
30
|
private DnetGenericApplicationProperties props;
|
31
|
|
32
|
public List<DatasourcePoint> getRepositoryMap() {
|
33
|
return Arrays.asList((new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/map", DatasourcePoint[].class));
|
34
|
}
|
35
|
|
36
|
public List<SimpleDatasourceDesc> simpleListDatasourcesByType(final String type) {
|
37
|
return Arrays.asList((new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/ds/" + type.trim(), SimpleDatasourceDesc[].class));
|
38
|
}
|
39
|
|
40
|
public String addDatasource(final DatasourceDesc ds) {
|
41
|
return (new RestTemplate()).postForObject(props.getDatasourceManagerUrl() + "/ds", ds, String.class);
|
42
|
}
|
43
|
|
44
|
public void deleteDatasource(final String dsId) {
|
45
|
(new RestTemplate()).delete(props.getDatasourceManagerUrl() + "/ds?dsId=" + dsId.trim());
|
46
|
}
|
47
|
|
48
|
public DatasourceDesc getDatasource(final String dsId) {
|
49
|
return (new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/ds?dsId=" + dsId.trim(), DatasourceDesc.class);
|
50
|
}
|
51
|
|
52
|
public List<DatasourceDesc> listDatasources(final String compliance, final String contentDescription, final String collectedFrom) {
|
53
|
final String url = props.getDatasourceManagerUrl() + "search/ds";
|
54
|
|
55
|
final Map<String, String> params = new HashMap<>();
|
56
|
if (StringUtils.isNotBlank(compliance)) {
|
57
|
params.put("compliance", compliance);
|
58
|
}
|
59
|
if (StringUtils.isNotBlank(contentDescription)) {
|
60
|
params.put("contentDescription", contentDescription);
|
61
|
}
|
62
|
if (StringUtils.isNotBlank(collectedFrom)) {
|
63
|
params.put("collectedFrom", collectedFrom);
|
64
|
}
|
65
|
|
66
|
return Arrays.asList((new RestTemplate()).getForObject(url, DatasourceDesc[].class, params));
|
67
|
}
|
68
|
|
69
|
public void updateLevelOfCompliance(final String dsId, final String ifaceId, final String level, final boolean override) {
|
70
|
(new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/api/compliance?dsId={dsId}&ifaceId={ifaceId}&level={level}&override={override}",
|
71
|
Object.class,
|
72
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId, "level", level, "override", override));
|
73
|
}
|
74
|
|
75
|
public void resetLevelOfCompliance(final String dsId, final String ifaceId) {
|
76
|
(new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/api/compliance?dsId={dsId}&ifaceId={ifaceId}&override=true",
|
77
|
Object.class,
|
78
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId));
|
79
|
}
|
80
|
|
81
|
public void updateBaseUrl(final String dsId, final String ifaceId, final String baseUrl) {
|
82
|
(new RestTemplate()).getForObject(
|
83
|
props.getDatasourceManagerUrl() + "/api/baseUrl?dsId={dsId}&ifaceId={ifaceId}&baseUrl={baseUrl}",
|
84
|
Object.class,
|
85
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId, "baseUrl", baseUrl));
|
86
|
}
|
87
|
|
88
|
public void updateActivationStatus(final String dsId, final String ifaceId, final boolean active) {
|
89
|
(new RestTemplate()).getForObject(
|
90
|
props.getDatasourceManagerUrl() + "/api/activation?dsId={dsId}&ifaceId={ifaceId}&active={active}",
|
91
|
Object.class,
|
92
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId, "active", active));
|
93
|
}
|
94
|
|
95
|
public void updateContentDescription(final String dsId, final String ifaceId, final String desc) {
|
96
|
(new RestTemplate()).getForObject(
|
97
|
props.getDatasourceManagerUrl() + "/api/contentDescr?dsId={dsId}&ifaceId={ifaceId}&desc={desc}",
|
98
|
Object.class,
|
99
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId, "desc", desc));
|
100
|
}
|
101
|
|
102
|
public void updateExtraField(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal) {
|
103
|
(new RestTemplate()).getForObject(
|
104
|
props.getDatasourceManagerUrl() + "/api/extraField/{field}?dsId={dsId}&ifaceId={ifaceId}&value={value}&preserveOriginal={preserveOriginal}",
|
105
|
Object.class,
|
106
|
ImmutableMap.of("field", field, "dsId", dsId, "ifaceId", ifaceId, "value", value, "preserveOriginal", preserveOriginal));
|
107
|
}
|
108
|
|
109
|
public void deleteExtraField(final String dsId, final String ifaceId, final String field) {
|
110
|
(new RestTemplate()).delete(
|
111
|
props.getDatasourceManagerUrl() + "/api/extraField/{field}?dsId={dsId}&ifaceId={ifaceId}",
|
112
|
ImmutableMap.of("field", field, "dsId", dsId, "ifaceId", ifaceId));
|
113
|
}
|
114
|
|
115
|
public void updateAccessParam(final String dsId, final String ifaceId, final String field, final String value, final boolean preserveOriginal) {
|
116
|
(new RestTemplate()).getForObject(
|
117
|
props.getDatasourceManagerUrl() + "/api/accessParam/{field}?dsId={dsId}&ifaceId={ifaceId}&value={value}&preserveOriginal={preserveOriginal}",
|
118
|
Object.class,
|
119
|
ImmutableMap.of("field", field, "dsId", dsId, "ifaceId", ifaceId, "value", value, "preserveOriginal", preserveOriginal));
|
120
|
}
|
121
|
|
122
|
public void deleteAccessParam(final String dsId, final String ifaceId, final String field) {
|
123
|
(new RestTemplate()).delete(
|
124
|
props.getDatasourceManagerUrl() + "/api/accessParam/{field}?dsId={dsId}&ifaceId={ifaceId}",
|
125
|
ImmutableMap.of("field", field, "dsId", dsId, "ifaceId", ifaceId));
|
126
|
}
|
127
|
|
128
|
public void addInterface(final String dsId, final IfaceDesc iface) {
|
129
|
final String url = props.getDatasourceManagerUrl() + "/api?dsId=" + dsId.trim();
|
130
|
(new RestTemplate()).postForObject(url, iface, Object.class);
|
131
|
}
|
132
|
|
133
|
public void deleteInterface(final String dsId, final String ifaceId) {
|
134
|
(new RestTemplate()).delete(
|
135
|
props.getDatasourceManagerUrl() + "/api?dsId={dsId}&ifaceId={ifaceId}",
|
136
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId));
|
137
|
}
|
138
|
|
139
|
public Date findNextScheduledExecution(final String dsId, final String ifaceId) {
|
140
|
return (new RestTemplate()).getForObject(
|
141
|
props.getDatasourceManagerUrl() + "/api/nextScheduledExecution?dsId={dsId}&ifaceId={ifaceId}",
|
142
|
Date.class,
|
143
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId));
|
144
|
}
|
145
|
|
146
|
public void bulkUpdateApiExtraFields(final String dsId, final String ifaceId, final Map<String, String> fields) {
|
147
|
(new RestTemplate()).postForObject(
|
148
|
props.getDatasourceManagerUrl() + "/api/extraFields?dsId={dsId}&ifaceId={ifaceId}", fields,
|
149
|
Object.class,
|
150
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId));
|
151
|
}
|
152
|
|
153
|
public void bulkUpdateApiAccessParams(final String dsId, final String ifaceId, final Map<String, String> accessParams) {
|
154
|
(new RestTemplate()).postForObject(
|
155
|
props.getDatasourceManagerUrl() + "/api/accessParams?dsId={dsId}&ifaceId={ifaceId}", accessParams,
|
156
|
Object.class,
|
157
|
ImmutableMap.of("dsId", dsId, "ifaceId", ifaceId));
|
158
|
}
|
159
|
|
160
|
@Cacheable(cacheNames = "apis_browsable_fields", key = "#root.methodName")
|
161
|
public List<BrowsableField> listBrowsableFields() {
|
162
|
return Arrays.asList((new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/browse/api/fields", BrowsableField[].class));
|
163
|
}
|
164
|
|
165
|
public List<BrowseTerm> browseField(final String field) {
|
166
|
return Arrays.asList((new RestTemplate()).getForObject(props.getDatasourceManagerUrl() + "/browse/api/" + field.trim(), BrowseTerm[].class));
|
167
|
}
|
168
|
|
169
|
@Cacheable("apis_search")
|
170
|
public List<SearchInterfacesEntry> searchInterface(final String field, final String value) {
|
171
|
return Arrays.asList((new RestTemplate()).getForObject(
|
172
|
props.getDatasourceManagerUrl() + "/search/api/{field}?value={value}",
|
173
|
SearchInterfacesEntry[].class,
|
174
|
ImmutableMap.of("field", field, "value", value)));
|
175
|
}
|
176
|
|
177
|
@CacheEvict(value = "apis_search", allEntries = true, beforeInvocation = true)
|
178
|
public void emptyCache() {}
|
179
|
|
180
|
}
|