Project

General

Profile

1
package eu.dnetlib.goldoa.service;
2

    
3
import com.fasterxml.jackson.databind.JsonNode;
4
import com.fasterxml.jackson.databind.ObjectMapper;
5
import eu.dnetlib.goldoa.domain.*;
6
//import eu.dnetlib.goldoa.domain.BankAccount;
7
import eu.dnetlib.goldoa.domain.Currency;
8
import eu.dnetlib.goldoa.domain.User;
9
import eu.dnetlib.goldoa.domain.Identifier;
10
import eu.dnetlib.goldoa.service.dao.PublicationDAO;
11
import org.apache.commons.codec.digest.DigestUtils;
12
import org.apache.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.stereotype.Service;
15
import org.springframework.transaction.annotation.Transactional;
16

    
17
import java.io.InputStream;
18
import java.net.URL;
19
import java.util.ArrayList;
20
import java.util.Calendar;
21
import java.util.Date;
22
import java.util.Iterator;
23
import java.util.TimeZone;
24

    
25

    
26
/**
27
 * Created by antleb on 3/13/15.
28
 */
29
@Transactional
30
@Service
31
public class PublicationManagerImpl implements PublicationManager {
32

    
33
	private static Logger logger = Logger.getLogger(PublicationManagerImpl.class);
34

    
35
	@Autowired
36
	private PublicationDAO publicationDAO;
37
	@Autowired
38
	private JournalManager journalManager;
39
	@Autowired
40
	private PublisherManager publisherManager;
41
	@Autowired
42
	private UserManager personManager;
43
	@Autowired
44
	private OrganizationManager organizationManager;
45

    
46
	@Override
47
	public Publication getPublication(String publicationId) {
48
	    return publicationDAO.getPublication(publicationId);
49
	}
50

    
51
	private void loadPublicationRelations(Publication publication) {
52
		publicationDAO.loadAffiliations(publication);
53
	}
54

    
55
	@Override
56
	public Publication savePublication(Publication publication) {
57

    
58
		if (publication.getId() == null) {
59
			publication.setSource("portal");
60
			publication.setId("portal::" + DigestUtils.md5Hex(publication.getTitle()));
61
		}
62

    
63

    
64
		//deletePublicationRelations(publication);
65

    
66
		try {
67
			insertPublicationRelations(publication);
68
		} catch (PersonManagerException e) {
69
			e.printStackTrace();
70
		} catch (OrganizationManagerException e) {
71
			e.printStackTrace();
72
		}
73

    
74
		if (publication.getJournal() != null) {
75
			if (!"doaj".equals(publication.getJournal().getSource())) {
76

    
77
				if (publication.getJournal().getPublisher() != null && !"doaj".equals(publication.getJournal().getPublisher().getSource()))
78
					publication.getJournal().setPublisher(publisherManager.savePublisher(publication.getJournal().getPublisher()));
79

    
80
				publication.setJournal(journalManager.saveJournal(publication.getJournal()));
81
			}
82

    
83
			publication.setJournal(journalManager.getJournal(publication.getJournal().getId()));
84
		} else {
85
			publication.setJournal(null);
86
		}
87

    
88
		if (publication.getPublisher() != null) {
89
			if (!"doaj".equals(publication.getPublisher().getSource()))
90
				publication.setPublisher(publisherManager.savePublisher(publication.getPublisher()));
91

    
92
			publication.setPublisher(publisherManager.getPublisher(publication.getPublisher().getId()));
93
		}
94
		publicationDAO.savePublication(publication);
95
		return publication;
96
	}
97

    
98
	private void insertPublicationRelations(Publication publication) throws PersonManagerException, OrganizationManagerException {
99
		/*publicationDAO.insertIdentifiers(publication);
100

    
101
		if (publication.getAuthors() != null)
102
			for (Affiliation aff : publication.getAuthors()) {
103
				if (aff.getOrganization() != null && aff.getOrganization().getId() == null)
104
					aff.getOrganization().setId(organizationManager.saveOrganization(aff.getOrganization()));
105

    
106
				if (aff.getUser() != null) {
107
					String personId = personManager.generateId(aff.getUser());
108

    
109
					try {
110
						boolean affiliationExists = false;
111
						Person temp = personManager.getById(personId);
112

    
113
						for (Affiliation a : temp.getAffiliations()) {
114
							if (a.getOrganization() != null && aff.getOrganization() != null && a.getOrganization().getId().equals(aff.getOrganization().getId()))
115
								affiliationExists = true;
116
						}
117

    
118
						if (!affiliationExists) {
119
							temp.getAffiliations().add(aff);
120

    
121
							personManager.saveUser(temp);
122
						}
123

    
124
						aff.setPerson(temp);
125
					} catch (PersonManagerException e) {
126
						if (e.getErrorCause() == PersonManagerException.ErrorCause.NOT_EXISTS) {
127
							List<Affiliation> affiliations = new ArrayList<Affiliation>();
128

    
129
							affiliations.add(aff);
130

    
131
							aff.getUser().setAffiliations(affiliations);
132

    
133
							personManager.saveUser(aff.getUser());
134
						}
135
					}
136
				}
137
			}
138

    
139
		publicationDAO.saveAffiliations(publication);*/
140
	}
141

    
142
	private void deletePublicationRelations(Publication publication) {
143
		//publicationDAO.deleteIdentifiers(publication);
144
		//publicationDAO.deleteAffiliations(publication);
145
	}
146

    
147
	@Override
148
	public Publication resolveDOI(String doi) throws ManagerException {
149
		Publication publication = new Publication();
150
		InputStream is = null;
151

    
152
		try {
153
			is = new URL("http://api.crossref.org/works/" + doi).openStream();
154
			ObjectMapper mapper = new ObjectMapper();
155
			JsonNode root = mapper.readTree(is);
156

    
157
			if (root.get("status").textValue().equals("ok")) {
158
				publication.setTitle(root.path("message").path("title").path(0).textValue());
159
				publication.setDoi(doi);
160
				publication.setSubjects(root.path("message").path("subject").textValue());
161

    
162
				publication.setSource("crossref");
163
				publication.setId("crossref::" + DigestUtils.md5Hex(doi));
164

    
165
				if ("journal-article".equals(root.path("message").path("type").textValue()))
166
					publication.setType(PublicationType.ARTICLE);
167
				else
168
					publication.setType(PublicationType.MONOGRAPH);
169

    
170
				publication.setDate(this.parsePublicationDate(root.path("message").path("issued")));
171
				publication.setJournal(this.parseJournal(root.path("message").path("container-title"), root.path("message").path("ISSN")));
172
				publication.getJournal().setPublisher(this.parsePublisher(root.path("message").path("publisher")));
173

    
174
				/*publication.setAffiliations(new ArrayList<Affiliation>());
175
				for (JsonNode author : root.path("message").path("author")) {
176
					publication.getAffiliations().add(this.parseAffiliation(author));
177
				}*/
178

    
179
				publication.setLicense(root.path("message").path("license").path(0).path("URL").textValue());
180

    
181
				publication.setPublicationIdentifiers(new ArrayList<Identifier>());
182
				publication.getPublicationIdentifiers().add(new Identifier("doi", doi));
183
			} else {
184
				throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
185
			}
186
		} catch (ManagerException e) {
187
			throw e;
188
		} catch (Exception e) {
189
			logger.error("Error resolving doi", e);
190

    
191
			throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
192
		} finally {
193
			try {
194
				if (is != null)
195
					is.close();
196
			} catch (Exception e) {
197
			}
198
		}
199

    
200
		return publication;
201
	}
202

    
203
	private Affiliation parseAffiliation(JsonNode author) {
204
		User user = new User();
205
		user.setFirstname(author.path("given").textValue());
206
		user.setLastname(author.path("family").textValue());
207
		user.setAffiliations(new ArrayList<Affiliation>());
208

    
209
		Affiliation af = new Affiliation();
210
		user.getAffiliations().add(af);
211
		return af;
212
	}
213

    
214
	private Publisher parsePublisher(JsonNode publisherName) {
215
		Publisher publisher = new Publisher();
216

    
217
		publisher.setName(publisherName.textValue());
218
		publisher.setBankAccount(new BankAccount());
219

    
220
		publisher.setId("crossref::" + DigestUtils.md5Hex(publisher.getName()));
221
		publisher.setSource("crossref");
222

    
223
		return publisher;
224
	}
225

    
226
	private Journal parseJournal(JsonNode name, JsonNode issn) {
227
		Journal journal = journalManager.getJournalByTitle(name.path(0).textValue());
228

    
229
		if (journal == null) {
230
			journal = new Journal();
231

    
232
			journal.setTitle(name.path(0).textValue());
233
			journal.setAlternativeTitle(name.path(1).textValue());
234

    
235
			StringBuilder sb = new StringBuilder();
236
			Iterator<JsonNode> ssn = issn.elements();
237
			while (ssn.hasNext()) {
238
				sb.append(ssn.next().textValue()).append(",");
239
			}
240

    
241
			if (sb.length() > 0)
242
				sb.deleteCharAt(sb.length() - 1);
243

    
244
			journal.setIssn(sb.toString());
245

    
246
			journal.setApccurrency(Currency.EUR);
247
			journal.setSource("crossref");
248
			journal.setId("crossref::" + DigestUtils.md5Hex(journal.getTitle()));
249
		}
250
		return journal;
251
	}
252

    
253
	private Date parsePublicationDate(JsonNode node) {
254
		Calendar c = Calendar.getInstance();
255

    
256
		c.clear();
257
		c.setTimeZone(TimeZone.getTimeZone("UTC"));
258

    
259
		c.set(Calendar.YEAR, node.path("date-parts").path(0).path(0).intValue());
260
		c.set(Calendar.MONTH, node.path("date-parts").path(0).path(1).intValue() - 1);
261
		c.set(Calendar.DAY_OF_MONTH, 1 + node.path("date-parts").path(0).path(2).intValue());
262
		c.set(Calendar.HOUR_OF_DAY, 0);
263

    
264

    
265
		return c.getTime();
266
	}
267
}
(20-20/29)