1
|
package eu.dnetlib.goldoa.service;
|
2
|
|
3
|
import com.fasterxml.jackson.databind.JsonNode;
|
4
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5
|
import com.fasterxml.jackson.databind.SerializationFeature;
|
6
|
import eu.dnetlib.goldoa.domain.*;
|
7
|
import eu.dnetlib.goldoa.service.dao.PublicationDAO;
|
8
|
import org.apache.commons.codec.digest.DigestUtils;
|
9
|
import org.apache.log4j.Logger;
|
10
|
import org.springframework.beans.factory.annotation.Autowired;
|
11
|
import org.springframework.stereotype.Service;
|
12
|
import org.springframework.transaction.annotation.Transactional;
|
13
|
|
14
|
import java.io.InputStream;
|
15
|
import java.math.BigInteger;
|
16
|
import java.net.URL;
|
17
|
import java.util.Calendar;
|
18
|
import java.util.Date;
|
19
|
import java.util.Iterator;
|
20
|
import java.util.TimeZone;
|
21
|
|
22
|
//import eu.dnetlib.goldoa.domain.BankAccount;
|
23
|
|
24
|
|
25
|
/**
|
26
|
* Created by antleb on 3/13/15.
|
27
|
*/
|
28
|
@Transactional
|
29
|
@Service("publicationManager")
|
30
|
public class PublicationManagerImpl implements PublicationManager {
|
31
|
|
32
|
private static Logger logger = Logger.getLogger(PublicationManagerImpl.class);
|
33
|
|
34
|
@Autowired
|
35
|
private PublicationDAO publicationDAO;
|
36
|
@Autowired
|
37
|
private JournalManager journalManager;
|
38
|
@Autowired
|
39
|
private PublisherManager publisherManager;
|
40
|
@Autowired
|
41
|
private UserManager personManager;
|
42
|
@Autowired
|
43
|
private OrganizationManager organizationManager;
|
44
|
|
45
|
@Override
|
46
|
public Publication getPublication(String publicationId) {
|
47
|
return publicationDAO.getPublication(publicationId);
|
48
|
}
|
49
|
|
50
|
@Override
|
51
|
public Publication savePublication(Publication publication) {
|
52
|
|
53
|
if (publication.getId() == null) {
|
54
|
publication.setSource("portal");
|
55
|
publication.setId("portal::" + DigestUtils.md5Hex(publication.getTitle()));
|
56
|
}
|
57
|
|
58
|
if(publication.getIdentifiers() != null){
|
59
|
for(Identifier id : publication.getIdentifiers())
|
60
|
if(id.getId() == null)
|
61
|
id.setId(this.generateId());
|
62
|
}
|
63
|
|
64
|
if(publication.getJournal()!=null)
|
65
|
journalManager.saveJournal(publication.getJournal());
|
66
|
|
67
|
return publicationDAO.savePublication(publication);
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
public Publication resolveDOI(String doi) throws ManagerException {
|
72
|
Publication publication = null;
|
73
|
try {
|
74
|
publication = resolveCrossrefDoi(doi);
|
75
|
} catch (ManagerException e) {
|
76
|
e.printStackTrace();
|
77
|
return null;
|
78
|
}
|
79
|
|
80
|
/*if(publication == null) {
|
81
|
try {
|
82
|
return resolveDataCiteDoi(doi);
|
83
|
} catch (ManagerException e) {
|
84
|
e.printStackTrace();
|
85
|
}
|
86
|
}
|
87
|
|
88
|
if(publication == null)
|
89
|
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);*/
|
90
|
|
91
|
return publication;
|
92
|
}
|
93
|
|
94
|
private Publication resolveDataCiteDoi(String doi) throws ManagerException {
|
95
|
Publication publication = null;
|
96
|
InputStream is = null;
|
97
|
try {
|
98
|
|
99
|
System.out.println("https://api.datacite.org/works/" + doi);
|
100
|
is = new URL("https://api.datacite.org/works/" + doi).openStream();
|
101
|
ObjectMapper mapper = new ObjectMapper();
|
102
|
mapper.enable(SerializationFeature.INDENT_OUTPUT);
|
103
|
JsonNode root = mapper.readTree(is);
|
104
|
|
105
|
System.out.println(mapper.writeValueAsString(root));
|
106
|
System.out.println(mapper.writeValueAsString(root.get("data").get("attributes").get("title")));
|
107
|
if(root.get("errors") == null){
|
108
|
publication = new Publication();
|
109
|
publication.setTitle(root.get("data").get("attributes").get("title").textValue());
|
110
|
publication.setDoi(doi);
|
111
|
//publication.setSubjects(root.get("data").get("attributes").);
|
112
|
publication.setSource("datacite");
|
113
|
publication.setId("datacite::" + DigestUtils.md5Hex(doi));
|
114
|
}
|
115
|
|
116
|
} catch (Exception e) {
|
117
|
logger.error("Error resolving datacite doi", e);
|
118
|
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
|
119
|
} finally {
|
120
|
try {
|
121
|
if (is != null)
|
122
|
is.close();
|
123
|
} catch (Exception e) {
|
124
|
}
|
125
|
}
|
126
|
return publication;
|
127
|
}
|
128
|
|
129
|
private Publication resolveCrossrefDoi(String doi) throws ManagerException {
|
130
|
Publication publication = null;
|
131
|
InputStream is = null;
|
132
|
try {
|
133
|
is = new URL("http://api.crossref.org/works/" + doi).openStream();
|
134
|
ObjectMapper mapper = new ObjectMapper();
|
135
|
JsonNode root = mapper.readTree(is);
|
136
|
|
137
|
if (root.get("status").textValue().equals("ok")) {
|
138
|
publication = new Publication();
|
139
|
publication.setTitle(root.path("message").path("title").path(0).textValue());
|
140
|
publication.setDoi(doi);
|
141
|
publication.setSubjects(root.path("message").path("subject").textValue());
|
142
|
publication.setSource("crossref");
|
143
|
publication.setId("crossref::" + DigestUtils.md5Hex(doi));
|
144
|
|
145
|
if ("journal-article".equals(root.path("message").path("type").textValue()))
|
146
|
publication.setType(PublicationType.ARTICLE);
|
147
|
else if("book-chapter".equals(root.path("message").path("type").textValue()))
|
148
|
publication.setType(PublicationType.BOOK_CHAPTER);
|
149
|
else
|
150
|
publication.setType(PublicationType.MONOGRAPH);
|
151
|
|
152
|
publication.setDate(this.parsePublicationDate(root.path("message").path("issued")));
|
153
|
|
154
|
if(publication.getType() == PublicationType.ARTICLE) {
|
155
|
publication.setJournal(this.parseJournal(root.path("message").path("container-title"), root.path("message").path("ISSN")));
|
156
|
publication.getJournal().setPublisher(this.parsePublisher(root.path("message").path("publisher")));
|
157
|
}else
|
158
|
publication.setPublisher(this.parsePublisher(root.path("message").path("publisher")));
|
159
|
|
160
|
for (JsonNode author : root.path("message").path("author"))
|
161
|
publication.getAuthors().add(this.parseAffiliation(author,publication));
|
162
|
|
163
|
publication.setLicense(root.path("message").path("license").path(0).path("URL").textValue());
|
164
|
publication.getIdentifiers().add(new Identifier("doi", doi));
|
165
|
} else {
|
166
|
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
|
167
|
}
|
168
|
} catch (ManagerException e) {
|
169
|
throw e;
|
170
|
} catch (Exception e) {
|
171
|
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
|
172
|
} finally {
|
173
|
try {
|
174
|
if (is != null)
|
175
|
is.close();
|
176
|
} catch (Exception e) {
|
177
|
}
|
178
|
}
|
179
|
|
180
|
return publication;
|
181
|
}
|
182
|
|
183
|
private Author parseAffiliation(JsonNode author,Publication publication) {
|
184
|
Author a = new Author();
|
185
|
|
186
|
a.setFirstname(author.path("given").textValue());
|
187
|
a.setLastname(author.path("family").textValue());
|
188
|
a.setId(DigestUtils.md5Hex(a.getFirstname() + a.getEmail()) + publication.getId());
|
189
|
if(a.getFirstname() != null)
|
190
|
return a;
|
191
|
return null;
|
192
|
}
|
193
|
|
194
|
private Publisher parsePublisher(JsonNode publisherName) {
|
195
|
|
196
|
Publisher publisher = publisherManager.getPublisherByName(publisherName.textValue());
|
197
|
if(publisher == null){
|
198
|
publisher = new Publisher();
|
199
|
|
200
|
publisher.setName(publisherName.textValue());
|
201
|
|
202
|
String value = publisherManager.getBankAddress().toString();
|
203
|
BigInteger id = publisherManager.getBankId();
|
204
|
publisher.setBankAccount(new BankAccount(id,null,value,
|
205
|
null,null,null));
|
206
|
|
207
|
publisher.setId("crossref::" + DigestUtils.md5Hex(publisher.getName()));
|
208
|
publisher.setSource("crossref");
|
209
|
}
|
210
|
return publisher;
|
211
|
}
|
212
|
|
213
|
private Journal parseJournal(JsonNode name, JsonNode issn) {
|
214
|
|
215
|
Journal journal = journalManager.getJournalByTitle(name.path(0).textValue());
|
216
|
if (journal == null) {
|
217
|
journal = new Journal();
|
218
|
|
219
|
journal.setTitle(name.path(0).textValue());
|
220
|
journal.setAlternativeTitle(name.path(1).textValue());
|
221
|
|
222
|
StringBuilder sb = new StringBuilder();
|
223
|
Iterator<JsonNode> ssn = issn.elements();
|
224
|
while (ssn.hasNext()) {
|
225
|
sb.append(ssn.next().textValue()).append(",");
|
226
|
}
|
227
|
|
228
|
if (sb.length() > 0)
|
229
|
sb.deleteCharAt(sb.length() - 1);
|
230
|
|
231
|
journal.setIssn(sb.toString());
|
232
|
|
233
|
journal.setApccurrency(Currency.EUR);
|
234
|
journal.setSource("crossref");
|
235
|
journal.setId("crossref::" + DigestUtils.md5Hex(journal.getTitle()));
|
236
|
}
|
237
|
|
238
|
return journal;
|
239
|
}
|
240
|
|
241
|
private Date parsePublicationDate(JsonNode node) {
|
242
|
Calendar c = Calendar.getInstance();
|
243
|
|
244
|
c.clear();
|
245
|
c.setTimeZone(TimeZone.getTimeZone("UTC"));
|
246
|
|
247
|
c.set(Calendar.YEAR, node.path("date-parts").path(0).path(0).intValue());
|
248
|
c.set(Calendar.MONTH, node.path("date-parts").path(0).path(1).intValue() - 1);
|
249
|
c.set(Calendar.DAY_OF_MONTH, 1 + node.path("date-parts").path(0).path(2).intValue());
|
250
|
c.set(Calendar.HOUR_OF_DAY, 0);
|
251
|
|
252
|
|
253
|
return c.getTime();
|
254
|
}
|
255
|
|
256
|
private BigInteger generateId(){
|
257
|
return publicationDAO.generateIdentifierID();
|
258
|
}
|
259
|
|
260
|
}
|