1
|
package eu.dnetlib.data.claims.migration.handler;
|
2
|
|
3
|
import eu.dnetlib.data.claims.migration.Result;
|
4
|
import eu.dnetlib.data.claims.migration.parser.DMFParser;
|
5
|
import eu.dnetlib.data.claimsDemo.ParsingClaimUtils;
|
6
|
import eu.dnetlib.data.claimsDemo.QueryGenerator;
|
7
|
import eu.dnetlib.data.claimsDemo.SearchUtils;
|
8
|
import eu.dnetlib.data.claimsDemo.SqlDAO;
|
9
|
import org.xml.sax.SAXException;
|
10
|
|
11
|
import javax.xml.parsers.ParserConfigurationException;
|
12
|
import javax.xml.xpath.XPathExpressionException;
|
13
|
import java.io.BufferedWriter;
|
14
|
import java.io.FileWriter;
|
15
|
import java.io.IOException;
|
16
|
import java.io.PrintWriter;
|
17
|
import java.sql.ResultSet;
|
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
|
|
26
|
SqlDAO sqlDAO = null;
|
27
|
QueryGenerator queryGenerator = null;
|
28
|
DMFParser dmfParser = null;
|
29
|
|
30
|
private String fetchDMFResult(String id) throws Exception {
|
31
|
ResultSet rs = sqlDAO.executePreparedQuery(queryGenerator.generateSelectDMFByIdQuery(id));
|
32
|
return rs.getString("xml");
|
33
|
}
|
34
|
|
35
|
public Result fetchResultByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException {
|
36
|
return DMFParser.dmf2Result(dmf);
|
37
|
}
|
38
|
|
39
|
public Result fetchResultById(String id) throws Exception {
|
40
|
return ParsingClaimUtils.getResultFromDMF(fetchDMFResult(id));
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
*
|
45
|
* @param dmf
|
46
|
* @return DOI from DMF - a *valid* one
|
47
|
* @throws IOException
|
48
|
* @throws SAXException
|
49
|
* @throws ParserConfigurationException
|
50
|
* @throws XPathExpressionException
|
51
|
*/
|
52
|
public String fetchDoiByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
53
|
|
54
|
return checkDoiValidity(DMFParser.getDOIIdentifierFromDMF(dmf));
|
55
|
}
|
56
|
public String fetchOrcidWorkByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
57
|
return DMFParser.getOrcidworkIdentifierFromDMF(dmf);
|
58
|
}
|
59
|
public String fetchEmbargoEndDateByDMF(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
60
|
return DMFParser.getEmbargoEndDateFromDMF(dmf);
|
61
|
}
|
62
|
public String fetchAccessRights(String dmf) throws IOException, SAXException, ParserConfigurationException, XPathExpressionException {
|
63
|
return DMFParser.getAccessRightsFromDMF(dmf);
|
64
|
}
|
65
|
|
66
|
/**
|
67
|
*
|
68
|
*
|
69
|
* @param doi
|
70
|
* @return DOI String : the given one if it's valid
|
71
|
* @throws Exception
|
72
|
*/
|
73
|
private static String checkDoiValidity(String doi) {
|
74
|
if(doi==null){
|
75
|
return null;
|
76
|
}
|
77
|
try{
|
78
|
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("invalid_dois.txt", true)));
|
79
|
boolean isValid= SearchUtils.isDoiValid(doi);
|
80
|
if(!isValid&&(doi.length() - (doi.replace("_", "")).length() > 1)){
|
81
|
String transformDoi = doi.replace("_", "-");
|
82
|
isValid=SearchUtils.isDoiValid(transformDoi);
|
83
|
if(isValid){
|
84
|
Date date= new java.util.Date();
|
85
|
out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with : "+transformDoi);
|
86
|
doi=transformDoi;
|
87
|
}
|
88
|
}else if(!isValid&&(doi.length() - (doi.replace("_", "")).length() == 1)) {
|
89
|
String transformDoi = doi.replace("_", "-");
|
90
|
isValid=SearchUtils.isDoiValid(transformDoi);
|
91
|
if(isValid){
|
92
|
Date date= new java.util.Date();
|
93
|
out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with : "+transformDoi);
|
94
|
doi = transformDoi;
|
95
|
}
|
96
|
}else if(!isValid) {
|
97
|
String transformDoi =doi.replaceAll("\\p{C}", "");
|
98
|
isValid=SearchUtils.isDoiValid(transformDoi);
|
99
|
if(isValid){
|
100
|
Date date= new java.util.Date();
|
101
|
out.println(new Timestamp(date.getTime())+" - Invalid doi: "+doi+" replaced with (zero-width-char): "+transformDoi);
|
102
|
doi = transformDoi;
|
103
|
}
|
104
|
}
|
105
|
out.close();
|
106
|
}catch (IOException e) {
|
107
|
e.printStackTrace();
|
108
|
System.err.println("Couldn't write to file " + "invalid_dois.txt");
|
109
|
}
|
110
|
return doi;
|
111
|
}
|
112
|
}
|