Project

General

Profile

« Previous | Next » 

Revision 47676

fine grained operations

View differences:

modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/repository/SearchApiRepository.java
1
package eu.dnetlib.datasource.publisher.repository;
2

  
3
import java.util.List;
4

  
5
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry;
6
import org.springframework.data.jpa.repository.JpaRepository;
7
import org.springframework.stereotype.Repository;
8

  
9
/**
10
 * Created by claudio on 12/04/2017.
11
 */
12
@Repository
13
public interface SearchApiRepository extends JpaRepository<SearchInterfacesEntry, String> {
14

  
15
	SearchInterfacesEntry findOneById(String id);
16

  
17
	List<SearchInterfacesEntry> findByRepoidContainingOrRepoNameContainingOrAlternativeNameContainingOrRepoPrefixContainingOrRepoOrganizationContainingAllIgnoreCase(
18
			String repoId,
19
			String repoName,
20
			String alternativeName,
21
			String repoPrefix,
22
			String repoOrganization);
23

  
24
}
25

  
26

  
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/clients/DatasourceInfoRetriever.java
1
package eu.dnetlib.datasource.publisher.clients;
2

  
3
import java.util.List;
4
import java.util.Queue;
5
import java.util.concurrent.*;
6
import javax.annotation.Resource;
7

  
8
import com.google.common.collect.Lists;
9
import com.google.common.util.concurrent.*;
10
import eu.dnetlib.datasource.publisher.ApiException;
11
import eu.dnetlib.datasource.publisher.clients.utils.IndexDsInfo;
12
import eu.dnetlib.datasource.publisher.clients.utils.IndexRecordsInfo;
13
import eu.dnetlib.datasource.publisher.model.AggregationInfo;
14
import eu.dnetlib.datasource.publisher.model.AggregationStage;
15
import eu.dnetlib.datasource.publisher.model.DatasourceResponse;
16
import eu.dnetlib.datasource.publisher.model.db.*;
17
import eu.dnetlib.datasource.publisher.repository.*;
18
import org.apache.commons.logging.Log;
19
import org.apache.commons.logging.LogFactory;
20
import org.springframework.beans.factory.annotation.Autowired;
21
import org.springframework.beans.factory.annotation.Value;
22
import org.springframework.data.domain.Pageable;
23
import org.springframework.data.domain.Slice;
24
import org.springframework.util.concurrent.ListenableFuture;
25
import org.springframework.util.concurrent.ListenableFutureCallback;
26

  
27
/**
28
 * Created by claudio on 20/10/2016.
29
 */
30
public class DatasourceInfoRetriever {
31

  
32
	private static final Log log = LogFactory.getLog(DatasourceInfoRetriever.class);
33

  
34
	@Autowired
35
	private MongoLoggerClient mongoLoggerClient;
36

  
37
	@Autowired
38
	private DatasourceIndexClient datasourceIndexClient;
39

  
40
	@Autowired
41
	private DatasourceRepository dsRepository;
42

  
43
	@Autowired
44
	private SearchApiRepository searchApiRepository;
45

  
46
	@Autowired
47
	private CountryTermRepository countryTermRepository;
48

  
49
	@Autowired
50
	private TypologyTermRepository typologyTermRepository;
51

  
52
	@Autowired
53
	private ProtocolTermRepository protocolTermRepository;
54

  
55
	@Autowired
56
	private CompatibilityTermRepository compatibilityTermRepository;
57

  
58
	@Autowired
59
	private ActivationTermRepository activationTermRepository;
60

  
61
	@Resource(name = "datasourceIsLookupClient")
62
	private ISLookupClient isLookupClient;
63

  
64
	private ListeningExecutorService service;
65

  
66
	private final static int WORKERS = 100;
67

  
68
	@Value("${datasource.publisher.timeout}")
69
	private int timeout = 10;
70

  
71
	public DatasourceInfoRetriever() {
72
		service = MoreExecutors.listeningDecorator(
73
				new ScheduledThreadPoolExecutor(WORKERS,
74
						new ThreadFactoryBuilder().setNameFormat("datasource-info-retriever-%d").build()));
75
	}
76

  
77
	public List<String> listIds(final Pageable pageable) throws ApiException {
78
		return dsRepository.findAll(pageable)
79
				.map(d -> d.getId())
80
				.getContent();
81
	}
82

  
83
	public List<DatasourceResponse> searchByName(final String name, final Pageable pageable) {
84

  
85
		final List<DatasourceResponse> datasourceResponse = Lists.newArrayList();
86

  
87
		final CountDownLatch outerLatch = new CountDownLatch(3);
88
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
89

  
90
		log.debug(String.format("search ds by name '%s'", name));
91
		final ListenableFuture<Slice<Datasource>> c =
92
				dsRepository.findByOfficialnameContainingOrEnglishnameContainingAllIgnoreCase(name, name, pageable);
93
		c.addCallback(getSearchCallback(outerLatch, errors, datasourceResponse));
94

  
95
		waitLatch(outerLatch, errors, timeout);
96

  
97
		return datasourceResponse;
98
	}
99

  
100
	public List<DatasourceResponse> searchByCountry(final String country, final Pageable pageable) {
101

  
102
		final List<DatasourceResponse> datasourceResponse = Lists.newArrayList();
103

  
104
		final CountDownLatch outerLatch = new CountDownLatch(3);
105
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
106

  
107
		log.debug(String.format("search ds by country '%s'", country));
108
		dsRepository.findByOrganizationsCountryIgnoreCase(country, pageable).addCallback(getSearchCallback(outerLatch, errors, datasourceResponse));
109

  
110
		waitLatch(outerLatch, errors, timeout);
111

  
112
		return datasourceResponse;
113
	}
114

  
115
	public List<DatasourceResponse> searchByContactemail(final String email, final Pageable pageable) {
116
		final List<DatasourceResponse> datasourceResponse = Lists.newArrayList();
117

  
118
		final CountDownLatch outerLatch = new CountDownLatch(3);
119
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
120

  
121
		log.debug(String.format("search ds by email '%s'", email));
122
		dsRepository.findByContactemailContainingAllIgnoreCase(email, pageable).addCallback(getSearchCallback(outerLatch, errors, datasourceResponse));
123

  
124
		waitLatch(outerLatch, errors, timeout);
125

  
126
		return datasourceResponse;
127
	}
128

  
129
	private ListenableFutureCallback<Slice<Datasource>> getSearchCallback(final CountDownLatch outerLatch,
130
			final Queue<Throwable> errors,
131
			final List<DatasourceResponse> datasourceResponse) {
132

  
133
		return new ListenableFutureCallback<Slice<Datasource>>() {
134

  
135
			@Override
136
			public void onSuccess(final Slice<Datasource> datasources) {
137
				datasources.forEach(d -> {
138
					final DatasourceResponse response = new DatasourceResponse();
139
					response.setDatasource(d);
140
					getAggregationHistory(d.getId(), outerLatch, errors, response);
141
					getIndexDsInfo(d.getId(), outerLatch, errors, response);
142
					datasourceResponse.add(response);
143
				});
144
				outerLatch.countDown();
145
			}
146

  
147
			@Override
148
			public void onFailure(final Throwable e) {
149
				errors.offer(e);
150
				outerLatch.countDown();
151
			}
152
		};
153
	}
154

  
155
	public ClientResponse getInfo(final String dsId) {
156

  
157
		final CountDownLatch outerLatch = new CountDownLatch(3);
158
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
159
		final DatasourceResponse datasourceResponse = new DatasourceResponse();
160

  
161
		getAggregationHistory(dsId, outerLatch, errors, datasourceResponse);
162

  
163
		dsRepository.findOneById(dsId).addCallback(new ListenableFutureCallback<Datasource>() {
164
			@Override
165
			public void onSuccess(final Datasource datasource) {
166
				datasourceResponse.setDatasource(datasource);
167
				outerLatch.countDown();
168
			}
169

  
170
			@Override
171
			public void onFailure(final Throwable e) {
172
				errors.offer(e);
173
				outerLatch.countDown();
174
			}
175
		});
176

  
177
		getIndexDsInfo(dsId, outerLatch, errors, datasourceResponse);
178

  
179
		waitLatch(outerLatch, errors, timeout);
180

  
181
		/*
182
		if (!errors.isEmpty()) {
183
			datasourceResponse.getResponseHeader().setError(Joiner.on("\n").skipNulls().join(errors.stream().map(e -> e.getMessage()).collect(Collectors.toList())));
184
			log.error(Joiner.on("\n").skipNulls().join(errors.stream().map(e -> ExceptionUtils.getFullStackTrace(e)).collect(Collectors.toList())));
185
		}
186
		*/
187

  
188
		return new ClientResponse().datasourceInfo(datasourceResponse).errors(errors);
189
	}
190

  
191
	public void setManaged(final String id, final boolean managed) {
192
		log.info(String.format("setting managed = '%s' for ds '%s'", managed, id));
193
		dsRepository.setManaged(id, managed);
194
	}
195

  
196
	public List<SearchInterfacesEntry> searchInterface(final String field, final String value) {
197
		switch (field) {
198
		case "__SEARCH__":
199
			return searchApiRepository
200
					.findByRepoidContainingOrRepoNameContainingOrAlternativeNameContainingOrRepoPrefixContainingOrRepoOrganizationContainingAllIgnoreCase(
201
							value, value, value, value, value);
202
		case "country":
203
			break;
204
		case "type":
205
			break;
206
		case "protocol":
207
			break;
208
		case "compliance":
209
			break;
210
		case "active":
211
			break;
212
		default:
213
			throw new IllegalArgumentException("");
214
		}
215
		return null;
216
	}
217

  
218
	public List<CountryTerm> browseCountries() {
219
		return countryTermRepository.findAll();
220
	}
221

  
222
	public List<TypologyTerm> browseTypologies() {
223
		return typologyTermRepository.findAll();
224
	}
225

  
226
	public List<ProtocolTerm> browseProtocols() {
227
		return protocolTermRepository.findAll();
228
	}
229

  
230
	public List<CompatibilityTerm> browseCompatibility() {
231
		return compatibilityTermRepository.findAll();
232
	}
233

  
234
	public List<ActivationTerm> browseActivation() {
235
		return activationTermRepository.findAll();
236
	}
237

  
238
	private void getIndexDsInfo(final String dsId,
239
			final CountDownLatch outerLatch,
240
			final Queue<Throwable> errors,
241
			final DatasourceResponse datasourceResponse) {
242
		Futures.addCallback(
243
				service.submit(() -> isLookupClient.calculateCurrentIndexDsInfo()),
244
				new FutureCallback<IndexDsInfo>() {
245

  
246
					public void onSuccess(final IndexDsInfo info) {
247

  
248
						final CountDownLatch innerLatch = new CountDownLatch(1);
249

  
250
						Futures.addCallback(
251
								service.submit(() -> datasourceIndexClient.getIndexInfo(dsId, info)),
252
								new FutureCallback<IndexRecordsInfo>() {
253
									public void onSuccess(IndexRecordsInfo info) {
254
										datasourceResponse.setIndexRecords(info.getCount()).setLastIndexingDate(info.getDate());
255
										innerLatch.countDown();
256
									}
257
									public void onFailure(Throwable e) {
258
										errors.offer(e);
259
										innerLatch.countDown();
260
									}
261
						});
262
						waitLatch(innerLatch, errors, timeout);
263

  
264
						outerLatch.countDown();
265
					}
266

  
267
					public void onFailure(final Throwable e) {
268
						errors.offer(e);
269
						outerLatch.countDown();
270
					}
271
		});
272
	}
273

  
274
	private void getAggregationHistory(final String dsId,
275
			final CountDownLatch outerLatch,
276
			final Queue<Throwable> errors,
277
			final DatasourceResponse datasourceResponse) {
278
		Futures.addCallback(
279
				service.submit(() -> mongoLoggerClient.getAggregationHistory(dsId)),
280
				new FutureCallback<List<AggregationInfo>>() {
281
					public void onSuccess(List<AggregationInfo> info) {
282
						setAggregationHistory(datasourceResponse, info);
283
						outerLatch.countDown();
284
					}
285
					public void onFailure(Throwable e) {
286
						errors.offer(e);
287
						outerLatch.countDown();
288
					}
289
		});
290
	}
291

  
292
	private void setAggregationHistory(DatasourceResponse datasourceResponse, final List<AggregationInfo> info) {
293
		datasourceResponse.setAggregationHistory(info);
294
		if (!info.isEmpty()) {
295
			datasourceResponse
296
					.setLastCollection(info.stream().filter(a -> AggregationStage.COLLECT.equals(a.getAggregationStage())).findFirst().get())
297
					.setLastTransformation(info.stream().filter(a -> AggregationStage.TRANSFORM.equals(a.getAggregationStage())).findFirst().get());
298
		}
299
	}
300

  
301
	private void waitLatch(final CountDownLatch latch, final Queue<Throwable> errors, final int waitSeconds) {
302
		try {
303
			if (!latch.await(waitSeconds, TimeUnit.SECONDS)) {
304
				errors.offer(new TimeoutException("Waiting for requests to complete has timed out."));
305
			}
306
		} catch (final InterruptedException e) {
307
			errors.offer(e);
308
		}
309
	}
310

  
311
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/repository/SearchInterfaceRepository.java
1
package eu.dnetlib.datasource.publisher.repository;
2

  
3
import java.util.List;
4

  
5
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry;
6
import org.springframework.data.jpa.repository.JpaRepository;
7
import org.springframework.stereotype.Repository;
8

  
9
/**
10
 * Created by claudio on 12/04/2017.
11
 */
12
@Repository
13
public interface SearchInterfaceRepository extends JpaRepository<SearchInterfacesEntry, String> {
14

  
15
	SearchInterfacesEntry findOneById(String id);
16

  
17
	List<SearchInterfacesEntry> findByRepoidContainingOrRepoNameContainingOrAlternativeNameContainingOrRepoPrefixContainingOrRepoOrganizationContainingAllIgnoreCase(
18
			String repoId,
19
			String repoName,
20
			String alternativeName,
21
			String repoPrefix,
22
			String repoOrganization);
23

  
24
}
25

  
26

  
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/repository/ApiRepository.java
1
package eu.dnetlib.datasource.publisher.repository;
2

  
3
import java.util.List;
4

  
5
import eu.dnetlib.datasource.publisher.model.db.Api;
6
import org.apache.commons.lang.StringUtils;
7
import org.apache.commons.lang3.RandomStringUtils;
8
import org.springframework.data.jpa.repository.JpaRepository;
9
import org.springframework.data.jpa.repository.Query;
10
import org.springframework.stereotype.Repository;
11

  
12
/**
13
 * Created by claudio on 15/06/2017.
14
 */
15
@Repository
16
public interface ApiRepository extends JpaRepository<Api, String> {
17

  
18
	String ID_PREFIX = "api_________::";
19

  
20
	static String createId(Api api) {
21
		return ID_PREFIX + api.getDatasource() + "::" + RandomStringUtils.randomAlphanumeric(8);
22
	}
23

  
24
	@Query("select a from Api a where a.datasource = ?1")
25
	List<Api> findByDatasource(String dsId);
26
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/repository/DatasourceRepository.java
35 35
	@Query("update Datasource d set d.managed = ?2 where d.id = ?1")
36 36
	void setManaged(String id, boolean managed);
37 37

  
38
	@Modifying
39
	@Transactional
40
	@Query("update Datasource d set d.officialname = ?2 where d.id = ?1")
41
	void setOfficialname(String id, String officialname);
42

  
43
	@Modifying
44
	@Transactional
45
	@Query("update Datasource d set d.englishname = ?2 where d.id = ?1")
46
	void setEnglishname(String id, String englishname);
47

  
48
	@Modifying
49
	@Transactional
50
	@Query("update Datasource d set d.latitude = ?2 where d.id = ?1")
51
	void setLatitude(String id, Double latitude);
52

  
53
	@Modifying
54
	@Transactional
55
	@Query("update Datasource d set d.longitude = ?2 where d.id = ?1")
56
	void setLongitude(String id, Double longitude);
57

  
38 58
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/db/ApiParamKey.java
3 3
import java.io.Serializable;
4 4
import java.util.Objects;
5 5
import javax.persistence.Embeddable;
6
import javax.persistence.JoinColumn;
7
import javax.persistence.ManyToOne;
6 8

  
9
import com.fasterxml.jackson.annotation.JsonIgnore;
10
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7 11
import com.google.common.collect.ComparisonChain;
8 12

  
9 13
/**
10 14
 * Created by claudio on 13/04/2017.
11 15
 */
12 16
@Embeddable
17
@JsonIgnoreProperties(ignoreUnknown = true)
13 18
public class ApiParamKey implements Serializable {
14 19

  
20
	@JsonIgnore
21
	@ManyToOne
22
	@JoinColumn(name = "api")
23
	private Api api = null;
24

  
15 25
	private String param;
16 26

  
17
	private String value;
27
	public ApiParamKey() {}
18 28

  
19 29
	public String getParam() {
20 30
		return param;
21 31
	}
22 32

  
23
	public String getValue() {
24
		return value;
25
	}
26

  
27 33
	public ApiParamKey setParam(final String param) {
28 34
		this.param = param;
29 35
		return this;
30 36
	}
31 37

  
32
	public ApiParamKey setValue(final String value) {
33
		this.value = value;
34
		return this;
38
	public Api getApi() {
39
		return api;
35 40
	}
36 41

  
42
	public void setApi(final Api api) {
43
		this.api = api;
44
	}
45

  
37 46
	@Override
38 47
	public int hashCode() {
39
		return Objects.hash(getParam(), getValue());
48
		return Objects.hash(getParam(), getApi().getId());
40 49
	}
41 50

  
42 51
	@Override
......
50 59
		ApiParamKey apk = (ApiParamKey) o;
51 60
		return ComparisonChain.start()
52 61
				.compare(getParam(), apk.getParam())
53
				.compare(getValue(), apk.getValue())
62
				.compare(getApi(), apk.getApi())
54 63
				.result() == 0;
55 64
	}
65

  
66

  
56 67
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/db/Organization.java
4 4
import java.util.Set;
5 5
import javax.persistence.*;
6 6

  
7
import com.fasterxml.jackson.annotation.JsonAutoDetect;
8 7
import com.fasterxml.jackson.annotation.JsonIgnore;
9 8
import io.swagger.annotations.ApiModel;
10 9

  
......
13 12
 */
14 13
@Entity
15 14
@Table(name = "dsm_organizations")
16
@JsonAutoDetect
15
//@JsonAutoDetect
17 16
@ApiModel(value = "Organization model", description = "provides organization details")
18 17
public class Organization {
19 18

  
......
67 66
			mappedBy = "organizations")
68 67
	private Set<Datasource> datasources;
69 68

  
69
	public Organization() {}
70

  
70 71
	public String getId() {
71 72
		return id;
72 73
	}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/db/Api.java
5 5
import java.util.Set;
6 6
import javax.persistence.*;
7 7

  
8
import com.fasterxml.jackson.annotation.JsonAutoDetect;
9
import com.fasterxml.jackson.annotation.JsonIgnore;
8
import com.google.common.collect.ComparisonChain;
10 9
import com.google.gson.Gson;
11 10
import io.swagger.annotations.ApiModel;
12 11

  
......
15 14
 */
16 15
@Entity
17 16
@Table(name = "dsm_api")
18
@JsonAutoDetect
19 17
@ApiModel(value = "Datasource Api model", description = "describes the datasource api")
20
public class Api {
18
public class Api implements Comparable<Api> {
21 19

  
22 20
	@Id
23 21
	private String id = null;
24 22

  
25 23
	private String protocol = null;
26
	@JsonIgnore
27
	@ManyToOne
28
	@JoinColumn(name = "datasource")
29
	private Datasource datasource = null;
24

  
25
	private String datasource = null;
30 26
	private String contentdescription = null;
31 27
	private Boolean active = false;
32 28
	private Boolean removable = false;
......
63 59

  
64 60
	private String baseurl = null;
65 61

  
66
	@OneToMany(mappedBy="api", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
62
	@OneToMany(mappedBy = "id.api", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
67 63
	private Set<Apiparam> apiparam;
68 64

  
65
	public Api() {}
66

  
69 67
	public String getId() {
70 68
		return id;
71 69
	}
......
74 72
		return protocol;
75 73
	}
76 74

  
77
	public Datasource getDatasource() {
75
	public String getDatasource() {
78 76
		return datasource;
79 77
	}
80 78

  
......
160 158
		return this;
161 159
	}
162 160

  
163
	public Api setDatasource(final Datasource datasource) {
161
	public Api setDatasource(final String datasource) {
164 162
		this.datasource = datasource;
165 163
		return this;
166 164
	}
......
277 275
		return new Gson().toJson(this);
278 276
	}
279 277

  
278
	@Override
279
	public int compareTo(final Api a) {
280
		return ComparisonChain.start()
281
				.compare(getId(), a.getId())
282
				.result();
283
	}
284

  
280 285
}
281 286

  
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/db/Identity.java
1 1
package eu.dnetlib.datasource.publisher.model.db;
2 2

  
3
import javax.persistence.*;
3
import javax.persistence.Entity;
4
import javax.persistence.Id;
5
import javax.persistence.Table;
4 6

  
5
import com.fasterxml.jackson.annotation.JsonAutoDetect;
6 7
import io.swagger.annotations.ApiModel;
7 8

  
8 9
/**
9 10
 * Created by claudio on 13/04/2017.
10 11
 */
12

  
11 13
@Entity
12 14
@Table(name = "dsm_identities")
13
@JsonAutoDetect
15
//@JsonAutoDetect
14 16
@ApiModel(value = "Identities model", description = "provides identity details")
15 17
public class Identity {
16 18

  
......
19 21

  
20 22
	private String issuertype;
21 23

  
22
	public String pid() {
24
	public Identity() {}
25

  
26
	public String getPid() {
23 27
		return this.pid;
24 28
	}
25 29

  
26
	public String issuertype() {
30
	public String getIssuertype() {
27 31
		return this.issuertype;
28 32
	}
29 33

  
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/db/Apiparam.java
1 1
package eu.dnetlib.datasource.publisher.model.db;
2 2

  
3
import javax.persistence.*;
3
import javax.persistence.EmbeddedId;
4
import javax.persistence.Entity;
5
import javax.persistence.Table;
4 6

  
5
import com.fasterxml.jackson.annotation.JsonAutoDetect;
6 7
import com.fasterxml.jackson.annotation.JsonIgnore;
8
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
7 9
import io.swagger.annotations.ApiModel;
8 10

  
9 11
/**
......
11 13
 */
12 14
@Entity
13 15
@Table(name = "dsm_apiparams")
14
@JsonAutoDetect
16
@JsonIgnoreProperties(ignoreUnknown = true)
15 17
@ApiModel(value = "Datasource Api params model", description = "describes the datasource api params")
16 18
public class Apiparam {
17 19

  
20
	@JsonIgnore
18 21
	@EmbeddedId
19 22
	private ApiParamKey id;
20 23

  
21
	@JsonIgnore
22
	@ManyToOne
23
	@JoinColumn(name = "api")
24
	private Api api = null;
24
	private String value;
25 25

  
26
	public Apiparam() {}
27

  
26 28
	public ApiParamKey getId() {
27 29
		return id;
28 30
	}
29 31

  
30
	public Api getApi() {
31
		return api;
32
	}
33

  
34 32
	public Apiparam setId(final ApiParamKey id) {
35 33
		this.id = id;
36 34
		return this;
37 35
	}
38 36

  
39
	public Apiparam setApi(final Api api) {
40
		this.api = api;
41
		return this;
37
	public String getValue() {
38
		return value;
42 39
	}
43 40

  
41
	public void setValue(final String value) {
42
		this.value = value;
43
	}
44

  
45
	public String getParam() {
46
		return id.getParam();
47
	}
48

  
49
	public void setParam(final String param) {
50
		getId().setParam(param);
51
	}
52

  
44 53
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/db/Datasource.java
4 4
import java.util.Set;
5 5
import javax.persistence.*;
6 6

  
7
import com.fasterxml.jackson.annotation.JsonAutoDetect;
8 7
import io.swagger.annotations.ApiModel;
9 8

  
10 9
/**
......
12 11
 */
13 12
@Entity
14 13
@Table(name = "dsm_datasources")
15
@JsonAutoDetect
14
//@JsonAutoDetect
16 15
@ApiModel(value = "Datasource model", description = "provides datasource details")
17 16
public class Datasource {
18 17

  
19 18
	@Id
20 19
	private String id = null;
21
	private String officialname = null; //findByOfficialnameContainingOrEnglishnameContaining
20
	private String officialname = null;
22 21
	private String englishname = null;
23 22
	private String websiteurl = null;
24 23
	private String logourl = null;
......
71 70

  
72 71
	private Boolean managed;
73 72

  
74
	@OneToMany(mappedBy="datasource", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
73
	/*
74
	@JsonIgnore
75
	@OneToMany(mappedBy = "datasource", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
75 76
	private Set<Api> api = null;
77
	*/
76 78

  
77 79
	@ManyToMany(cascade = { CascadeType.PERSIST, CascadeType.MERGE }, fetch = FetchType.EAGER)
78 80
	@JoinTable(
......
90 92
			inverseJoinColumns = @JoinColumn(name="pid"))
91 93
	private Set<Identity> identities;
92 94

  
95
	public Datasource() {}
96

  
93 97
	public String getId() {
94 98
		return id;
95 99
	}
......
254 258
		return managed;
255 259
	}
256 260

  
257
	public Set<Api> getApi() {
258
		return api;
259
	}
260

  
261 261
	public Set<Organization> getOrganizations() {
262 262
		return organizations;
263 263
	}
......
471 471
		return this;
472 472
	}
473 473

  
474
	public Datasource setApi(final Set<Api> api) {
475
		this.api = api;
476
		return this;
477
	}
478

  
479 474
	public Datasource setOrganizations(final Set<Organization> organizations) {
480 475
		this.organizations = organizations;
481 476
		return this;
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/model/AggregationStage.java
18 18
		case "TRANSFORM":
19 19
		case "TRANSFORMATION":
20 20
			return AggregationStage.TRANSFORM;
21

  
21 22
		}
22 23
		throw new IllegalArgumentException("invalid AggregationStage: " + s);
23 24
	}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/clients/MongoLoggerClient.java
26 26

  
27 27
import static com.mongodb.client.model.Filters.and;
28 28
import static com.mongodb.client.model.Filters.eq;
29
import static com.mongodb.client.model.Filters.regex;
29 30

  
30 31
/**
31 32
 * Created by claudio on 20/10/2016.
......
51 52
		return and(
52 53
				eq("parentDatasourceId", dsId),
53 54
				eq("system:profileFamily", "aggregator"),
54
				eq("system:isCompletedSuccessfully", "true"));
55
				eq("system:isCompletedSuccessfully", "true"),
56
				regex("system:wfName", "(collect|transform)", "i"));
55 57
	}
56 58

  
57 59
	private synchronized MongoCollection<Document> getCollection() {
......
65 67
	@Cacheable("datasources-mongo-cache")
66 68
	public List<AggregationInfo> getAggregationHistory(final String dsId) throws ApiException {
67 69

  
68
		log.warn("getAggregationHistory(): not using cache");
70
		log.warn(String.format("getAggregationHistory(dsId = %s): not using cache", dsId));
69 71

  
70 72
		final Bson query = getQuery(dsId);
71

  
72 73
		return Utils.stream(getCollection().find(query).projection(fields).limit(limit).sort(dbo("system:startHumanDate", -1)).iterator())
73 74
				.map(getMapper())
74 75
				.filter(ai -> ai.getNumberOfRecords() >= 0 && StringUtils.isNotBlank(ai.getDate()))
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/clients/DatasourceDao.java
1
package eu.dnetlib.datasource.publisher.clients;
2

  
3
import java.util.List;
4
import java.util.Queue;
5
import java.util.concurrent.*;
6
import javax.annotation.Resource;
7

  
8
import com.google.common.collect.Lists;
9
import com.google.common.util.concurrent.*;
10
import eu.dnetlib.datasource.publisher.ApiException;
11
import eu.dnetlib.datasource.publisher.clients.utils.IndexDsInfo;
12
import eu.dnetlib.datasource.publisher.clients.utils.IndexRecordsInfo;
13
import eu.dnetlib.datasource.publisher.model.AggregationInfo;
14
import eu.dnetlib.datasource.publisher.model.AggregationStage;
15
import eu.dnetlib.datasource.publisher.model.DatasourceResponse;
16
import eu.dnetlib.datasource.publisher.model.db.*;
17
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry;
18
import eu.dnetlib.datasource.publisher.repository.*;
19
import org.apache.commons.lang.StringUtils;
20
import org.apache.commons.lang3.exception.ExceptionUtils;
21
import org.apache.commons.logging.Log;
22
import org.apache.commons.logging.LogFactory;
23
import org.apache.http.HttpStatus;
24
import org.springframework.beans.factory.annotation.Autowired;
25
import org.springframework.beans.factory.annotation.Value;
26
import org.springframework.data.domain.Pageable;
27
import org.springframework.data.domain.Slice;
28
import org.springframework.util.concurrent.ListenableFuture;
29
import org.springframework.util.concurrent.ListenableFutureCallback;
30

  
31
/**
32
 * Created by claudio on 20/10/2016.
33
 */
34
public class DatasourceDao {
35

  
36
	private static final Log log = LogFactory.getLog(DatasourceDao.class);
37

  
38
	@Autowired
39
	private MongoLoggerClient mongoLoggerClient;
40

  
41
	@Autowired
42
	private DatasourceIndexClient datasourceIndexClient;
43

  
44
	@Autowired
45
	private DatasourceRepository dsRepository;
46

  
47
	@Autowired
48
	private SearchInterfaceRepository searchInterfaceRepository;
49

  
50
	@Autowired
51
	private CountryTermRepository countryTermRepository;
52

  
53
	@Autowired
54
	private TypologyTermRepository typologyTermRepository;
55

  
56
	@Autowired
57
	private ProtocolTermRepository protocolTermRepository;
58

  
59
	@Autowired
60
	private CompatibilityTermRepository compatibilityTermRepository;
61

  
62
	@Autowired
63
	private ActivationTermRepository activationTermRepository;
64

  
65
	@Autowired
66
	private ApiRepository apiRepository;
67

  
68
	@Resource(name = "datasourceIsLookupClient")
69
	private ISLookupClient isLookupClient;
70

  
71
	private ListeningExecutorService service;
72

  
73
	private final static int WORKERS = 100;
74

  
75
	@Value("${datasource.publisher.timeout}")
76
	private int timeout = 10;
77

  
78
	public DatasourceDao() {
79
		service = MoreExecutors.listeningDecorator(
80
				new ScheduledThreadPoolExecutor(WORKERS,
81
						new ThreadFactoryBuilder().setNameFormat("datasource-info-retriever-%d").build()));
82
	}
83

  
84
	public List<String> listIds(final Pageable pageable) throws ApiException {
85
		return dsRepository.findAll(pageable)
86
				.map(d -> d.getId())
87
				.getContent();
88
	}
89

  
90
	public List<DatasourceResponse> searchByName(final String name, final Pageable pageable) {
91

  
92
		final List<DatasourceResponse> datasourceResponse = Lists.newArrayList();
93

  
94
		final CountDownLatch outerLatch = new CountDownLatch(3);
95
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
96

  
97
		log.debug(String.format("search ds by name '%s'", name));
98
		final ListenableFuture<Slice<Datasource>> c =
99
				dsRepository.findByOfficialnameContainingOrEnglishnameContainingAllIgnoreCase(name, name, pageable);
100
		c.addCallback(getSearchCallback(outerLatch, errors, datasourceResponse));
101

  
102
		waitLatch(outerLatch, errors, timeout);
103

  
104
		return datasourceResponse;
105
	}
106

  
107
	public List<DatasourceResponse> searchByCountry(final String country, final Pageable pageable) {
108

  
109
		final List<DatasourceResponse> datasourceResponse = Lists.newArrayList();
110

  
111
		final CountDownLatch outerLatch = new CountDownLatch(3);
112
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
113

  
114
		log.debug(String.format("search ds by country '%s'", country));
115
		//dsRepository.findByOrganizationsCountryIgnoreCase(country, pageable).addCallback(getSearchCallback(outerLatch, errors, datasourceResponse));
116

  
117
		waitLatch(outerLatch, errors, timeout);
118

  
119
		return datasourceResponse;
120
	}
121

  
122
	public List<DatasourceResponse> searchByContactemail(final String email, final Pageable pageable) {
123
		final List<DatasourceResponse> datasourceResponse = Lists.newArrayList();
124

  
125
		final CountDownLatch outerLatch = new CountDownLatch(3);
126
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
127

  
128
		log.debug(String.format("search ds by email '%s'", email));
129
		dsRepository.findByContactemailContainingAllIgnoreCase(email, pageable).addCallback(getSearchCallback(outerLatch, errors, datasourceResponse));
130

  
131
		waitLatch(outerLatch, errors, timeout);
132

  
133
		return datasourceResponse;
134
	}
135

  
136
	private ListenableFutureCallback<Slice<Datasource>> getSearchCallback(final CountDownLatch outerLatch,
137
			final Queue<Throwable> errors,
138
			final List<DatasourceResponse> datasourceResponse) {
139

  
140
		return new ListenableFutureCallback<Slice<Datasource>>() {
141

  
142
			@Override
143
			public void onSuccess(final Slice<Datasource> datasources) {
144
				datasources.forEach(d -> {
145
					final DatasourceResponse response = new DatasourceResponse();
146
					response.setDatasource(d);
147
					getAggregationHistory(d.getId(), outerLatch, errors, response);
148
					getIndexDsInfo(d.getId(), outerLatch, errors, response);
149
					datasourceResponse.add(response);
150
				});
151
				outerLatch.countDown();
152
			}
153

  
154
			@Override
155
			public void onFailure(final Throwable e) {
156
				errors.offer(e);
157
				outerLatch.countDown();
158
			}
159
		};
160
	}
161

  
162
	public ClientResponse getInfo(final String dsId) {
163

  
164
		final CountDownLatch outerLatch = new CountDownLatch(3);
165
		final Queue<Throwable> errors = new ConcurrentLinkedQueue<>();
166
		final DatasourceResponse datasourceResponse = new DatasourceResponse();
167

  
168
		getAggregationHistory(dsId, outerLatch, errors, datasourceResponse);
169

  
170
		dsRepository.findOneById(dsId).addCallback(new ListenableFutureCallback<Datasource>() {
171
			@Override
172
			public void onSuccess(final Datasource datasource) {
173
				datasourceResponse.setDatasource(datasource);
174
				outerLatch.countDown();
175
			}
176

  
177
			@Override
178
			public void onFailure(final Throwable e) {
179
				log.error(ExceptionUtils.getStackTrace(e));
180
				errors.offer(e);
181
				outerLatch.countDown();
182
			}
183
		});
184

  
185
		getIndexDsInfo(dsId, outerLatch, errors, datasourceResponse);
186

  
187
		waitLatch(outerLatch, errors, timeout);
188

  
189
		/*
190
		if (!errors.isEmpty()) {
191
			datasourceResponse.getResponseHeader().setError(Joiner.on("\n").skipNulls().join(errors.stream().map(e -> e.getMessage()).collect(Collectors.toList())));
192
			log.error(Joiner.on("\n").skipNulls().join(errors.stream().map(e -> ExceptionUtils.getFullStackTrace(e)).collect(Collectors.toList())));
193
		}
194
		*/
195

  
196
		return new ClientResponse().datasourceInfo(datasourceResponse).errors(errors);
197
	}
198

  
199
	public void setManaged(final String id, final boolean managed) {
200
		log.info(String.format("setting managed = '%s' for ds '%s'", managed, id));
201
		dsRepository.setManaged(id, managed);
202
	}
203

  
204
	public List<SearchInterfacesEntry> searchInterface(final String field, final String value) {
205
		switch (field) {
206
		case "__SEARCH__":
207
			return searchInterfaceRepository
208
					.findByRepoidContainingOrRepoNameContainingOrAlternativeNameContainingOrRepoPrefixContainingOrRepoOrganizationContainingAllIgnoreCase(
209
							value, value, value, value, value);
210
		case "country":
211
			break;
212
		case "type":
213
			break;
214
		case "protocol":
215
			break;
216
		case "compliance":
217
			break;
218
		case "active":
219
			break;
220
		default:
221
			throw new IllegalArgumentException("");
222
		}
223
		return null;
224
	}
225

  
226
	public List<CountryTerm> browseCountries() {
227
		return countryTermRepository.findAll();
228
	}
229

  
230
	public List<TypologyTerm> browseTypologies() {
231
		return typologyTermRepository.findAll();
232
	}
233

  
234
	public List<ProtocolTerm> browseProtocols() {
235
		return protocolTermRepository.findAll();
236
	}
237

  
238
	public List<CompatibilityTerm> browseCompatibility() {
239
		return compatibilityTermRepository.findAll();
240
	}
241

  
242
	public List<ActivationTerm> browseActivation() {
243
		return activationTermRepository.findAll();
244
	}
245

  
246
	public List<Api> getApi(final String dsId) {
247
		return apiRepository.findByDatasource(dsId);
248
	}
249

  
250
	public void deleteApi(final String apiId) {
251
		apiRepository.delete(apiId);
252
		log.info(String.format("deleted api '%s'", apiId));
253
	}
254

  
255
	public void addApi(final Api api) {
256

  
257
		if (StringUtils.isBlank(api.getId())) {
258
			api.setId(ApiRepository.createId(api));
259
			log.info(String.format("missing api id, created '%s'"));
260
		}
261

  
262
		apiRepository.save(api);
263
	}
264

  
265
	public boolean exist(final Datasource d) throws ApiException {
266
		try {
267
			return dsRepository.findOneById(d.getId()).get() != null;
268
		} catch (Exception e) {
269
			log.error(e);
270
			throw new ApiException(HttpStatus.SC_INTERNAL_SERVER_ERROR, String.format("error retrieving datasource information '%s'", d.getId()), e);
271
		}
272
	}
273

  
274
	public Datasource save(final Datasource d) {
275
		log.info(String.format("saving datasource '%s'", d.getId()));
276
		final Datasource datasource = dsRepository.save(d);
277
		log.info(String.format("saved datasource '%s'", datasource.getId()));
278
		return datasource;
279
	}
280

  
281
	public void updateOfficialName(final String dsId, final String officialname) {
282
		dsRepository.setOfficialname(dsId, officialname);
283
		log.info(String.format("updated datasource '%s' with officialname '%s'", dsId, officialname));
284
	}
285

  
286
	public void updateEnglishName(final String dsId, final String englishname) {
287
		dsRepository.setEnglishname(dsId, englishname);
288
		log.info(String.format("updated datasource '%s' with englishname '%s'", dsId, englishname));
289
	}
290

  
291
	public void updateLatitude(final String dsId, final Double latitude) {
292
		dsRepository.setLatitude(dsId, latitude);
293
		log.info(String.format("updated datasource '%s' with latitude '%s'", dsId, latitude));
294
	}
295

  
296
	public void updateLongitude(final String dsId, final Double longitude) {
297
		dsRepository.setLongitude(dsId, longitude);
298
		log.info(String.format("updated datasource '%s' with longitude '%s'", dsId, longitude));
299
	}
300

  
301
	private void getIndexDsInfo(final String dsId,
302
			final CountDownLatch outerLatch,
303
			final Queue<Throwable> errors,
304
			final DatasourceResponse datasourceResponse) {
305
		Futures.addCallback(
306
				service.submit(() -> isLookupClient.calculateCurrentIndexDsInfo()),
307
				new FutureCallback<IndexDsInfo>() {
308

  
309
					public void onSuccess(final IndexDsInfo info) {
310

  
311
						final CountDownLatch innerLatch = new CountDownLatch(1);
312

  
313
						Futures.addCallback(
314
								service.submit(() -> datasourceIndexClient.getIndexInfo(dsId, info)),
315
								new FutureCallback<IndexRecordsInfo>() {
316
									public void onSuccess(IndexRecordsInfo info) {
317
										datasourceResponse.setIndexRecords(info.getCount()).setLastIndexingDate(info.getDate());
318
										innerLatch.countDown();
319
									}
320
									public void onFailure(Throwable e) {
321
										errors.offer(e);
322
										innerLatch.countDown();
323
									}
324
						});
325
						waitLatch(innerLatch, errors, timeout);
326

  
327
						outerLatch.countDown();
328
					}
329

  
330
					public void onFailure(final Throwable e) {
331
						log.error(ExceptionUtils.getStackTrace(e));
332
						errors.offer(e);
333
						outerLatch.countDown();
334
					}
335
		});
336
	}
337

  
338
	private void getAggregationHistory(final String dsId,
339
			final CountDownLatch outerLatch,
340
			final Queue<Throwable> errors,
341
			final DatasourceResponse datasourceResponse) {
342
		Futures.addCallback(
343
				service.submit(() -> mongoLoggerClient.getAggregationHistory(dsId)),
344
				new FutureCallback<List<AggregationInfo>>() {
345
					public void onSuccess(List<AggregationInfo> info) {
346
						setAggregationHistory(datasourceResponse, info);
347
						outerLatch.countDown();
348
					}
349
					public void onFailure(Throwable e) {
350
						log.error(ExceptionUtils.getStackTrace(e));
351
						errors.offer(e);
352
						outerLatch.countDown();
353
					}
354
		});
355
	}
356

  
357
	private void setAggregationHistory(DatasourceResponse datasourceResponse, final List<AggregationInfo> info) {
358
		datasourceResponse.setAggregationHistory(info);
359
		if (!info.isEmpty()) {
360
			datasourceResponse
361
					.setLastCollection(info.stream().filter(a -> AggregationStage.COLLECT.equals(a.getAggregationStage())).findFirst().get())
362
					.setLastTransformation(info.stream().filter(a -> AggregationStage.TRANSFORM.equals(a.getAggregationStage())).findFirst().get());
363
		}
364
	}
365

  
366
	private void waitLatch(final CountDownLatch latch, final Queue<Throwable> errors, final int waitSeconds) {
367
		try {
368
			if (!latch.await(waitSeconds, TimeUnit.SECONDS)) {
369
				errors.offer(new TimeoutException("Waiting for requests to complete has timed out."));
370
			}
371
		} catch (final InterruptedException e) {
372
			errors.offer(e);
373
		}
374
	}
375

  
376
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/DatasourcesApi.java
5 5
import eu.dnetlib.datasource.publisher.model.BrowseTerm;
6 6
import eu.dnetlib.datasource.publisher.model.DatasourceResponse;
7 7
import eu.dnetlib.datasource.publisher.model.Response;
8
import eu.dnetlib.datasource.publisher.model.db.Api;
9
import eu.dnetlib.datasource.publisher.model.db.Datasource;
8 10
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry;
9
import io.swagger.annotations.*;
11
import io.swagger.annotations.ApiOperation;
12
import io.swagger.annotations.ApiResponse;
13
import io.swagger.annotations.ApiResponses;
10 14

  
11
@Api(value = "datasources", description = "the datasource manager API")
15
@io.swagger.annotations.Api(value = "datasources", description = "the datasource manager API")
12 16
public interface DatasourcesApi {
13 17

  
14 18
    @ApiOperation(value = "list identifiers", notes = "List the Datasource identifiers.", response = List.class)
15 19
    @ApiResponses(value = { 
16
        @ApiResponse(code = 200, message = "OK", response = List.class),
20
        @ApiResponse(code = 200, message = "OK", response = String[].class),
17 21
        @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
18 22
    List<String> listIds(int page, int size) throws ApiException;
19 23

  
20 24
    @ApiOperation(value = "get datasource by id", notes = "Returns Datasource details.", response = DatasourceResponse.class)
21 25
    @ApiResponses(value = { 
22
        @ApiResponse(code = 200, message = "OK", response = DatasourceResponse.class),
26
        @ApiResponse(code = 200, message = "OK", response = DatasourceResponse[].class),
23 27
        @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
24 28
    DatasourceResponse getDs(String id);
25 29

  
26
    @ApiOperation(value = "search datasources by name", notes = "Returns list of Datasource details.", response = DatasourceResponse.class)
30
    @ApiOperation(value = "search datasources by name", notes = "Returns list of Datasource details.", response = DatasourceResponse[].class)
27 31
    @ApiResponses(value = {
28
            @ApiResponse(code = 200, message = "OK", response = DatasourceResponse.class),
32
            @ApiResponse(code = 200, message = "OK", response = DatasourceResponse[].class),
29 33
            @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
30 34
    List<DatasourceResponse> searchByName(String name, int page, int size);
31 35

  
32
    @ApiOperation(value = "search datasources by contact email", notes = "Returns list of Datasource details.", response = DatasourceResponse.class)
36
    @ApiOperation(value = "search datasources by contact email", notes = "Returns list of Datasource details.", response = DatasourceResponse[].class)
33 37
    @ApiResponses(value = {
34
            @ApiResponse(code = 200, message = "OK", response = DatasourceResponse.class),
38
            @ApiResponse(code = 200, message = "OK", response = DatasourceResponse[].class),
35 39
            @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
36 40
    List<DatasourceResponse> searchByContactemail(String contactemail, int page, int size);
37 41

  
38
    @ApiOperation(value = "search datasources by country", notes = "Returns list of Datasource details.", response = DatasourceResponse.class)
42
    @ApiOperation(value = "search datasources by country", notes = "Returns list of Datasource details.", response = DatasourceResponse[].class)
39 43
    @ApiResponses(value = {
40
            @ApiResponse(code = 200, message = "OK", response = DatasourceResponse.class),
44
            @ApiResponse(code = 200, message = "OK", response = DatasourceResponse[].class),
41 45
            @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
42 46
    List<DatasourceResponse> searchByCountry(String country, int page, int size);
43 47

  
44
    @ApiOperation(value = "search among datasource APIs", notes = "Returns Datasource Api details.", response = SearchInterfacesEntry.class)
48
    @ApiOperation(value = "search among datasource APIs", notes = "Returns Datasource Api details.", response = SearchInterfacesEntry[].class)
45 49
    @ApiResponses(value = {
46
            @ApiResponse(code = 200, message = "OK", response = SearchInterfacesEntry.class),
50
            @ApiResponse(code = 200, message = "OK", response = SearchInterfacesEntry[].class),
47 51
            @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
48 52
    List<SearchInterfacesEntry> searchInterface(String field, String value);
49 53

  
50
    @ApiOperation(value = "terms and count for the given field", notes = "Returns the list of BrowseTerms.", response = BrowseTerm.class)
54
    @ApiOperation(value = "terms and count for the given field", notes = "Returns the list of BrowseTerms.", response = BrowseTerm[].class)
51 55
    @ApiResponses(value = {
52
            @ApiResponse(code = 200, message = "OK", response = BrowseTerm.class),
56
            @ApiResponse(code = 200, message = "OK", response = BrowseTerm[].class),
53 57
            @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
54 58
    List<? extends BrowseTerm> browseField(String field) throws ApiException;
55 59

  
60
    @ApiOperation(value = "get the list of API for a given datasource", notes = "Returns the list of API for a given datasource.", httpMethod = "GET", response = Api[].class)
61
    @ApiResponses(value = {
62
            @ApiResponse(code = 200, message = "OK", response = Api[].class),
63
            @ApiResponse(code = 500, message = "unexpected error", response = Response.class) })
64
    List<Api> getApi(String dsId) throws ApiException;
65

  
66
    @ApiOperation(value = "delete an API", notes = "delete an API", httpMethod = "DELETE")
67
    @ApiResponses(value = {
68
            @ApiResponse(code = 200, message = "OK"),
69
            @ApiResponse(code = 500, message = "unexpected error") })
70
    void deleteApi(String apiId) throws ApiException;
71

  
72
    @ApiOperation(value = "adds an API to one Datasource", notes = "adds an API to one Datasource", httpMethod = "POST")
73
    @ApiResponses(value = {
74
            @ApiResponse(code = 200, message = "OK"),
75
            @ApiResponse(code = 500, message = "unexpected error") })
76
    void addApi(Api api) throws ApiException;
77

  
56 78
    @ApiOperation(value = "set the managed status for a given datasource", notes = "set the managed status for a given datasource", httpMethod = "POST")
57 79
    @ApiResponses(value = {
58 80
            @ApiResponse(code = 200, message = "OK"),
59 81
            @ApiResponse(code = 500, message = "unexpected error") })
60 82
    void setManaged(String id, boolean managed);
61 83

  
84
    @ApiOperation(value = "adds a new datasource", notes = "adds a new datasource", httpMethod = "POST")
85
    @ApiResponses(value = {
86
            @ApiResponse(code = 200, message = "OK"),
87
            @ApiResponse(code = 500, message = "unexpected error") })
88
    void saveDatasource(Datasource datasource) throws ApiException;
89

  
90
    @ApiOperation(value = "updates a datasource official name", notes = "updates a datasource official name", httpMethod = "POST")
91
    @ApiResponses(value = {
92
            @ApiResponse(code = 200, message = "OK"),
93
            @ApiResponse(code = 500, message = "unexpected error") })
94
    void updateOfficialname(String dsId, String officialname) throws ApiException;
95

  
96
    @ApiOperation(value = "updates a datasource english name", notes = "updates a datasource english name", httpMethod = "POST")
97
    @ApiResponses(value = {
98
            @ApiResponse(code = 200, message = "OK"),
99
            @ApiResponse(code = 500, message = "unexpected error") })
100
    void updateEnglishname(String dsId, String english) throws ApiException;
101

  
102
    @ApiOperation(value = "updates a datasource latitude", notes = "updates a datasource latitude", httpMethod = "POST")
103
    @ApiResponses(value = {
104
            @ApiResponse(code = 200, message = "OK"),
105
            @ApiResponse(code = 500, message = "unexpected error") })
106
    void updateLatitude(String dsId, Double latitude) throws ApiException;
107

  
108
    @ApiOperation(value = "updates a datasource longitude", notes = "updates a datasource longitude", httpMethod = "POST")
109
    @ApiResponses(value = {
110
            @ApiResponse(code = 200, message = "OK"),
111
            @ApiResponse(code = 500, message = "unexpected error") })
112
    void updateLongitude(String dsId, Double longitude) throws ApiException;
113

  
62 114
}
modules/dnet-datasource-rest-api/trunk/src/main/java/eu/dnetlib/datasource/publisher/DatasourcesApiController.java
4 4

  
5 5
import eu.dnetlib.common.rmi.DNetRestDocumentation;
6 6
import eu.dnetlib.datasource.publisher.clients.ClientResponse;
7
import eu.dnetlib.datasource.publisher.clients.DatasourceInfoRetriever;
7
import eu.dnetlib.datasource.publisher.clients.DatasourceDao;
8 8
import eu.dnetlib.datasource.publisher.model.BrowseTerm;
9 9
import eu.dnetlib.datasource.publisher.model.DatasourceResponse;
10
import eu.dnetlib.datasource.publisher.model.db.Api;
11
import eu.dnetlib.datasource.publisher.model.db.Datasource;
10 12
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry;
13

  
14
import org.apache.commons.lang.StringUtils;
11 15
import org.apache.commons.logging.Log;
12 16
import org.apache.commons.logging.LogFactory;
13 17
import org.apache.http.HttpStatus;
......
22 26
	private static final Log log = LogFactory.getLog(DatasourcesApiController.class);
23 27

  
24 28
	@Autowired
25
	private DatasourceInfoRetriever dsInfoRetriever;
29
	private DatasourceDao dsDao;
26 30

  
27 31
	@Override
28 32
	@RequestMapping(value = "/ds/list/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
29 33
    public List<String> listIds(@PathVariable int page, @PathVariable int size) throws ApiException {
30
	    return dsInfoRetriever.listIds(new PageRequest(page, size));
34
	    return dsDao.listIds(new PageRequest(page, size));
31 35
    }
32 36

  
33 37
    @Override
......
38 42
		    log.debug(String.format("getDatasourceInfo(dsId = %s)", id));
39 43
	    }
40 44

  
41
	    final ClientResponse clientResponse = dsInfoRetriever.getInfo(id);
45
	    final ClientResponse clientResponse = dsDao.getInfo(id);
42 46
    	return clientResponse.getDatasourceResponse();
43 47
    }
44 48

  
......
46 50
	@RequestMapping(value = "/ds/search/name/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
47 51
	public List<DatasourceResponse> searchByName(
48 52
			@RequestParam final String name, @PathVariable final int page, @PathVariable final int size) {
49
		return dsInfoRetriever.searchByName(name, new PageRequest(page, size));
53
		return dsDao.searchByName(name, new PageRequest(page, size));
50 54
	}
51 55

  
52 56
	@Override
53 57
	@RequestMapping(value = "/ds/search/email/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
54 58
	public List<DatasourceResponse> searchByContactemail(
55 59
			@RequestParam final String contactemail, @PathVariable final int page, @PathVariable final int size) {
56
		return dsInfoRetriever.searchByContactemail(contactemail, new PageRequest(page, size));
60
		return dsDao.searchByContactemail(contactemail, new PageRequest(page, size));
57 61
	}
58 62

  
59 63
	@Override
60 64
	@RequestMapping(value = "/ds/search/country/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
61 65
	public List<DatasourceResponse> searchByCountry(
62 66
			@RequestParam final String country, @PathVariable final int page, @PathVariable final int size) {
63
		return dsInfoRetriever.searchByCountry(country, new PageRequest(page, size));
67
		return dsDao.searchByCountry(country, new PageRequest(page, size));
64 68
	}
65 69

  
66 70
	@Override
67 71
	@RequestMapping(value = "/api/search/{field}", produces = { "application/json" }, method = RequestMethod.GET)
68 72
	public List<SearchInterfacesEntry> searchInterface(@PathVariable final String field, @RequestParam final String value) {
69
		return dsInfoRetriever.searchInterface(field, value);
73
		return dsDao.searchInterface(field, value);
70 74
	}
71 75

  
72 76
	@Override
......
74 78
	public List<? extends BrowseTerm> browseField(@PathVariable final String field) throws ApiException {
75 79
		switch (field) {
76 80
		case "country":
77
			return dsInfoRetriever.browseCountries();
81
			return dsDao.browseCountries();
78 82
		case "typology":
79
			return dsInfoRetriever.browseTypologies();
83
			return dsDao.browseTypologies();
80 84
		case "protocol":
81
			return dsInfoRetriever.browseProtocols();
85
			return dsDao.browseProtocols();
82 86
		case "compatibility":
83
			return dsInfoRetriever.browseCompatibility();
87
			return dsDao.browseCompatibility();
84 88
		case "activation":
85
			return dsInfoRetriever.browseActivation();
89
			return dsDao.browseActivation();
86 90
		default:
87 91
			throw new ApiException(HttpStatus.SC_BAD_REQUEST, String.format("unsupported browse field '%s'", field));
88 92
		}
89 93
	}
90 94

  
91 95
	@Override
96
	@RequestMapping(value = "/ds/api/{dsId}", produces = { "application/json" }, method = RequestMethod.GET)
97
	public List<Api> getApi(@PathVariable final String dsId) throws ApiException {
98
		return dsDao.getApi(dsId);
99
	}
100

  
101
	@Override
102
	@RequestMapping(value = "/ds/api/{dsId}", method = RequestMethod.DELETE)
103
	public void deleteApi(@PathVariable final String apiId) throws ApiException {
104
		dsDao.deleteApi(apiId);
105
	}
106

  
107
	@Override
108
	@RequestMapping(value = "/ds/api/add", method = RequestMethod.POST)
109
	public void addApi(@RequestParam final Api api) throws ApiException {
110
		if (StringUtils.isBlank(api.getDatasource())) {
111
			throw new ApiException(HttpStatus.SC_BAD_REQUEST, "missing datasource id");
112
		}
113
		dsDao.addApi(api);
114
	}
115

  
116
	@Override
92 117
	@RequestMapping(value = "/ds/manage", method = RequestMethod.POST)
93 118
	public void setManaged(@RequestParam final String id, @RequestParam final boolean managed) {
94
		dsInfoRetriever.setManaged(id, managed);
119
		dsDao.setManaged(id, managed);
95 120
	}
96 121

  
122
	@Override
123
	@RequestMapping(value = "/ds/add", method = RequestMethod.POST)
124
	public void saveDatasource(@RequestBody final Datasource datasource) throws ApiException {
125
		if (dsDao.exist(datasource)) { //TODO further check that the DS doesn't have any API
... This diff was truncated because it exceeds the maximum size that can be displayed.

Also available in: Unified diff