1 |
35440
|
antonis.le
|
package eu.dnetlib.goldoa.service;
|
2 |
|
|
|
3 |
35501
|
antonis.le
|
import com.fasterxml.jackson.databind.JsonNode;
|
4 |
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
5 |
35508
|
antonis.le
|
import eu.dnetlib.goldoa.domain.*;
|
6 |
35669
|
antonis.le
|
import eu.dnetlib.goldoa.service.dao.JournalDAO;
|
7 |
35518
|
antonis.le
|
import eu.dnetlib.goldoa.service.dao.PublicationDAO;
|
8 |
35669
|
antonis.le
|
import eu.dnetlib.goldoa.service.dao.PublisherDAO;
|
9 |
35508
|
antonis.le
|
import org.apache.commons.codec.digest.DigestUtils;
|
10 |
35501
|
antonis.le
|
import org.apache.log4j.Logger;
|
11 |
35518
|
antonis.le
|
import org.springframework.beans.factory.annotation.Autowired;
|
12 |
35542
|
antonis.le
|
import org.springframework.transaction.annotation.Transactional;
|
13 |
35468
|
antonis.le
|
|
14 |
35501
|
antonis.le
|
import java.io.InputStream;
|
15 |
|
|
import java.net.URL;
|
16 |
35639
|
antonis.le
|
import java.util.*;
|
17 |
35501
|
antonis.le
|
|
18 |
35440
|
antonis.le
|
/**
|
19 |
|
|
* Created by antleb on 3/13/15.
|
20 |
|
|
*/
|
21 |
35542
|
antonis.le
|
@Transactional
|
22 |
35468
|
antonis.le
|
public class PublicationManagerImpl implements PublicationManager {
|
23 |
|
|
|
24 |
35501
|
antonis.le
|
private static Logger logger = Logger.getLogger(PublicationManagerImpl.class);
|
25 |
|
|
|
26 |
35518
|
antonis.le
|
@Autowired
|
27 |
|
|
private PublicationDAO publicationDAO;
|
28 |
35669
|
antonis.le
|
@Autowired
|
29 |
|
|
private JournalDAO journalDAO;
|
30 |
|
|
@Autowired
|
31 |
|
|
private PublisherDAO publisherDAO;
|
32 |
35749
|
antonis.le
|
@Autowired
|
33 |
|
|
private PersonManager personManager;
|
34 |
|
|
@Autowired
|
35 |
|
|
private OrganizationManager organizationManager;
|
36 |
35518
|
antonis.le
|
|
37 |
35468
|
antonis.le
|
@Override
|
38 |
|
|
public Publication getPublication(String publicationId) {
|
39 |
35535
|
antonis.le
|
Publication publication = publicationDAO.getPublication(publicationId);
|
40 |
|
|
|
41 |
|
|
loadPublicationRelations(publication);
|
42 |
|
|
|
43 |
35679
|
stefania.m
|
if (publication.getJournal() != null && publication.getJournal().getId() != null)
|
44 |
35669
|
antonis.le
|
publication.setJournal(journalDAO.getJournal(publication.getJournal().getId()));
|
45 |
35679
|
stefania.m
|
else
|
46 |
|
|
publication.setJournal(null);
|
47 |
|
|
|
48 |
|
|
if (publication.getPublisher() != null && publication.getPublisher().getId() != null)
|
49 |
35669
|
antonis.le
|
publication.setPublisher(publisherDAO.getPublisher(publication.getPublisher().getId()));
|
50 |
35679
|
stefania.m
|
else
|
51 |
|
|
publication.setPublisher(null);
|
52 |
35669
|
antonis.le
|
|
53 |
35535
|
antonis.le
|
return publication;
|
54 |
35468
|
antonis.le
|
}
|
55 |
|
|
|
56 |
35535
|
antonis.le
|
private void loadPublicationRelations(Publication publication) {
|
57 |
|
|
publicationDAO.loadAffiliations(publication);
|
58 |
|
|
}
|
59 |
|
|
|
60 |
35468
|
antonis.le
|
@Override
|
61 |
35480
|
antonis.le
|
public Publication savePublication(Publication publication) {
|
62 |
35521
|
antonis.le
|
|
63 |
|
|
if (publication.getId() == null) {
|
64 |
|
|
publication.setSource("portal");
|
65 |
|
|
publication.setId("portal::" + DigestUtils.md5Hex(publication.getTitle()));
|
66 |
|
|
}
|
67 |
|
|
|
68 |
|
|
publicationDAO.savePublication(publication);
|
69 |
|
|
deletePublicationRelations(publication);
|
70 |
35749
|
antonis.le
|
try {
|
71 |
|
|
insertPublicationRelations(publication);
|
72 |
|
|
} catch (PersonManagerException e) {
|
73 |
|
|
e.printStackTrace();
|
74 |
|
|
} catch (OrganizationManagerException e) {
|
75 |
|
|
e.printStackTrace();
|
76 |
|
|
}
|
77 |
35521
|
antonis.le
|
|
78 |
35679
|
stefania.m
|
if (publication.getJournal() != null && publication.getJournal().getId() != null)
|
79 |
35669
|
antonis.le
|
publication.setJournal(journalDAO.getJournal(publication.getJournal().getId()));
|
80 |
35679
|
stefania.m
|
else
|
81 |
|
|
publication.setJournal(null);
|
82 |
|
|
|
83 |
|
|
if (publication.getPublisher() != null && publication.getPublisher().getId() != null)
|
84 |
35669
|
antonis.le
|
publication.setPublisher(publisherDAO.getPublisher(publication.getPublisher().getId()));
|
85 |
35679
|
stefania.m
|
else
|
86 |
|
|
publication.setPublisher(null);
|
87 |
35669
|
antonis.le
|
|
88 |
35521
|
antonis.le
|
return publication;
|
89 |
35468
|
antonis.le
|
}
|
90 |
35501
|
antonis.le
|
|
91 |
35749
|
antonis.le
|
private void insertPublicationRelations(Publication publication) throws PersonManagerException, OrganizationManagerException {
|
92 |
35521
|
antonis.le
|
publicationDAO.insertIdentifiers(publication);
|
93 |
35749
|
antonis.le
|
|
94 |
|
|
if (publication.getAuthors() != null)
|
95 |
|
|
for (Affiliation aff:publication.getAuthors()) {
|
96 |
36089
|
antonis.le
|
if (aff.getOrganization() != null && aff.getOrganization().getId() == null)
|
97 |
|
|
aff.getOrganization().setId(organizationManager.saveOrganization(aff.getOrganization()));
|
98 |
|
|
|
99 |
|
|
if (aff.getPerson() != null) {
|
100 |
|
|
String personId = personManager.generateId(aff.getPerson());
|
101 |
|
|
|
102 |
|
|
try {
|
103 |
|
|
boolean affiliationExists = false;
|
104 |
|
|
Person temp = personManager.getById(personId);
|
105 |
|
|
|
106 |
|
|
for (Affiliation a:temp.getAffiliations()) {
|
107 |
|
|
if (a.getOrganization() != null && aff.getOrganization() != null && a.getOrganization().getId().equals(aff.getOrganization().getId()))
|
108 |
|
|
affiliationExists = true;
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
if (!affiliationExists) {
|
112 |
|
|
temp.getAffiliations().add(aff);
|
113 |
|
|
|
114 |
|
|
personManager.savePerson(temp);
|
115 |
|
|
}
|
116 |
|
|
|
117 |
|
|
aff.setPerson(temp);
|
118 |
|
|
} catch (PersonManagerException e) {
|
119 |
|
|
if (e.getErrorCause() == PersonManagerException.ErrorCause.NOT_EXISTS)
|
120 |
|
|
personManager.savePerson(aff.getPerson());
|
121 |
|
|
}
|
122 |
|
|
}
|
123 |
35749
|
antonis.le
|
}
|
124 |
|
|
|
125 |
35521
|
antonis.le
|
publicationDAO.saveAffiliations(publication);
|
126 |
|
|
}
|
127 |
|
|
|
128 |
|
|
private void deletePublicationRelations(Publication publication) {
|
129 |
|
|
publicationDAO.deleteIdentifiers(publication);
|
130 |
|
|
publicationDAO.deleteAffiliations(publication);
|
131 |
|
|
}
|
132 |
|
|
|
133 |
35501
|
antonis.le
|
@Override
|
134 |
35518
|
antonis.le
|
public Publication resolveDOI(String doi) throws ManagerException {
|
135 |
35501
|
antonis.le
|
Publication publication = new Publication();
|
136 |
|
|
InputStream is = null;
|
137 |
|
|
|
138 |
|
|
try {
|
139 |
|
|
is = new URL("http://api.crossref.org/works/" + doi).openStream();
|
140 |
|
|
ObjectMapper mapper = new ObjectMapper();
|
141 |
|
|
JsonNode root = mapper.readTree(is);
|
142 |
|
|
|
143 |
|
|
if (root.get("status").textValue().equals("ok")) {
|
144 |
|
|
publication.setTitle(root.path("message").path("title").path(0).textValue());
|
145 |
|
|
publication.setDoi(doi);
|
146 |
|
|
publication.setSubjects(root.path("message").path("subject").textValue());
|
147 |
|
|
|
148 |
|
|
publication.setSource("crossref");
|
149 |
35509
|
antonis.le
|
publication.setId("crossref::" + DigestUtils.md5Hex(doi));
|
150 |
|
|
|
151 |
35516
|
antonis.le
|
if ("journal-article".equals(root.path("message").path("type").textValue()))
|
152 |
|
|
publication.setType(Publication.Type.ARTICLE);
|
153 |
|
|
else
|
154 |
|
|
publication.setType(Publication.Type.MONOGRAPH);
|
155 |
|
|
|
156 |
35508
|
antonis.le
|
publication.setPublicationDate(this.parsePublicationDate(root.path("message").path("issued")));
|
157 |
35509
|
antonis.le
|
publication.setJournal(this.parseJournal(root.path("message").path("container-title"), root.path("message").path("ISSN")));
|
158 |
35508
|
antonis.le
|
publication.getJournal().setPublisher(this.parsePublisher(root.path("message").path("publisher")));
|
159 |
35501
|
antonis.le
|
|
160 |
35508
|
antonis.le
|
publication.setAuthors(new ArrayList<Affiliation>());
|
161 |
|
|
for (JsonNode author : root.path("message").path("author")) {
|
162 |
|
|
publication.getAuthors().add(this.parseAffiliation(author));
|
163 |
|
|
}
|
164 |
35516
|
antonis.le
|
|
165 |
|
|
publication.setIdentifiers(new ArrayList<PublicationIdentifier>());
|
166 |
|
|
publication.getIdentifiers().add(new PublicationIdentifier("doi", doi));
|
167 |
35518
|
antonis.le
|
} else {
|
168 |
|
|
throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
|
169 |
35501
|
antonis.le
|
}
|
170 |
35518
|
antonis.le
|
} catch (ManagerException e) {
|
171 |
|
|
throw e;
|
172 |
35501
|
antonis.le
|
} catch (Exception e) {
|
173 |
|
|
logger.error("Error resolving doi", e);
|
174 |
|
|
|
175 |
35518
|
antonis.le
|
throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
|
176 |
35501
|
antonis.le
|
} finally {
|
177 |
|
|
try {
|
178 |
|
|
if (is != null)
|
179 |
|
|
is.close();
|
180 |
|
|
} catch (Exception e) {}
|
181 |
|
|
}
|
182 |
|
|
|
183 |
|
|
return publication;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
35508
|
antonis.le
|
private Affiliation parseAffiliation(JsonNode author) {
|
187 |
|
|
Person person = new Person();
|
188 |
|
|
|
189 |
|
|
person.setName(author.path("given").textValue());
|
190 |
|
|
person.setLastname(author.path("family").textValue());
|
191 |
35831
|
antonis.le
|
person.setAffiliations(new ArrayList<Affiliation>());
|
192 |
35508
|
antonis.le
|
|
193 |
|
|
Affiliation affiliation = new Affiliation();
|
194 |
|
|
affiliation.setPerson(person);
|
195 |
|
|
|
196 |
35831
|
antonis.le
|
person.getAffiliations().add(affiliation);
|
197 |
|
|
|
198 |
35508
|
antonis.le
|
return affiliation;
|
199 |
35502
|
antonis.le
|
}
|
200 |
|
|
|
201 |
35508
|
antonis.le
|
private Publisher parsePublisher(JsonNode publisherName) {
|
202 |
|
|
Publisher publisher = new Publisher();
|
203 |
|
|
|
204 |
|
|
publisher.setName(publisherName.textValue());
|
205 |
|
|
|
206 |
|
|
publisher.setId("crossref::" + DigestUtils.md5Hex(publisher.getName()));
|
207 |
|
|
publisher.setSource("crossref");
|
208 |
|
|
|
209 |
|
|
return publisher;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
private Journal parseJournal(JsonNode name, JsonNode issn) {
|
213 |
35840
|
antonis.le
|
Journal journal;
|
214 |
|
|
List<Vocabulary> journals = journalDAO.search(name.path(0).textValue());
|
215 |
35508
|
antonis.le
|
|
216 |
35840
|
antonis.le
|
if (journals.size() > 0) {
|
217 |
|
|
journal = journalDAO.getJournal(journals.get(0).getId());
|
218 |
|
|
} else {
|
219 |
|
|
journal = new Journal();
|
220 |
35508
|
antonis.le
|
|
221 |
35840
|
antonis.le
|
journal.setTitle(name.path(0).textValue());
|
222 |
|
|
journal.setAlternativeTitle(name.path(1).textValue());
|
223 |
35508
|
antonis.le
|
|
224 |
35840
|
antonis.le
|
StringBuilder sb = new StringBuilder();
|
225 |
|
|
Iterator<JsonNode> ssn = issn.elements();
|
226 |
|
|
while (ssn.hasNext()) {
|
227 |
|
|
sb.append(ssn.next().textValue()).append(",");
|
228 |
|
|
}
|
229 |
35639
|
antonis.le
|
|
230 |
35840
|
antonis.le
|
if (sb.length() > 0)
|
231 |
|
|
sb.deleteCharAt(sb.length() - 1);
|
232 |
35639
|
antonis.le
|
|
233 |
35840
|
antonis.le
|
journal.setIssn(sb.toString());
|
234 |
35508
|
antonis.le
|
|
235 |
35840
|
antonis.le
|
journal.setSource("crossref");
|
236 |
|
|
journal.setId("crossref::" + DigestUtils.md5Hex(journal.getTitle()));
|
237 |
|
|
}
|
238 |
|
|
|
239 |
35508
|
antonis.le
|
return journal;
|
240 |
|
|
}
|
241 |
|
|
|
242 |
|
|
private Date parsePublicationDate(JsonNode node) {
|
243 |
35501
|
antonis.le
|
Calendar c = Calendar.getInstance();
|
244 |
|
|
|
245 |
|
|
c.clear();
|
246 |
35508
|
antonis.le
|
c.setTimeZone(TimeZone.getTimeZone("UTC"));
|
247 |
35501
|
antonis.le
|
|
248 |
35508
|
antonis.le
|
c.set(Calendar.YEAR, node.path("date-parts").path(0).path(0).intValue());
|
249 |
|
|
c.set(Calendar.MONTH, node.path("date-parts").path(0).path(1).intValue() - 1);
|
250 |
|
|
c.set(Calendar.DAY_OF_MONTH, 1 + node.path("date-parts").path(0).path(2).intValue());
|
251 |
|
|
c.set(Calendar.HOUR_OF_DAY, 0);
|
252 |
35501
|
antonis.le
|
|
253 |
35508
|
antonis.le
|
|
254 |
35501
|
antonis.le
|
return c.getTime();
|
255 |
|
|
}
|
256 |
35509
|
antonis.le
|
|
257 |
35518
|
antonis.le
|
public PublicationDAO getPublicationDAO() {
|
258 |
|
|
return publicationDAO;
|
259 |
|
|
}
|
260 |
35509
|
antonis.le
|
|
261 |
35518
|
antonis.le
|
public void setPublicationDAO(PublicationDAO publicationDAO) {
|
262 |
|
|
this.publicationDAO = publicationDAO;
|
263 |
35509
|
antonis.le
|
}
|
264 |
35749
|
antonis.le
|
|
265 |
|
|
public JournalDAO getJournalDAO() {
|
266 |
|
|
return journalDAO;
|
267 |
|
|
}
|
268 |
|
|
|
269 |
|
|
public void setJournalDAO(JournalDAO journalDAO) {
|
270 |
|
|
this.journalDAO = journalDAO;
|
271 |
|
|
}
|
272 |
|
|
|
273 |
|
|
public PublisherDAO getPublisherDAO() {
|
274 |
|
|
return publisherDAO;
|
275 |
|
|
}
|
276 |
|
|
|
277 |
|
|
public void setPublisherDAO(PublisherDAO publisherDAO) {
|
278 |
|
|
this.publisherDAO = publisherDAO;
|
279 |
|
|
}
|
280 |
35440
|
antonis.le
|
}
|