Project

General

Profile

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.Autowired;
18
import org.springframework.beans.factory.annotation.Value;
19
import org.springframework.http.HttpStatus;
20
import org.springframework.stereotype.Controller;
21
import org.springframework.web.bind.annotation.*;
22

    
23
/**
24
 * Created by michele on 11/11/15.
25
 */
26
@Controller
27
@DNetRestDocumentation
28
public class OpenaireResultSubmitter {
29

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

    
32
	@Value(value = "${openaire.api.community}")
33
	private String community_api;
34

    
35
	@Value(value = "oaf.schema.location")
36
	private String oafSchemaLocation;
37

    
38
	@Resource
39
	private UniqueServiceLocator serviceLocator;
40

    
41
	@Resource
42
	private OafToIndexRecordFactory oafToIndexRecordFactory;
43

    
44
	@Resource
45
	private RecentResultsQueue recentResultsQueue;
46

    
47
	@Resource(name = "openaireplusApisVelocityEngine")
48
	private VelocityEngine velocityEngine;
49

    
50
	@Resource(name = "resultSubmitterClientMap")
51
	private IndexClientMap clientMap;
52

    
53
	@Resource(name = "resultSubmitterService")
54
	private ResultSubmitterService submitterService;
55

    
56
	@Autowired
57
	private IndexDSRetriever indexDSRetriever;
58

    
59
	@RequestMapping(value = { "/api/admin/autocommit/active" }, method = RequestMethod.GET)
60
	public @ResponseBody Boolean getAutocommit() throws DirecIndexApiException {
61
		return submitterService.isAutocommitactive();
62
	}
63

    
64
	@RequestMapping(value = { "/api/admin/autocommit/active" }, method = RequestMethod.POST)
65
	public @ResponseBody Boolean setAutocommit(@RequestParam(value = "active", required = true) final Boolean active) throws DirecIndexApiException {
66
		submitterService.setAutocommitactive(active);
67
		log.info(String.format("automatic commit, active '%s', frequency '%s'", submitterService.isAutocommitactive(), submitterService.getCommitfrquency()));
68
		return submitterService.isAutocommitactive();
69
	}
70

    
71
	@RequestMapping(value="/api/admin/evictCache", method=RequestMethod.GET)
72
	@ResponseStatus(value = HttpStatus.OK)
73
	public void evictCache(){
74
		indexDSRetriever.evictCache();
75
	}
76

    
77
	@Deprecated
78
	@RequestMapping(value = { "/api/publications/feedJson", "/api/results/feedJson" }, method = RequestMethod.POST)
79
	public @ResponseBody String feedObjectJson(@RequestParam(value = "json", required = true) final String json,
80
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws DirecIndexApiException {
81
		final ResultEntry pub = new Gson().fromJson(json, ResultEntry.class);
82
		return feedObject(pub, commit);
83
	}
84

    
85

    
86
	@RequestMapping(value = { "/api/results/feedObject" }, method = RequestMethod.POST)
87
	public @ResponseBody String feedResult(@RequestBody final ResultEntry pub,
88
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit)
89
			throws DirecIndexApiException {
90
		return feed(pub,commit);
91

    
92
	}
93

    
94
	@Deprecated
95
	@RequestMapping(value = { "/api/publications/feedObject" }, method = RequestMethod.POST)
96
	public @ResponseBody String feedObject(@RequestBody final ResultEntry pub,
97
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit)
98
			throws DirecIndexApiException {
99
		return feed(pub, commit);
100
	}
101

    
102

    
103
	@RequestMapping(value = "/api/result/{openaireId}", method = RequestMethod.DELETE)
104
	public @ResponseBody boolean deleteResultWithOpenaireId(
105
			@PathVariable(value = "openaireId") final String openaireId,
106
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws DirecIndexApiException {
107

    
108
		return deleteResult(openaireId, commit);
109
	}
110

    
111
	@RequestMapping(value = "/api/results", method = RequestMethod.DELETE)
112
	public @ResponseBody boolean deleteResultWithOriginalId(
113
			@RequestParam(value = "originalId", required = true) final String originalId,
114
			@RequestParam(value = "collectedFromId", required = true) final String collectedFromId,
115
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws Exception {
116

    
117
		final String openaireId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
118
		return deleteResult(openaireId, commit);
119
	}
120

    
121
	@Deprecated
122
	@RequestMapping(value = { "/api/publications/deleteObject", "/api/results/delete" }, method = RequestMethod.POST)
123
	public @ResponseBody boolean deleteResultPost(
124
			@RequestParam(value = "originalId", required = true) final String originalId,
125
			@RequestParam(value = "collectedFromId", required = true) final String collectedFromId,
126
			@RequestParam(value = "commit", required = false, defaultValue = "true") final boolean commit) throws Exception {
127

    
128
		final String openaireId = ResultEntry.calculateOpenaireId(originalId, collectedFromId, serviceLocator.getService(ISLookUpService.class));
129
		return deleteResult(openaireId, commit);
130
	}
131

    
132
	@Deprecated
133
	private String feed(final ResultEntry pub, final boolean commit) throws DirecIndexApiException {
134
		return feed(pub);
135
	}
136

    
137
	private String feed(final ResultEntry pub) throws DirecIndexApiException {
138
		try {
139

    
140
			final String oafRecord = pub.asOafRecord(velocityEngine, serviceLocator.getService(ISLookUpService.class), oafSchemaLocation, community_api);
141

    
142
			for(Map.Entry<IndexDsInfo, CloudIndexClient> e : clientMap.getClients().entrySet()) {
143
				final IndexDsInfo idx = e.getKey();
144
				final CloudIndexClient client = e.getValue();
145
				client.feed(oafRecord, idx.getIndexDsId(), oafToIndexRecordFactory.newTransformer(idx.getFormat()), false);
146
			}
147

    
148
			recentResultsQueue.add(oafRecord);
149

    
150
			return pub.getOpenaireId();
151
		} catch (final Throwable e) {
152
			log.error("Error saving record", e);
153
			log.debug(pub.toString());
154
			throw new DirecIndexApiException("Error adding publication: " + e.getMessage(), e);
155
		}
156
	}
157

    
158
	private boolean deleteResult(final String openaireId, final boolean commit) throws DirecIndexApiException {
159
		try {
160
			for(Map.Entry<IndexDsInfo, CloudIndexClient> e : clientMap.getClients().entrySet()) {
161
				final IndexDsInfo idx = e.getKey();
162
				final CloudIndexClient client = e.getValue();
163
				client.remove(openaireId, false);
164
				log.info("Deleted result with id: " + openaireId + " from: " + idx.getIndexBaseUrl());
165
			}
166

    
167
			recentResultsQueue.remove(openaireId);
168
			return true;
169
		} catch (Throwable e) {
170
			throw new DirecIndexApiException("Error deleting publication: " + e.getMessage(), e);
171
		}
172
	}
173

    
174
	@ExceptionHandler(Exception.class)
175
	@ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR)
176
	public @ResponseBody ErrorMessage handleException(final Exception e) {
177
		log.error("Error in direct index API", e);
178
		return new ErrorMessage(e);
179
	}
180

    
181
	public class ErrorMessage {
182

    
183
		private final String message;
184
		private final String stacktrace;
185

    
186
		public ErrorMessage(final Exception e) {
187
			this(e.getMessage(), ExceptionUtils.getStackTrace(e));
188
		}
189

    
190
		public ErrorMessage(final String message, final String stacktrace) {
191
			this.message = message;
192
			this.stacktrace = stacktrace;
193
		}
194

    
195
		public String getMessage() {
196
			return this.message;
197
		}
198

    
199
		public String getStacktrace() {
200
			return this.stacktrace;
201
		}
202

    
203
	}
204

    
205
}
(6-6/8)