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.service.dao.PublicationDAO;
7
import org.apache.commons.codec.digest.DigestUtils;
8
import org.apache.log4j.Logger;
9
import org.springframework.beans.factory.annotation.Autowired;
10
import org.springframework.stereotype.Service;
11
import org.springframework.transaction.annotation.Transactional;
12

    
13
import java.io.InputStream;
14
import java.math.BigInteger;
15
import java.net.URL;
16
import java.util.Calendar;
17
import java.util.Date;
18
import java.util.Iterator;
19
import java.util.TimeZone;
20

    
21
//import eu.dnetlib.goldoa.domain.BankAccount;
22

    
23

    
24
/**
25
 * Created by antleb on 3/13/15.
26
 */
27
@Transactional
28
@Service("publicationManager")
29
public class PublicationManagerImpl implements PublicationManager {
30

    
31
	private static Logger logger = Logger.getLogger(PublicationManagerImpl.class);
32

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

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

    
52
		if (publication.getId() == null) {
53
			publication.setSource("portal");
54
			publication.setId("portal::" + DigestUtils.md5Hex(publication.getTitle()));
55
		}
56

    
57
		if(publication.getIdentifiers() != null){
58
			for(Identifier id : publication.getIdentifiers())
59
				id.setId(this.generateId());
60
		}
61

    
62
		/*if (publication.getJournal() != null) {
63
			if (!"doaj".equals(publication.getJournal().getSource())) {
64
				if (publication.getJournal().getPublisher() != null && !"doaj".equals(publication.getJournal().getPublisher().getSource())){
65
					//publication.getJournal().setPublisher(publisherManager.savePublisher(publication.getJournal().getPublisher()));
66
				}
67
				publication.setJournal(journalManager.saveJournal(publication.getJournal()));
68
			}
69

    
70
			publication.setJournal(journalManager.getJournal(publication.getJournal().getId()));
71
		} else {
72
			publication.setJournal(null);
73
		}
74
		if (publication.getPublisher() != null) {
75
			if (!"doaj".equals(publication.getPublisher().getSource())){
76
				System.out.println(publication.getPublisher().getSource());
77
				//publication.setPublisher(publisherManager.savePublisher(publication.getPublisher()));
78
			}
79
			//System.out.println(publication.getPublisher().getId());
80
			//publication.setPublisher(publisherManager.getPublisher(publication.getPublisher().getId()));
81
		}*/
82
		return publicationDAO.savePublication(publication);
83
	}
84

    
85
	@Override
86
	public Publication resolveDOI(String doi) throws ManagerException {
87
		Publication publication = null;
88
		InputStream is = null;
89
		try {
90
			is = new URL("http://api.crossref.org/works/" + doi).openStream();
91
			ObjectMapper mapper = new ObjectMapper();
92
			JsonNode root = mapper.readTree(is);
93

    
94
			if (root.get("status").textValue().equals("ok")) {
95
				publication = new Publication();
96
				publication.setTitle(root.path("message").path("title").path(0).textValue());
97
				publication.setDoi(doi);
98
				publication.setSubjects(root.path("message").path("subject").textValue());
99
				publication.setSource("crossref");
100
				publication.setId("crossref::" + DigestUtils.md5Hex(doi));
101

    
102
				if ("journal-article".equals(root.path("message").path("type").textValue()))
103
					publication.setType(PublicationType.ARTICLE);
104
				else
105
					publication.setType(PublicationType.MONOGRAPH);
106

    
107
				publication.setDate(this.parsePublicationDate(root.path("message").path("issued")));
108
				publication.setJournal(this.parseJournal(root.path("message").path("container-title"), root.path("message").path("ISSN")));
109
				publication.getJournal().setPublisher(this.parsePublisher(root.path("message").path("publisher")));
110

    
111
				for (JsonNode author : root.path("message").path("author"))
112
					publication.getAuthors().add(this.parseAffiliation(author,publication));
113

    
114
				publication.setLicense(root.path("message").path("license").path(0).path("URL").textValue());
115
				publication.getIdentifiers().add(new Identifier("doi", doi));
116
			} else {
117
				throw new ManagerException(ManagerException.ErrorCause.NOT_EXISTS);
118
			}
119
		} catch (ManagerException e) {
120
			throw e;
121
		} catch (Exception e) {
122
			logger.error("Error resolving doi", e);
123
			throw new ManagerException(ManagerException.ErrorCause.UNKNOWN);
124
		} finally {
125
			try {
126
				if (is != null)
127
					is.close();
128
			} catch (Exception e) {
129
			}
130
		}
131

    
132
		return publication;
133
	}
134

    
135
	private Author parseAffiliation(JsonNode author,Publication publication) {
136
		Author a = new Author();
137

    
138
		a.setFirstname(author.path("given").textValue());
139
		a.setLastname(author.path("family").textValue());
140
		a.setId(DigestUtils.md5Hex(a.getFirstname() + a.getEmail()) + publication.getId());
141
		return a;
142
	}
143

    
144
	private Publisher parsePublisher(JsonNode publisherName) {
145

    
146
		Publisher publisher = publisherManager.getPublisherByName(publisherName.textValue());
147
		if(publisher == null){
148
			publisher = new Publisher();
149

    
150
			publisher.setName(publisherName.textValue());
151

    
152
			String value = publisherManager.getBankAddress().toString();
153
			BigInteger id = publisherManager.getBankId();
154
			publisher.setBankAccount(new BankAccount(id,null,value,
155
					null,null,null));
156

    
157
			publisher.setId("crossref::" + DigestUtils.md5Hex(publisher.getName()));
158
			publisher.setSource("crossref");
159
		}
160
		return publisher;
161
	}
162

    
163
	private Journal parseJournal(JsonNode name, JsonNode issn) {
164

    
165
		Journal journal = journalManager.getJournalByTitle(name.path(0).textValue());
166
		if (journal == null) {
167
			journal = new Journal();
168

    
169
			journal.setTitle(name.path(0).textValue());
170
			journal.setAlternativeTitle(name.path(1).textValue());
171

    
172
			StringBuilder sb = new StringBuilder();
173
			Iterator<JsonNode> ssn = issn.elements();
174
			while (ssn.hasNext()) {
175
				sb.append(ssn.next().textValue()).append(",");
176
			}
177

    
178
			if (sb.length() > 0)
179
				sb.deleteCharAt(sb.length() - 1);
180

    
181
			journal.setIssn(sb.toString());
182

    
183
			journal.setApccurrency(Currency.EUR);
184
			journal.setSource("crossref");
185
			journal.setId("crossref::" + DigestUtils.md5Hex(journal.getTitle()));
186
		}
187

    
188
		return journal;
189
	}
190

    
191
	private Date parsePublicationDate(JsonNode node) {
192
		Calendar c = Calendar.getInstance();
193

    
194
		c.clear();
195
		c.setTimeZone(TimeZone.getTimeZone("UTC"));
196

    
197
		c.set(Calendar.YEAR, node.path("date-parts").path(0).path(0).intValue());
198
		c.set(Calendar.MONTH, node.path("date-parts").path(0).path(1).intValue() - 1);
199
		c.set(Calendar.DAY_OF_MONTH, 1 + node.path("date-parts").path(0).path(2).intValue());
200
		c.set(Calendar.HOUR_OF_DAY, 0);
201

    
202

    
203
		return c.getTime();
204
	}
205

    
206
	private BigInteger generateId(){
207
		return publicationDAO.generateIdentifierID();
208
	}
209

    
210
}
(20-20/29)