1
|
package eu.dnetlib.openaire.directindex.api;
|
2
|
|
3
|
import java.util.Map;
|
4
|
import javax.annotation.Resource;
|
5
|
|
6
|
import com.google.gson.Gson;
|
7
|
import eu.dnetlib.common.rmi.DNetRestDocumentation;
|
8
|
import eu.dnetlib.data.index.CloudIndexClient;
|
9
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
10
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
11
|
import eu.dnetlib.openaire.directindex.objects.ResultEntry;
|
12
|
import eu.dnetlib.openaire.directindex.utils.OafToIndexRecordFactory;
|
13
|
import org.apache.commons.lang.exception.ExceptionUtils;
|
14
|
import org.apache.commons.logging.Log;
|
15
|
import org.apache.commons.logging.LogFactory;
|
16
|
import org.apache.velocity.app.VelocityEngine;
|
17
|
import org.springframework.beans.factory.annotation.Value;
|
18
|
import org.springframework.http.HttpStatus;
|
19
|
import org.springframework.stereotype.Controller;
|
20
|
import org.springframework.web.bind.annotation.*;
|
21
|
|
22
|
/**
|
23
|
* Created by michele on 11/11/15.
|
24
|
*/
|
25
|
@Controller
|
26
|
@DNetRestDocumentation
|
27
|
public class OpenaireResultSubmitter {
|
28
|
|
29
|
private static final Log log = LogFactory.getLog(OpenaireResultSubmitter.class);
|
30
|
|
31
|
@Value(value = "oaf.schema.location")
|
32
|
private String oafSchemaLocation;
|
33
|
|
34
|
@Resource
|
35
|
private UniqueServiceLocator serviceLocator;
|
36
|
|
37
|
@Resource
|
38
|
private OafToIndexRecordFactory oafToIndexRecordFactory;
|
39
|
|
40
|
@Resource
|
41
|
private RecentResultsQueue recentResultsQueue;
|
42
|
|
43
|
@Resource(name = "openaireplusApisVelocityEngine")
|
44
|
private VelocityEngine velocityEngine;
|
45
|
|
46
|
@Resource(name = "resultSubmitterClientMap")
|
47
|
private IndexClientMap clientMap;
|
48
|
|
49
|
@Resource(name = "resultSubmitterService")
|
50
|
private ResultSubmitterService submitterService;
|
51
|
|
52
|
@RequestMapping(value = { "/api/admin/autocommit/active" }, method = RequestMethod.GET)
|
53
|
public @ResponseBody Boolean getAutocommit() throws DirecIndexApiException {
|
54
|
return submitterService.isAutocommitactive();
|
55
|
}
|
56
|
|
57
|
@RequestMapping(value = { "/api/admin/autocommit/active" }, method = RequestMethod.POST)
|
58
|
public @ResponseBody Boolean setAutocommit(@RequestParam(value = "active", required = true) final Boolean active) throws DirecIndexApiException {
|
59
|
submitterService.setAutocommitactive(active);
|
60
|
log.info(String.format("automatic commit, active '%s', frequency '%s'", submitterService.isAutocommitactive(), submitterService.getCommitfrquency()));
|
61
|
return submitterService.isAutocommitactive();
|
62
|
}
|
63
|
|
64
|
@Deprecated
|
65
|
@RequestMapping(value = { "/api/publications/feedJson", "/api/results/feedJson" }, method = RequestMethod.POST)
|
66
|
public @ResponseBody String feedObjectJson(@RequestParam(value = "json", required = true) final String json,
|
67
|
@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws DirecIndexApiException {
|
68
|
final ResultEntry pub = new Gson().fromJson(json, ResultEntry.class);
|
69
|
return feedObject(pub, commit);
|
70
|
}
|
71
|
|
72
|
@RequestMapping(value = { "/api/results/feedObject" }, method = RequestMethod.POST)
|
73
|
public @ResponseBody String feedResult(@RequestBody final ResultEntry pub,
|
74
|
@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit)
|
75
|
throws DirecIndexApiException {
|
76
|
return feed(pub, commit);
|
77
|
}
|
78
|
|
79
|
@Deprecated
|
80
|
@RequestMapping(value = { "/api/publications/feedObject" }, method = RequestMethod.POST)
|
81
|
public @ResponseBody String feedObject(@RequestBody final ResultEntry pub,
|
82
|
@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit)
|
83
|
throws DirecIndexApiException {
|
84
|
return feed(pub, commit);
|
85
|
}
|
86
|
|
87
|
|
88
|
@RequestMapping(value = "/api/result/{openaireId}", method = RequestMethod.DELETE)
|
89
|
public @ResponseBody boolean deleteResultWithOpenaireId(
|
90
|
@PathVariable(value = "openaireId") final String openaireId,
|
91
|
@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws DirecIndexApiException {
|
92
|
|
93
|
return deleteResult(openaireId, commit);
|
94
|
}
|
95
|
|
96
|
@RequestMapping(value = "/api/results", method = RequestMethod.DELETE)
|
97
|
public @ResponseBody boolean deleteResultWithOriginalId(
|
98
|
@RequestParam(value = "originalId", required = true) final String originalId,
|
99
|
@RequestParam(value = "collectedFromId", required = true) final String collectedFromId,
|
100
|
@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws Exception {
|
101
|
|
102
|
final String openaireId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
|
103
|
return deleteResult(openaireId, commit);
|
104
|
}
|
105
|
|
106
|
@RequestMapping(value="/api/evictCache", method=RequestMethod.GET)
|
107
|
public void evictCache(){
|
108
|
clientMap.evictCache();
|
109
|
}
|
110
|
|
111
|
@Deprecated
|
112
|
@RequestMapping(value = { "/api/publications/deleteObject", "/api/results/delete" }, method = RequestMethod.POST)
|
113
|
public @ResponseBody boolean deleteResultPost(
|
114
|
@RequestParam(value = "originalId", required = true) final String originalId,
|
115
|
@RequestParam(value = "collectedFromId", required = true) final String collectedFromId,
|
116
|
@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws Exception {
|
117
|
|
118
|
final String openaireId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
|
119
|
return deleteResult(openaireId, commit);
|
120
|
}
|
121
|
|
122
|
@Deprecated
|
123
|
private String feed(final ResultEntry pub, final boolean commit) throws DirecIndexApiException {
|
124
|
return feed(pub);
|
125
|
}
|
126
|
|
127
|
private String feed(final ResultEntry pub) throws DirecIndexApiException {
|
128
|
try {
|
129
|
final String oafRecord = pub.asOafRecord(velocityEngine, serviceLocator.getService(ISLookUpService.class), oafSchemaLocation);
|
130
|
|
131
|
for(Map.Entry<IndexDsInfo, CloudIndexClient> e : clientMap.getClients().entrySet()) {
|
132
|
final IndexDsInfo idx = e.getKey();
|
133
|
final CloudIndexClient client = e.getValue();
|
134
|
client.feed(oafRecord, idx.getIndexDsId(), oafToIndexRecordFactory.newTransformer(idx.getFormat()), false);
|
135
|
}
|
136
|
|
137
|
recentResultsQueue.add(oafRecord);
|
138
|
|
139
|
return pub.getOpenaireId();
|
140
|
} catch (final Throwable e) {
|
141
|
log.error("Error saving record", e);
|
142
|
log.debug(pub.toString());
|
143
|
throw new DirecIndexApiException("Error adding publication: " + e.getMessage(), e);
|
144
|
}
|
145
|
}
|
146
|
|
147
|
private boolean deleteResult(final String openaireId, final boolean commit) throws DirecIndexApiException {
|
148
|
try {
|
149
|
for(Map.Entry<IndexDsInfo, CloudIndexClient> e : clientMap.getClients().entrySet()) {
|
150
|
final IndexDsInfo idx = e.getKey();
|
151
|
final CloudIndexClient client = e.getValue();
|
152
|
client.remove(openaireId, false);
|
153
|
log.info("Deleted result with id: " + openaireId + " from: " + idx.getIndexBaseUrl());
|
154
|
}
|
155
|
|
156
|
recentResultsQueue.remove(openaireId);
|
157
|
return true;
|
158
|
} catch (Throwable e) {
|
159
|
throw new DirecIndexApiException("Error deleting publication: " + e.getMessage(), e);
|
160
|
}
|
161
|
}
|
162
|
|
163
|
@ExceptionHandler(Exception.class)
|
164
|
@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
|
165
|
public @ResponseBody ErrorMessage handleException(final Exception e) {
|
166
|
log.error("Error in direct index API", e);
|
167
|
return new ErrorMessage(e);
|
168
|
}
|
169
|
|
170
|
public class ErrorMessage {
|
171
|
|
172
|
private final String message;
|
173
|
private final String stacktrace;
|
174
|
|
175
|
public ErrorMessage(final Exception e) {
|
176
|
this(e.getMessage(), ExceptionUtils.getStackTrace(e));
|
177
|
}
|
178
|
|
179
|
public ErrorMessage(final String message, final String stacktrace) {
|
180
|
this.message = message;
|
181
|
this.stacktrace = stacktrace;
|
182
|
}
|
183
|
|
184
|
public String getMessage() {
|
185
|
return this.message;
|
186
|
}
|
187
|
|
188
|
public String getStacktrace() {
|
189
|
return this.stacktrace;
|
190
|
}
|
191
|
|
192
|
}
|
193
|
|
194
|
}
|