1
|
package eu.dnetlib.data.claimsDemo;
|
2
|
|
3
|
//import eu.dnetlib.data.claims.migration.Claim;
|
4
|
import eu.dnetlib.data.claims.migration.*;
|
5
|
import org.xml.sax.SAXException;
|
6
|
|
7
|
import javax.xml.parsers.ParserConfigurationException;
|
8
|
import java.io.BufferedWriter;
|
9
|
import java.io.FileWriter;
|
10
|
import java.io.IOException;
|
11
|
import java.io.PrintWriter;
|
12
|
import java.sql.ResultSet;
|
13
|
import java.sql.SQLException;
|
14
|
import java.sql.Timestamp;
|
15
|
import java.text.ParseException;
|
16
|
import java.text.SimpleDateFormat;
|
17
|
import java.util.Date;
|
18
|
|
19
|
/**
|
20
|
* Created by argirok on 24/11/2015.
|
21
|
*/
|
22
|
public class ClaimOperations {
|
23
|
private SqlDAO sqlDAO;
|
24
|
public ClaimOperations() {
|
25
|
|
26
|
}
|
27
|
|
28
|
public SqlDAO getSqlDAO() {
|
29
|
return sqlDAO;
|
30
|
}
|
31
|
|
32
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
33
|
this.sqlDAO = sqlDAO;
|
34
|
}
|
35
|
|
36
|
|
37
|
public Result getDMFClaim(ResultSet rs) throws Exception {
|
38
|
String xml = rs.getString("xml");
|
39
|
Result r = ParsingClaimUtils.getResultFromDMF(xml);
|
40
|
return r;
|
41
|
}
|
42
|
public Claim getConceptDMFClaim(ResultSet rs) throws Exception {
|
43
|
Claim claim = new Claim();
|
44
|
claim.setId(rs.getString("id"));
|
45
|
claim.setDate(rs.getDate("date"));
|
46
|
claim.setUserMail(rs.getString("agent"));
|
47
|
String xml = rs.getString("xml");
|
48
|
claim = ParsingClaimUtils.getClaimFromConceptDMF(claim, xml);
|
49
|
claim.setTargetType(((Result) claim.getTarget()).getResultType());
|
50
|
claim.setSourceType("context");
|
51
|
claim=this.enrichClaimFromDMF(claim);
|
52
|
return claim;
|
53
|
}
|
54
|
|
55
|
public Claim getRelationClaim(ResultSet rs) throws SQLException, IOException, SAXException, ParserConfigurationException {
|
56
|
Claim claim = new Claim();
|
57
|
claim.setId(rs.getString("id"));
|
58
|
SimpleDateFormat format = new SimpleDateFormat("YYY-MM-dd HH:mm:ss.SSS");
|
59
|
String dateStr = rs.getString("date");
|
60
|
Date date = null;
|
61
|
try {
|
62
|
date = format.parse(dateStr);
|
63
|
claim.setDate(date);
|
64
|
} catch (ParseException e) {
|
65
|
e.printStackTrace();
|
66
|
claim.setDate(rs.getDate("date"));
|
67
|
}
|
68
|
|
69
|
claim.setUserMail(rs.getString("agent"));
|
70
|
claim=ParsingClaimUtils.getRelationClaim(claim,rs.getString("xml"));
|
71
|
claim=this.enrichClaimFromDMF(claim);
|
72
|
return claim;
|
73
|
}
|
74
|
public String getQueryInsertClaim(Claim claim) {
|
75
|
if (claim.getSourceType().equals("context")) {
|
76
|
return sqlDAO.getQueryGenerator().insertClaimhasContext( (Result)claim.getTarget(), (Context) claim.getSource(), claim);
|
77
|
} else if (claim.getSourceType().equals("project")) {
|
78
|
return sqlDAO.getQueryGenerator().insertClaimhasProject( (Result)claim.getTarget(), (Project) claim.getSource(), claim);
|
79
|
} else{
|
80
|
return sqlDAO.getQueryGenerator().insertClaimhasResult( (Result)claim.getTarget(), (Result) claim.getSource(), claim);
|
81
|
}
|
82
|
}
|
83
|
|
84
|
private Claim enrichClaimFromDMF(Claim claim){
|
85
|
if((claim.getSourceType()==null)||(claim.getSourceType().equals("publication")||claim.getSourceType().equals("dataset"))){
|
86
|
Result source=(Result)claim.getSource();
|
87
|
claim.setSource(enrichResultFromDMF(claim.getId(), source));
|
88
|
claim.setSourceType(((Result) claim.getSource()).getResultType());
|
89
|
}
|
90
|
if(claim.getTargetType()==null||(claim.getTargetType().equals("publication")||claim.getTargetType().equals("dataset"))){
|
91
|
Result target=(Result)claim.getTarget();
|
92
|
claim.setTarget(enrichResultFromDMF(claim.getId(),target));
|
93
|
claim.setTargetType(((Result) claim.getTarget()).getResultType());
|
94
|
}
|
95
|
return claim;
|
96
|
}
|
97
|
private Result enrichResultFromDMF(String claimId, Result r){
|
98
|
try {
|
99
|
/*if(r.getCollectedFrom().contains("::openaire")){
|
100
|
return r;
|
101
|
}*/
|
102
|
r=checkForDOI(r);
|
103
|
Result dmfResult=this.getDMFClaimById(r.getOpenaireId());
|
104
|
if(dmfResult!=null){
|
105
|
if(r.getResultType()==null){
|
106
|
r.setResultType(dmfResult.getResultType());
|
107
|
}
|
108
|
if(r.getTitle()==null){
|
109
|
r.setTitle(dmfResult.getTitle());
|
110
|
}
|
111
|
if(r.getDoi()==null){
|
112
|
r.setDoi(dmfResult.getDoi());
|
113
|
}
|
114
|
if(r.getCollectedFrom()==null){
|
115
|
r.setCollectedFrom(dmfResult.getCollectedFrom());
|
116
|
}
|
117
|
if(r.getAccessRights()==null){
|
118
|
r.setAccessRights(dmfResult.getAccessRights());
|
119
|
}
|
120
|
if(r.getExternal_url()==null){
|
121
|
r.setExternal_url(dmfResult.getExternal_url());
|
122
|
}
|
123
|
r.setXml(dmfResult.getXml());
|
124
|
try{
|
125
|
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("enriched_results.txt", true)));
|
126
|
Date date= new java.util.Date();
|
127
|
out.println(new Timestamp(date.getTime())+" - In claim - "+claimId+" result - "+r.getOpenaireId()+" enriched from dmf");
|
128
|
out.close();
|
129
|
}catch (IOException e) {
|
130
|
e.printStackTrace();
|
131
|
System.err.println("Couldn't write to file " + "enriched_results.txt");
|
132
|
}
|
133
|
}
|
134
|
r=checkForDOI(r);
|
135
|
//if type is still null => decide on provider
|
136
|
if(r.getResultType()==null){
|
137
|
if(r.getCollectedFrom()!=null) {
|
138
|
if (r.getCollectedFrom().contains("crossref") || r.getCollectedFrom().contains("orcid")) {
|
139
|
r.setResultType("publication");
|
140
|
} else if (r.getCollectedFrom().contains("datacite")) {
|
141
|
r.setResultType("dataset");
|
142
|
}
|
143
|
}else{
|
144
|
r.setResultType("unknown");
|
145
|
}
|
146
|
}
|
147
|
return r;
|
148
|
} catch (Exception e) {
|
149
|
e.printStackTrace();
|
150
|
return r;
|
151
|
}
|
152
|
}
|
153
|
/*
|
154
|
Checks if the doi is valid. If it is not then replaces the DOI with the valid one.
|
155
|
*/
|
156
|
private Result checkForDOI(Result r) throws Exception {
|
157
|
if(r.getDoi()==null){
|
158
|
return r;
|
159
|
}
|
160
|
SearchUtils s= new SearchUtils();
|
161
|
String validDOI=s.isValidDoi(r.getDoi());
|
162
|
if(!validDOI.equals(r.getDoi())){
|
163
|
r.setXml(r.getXml().replace(r.getDoi(),validDOI));
|
164
|
r.setDoi(validDOI);
|
165
|
}
|
166
|
return r;
|
167
|
}
|
168
|
public Result getDMFClaimById(String resultId) throws Exception {
|
169
|
ResultSet rs=sqlDAO.executePreparedQuery(sqlDAO.getQueryGenerator().selectDMFById(resultId));
|
170
|
Result r =null;
|
171
|
if(rs.next()) {
|
172
|
r = this.getDMFClaim(rs);
|
173
|
}
|
174
|
return r;
|
175
|
}
|
176
|
}
|