1
|
package eu.dnetlib.data.claimsDemo;
|
2
|
|
3
|
import eu.dnetlib.data.claims.migration.Claim;
|
4
|
import eu.dnetlib.data.claims.migration.Context;
|
5
|
import eu.dnetlib.data.claims.migration.Project;
|
6
|
import eu.dnetlib.data.claims.migration.Result;
|
7
|
import org.xml.sax.SAXException;
|
8
|
|
9
|
import javax.xml.parsers.ParserConfigurationException;
|
10
|
import java.io.BufferedWriter;
|
11
|
import java.io.FileWriter;
|
12
|
import java.io.IOException;
|
13
|
import java.io.PrintWriter;
|
14
|
import java.sql.ResultSet;
|
15
|
import java.sql.SQLException;
|
16
|
import java.sql.Timestamp;
|
17
|
import java.util.Date;
|
18
|
|
19
|
/**
|
20
|
* Created by argirok on 24/11/2015.
|
21
|
*/
|
22
|
/*
|
23
|
Build operations is the class that is responsible for building of a claim Object
|
24
|
given a tupple (rs) from the claim db.
|
25
|
*/
|
26
|
public class ClaimBuilder {
|
27
|
private SqlDAO sqlDAO;
|
28
|
private QueryGenerator queryGenerator;
|
29
|
|
30
|
public ClaimBuilder() {
|
31
|
|
32
|
}
|
33
|
|
34
|
public SqlDAO getSqlDAO() {
|
35
|
return sqlDAO;
|
36
|
}
|
37
|
|
38
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
39
|
this.sqlDAO = sqlDAO;
|
40
|
}
|
41
|
|
42
|
|
43
|
public Result getDMFClaim(ResultSet rs) throws Exception {
|
44
|
String xml = rs.getString("xml");
|
45
|
Result r = ParsingClaimUtils.getResultFromDMF(xml);
|
46
|
return r;
|
47
|
}
|
48
|
public Claim getConceptDMFClaim(ResultSet rs) throws Exception {
|
49
|
Claim claim = new Claim();
|
50
|
claim.setId(rs.getString("id"));
|
51
|
claim.setDate(rs.getDate("date"));
|
52
|
claim.setUserMail(rs.getString("agent"));
|
53
|
String xml = rs.getString("xml");
|
54
|
claim = ParsingClaimUtils.getClaimFromConceptDMF(claim, xml);
|
55
|
claim.setTargetType(((Result) claim.getTarget()).getResultType());
|
56
|
claim.setSourceType(ClaimUtils.CONTEXT);
|
57
|
claim=this.enrichClaimFromDMFAndExternalSources(claim);
|
58
|
return claim;
|
59
|
}
|
60
|
|
61
|
public Claim getRelationClaim(ResultSet rs) throws SQLException, IOException, SAXException, ParserConfigurationException {
|
62
|
Claim claim = new Claim();
|
63
|
claim.setId(rs.getString("id"));
|
64
|
claim.setDate(rs.getDate("date"));
|
65
|
claim.setUserMail(rs.getString("agent"));
|
66
|
claim=ParsingClaimUtils.getRelationClaim(claim,rs.getString("xml"));
|
67
|
claim=this.enrichClaimFromDMFAndExternalSources(claim);
|
68
|
return claim;
|
69
|
}
|
70
|
public String getQueryInsertClaim(Claim claim) {
|
71
|
if (claim.getSourceType().equals(ClaimUtils.CONTEXT)) {
|
72
|
return queryGenerator.generateInsertClaimContextQuery((Result)claim.getTarget(), (Context) claim.getSource(), claim);
|
73
|
} else if (claim.getSourceType().equals(ClaimUtils.PROJECT)) {
|
74
|
return queryGenerator.generateInsertClaimProjectQuery((Result)claim.getTarget(), (Project) claim.getSource(), claim);
|
75
|
} else{
|
76
|
return queryGenerator.generateInsertClaimResultQuery((Result)claim.getTarget(), (Result) claim.getSource(), claim);
|
77
|
}
|
78
|
}
|
79
|
@Deprecated
|
80
|
private Claim enrichClaimFromDMF(Claim claim){
|
81
|
if((claim.getSourceType()==null)||(claim.getSourceType().equals(ClaimUtils.PUBLICATION)||claim.getSourceType().equals(ClaimUtils.DATASET))){
|
82
|
Result source=(Result)claim.getSource();
|
83
|
claim.setSource(enrichResultFromDMF(claim.getId(), source));
|
84
|
claim.setSourceType(((Result) claim.getSource()).getResultType());
|
85
|
}
|
86
|
if(claim.getTargetType()==null||(claim.getTargetType().equals(ClaimUtils.PUBLICATION)||claim.getTargetType().equals(ClaimUtils.DATASET))){
|
87
|
Result target=(Result)claim.getTarget();
|
88
|
claim.setTarget(enrichResultFromDMF(claim.getId(),target));
|
89
|
claim.setTargetType(((Result) claim.getTarget()).getResultType());
|
90
|
}
|
91
|
return claim;
|
92
|
}
|
93
|
private Claim enrichClaimFromDMFAndExternalSources(Claim claim){
|
94
|
if((claim.getSourceType()==null)||(claim.getSourceType().equals(ClaimUtils.PUBLICATION)||claim.getSourceType().equals(ClaimUtils.DATASET))){
|
95
|
claim.setSource(enrichResultFromDMF(claim.getId(), (Result)claim.getSource()));
|
96
|
if(((Result)claim.getSource()).getOpenaireId().contains("datacite")){
|
97
|
claim.setSource(ParsingClaimUtils.getXmlfromDatacite(((Result)claim.getSource())));
|
98
|
}else if(((Result)claim.getSource()).getOpenaireId().contains("crossref")){
|
99
|
claim.setSource(ParsingClaimUtils.getJsonfromCrossref((Result) claim.getSource()));
|
100
|
}
|
101
|
claim.setSourceType(((Result) claim.getSource()).getResultType());
|
102
|
}
|
103
|
if(claim.getTargetType()==null||(claim.getTargetType().equals(ClaimUtils.PUBLICATION)||claim.getTargetType().equals(ClaimUtils.DATASET))){
|
104
|
//Result target=(Result)claim.getTarget();
|
105
|
claim.setTarget(enrichResultFromDMF(claim.getId(),(Result)claim.getTarget()));
|
106
|
if(((Result) claim.getTarget()).getOpenaireId().contains("datacite")){
|
107
|
claim.setTarget(ParsingClaimUtils.getXmlfromDatacite((Result) claim.getTarget()));
|
108
|
}else if(((Result) claim.getTarget()).getOpenaireId().contains("crossref")){
|
109
|
|
110
|
claim.setTarget(ParsingClaimUtils.getJsonfromCrossref((Result) claim.getTarget()));
|
111
|
}
|
112
|
claim.setTargetType(((Result) claim.getTarget()).getResultType());
|
113
|
}
|
114
|
return claim;
|
115
|
}
|
116
|
private Result enrichResultFromDMF(String claimId, Result r){
|
117
|
try {
|
118
|
/*if(r.getCollectedFrom().contains("::openaire")){
|
119
|
return r;
|
120
|
}*/
|
121
|
r=checkForDOI(r);
|
122
|
Result dmfResult=this.getDMFClaimById(r.getOpenaireId());
|
123
|
if(dmfResult!=null){
|
124
|
if(r.getResultType()==null){
|
125
|
r.setResultType(dmfResult.getResultType());
|
126
|
}
|
127
|
if(r.getTitle()==null){
|
128
|
r.setTitle(dmfResult.getTitle());
|
129
|
}
|
130
|
if(r.getDoi()==null){
|
131
|
r.setDoi(dmfResult.getDoi());
|
132
|
}
|
133
|
if(r.getAccessRights()==null){
|
134
|
r.setAccessRights(dmfResult.getAccessRights());
|
135
|
}
|
136
|
if(r.getEmbargoEndDate()==null){
|
137
|
r.setEmbargoEndDate(dmfResult.getEmbargoEndDate());
|
138
|
}
|
139
|
if(r.getExternal_url()==null){
|
140
|
r.setExternal_url(dmfResult.getExternal_url());
|
141
|
}
|
142
|
|
143
|
//we don't need the dmf
|
144
|
// //TODO check when we should keep dmf in xml
|
145
|
// if(!r.isFound()) {
|
146
|
// if(r.getCollectedFrom()==null){
|
147
|
// r.setCollectedFrom(dmfResult.getCollectedFrom());
|
148
|
// }
|
149
|
// r.setXml(dmfResult.getXml());
|
150
|
// }
|
151
|
|
152
|
try{
|
153
|
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("enriched_results.txt", true)));
|
154
|
Date date= new java.util.Date();
|
155
|
out.println(new Timestamp(date.getTime())+" - In claim - "+claimId+" result - "+r.getOpenaireId()+" enriched from dmf");
|
156
|
out.close();
|
157
|
}catch (IOException e) {
|
158
|
e.printStackTrace();
|
159
|
System.err.println("Couldn't write to file " + "enriched_results.txt");
|
160
|
}
|
161
|
}
|
162
|
r=checkForDOI(r);
|
163
|
//if type is still null => decide on provider
|
164
|
if(r.getResultType()==null){
|
165
|
if(r.getCollectedFrom()!=null) {
|
166
|
if ((r.getCollectedFrom().contains("crossref"))||( r.getCollectedFrom().contains("orcid") )) {
|
167
|
r.setResultType(ClaimUtils.PUBLICATION);
|
168
|
} else if (r.getCollectedFrom().contains("datacite") ) {
|
169
|
r.setResultType(ClaimUtils.DATASET);
|
170
|
}else{
|
171
|
r.setResultType("unknown");
|
172
|
}
|
173
|
}else{
|
174
|
if (( r.getOpenaireId().contains("crossref") )||( r.getOpenaireId().contains("orcid") )) {
|
175
|
r.setResultType(ClaimUtils.PUBLICATION);
|
176
|
} else if ( r.getOpenaireId().contains("datacite") ) {
|
177
|
r.setResultType(ClaimUtils.DATASET);
|
178
|
}else{
|
179
|
r.setResultType("unknown");
|
180
|
}
|
181
|
}
|
182
|
}
|
183
|
return r;
|
184
|
} catch (Exception e) {
|
185
|
e.printStackTrace();
|
186
|
return r;
|
187
|
}
|
188
|
}
|
189
|
/*
|
190
|
Checks if the doi is valid. If it is not then replaces the DOI with the valid one.
|
191
|
*/
|
192
|
private Result checkForDOI(Result r) throws Exception {
|
193
|
if(r.getDoi()==null){
|
194
|
return r;
|
195
|
}
|
196
|
SearchUtils s= new SearchUtils();
|
197
|
String validDOI=s.isValidDoi(r.getDoi());
|
198
|
if(!validDOI.equals(r.getDoi())){
|
199
|
if(r.getXml()!=null) {
|
200
|
r.setXml(r.getXml().replace(r.getDoi(), validDOI));
|
201
|
}
|
202
|
if(r.getExternal_url()!=null){
|
203
|
r.setExternal_url(r.getExternal_url().replace(r.getDoi(), validDOI));
|
204
|
}
|
205
|
r.setDoi(validDOI);
|
206
|
}
|
207
|
return r;
|
208
|
}
|
209
|
public Result getDMFClaimById(String resultId) throws Exception {
|
210
|
ResultSet rs=sqlDAO.executePreparedQuery(queryGenerator.generateSelectDMFById((resultId)));
|
211
|
Result r =null;
|
212
|
if(rs.next()) {
|
213
|
r = this.getDMFClaim(rs);
|
214
|
}
|
215
|
return r;
|
216
|
}
|
217
|
}
|