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.List;
24
import java.util.TimeZone;
25

    
26

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

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

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

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

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

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

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

    
64
		publicationDAO.savePublication(publication);
65
		/*
66
		deletePublicationRelations(publication);
67

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

    
76
		if (publication.getJournal() != null) {
77
			if (!"doaj".equals(publication.getJournal().getSource())) {
78

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

    
82
				publication.setJournal(journalManager.saveJournal(publication.getJournal()));
83
			}
84

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

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

    
94
			publication.setPublisher(publisherManager.getPublisher(publication.getPublisher().getId()));
95
		}
96
        */
97
		return publication;
98
	}
99

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

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

    
108
				if (aff.getPerson() != null) {
109
					String personId = personManager.generateId(aff.getPerson());
110

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

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

    
120
						if (!affiliationExists) {
121
							temp.getAffiliations().add(aff);
122

    
123
							personManager.saveUser(temp);
124
						}
125

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

    
131
							affiliations.add(aff);
132

    
133
							aff.getPerson().setAffiliations(affiliations);
134

    
135
							personManager.saveUser(aff.getPerson());
136
						}
137
					}
138
				}
139
			}
140

    
141
		publicationDAO.saveAffiliations(publication);*/
142
	}
143

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

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

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

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

    
164
				publication.setSource("crossref");
165
				publication.setId("crossref::" + DigestUtils.md5Hex(doi));
166

    
167
				if ("journal-article".equals(root.path("message").path("type").textValue()))
168
					publication.setType(Publication.Type.ARTICLE);
169
				else
170
					publication.setType(Publication.Type.MONOGRAPH);
171

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

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

    
181
				publication.setLicense(root.path("message").path("license").path(0).path("URL").textValue());
182

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

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

    
202
		return publication;
203
	}
204

    
205
	private Affiliation parseAffiliation(JsonNode author) {
206
		/*Person person = new Person();
207

    
208
		person.setName(author.path("given").textValue());
209
		person.setLastname(author.path("family").textValue());
210
		person.setAffiliations(new ArrayList<Affiliation>());
211

    
212
		Affiliation affiliation = new Affiliation();
213
		affiliation.setPerson(person);
214

    
215
		person.getAffiliations().add(affiliation);
216

    
217
		return affiliation;*/
218
		return null;
219
	}
220

    
221
	private Publisher parsePublisher(JsonNode publisherName) {
222
		/*Publisher publisher = new Publisher();
223

    
224
		publisher.setName(publisherName.textValue());
225
		publisher.setBankAccount(new BankAccount());
226

    
227
		publisher.setId("crossref::" + DigestUtils.md5Hex(publisher.getName()));
228
		publisher.setSource("crossref");
229

    
230
		return publisher;*/
231
		return null;
232
	}
233

    
234
	private Journal parseJournal(JsonNode name, JsonNode issn) {
235
		Journal journal = journalManager.getJournalByTitle(name.path(0).textValue());
236

    
237
		if (journal == null) {
238
			journal = new Journal();
239

    
240
			journal.setTitle(name.path(0).textValue());
241
			journal.setAlternativeTitle(name.path(1).textValue());
242

    
243
			StringBuilder sb = new StringBuilder();
244
			Iterator<JsonNode> ssn = issn.elements();
245
			while (ssn.hasNext()) {
246
				sb.append(ssn.next().textValue()).append(",");
247
			}
248

    
249
			if (sb.length() > 0)
250
				sb.deleteCharAt(sb.length() - 1);
251

    
252
			journal.setIssn(sb.toString());
253

    
254
			journal.setApcCurrency(Currency.EUR);
255
			journal.setSource("crossref");
256
			journal.setId("crossref::" + DigestUtils.md5Hex(journal.getTitle()));
257
		}
258

    
259
		return journal;
260
	}
261

    
262
	private Date parsePublicationDate(JsonNode node) {
263
		Calendar c = Calendar.getInstance();
264

    
265
		c.clear();
266
		c.setTimeZone(TimeZone.getTimeZone("UTC"));
267

    
268
		c.set(Calendar.YEAR, node.path("date-parts").path(0).path(0).intValue());
269
		c.set(Calendar.MONTH, node.path("date-parts").path(0).path(1).intValue() - 1);
270
		c.set(Calendar.DAY_OF_MONTH, 1 + node.path("date-parts").path(0).path(2).intValue());
271
		c.set(Calendar.HOUR_OF_DAY, 0);
272

    
273

    
274
		return c.getTime();
275
	}
276
}
(20-20/29)