Project

General

Profile

1
/**
2
 *
3
 */
4
package eu.dnetlib.resolver;
5

    
6
import java.util.List;
7

    
8
import com.google.common.collect.Lists;
9
import com.google.gson.JsonArray;
10
import com.google.gson.JsonElement;
11
import com.google.gson.JsonObject;
12
import com.google.gson.JsonParser;
13
import eu.dnetlib.resolver.model.CompletionStatus;
14
import eu.dnetlib.resolver.model.ObjectProvenance;
15
import eu.dnetlib.resolver.model.ObjectType;
16
import eu.dnetlib.resolver.model.ResolvedObject;
17
import org.apache.commons.lang3.ArrayUtils;
18
import org.apache.commons.lang3.StringUtils;
19
import org.apache.commons.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21

    
22
/**
23
 * @author sandro
24
 */
25
public class CrossRefParserJSON {
26

    
27
	/**
28
	 * The Constant CROSSREF_NS_PREFIX.
29
	 */
30
	private static final String CROSSREF_NS_PREFIX = "crossref____";
31
	private static final Log log = LogFactory.getLog(CrossRefParserJSON.class); // NOPMD by marko on 11/24/08 5:02 PM
32

    
33
	public ResolvedObject parseRecord(final String record) {
34

    
35
		log.debug("Start to parsing " + record);
36

    
37
		if (record == null) return null;
38
		JsonElement jElement = new JsonParser().parse(record);
39
		JsonObject mainObj = jElement.getAsJsonObject();
40
		if (mainObj.get("message").isJsonNull()) {
41
			return null;
42
		}
43
		JsonObject message = mainObj.getAsJsonObject("message");
44
		ResolvedObject currentObject = new ResolvedObject();
45

    
46
		if (message.get("DOI") != null) {
47
			final String doi = message.get("DOI").getAsString();
48
			log.debug("found doi" + doi);
49
			currentObject.setPid(doi);
50
			currentObject.setPidType("doi");
51
		}
52

    
53
		if ((!message.get("created").isJsonNull()) && (message.getAsJsonObject("created").get("date-time") != null)) {
54
			currentObject.setDate(message.getAsJsonObject("created").get("date-time").getAsString());
55
		}
56

    
57
		if (!message.get("title").isJsonNull()) {
58
			currentObject.setTitles(Lists.newArrayList(message.get("title").toString().replace("[", "").replace("]", "")));
59
		}
60

    
61
		if (message.get("author") != null && !message.get("author").isJsonNull()) {
62
			JsonArray author = message.getAsJsonArray("author");
63
			List<String> authorList = Lists.newArrayList();
64
			for (JsonElement anAuthor : author) {
65
				JsonObject currentAuth = anAuthor.getAsJsonObject();
66

    
67
				String family = "";
68
				String given = "";
69
				if (currentAuth != null && currentAuth.get("family") != null && !currentAuth.get("family").isJsonNull()) {
70
					family = currentAuth.get("family").getAsString();
71
				}
72
				if (currentAuth != null && currentAuth.get("given") != null && !currentAuth.get("given").isJsonNull()) {
73
					given = currentAuth.get("given").getAsString();
74
				}
75
				authorList.add(String.format("%s %s", family, given));
76
			}
77
			currentObject.setAuthors(authorList);
78
		}
79
		ObjectProvenance provenance = new ObjectProvenance();
80
		AbstractPIDResolver.setDatasourceProvenace(provenance, CROSSREF_NS_PREFIX);
81
		if (message.get("publisher") != null && !message.get("publisher").isJsonNull()) {
82
			provenance.setPublisher(message.get("publisher").getAsString());
83
		}
84
		currentObject.fixContribution(provenance);
85
		currentObject.setDatasourceProvenance(Lists.newArrayList(provenance));
86
		currentObject.setCompletionStatus(CompletionStatus.complete.toString());
87

    
88
		if (!message.get("type").isJsonNull()) {
89
			final String type = message.get("type").getAsString();
90
			if ("component".equals(type) || "dataset".equals(type)) {
91
				currentObject.setType(ObjectType.dataset);
92
			} else
93
				currentObject.setType(ObjectType.publication);
94
		}
95
		return currentObject;
96

    
97
	}
98

    
99
	/**
100
	 * @param fullCitations
101
	 * @param result
102
	 */
103
	private void parseFullCitation(final String fullCitations, final ResolvedObject result) {
104
		if (StringUtils.isEmpty(fullCitations))
105
			return;
106

    
107
		String beforeTitles[] = fullCitations.split("'");
108

    
109
		if (ArrayUtils.isEmpty(beforeTitles))
110
			return;
111

    
112
		String[] data = beforeTitles[0].split(",");
113
		if (ArrayUtils.isEmpty(data)) return;
114
		if (data.length > 1) {
115
			String date = data[data.length - 2];
116
			result.setDate(date.trim());
117
			List<String> authors = Lists.newArrayList();
118
			for (int i = 0; i < data.length - 2; i++) {
119
				authors.add(data[i]);
120
			}
121
			result.setAuthors(authors);
122
		}
123
	}
124
}
(3-3/14)