1
|
package eu.dnetlib.openaire.api;
|
2
|
|
3
|
import javax.annotation.Resource;
|
4
|
|
5
|
import com.google.common.base.Function;
|
6
|
import com.google.gson.Gson;
|
7
|
import eu.dnetlib.data.index.CloudIndexClient;
|
8
|
import eu.dnetlib.data.index.CloudIndexClientFactory;
|
9
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
10
|
import eu.dnetlib.msro.workflows.nodes.index.OafToIndexRecordFactory;
|
11
|
import eu.dnetlib.openaire.api.objects.PublicationEntry;
|
12
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
13
|
import eu.dnetlib.rmi.manager.MSROException;
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.apache.commons.lang3.StringUtils;
|
16
|
import org.apache.velocity.app.VelocityEngine;
|
17
|
import org.springframework.beans.factory.annotation.Value;
|
18
|
import org.springframework.core.io.ClassPathResource;
|
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
|
public class SinglePublicationSubmitter {
|
27
|
|
28
|
@Value(value = "oaf.schema.location")
|
29
|
private String oafSchemaLocation;
|
30
|
|
31
|
@Resource
|
32
|
private UniqueServiceLocator serviceLocator;
|
33
|
|
34
|
@Resource
|
35
|
private OafToIndexRecordFactory oafToIndexRecordFactory;
|
36
|
|
37
|
@Resource
|
38
|
private RecentPublicationsQueue recentPublicationsQueue;
|
39
|
|
40
|
@Resource(name = "openaireplusApisVelocityEngine")
|
41
|
private VelocityEngine velocityEngine;
|
42
|
|
43
|
@Value(value = "${openaireplus.msro.api.findSolrIndexUrl.xquery}")
|
44
|
private ClassPathResource findSolrIndexUrl;
|
45
|
|
46
|
@Value(value = "${openaireplus.msro.api.findIndexDsInfo.xquery}")
|
47
|
private ClassPathResource findIndexDsInfo;
|
48
|
|
49
|
@RequestMapping(value = "/api/publications/feedJson", method = RequestMethod.POST)
|
50
|
public
|
51
|
@ResponseBody
|
52
|
boolean addPublication(@RequestParam(value = "json", required = true) final String json) throws MSROException {
|
53
|
final PublicationEntry pub = new Gson().fromJson(json, PublicationEntry.class);
|
54
|
return addPublication(pub);
|
55
|
}
|
56
|
|
57
|
@RequestMapping(value = "/api/publications/feedObject", method = RequestMethod.POST)
|
58
|
public
|
59
|
@ResponseBody
|
60
|
boolean addPublication(@RequestBody final PublicationEntry pub) throws MSROException {
|
61
|
|
62
|
if (StringUtils.isBlank(pub.getOriginalId())) {
|
63
|
throw new MSROException("A required field is missing: originalId");
|
64
|
}
|
65
|
if (StringUtils.isBlank(pub.getTitle())) {
|
66
|
throw new MSROException("A required field is missing: title");
|
67
|
}
|
68
|
if (StringUtils.isBlank(pub.getUrl())) {
|
69
|
throw new MSROException("A required field is missing: url");
|
70
|
}
|
71
|
if (StringUtils.isBlank(pub.getLicenseCode())) {
|
72
|
throw new MSROException("A required field is missing: licenceCode");
|
73
|
}
|
74
|
if (StringUtils.isBlank(pub.getResourceType())) {
|
75
|
throw new MSROException("A required field is missing: resourceType");
|
76
|
}
|
77
|
if (StringUtils.isBlank(pub.getCollectedFromId())) {
|
78
|
throw new MSROException("A required field is missing: collectedFromId");
|
79
|
}
|
80
|
|
81
|
/*if (StringUtils.isBlank(pub.getHostedById())) {
|
82
|
throw new MSROException("A required field is missing: hostedById");
|
83
|
}*/
|
84
|
|
85
|
CloudIndexClient idxClient = null;
|
86
|
|
87
|
try {
|
88
|
final String baseUrl = calculateIndexBaseUrl();
|
89
|
final String[] arr = calculateCurrentIndexDsInfo().split("@@@");
|
90
|
final String indexDsId = arr[0].trim();
|
91
|
final String format = arr[1].trim();
|
92
|
final String coll = arr[2].trim();
|
93
|
|
94
|
idxClient = CloudIndexClientFactory.newIndexClient(baseUrl, coll, false);
|
95
|
|
96
|
final String oafRecord = pub.asOafRecord(velocityEngine, serviceLocator.getService(ISLookUpService.class), oafSchemaLocation);
|
97
|
|
98
|
recentPublicationsQueue.add(oafRecord);
|
99
|
|
100
|
//TODO check runtime works
|
101
|
final Function<String, String> xslt = (Function<String, String>) oafToIndexRecordFactory.newTransformer(format);
|
102
|
return (idxClient.feed(oafRecord, indexDsId, xslt) == 0);
|
103
|
} catch (Throwable e) {
|
104
|
throw new MSROException("Error adding publication: " + e.getMessage(), e);
|
105
|
} finally {
|
106
|
if (idxClient != null) {
|
107
|
idxClient.close();
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
private String calculateCurrentIndexDsInfo() throws Exception {
|
113
|
final String query = IOUtils.toString(findIndexDsInfo.getInputStream());
|
114
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
|
115
|
}
|
116
|
|
117
|
private String calculateIndexBaseUrl() throws Exception {
|
118
|
final String query = IOUtils.toString(findSolrIndexUrl.getInputStream());
|
119
|
return serviceLocator.getService(ISLookUpService.class).getResourceProfileByQuery(query);
|
120
|
}
|
121
|
|
122
|
}
|