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