1
|
package eu.dnetlib.data.claims.migration.handler;
|
2
|
|
3
|
import eu.dnetlib.data.claims.migration.ClaimValidation;
|
4
|
import eu.dnetlib.data.claims.migration.entity.Result;
|
5
|
import eu.dnetlib.data.claims.migration.parser.DMFParser;
|
6
|
import eu.dnetlib.data.claimsDemo.*;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.xml.sax.SAXException;
|
9
|
|
10
|
import javax.xml.parsers.ParserConfigurationException;
|
11
|
import javax.xml.xpath.XPathExpressionException;
|
12
|
import java.io.BufferedWriter;
|
13
|
import java.io.FileWriter;
|
14
|
import java.io.IOException;
|
15
|
import java.io.PrintWriter;
|
16
|
import java.sql.ResultSet;
|
17
|
import java.sql.SQLException;
|
18
|
import java.sql.Timestamp;
|
19
|
import java.util.Date;
|
20
|
|
21
|
/**
|
22
|
* Created by kiatrop on 5/2/2016.
|
23
|
*/
|
24
|
public class DMFResultHandler {
|
25
|
private static final Logger logger = Logger.getLogger(DMFResultHandler.class);
|
26
|
|
27
|
SqlDAO sqlDAO = null;
|
28
|
QueryGenerator queryGenerator = null;
|
29
|
DMFParser dmfParser = null;
|
30
|
|
31
|
/**
|
32
|
* Given an openaire id decides the datasource that the result was collected from
|
33
|
* Default datasource is openaire
|
34
|
* @param openaireId
|
35
|
* @return collectedFrom datasource
|
36
|
*/
|
37
|
public static String identifyCollectedFrom(String openaireId){
|
38
|
if(openaireId==null){
|
39
|
return null;
|
40
|
}
|
41
|
if(openaireId.contains("crossref")){
|
42
|
return ClaimUtils.COLLECTED_FROM_CROSSREF;
|
43
|
}else if (openaireId.contains("datacite")){
|
44
|
return ClaimUtils.COLLECTED_FROM_DATACITE;
|
45
|
} else if (openaireId.contains("orcid")){
|
46
|
return ClaimUtils.COLLECTED_FROM_ORCID;
|
47
|
}else{
|
48
|
return ClaimUtils.COLLECTED_FROM_OPENAIRE;
|
49
|
}
|
50
|
|
51
|
}
|
52
|
|
53
|
private String fetchDMFResult(String id) throws SQLStoreException, SQLException {
|
54
|
ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateSelectDMFByIdQuery(id));
|
55
|
return rs.getString("xml");
|
56
|
}
|
57
|
|
58
|
public Result fetchResultByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException {
|
59
|
return dmfParser.dmf2Result(dmf);
|
60
|
}
|
61
|
|
62
|
public Result fetchResultById(String id) throws SQLStoreException, SQLException, ParserConfigurationException, SAXException, IOException {
|
63
|
//TODO check
|
64
|
return fetchResultByDMF(fetchDMFResult(id));
|
65
|
// return ParsingClaimUtils.getResultFromDMF(fetchDMFResult(id));
|
66
|
}
|
67
|
|
68
|
/**
|
69
|
*
|
70
|
* @param dmf
|
71
|
* @return DOI from DMF - a *valid* one
|
72
|
* @throws IOException
|
73
|
* @throws SAXException
|
74
|
* @throws ParserConfigurationException
|
75
|
* @throws XPathExpressionException
|
76
|
*/
|
77
|
public String fetchDoiByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
78
|
|
79
|
return checkDoiValidity(dmfParser.getDOIIdentifierFromDMF(dmf));
|
80
|
}
|
81
|
public String fetchOrcidWorkByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
82
|
return dmfParser.getOrcidworkIdentifierFromDMF(dmf);
|
83
|
}
|
84
|
public String fetchEmbargoEndDateByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
85
|
return dmfParser.getEmbargoEndDateFromDMF(dmf);
|
86
|
}
|
87
|
public String fetchAccessRights(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
88
|
return dmfParser.getAccessRightsFromDMF(dmf);
|
89
|
}
|
90
|
|
91
|
/**
|
92
|
*
|
93
|
*
|
94
|
* @param doi
|
95
|
* @return DOI String : the given one if it's valid
|
96
|
* @throws Exception
|
97
|
*/
|
98
|
public static String checkDoiValidity(String doi) {
|
99
|
if(doi==null){
|
100
|
return null;
|
101
|
}
|
102
|
String newDoi = doi;
|
103
|
try{
|
104
|
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("invalid_dois.txt", true)));
|
105
|
boolean isValid= SearchUtils.isDoiValid(doi);
|
106
|
if(!isValid&&(doi.length() - (doi.replace("_", "")).length() > 1)){
|
107
|
String transformDoi = doi.replace("_", "-");
|
108
|
isValid=SearchUtils.isDoiValid(transformDoi);
|
109
|
if(isValid){
|
110
|
Date date= new java.util.Date();
|
111
|
out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with : "+transformDoi);
|
112
|
newDoi=transformDoi;
|
113
|
}
|
114
|
}else if(!isValid&&(doi.length() - (doi.replace("_", "")).length() == 1)) {
|
115
|
String transformDoi = doi.replace("_", "-");
|
116
|
isValid=SearchUtils.isDoiValid(transformDoi);
|
117
|
if(isValid){
|
118
|
Date date= new java.util.Date();
|
119
|
out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with : "+transformDoi);
|
120
|
newDoi = transformDoi;
|
121
|
}
|
122
|
}else if(!isValid) {
|
123
|
String transformDoi =doi.replaceAll("\\p{C}", "");
|
124
|
isValid=SearchUtils.isDoiValid(transformDoi);
|
125
|
if(isValid){
|
126
|
Date date= new java.util.Date();
|
127
|
out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with (zero-width-char): "+transformDoi);
|
128
|
newDoi = transformDoi;
|
129
|
}
|
130
|
}
|
131
|
out.close();
|
132
|
}catch (IOException e) {
|
133
|
logger.error("Couldn't write to file " + "invalid_dois.txt",e);
|
134
|
}
|
135
|
return newDoi;
|
136
|
}
|
137
|
|
138
|
public SqlDAO getSqlDAO() {
|
139
|
return sqlDAO;
|
140
|
}
|
141
|
|
142
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
143
|
this.sqlDAO = sqlDAO;
|
144
|
}
|
145
|
|
146
|
public QueryGenerator getQueryGenerator() {
|
147
|
return queryGenerator;
|
148
|
}
|
149
|
|
150
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
151
|
this.queryGenerator = queryGenerator;
|
152
|
}
|
153
|
|
154
|
public DMFParser getDmfParser() {
|
155
|
return dmfParser;
|
156
|
}
|
157
|
|
158
|
public void setDmfParser(DMFParser dmfParser) {
|
159
|
this.dmfParser = dmfParser;
|
160
|
}
|
161
|
|
162
|
}
|