1
|
package eu.dnetlib.openaire.api.objects;
|
2
|
|
3
|
import java.text.SimpleDateFormat;
|
4
|
import java.util.*;
|
5
|
import java.util.concurrent.TimeUnit;
|
6
|
|
7
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
8
|
import eu.dnetlib.miscutils.functional.hash.Hashing;
|
9
|
import eu.dnetlib.openaire.api.SinglePublicationSubmitterUtils;
|
10
|
import eu.dnetlib.rmi.enabling.ISLookUpException;
|
11
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
12
|
import org.apache.commons.lang3.StringUtils;
|
13
|
import org.apache.commons.logging.Log;
|
14
|
import org.apache.commons.logging.LogFactory;
|
15
|
import org.apache.velocity.app.VelocityEngine;
|
16
|
import org.apache.velocity.tools.generic.EscapeTool;
|
17
|
import org.springframework.ui.velocity.VelocityEngineUtils;
|
18
|
|
19
|
/**
|
20
|
* Created by michele on 02/12/15.
|
21
|
*/
|
22
|
public class PublicationEntry {
|
23
|
|
24
|
private static final Map<String, Map<String, String>> cached_vocabularies = new HashMap<String, Map<String, String>>();
|
25
|
private static final Map<String, DatasourceEntry> cached_datasources = new HashMap<String, DatasourceEntry>();
|
26
|
private static final Map<String, String> cached_contexts = new HashMap<String, String>();
|
27
|
private static final Log log = LogFactory.getLog(PublicationEntry.class);
|
28
|
private static long last_cache_update = 0;
|
29
|
private String originalId;
|
30
|
private String title;
|
31
|
private List<String> authors = new ArrayList<String>();
|
32
|
private String publisher;
|
33
|
private String description;
|
34
|
private String language;
|
35
|
private List<PidEntry> pids = new ArrayList<PidEntry>();
|
36
|
private String licenseCode;
|
37
|
private String embargoEndDate;
|
38
|
private String resourceType;
|
39
|
private String url;
|
40
|
private String collectedFromId;
|
41
|
private String hostedById;
|
42
|
// String according to openaire guidelines:
|
43
|
// info:eu-repo/grantAgreement/Funder/FundingProgram/ProjectID/[Jurisdiction]/[ProjectName]/[ProjectAcronym]
|
44
|
private List<String> contexts = new ArrayList<String>();
|
45
|
// String according to the EGI context profile, example: egi::classification::natsc::math
|
46
|
private List<String> linksToProjects = new ArrayList<String>();
|
47
|
|
48
|
public PublicationEntry() {
|
49
|
}
|
50
|
|
51
|
private synchronized static DatasourceEntry getDatasourceInfo(final String dsId, final ISLookUpService lookupService) throws ISLookUpException {
|
52
|
if (StringUtils
|
53
|
.isBlank(dsId)) { return new DatasourceEntry("openaire____::1256f046-bf1f-4afc-8b47-d0b147148b18", "Unknown Repository", "unknown_____"); }
|
54
|
|
55
|
if (!cached_datasources.containsKey(dsId)) {
|
56
|
final String query =
|
57
|
"collection('/db/DRIVER/RepositoryServiceResources/RepositoryServiceResourceType')//CONFIGURATION[./DATASOURCE_ORIGINAL_ID='" + dsId
|
58
|
+ "']/concat(./OFFICIAL_NAME, ' @@@ ', .//FIELD/value[../key='NamespacePrefix'])";
|
59
|
final String s = lookupService.getResourceProfileByQuery(query);
|
60
|
final String[] arr = s.split("@@@");
|
61
|
|
62
|
final DatasourceEntry ds = new DatasourceEntry(dsId, arr[0].trim(), arr[1].trim());
|
63
|
|
64
|
if (StringUtils.isBlank(ds.getName()) || StringUtils.isBlank(ds.getPrefix())) {
|
65
|
log.error("Invalid datasource id: " + dsId);
|
66
|
throw new ISLookUpException("Invalid datasource id: " + dsId);
|
67
|
} else {
|
68
|
cached_datasources.put(dsId, ds);
|
69
|
}
|
70
|
}
|
71
|
|
72
|
return cached_datasources.get(dsId);
|
73
|
|
74
|
}
|
75
|
|
76
|
private synchronized static Map<String, String> getVocabulary(final String voc, final ISLookUpService lookupService) throws ISLookUpException {
|
77
|
|
78
|
if (((DateUtils.now() - last_cache_update) < TimeUnit.MINUTES.toMillis(15)) && cached_vocabularies.containsKey(voc)) {
|
79
|
return cached_vocabularies.get(voc);
|
80
|
} else {
|
81
|
final String query = "collection('/db/DRIVER/VocabularyDSResources/VocabularyDSResourceType')[.//VOCABULARY_NAME/@code='" + voc
|
82
|
+ "']//TERM/concat(@code, ' @@@ ', @english_name)";
|
83
|
|
84
|
final Map<String, String> map = new HashMap<String, String>();
|
85
|
for (final String s : lookupService.quickSearchProfile(query)) {
|
86
|
final String[] arr = s.split("@@@");
|
87
|
map.put(arr[0].trim(), arr[1].trim());
|
88
|
}
|
89
|
|
90
|
cached_vocabularies.put(voc, map);
|
91
|
|
92
|
last_cache_update = DateUtils.now();
|
93
|
|
94
|
return map;
|
95
|
}
|
96
|
}
|
97
|
|
98
|
private synchronized static Map<String, String> getContexts(final ISLookUpService lookupService) throws ISLookUpException {
|
99
|
if (((DateUtils.now() - last_cache_update) > TimeUnit.MINUTES.toMillis(15)) || cached_contexts.isEmpty()) {
|
100
|
final String query =
|
101
|
"collection('/db/DRIVER/ContextDSResources/ContextDSResourceType')[.//context/@type='community']//*[name()='context' or name()='category' or name()='concept']/concat(@id, ' @@@ ', @label)";
|
102
|
|
103
|
cached_contexts.clear();
|
104
|
for (final String s : lookupService.quickSearchProfile(query)) {
|
105
|
final String[] arr = s.split("@@@");
|
106
|
cached_contexts.put(arr[0].trim(), arr[1].trim());
|
107
|
}
|
108
|
last_cache_update = DateUtils.now();
|
109
|
}
|
110
|
return cached_contexts;
|
111
|
}
|
112
|
|
113
|
public String getEmbargoEndDate() {
|
114
|
return embargoEndDate;
|
115
|
}
|
116
|
|
117
|
public void setEmbargoEndDate(final String embargoEndDate) {
|
118
|
this.embargoEndDate = embargoEndDate;
|
119
|
}
|
120
|
|
121
|
public String getOriginalId() {
|
122
|
return originalId;
|
123
|
}
|
124
|
|
125
|
public void setOriginalId(final String originalId) {
|
126
|
this.originalId = originalId;
|
127
|
}
|
128
|
|
129
|
public String getTitle() {
|
130
|
return title;
|
131
|
}
|
132
|
|
133
|
public void setTitle(final String title) {
|
134
|
this.title = title;
|
135
|
}
|
136
|
|
137
|
public List<String> getAuthors() {
|
138
|
return authors;
|
139
|
}
|
140
|
|
141
|
public void setAuthors(final List<String> authors) {
|
142
|
this.authors = authors;
|
143
|
}
|
144
|
|
145
|
public String getPublisher() {
|
146
|
return publisher;
|
147
|
}
|
148
|
|
149
|
public void setPublisher(final String publisher) {
|
150
|
this.publisher = publisher;
|
151
|
}
|
152
|
|
153
|
public String getDescription() {
|
154
|
return description;
|
155
|
}
|
156
|
|
157
|
public void setDescription(final String description) {
|
158
|
this.description = description;
|
159
|
}
|
160
|
|
161
|
public String getLanguage() {
|
162
|
return language;
|
163
|
}
|
164
|
|
165
|
public void setLanguage(final String language) {
|
166
|
this.language = language;
|
167
|
}
|
168
|
|
169
|
public List<PidEntry> getPids() {
|
170
|
return pids;
|
171
|
}
|
172
|
|
173
|
public void setPids(final List<PidEntry> pids) {
|
174
|
this.pids = pids;
|
175
|
}
|
176
|
|
177
|
public String getLicenseCode() {
|
178
|
return licenseCode;
|
179
|
}
|
180
|
|
181
|
public void setLicenseCode(final String licenseCode) {
|
182
|
this.licenseCode = licenseCode;
|
183
|
}
|
184
|
|
185
|
public String getResourceType() {
|
186
|
return resourceType;
|
187
|
}
|
188
|
|
189
|
public void setResourceType(final String resourceType) {
|
190
|
this.resourceType = resourceType;
|
191
|
}
|
192
|
|
193
|
public String getUrl() {
|
194
|
return url;
|
195
|
}
|
196
|
|
197
|
public void setUrl(final String url) {
|
198
|
this.url = url;
|
199
|
}
|
200
|
|
201
|
public String getCollectedFromId() {
|
202
|
return collectedFromId;
|
203
|
}
|
204
|
|
205
|
public void setCollectedFromId(final String collectedFromId) {
|
206
|
this.collectedFromId = collectedFromId;
|
207
|
}
|
208
|
|
209
|
public String getHostedById() {
|
210
|
return hostedById;
|
211
|
}
|
212
|
|
213
|
public void setHostedById(final String hostedById) {
|
214
|
this.hostedById = hostedById;
|
215
|
}
|
216
|
|
217
|
public List<String> getContexts() {
|
218
|
return contexts;
|
219
|
}
|
220
|
|
221
|
public void setContexts(final List<String> contexts) {
|
222
|
this.contexts = contexts;
|
223
|
}
|
224
|
|
225
|
public List<String> getLinksToProjects() {
|
226
|
return linksToProjects;
|
227
|
}
|
228
|
|
229
|
public void setLinksToProjects(final List<String> linksToProjects) {
|
230
|
this.linksToProjects = linksToProjects;
|
231
|
}
|
232
|
|
233
|
public String asOafRecord(final VelocityEngine ve,
|
234
|
final ISLookUpService lookupService,
|
235
|
final String oafSchemaLocation) throws Exception {
|
236
|
|
237
|
final DatasourceEntry collectedFromEntry = getDatasourceInfo(collectedFromId, lookupService);
|
238
|
final DatasourceEntry hostedByEntry = getDatasourceInfo(hostedById, lookupService);
|
239
|
|
240
|
final String objId = collectedFromEntry.getPrefix() + "::" + Hashing.md5(originalId);
|
241
|
|
242
|
final Map<String, Object> model = new HashMap<String, Object>();
|
243
|
model.put("esc", new EscapeTool());
|
244
|
model.put("util", new SinglePublicationSubmitterUtils(lookupService));
|
245
|
model.put("pub", this);
|
246
|
model.put("objIdentifier", objId);
|
247
|
model.put("oafSchemaLocation", oafSchemaLocation);
|
248
|
model.put("licenses", getVocabulary("dnet:access_modes", lookupService));
|
249
|
model.put("resourceTypes", getVocabulary("dnet:publication_resource", lookupService));
|
250
|
model.put("pidTypes", getVocabulary("dnet:pid_types", lookupService));
|
251
|
model.put("languages", getVocabulary("dnet:languages", lookupService));
|
252
|
model.put("contexts", getContexts(lookupService));
|
253
|
model.put("dateOfCollection", (new SimpleDateFormat("yyyy-MM-dd\'T\'hh:mm:ss\'Z\'")).format(new Date()));
|
254
|
model.put("collectedFrom", collectedFromEntry);
|
255
|
model.put("hostedBy", hostedByEntry);
|
256
|
|
257
|
return VelocityEngineUtils.mergeTemplateIntoString(ve, "/eu/dnetlib/msro/openaireplus/api/indexRecord.xml.vm", "UTF-8", model);
|
258
|
}
|
259
|
}
|