1
|
package eu.dnetlib.data.claims.handler;
|
2
|
|
3
|
import eu.dnetlib.data.claims.utils.ClaimValidation;
|
4
|
import eu.dnetlib.data.claims.utils.ClaimValidationException;
|
5
|
import eu.dnetlib.data.claims.entity.Claim;
|
6
|
import eu.dnetlib.data.claims.entity.OpenaireEntity;
|
7
|
import eu.dnetlib.data.claims.entity.Project;
|
8
|
import eu.dnetlib.data.claims.entity.Result;
|
9
|
import eu.dnetlib.data.claims.utils.ClaimUtils;
|
10
|
import eu.dnetlib.data.claims.utils.ContextUtils;
|
11
|
import eu.dnetlib.data.claims.utils.QueryGenerator;
|
12
|
import eu.dnetlib.data.claims.sql.SQLStoreException;
|
13
|
import eu.dnetlib.data.claims.sql.SqlDAO;
|
14
|
import org.apache.log4j.Logger;
|
15
|
|
16
|
import java.sql.ResultSet;
|
17
|
import java.sql.SQLException;
|
18
|
import java.util.ArrayList;
|
19
|
import java.util.Date;
|
20
|
import java.util.List;
|
21
|
|
22
|
/**
|
23
|
* Created by kiatrop on 5/2/2016.
|
24
|
*/
|
25
|
public class ClaimHandler {
|
26
|
|
27
|
SqlDAO sqlDAO = null;
|
28
|
QueryGenerator queryGenerator = null;
|
29
|
ResultHandler resultHandler = null;
|
30
|
ProjectHandler projectHandler = null;
|
31
|
ClaimValidation claimValidation = null;
|
32
|
Boolean useProductionIndex = null;
|
33
|
|
34
|
private static final Logger logger = Logger.getLogger(ClaimHandler.class);
|
35
|
|
36
|
/**
|
37
|
* Saves a list of claims in DB
|
38
|
* @param claims
|
39
|
*/
|
40
|
public void saveClaims(List<Claim> claims){
|
41
|
for(Claim claim:claims){
|
42
|
try {
|
43
|
saveClaim(claim);
|
44
|
} catch (SQLStoreException e) {
|
45
|
logger.error("Claim couldn't be saved : " + claim.getId() + "\n" + claim.toString(), e);
|
46
|
} catch (SQLException e) {
|
47
|
logger.error("Claim couldn't be saved : " + claim.getId() + "\n" + claim.toString(), e);
|
48
|
}
|
49
|
}
|
50
|
}
|
51
|
public String generateSaveQueryForClaim(Claim claim, ArrayList<Object> params){
|
52
|
|
53
|
String query= null;
|
54
|
query = queryGenerator.generateInsertFullClaimQuery(claim, params);
|
55
|
return query;
|
56
|
}
|
57
|
/**
|
58
|
* saves a claim in DB
|
59
|
* @param claim
|
60
|
* @throws Exception
|
61
|
*/
|
62
|
//TODO make private - used in Migration
|
63
|
public String saveClaim(Claim claim) throws SQLStoreException, SQLException {
|
64
|
logger.info("Saving claim...");
|
65
|
String id = null;
|
66
|
ArrayList<Object> params = new ArrayList<>();
|
67
|
String query = generateSaveQueryForClaim(claim, params);
|
68
|
ResultSet rs=sqlDAO.executePreparedQuery(query, params);
|
69
|
if(rs.next()) {
|
70
|
id = rs.getString(1);
|
71
|
}
|
72
|
rs.close();
|
73
|
return id;
|
74
|
}
|
75
|
// - used in Migration
|
76
|
public void saveOrphanClaimId(String id) throws SQLStoreException, Exception {
|
77
|
logger.info("Saving orphan claim id...");
|
78
|
|
79
|
String query = queryGenerator.generateInsertQueryForClaimsOrphanIds(id);
|
80
|
sqlDAO.executeUpdateQuery(query);
|
81
|
}
|
82
|
|
83
|
/**
|
84
|
* Gets the main information for a claim relation - builds a claim object
|
85
|
* exports metadata
|
86
|
* and saves the claim relations in the DB
|
87
|
* @param user
|
88
|
* @param sourceType
|
89
|
* @param sourceId
|
90
|
* @param sourceCollectedFrom
|
91
|
* @param targetType
|
92
|
* @param targetId
|
93
|
* @param targetCollectedFrom
|
94
|
* @throws Exception
|
95
|
*/
|
96
|
public String buildAndInsertClaim(String user, String sourceType, String sourceId, String sourceCollectedFrom, String sourceAccessRights, String sourceEmbargoDate, String targetType, String targetId, String targetCollectedFrom, String targetAccessRights, String targetEmbargoDate) throws Exception, SQLStoreException {
|
97
|
String id = null;
|
98
|
|
99
|
claimValidation.validateCollectedFrom(targetCollectedFrom);
|
100
|
claimValidation.validateCollectedFrom(sourceCollectedFrom);
|
101
|
logger.info("Trying to create a claim {user:"+user+", sourceType:"+sourceType+", sourceId:"+sourceId+", sourceCollectedFrom:"+sourceCollectedFrom+", sourceAccessRights:"+sourceAccessRights
|
102
|
+", sourceEmbargoDate:"+sourceEmbargoDate +", targetType:"+targetType +", targetId:"+targetId +", targetCollectedFrom:"+targetCollectedFrom +", targetAccessRights:"+targetAccessRights
|
103
|
+", targetEmbargoDate:"+targetEmbargoDate+"}");
|
104
|
Claim claim = buildClaim(user,sourceType,sourceId,sourceCollectedFrom, sourceAccessRights, sourceEmbargoDate, targetType, targetId, targetCollectedFrom, targetAccessRights, targetEmbargoDate);
|
105
|
if(claimValidation.validateClaim(claim)) {
|
106
|
claim = exportMedatataForClaim(claim);
|
107
|
id = saveClaim(claim);
|
108
|
}else{
|
109
|
logger.info("claim is not valid");
|
110
|
}
|
111
|
return id;
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* Gets the main information of a relation claim and
|
116
|
* builds a claim object
|
117
|
* @param user
|
118
|
* @param sourceType publication, dataset, project, context
|
119
|
* @param sourceId openaireId, doi or orcid workid
|
120
|
* @param sourceCollectedFrom only for source with type publication, dataset
|
121
|
* @param targetType publication, dataset, project, context
|
122
|
* @param targetId openaireId, doi or orcid workid
|
123
|
* @param targetCollectedFrom only for target with type publication, dataset
|
124
|
* @return a claim object
|
125
|
* @throws Exception
|
126
|
*/
|
127
|
public Claim buildClaim(String user, String sourceType,String sourceId,String sourceCollectedFrom, String sourceAccessRights, String sourceEmbargoDate, String targetType,String targetId,String targetCollectedFrom, String targetAccessRights, String targetEmbargoDate ) throws Exception {
|
128
|
Claim claim = new Claim();
|
129
|
claim.setUserMail(user);
|
130
|
claim.setDate(new Date());
|
131
|
//date
|
132
|
claim.setSourceType(sourceType);
|
133
|
claim.setTargetType(targetType);
|
134
|
claim.setSource(buildOpenaireEntity(sourceId,sourceType,sourceCollectedFrom,sourceAccessRights, sourceEmbargoDate));
|
135
|
claim.setTarget(buildOpenaireEntity(targetId,targetType,targetCollectedFrom, targetAccessRights, targetEmbargoDate));
|
136
|
|
137
|
if (claim.getSource() == null) {
|
138
|
throw new ClaimValidationException("The source id is invalid.");
|
139
|
}
|
140
|
|
141
|
if (claim.getTarget() == null) {
|
142
|
throw new ClaimValidationException("The target id is invalid.");
|
143
|
}
|
144
|
//TODO from external sources need an OPenaireID
|
145
|
return claim;
|
146
|
|
147
|
}
|
148
|
|
149
|
public boolean updateClaimCurationInfo(String curatedBy, String claimId, boolean approved) throws SQLStoreException, SQLException, Exception {
|
150
|
logger.info("Updating claim curation info...");
|
151
|
ArrayList<Object> params = new ArrayList<>();
|
152
|
String query = queryGenerator.generateUpdateClaimCuration(curatedBy, claimId, approved, params);
|
153
|
//ResultSet rs = sqlDAO.executeUpdateQuery(query, params);
|
154
|
return sqlDAO.executeUpdateQuery(query, params);
|
155
|
|
156
|
/*
|
157
|
boolean success = false;
|
158
|
while (rs.next())
|
159
|
{
|
160
|
success = true;
|
161
|
}
|
162
|
rs.close();
|
163
|
return success;
|
164
|
*/
|
165
|
}
|
166
|
|
167
|
|
168
|
/**
|
169
|
* Gets the main information of an openaire entityinfrastruct_::openaire
|
170
|
* and returns a full object
|
171
|
* @param id
|
172
|
* @param type
|
173
|
* @param collectedFrom
|
174
|
* @return openaire Entity (Context, Result, Project)
|
175
|
* @throws Exception
|
176
|
*/
|
177
|
public OpenaireEntity buildOpenaireEntity(String id, String type, String collectedFrom, String accessRights, String embargoDate) throws Exception {
|
178
|
if(type==null){
|
179
|
return null;
|
180
|
}
|
181
|
else if(type.equals(ClaimUtils.CONTEXT)){
|
182
|
return ContextUtils.fetchContextById(id, useProductionIndex);
|
183
|
}else if (type.equals(ClaimUtils.PROJECT)){
|
184
|
Project project = projectHandler.fetchProjectByID(id, false);
|
185
|
if(project == null){
|
186
|
project = projectHandler.fetchProjectByID(id, true);
|
187
|
}
|
188
|
if(project == null){
|
189
|
logger.error("Project with id:"+id + " couldn't be fetched.");
|
190
|
}
|
191
|
|
192
|
return project;
|
193
|
}else if (type.equals(ClaimUtils.PUBLICATION)||type.equals(ClaimUtils.DATASET)||type.equals(ClaimUtils.SOFTWARE) ||type.equals(ClaimUtils.OTHER) ){
|
194
|
ExternalRecordHandler externalRecordHandler = new ExternalRecordHandler();
|
195
|
if(collectedFrom == null){
|
196
|
return null;
|
197
|
}else if(collectedFrom.equals(ClaimUtils.CROSSREF)){
|
198
|
|
199
|
Result result = externalRecordHandler.fetchResultfromCrossref(id);
|
200
|
if(result == null){
|
201
|
logger.error("Record with id: " + id + " couldn't be fetched from crossref.");
|
202
|
}
|
203
|
|
204
|
result.setAccessRights(accessRights);
|
205
|
result.setEmbargoEndDate(embargoDate);
|
206
|
return result;
|
207
|
}else if (collectedFrom.equals(ClaimUtils.DATACITE)){
|
208
|
Result result = externalRecordHandler.fetchResultfromDatacite(id);
|
209
|
if(result == null){
|
210
|
logger.error("Record with id:"+id + " couldn't be fetched from datacite.");
|
211
|
}else {
|
212
|
result.setAccessRights(accessRights);
|
213
|
result.setEmbargoEndDate(embargoDate);
|
214
|
}
|
215
|
return result;
|
216
|
}else if (collectedFrom.equals(ClaimUtils.ORCID)){
|
217
|
Result result = externalRecordHandler.fetchResultfromOrcid(id);
|
218
|
if(result == null){
|
219
|
logger.error("Record with id:"+id + " couldn't be fetched from ORCID.");
|
220
|
}else {
|
221
|
result.setAccessRights(accessRights);
|
222
|
result.setEmbargoEndDate(embargoDate);
|
223
|
}
|
224
|
return result;
|
225
|
}else if (collectedFrom.equals(ClaimUtils.OPENAIRE)){
|
226
|
IndexResultHandler indexResultHandler = new IndexResultHandler();
|
227
|
Result result = null;
|
228
|
if(type.equals(ClaimUtils.PUBLICATION)){
|
229
|
result = indexResultHandler.fetchPublicationById(id,useProductionIndex);
|
230
|
|
231
|
}else if(type.equals(ClaimUtils.DATASET)){
|
232
|
result = indexResultHandler.fetchDatasetById(id,useProductionIndex);
|
233
|
}else if(type.equals(ClaimUtils.SOFTWARE)){
|
234
|
result = indexResultHandler.fetchSoftwareById(id,useProductionIndex);
|
235
|
}else if(type.equals(ClaimUtils.OTHER)){
|
236
|
result = indexResultHandler.fetchOtherById(id,useProductionIndex);
|
237
|
}
|
238
|
if(result == null){
|
239
|
logger.error("Record with id:"+id + " and type " + type + " couldn't be fetched from openaire.");
|
240
|
}
|
241
|
return result;
|
242
|
}
|
243
|
}
|
244
|
return null;
|
245
|
}
|
246
|
|
247
|
|
248
|
/**
|
249
|
* Exports the metadata for the source and the target of a claim and sets the proper field
|
250
|
* @param claim
|
251
|
* @return claim with the proper metadata path of the result of source and target
|
252
|
*/
|
253
|
public Claim exportMedatataForClaim(Claim claim){
|
254
|
if(claim.getTargetType().equals(ClaimUtils.DATASET)||claim.getTargetType().equals(ClaimUtils.PUBLICATION)){
|
255
|
String path = resultHandler.exportMetadataFileForResult((Result)claim.getTarget());
|
256
|
((Result) claim.getTarget()).setRecordPath(path);
|
257
|
}
|
258
|
if(claim.getSourceType().equals(ClaimUtils.DATASET)||claim.getSourceType().equals(ClaimUtils.PUBLICATION)){
|
259
|
String path = resultHandler.exportMetadataFileForResult((Result)claim.getSource());
|
260
|
((Result) claim.getSource()).setRecordPath(path);
|
261
|
}
|
262
|
return claim;
|
263
|
|
264
|
}
|
265
|
|
266
|
public void deleteClaim(String user, String claimId) throws SQLStoreException, Exception {
|
267
|
|
268
|
ArrayList<Object> params = new ArrayList<>();
|
269
|
String query = queryGenerator.generateSelectClaimQuery(claimId,user, params);
|
270
|
ResultSet rs = sqlDAO.executePreparedQuery(query, params);
|
271
|
if(rs.next()) {
|
272
|
String sourceType =rs.getString(2);
|
273
|
String sourceId =rs.getString(3);
|
274
|
String targetType =rs.getString(4);
|
275
|
String targetId =rs.getString(5);
|
276
|
this.checkRecordFile(sourceType, sourceId);
|
277
|
this.checkRecordFile(targetType, targetId);
|
278
|
|
279
|
ArrayList<Object> params2 = new ArrayList<>();
|
280
|
String query2 = queryGenerator.generateDeleteFullClaimQuery(claimId,user,sourceType,sourceId,targetType,targetId, params2);
|
281
|
sqlDAO.executeUpdateQuery(query2, params2);
|
282
|
}else{
|
283
|
logger.error("Claim with id : "+ claimId+" user: "+user+" couldn't be deleted." );
|
284
|
}
|
285
|
rs.close();
|
286
|
}
|
287
|
|
288
|
public void checkRecordFile(String type, String id) throws SQLStoreException, SQLException {
|
289
|
if(type.equals("publication") || type.equals("dataset")|| type.equals("other")|| type.equals("software")){
|
290
|
this.resultHandler.deleteRecordFile(type,id);
|
291
|
}
|
292
|
}
|
293
|
public boolean deleteClaim(String claimId) throws Exception, SQLStoreException {
|
294
|
|
295
|
ArrayList<Object> params = new ArrayList<>();
|
296
|
String query = queryGenerator.generateSelectClaimQuery(claimId, params);
|
297
|
ResultSet rs = sqlDAO.executePreparedQuery(query, params);
|
298
|
if(rs.next()) {
|
299
|
String sourceType =rs.getString(2);
|
300
|
String sourceId =rs.getString(3);
|
301
|
String targetType =rs.getString(4);
|
302
|
String targetId =rs.getString(5);
|
303
|
String user =rs.getString(6);
|
304
|
this.checkRecordFile(sourceType, sourceId);
|
305
|
this.checkRecordFile(targetType, targetId);
|
306
|
ArrayList<Object> params2 = new ArrayList<>();
|
307
|
String query2 = queryGenerator.generateDeleteFullClaimQuery(claimId,user,sourceType,sourceId,targetType,targetId, params2);
|
308
|
sqlDAO.executeUpdateQuery(query2, params2);
|
309
|
rs.close();
|
310
|
return true;
|
311
|
}
|
312
|
rs.close();
|
313
|
logger.error("Couldn't delete claim with id : " + claimId +". It doesn't not exist.");
|
314
|
return false;
|
315
|
|
316
|
}
|
317
|
|
318
|
public SqlDAO getSqlDAO() {
|
319
|
return sqlDAO;
|
320
|
}
|
321
|
|
322
|
public void setSqlDAO(SqlDAO sqlDAO) {
|
323
|
this.sqlDAO = sqlDAO;
|
324
|
}
|
325
|
|
326
|
public QueryGenerator getQueryGenerator() {
|
327
|
return queryGenerator;
|
328
|
}
|
329
|
|
330
|
public void setQueryGenerator(QueryGenerator queryGenerator) {
|
331
|
this.queryGenerator = queryGenerator;
|
332
|
}
|
333
|
|
334
|
public ResultHandler getResultHandler() {
|
335
|
return resultHandler;
|
336
|
}
|
337
|
|
338
|
public void setResultHandler(ResultHandler resultHandler) {
|
339
|
this.resultHandler = resultHandler;
|
340
|
}
|
341
|
|
342
|
public ProjectHandler getProjectHandler() {
|
343
|
return projectHandler;
|
344
|
}
|
345
|
|
346
|
public void setProjectHandler(ProjectHandler projectHandler) {
|
347
|
this.projectHandler = projectHandler;
|
348
|
}
|
349
|
|
350
|
public ClaimValidation getClaimValidation() {
|
351
|
return claimValidation;
|
352
|
}
|
353
|
|
354
|
public void setClaimValidation(ClaimValidation claimValidation) {
|
355
|
this.claimValidation = claimValidation;
|
356
|
}
|
357
|
|
358
|
public Boolean getUseProductionIndex() {
|
359
|
return useProductionIndex;
|
360
|
}
|
361
|
|
362
|
public void setUseProductionIndex(Boolean useProductionIndex) {
|
363
|
this.useProductionIndex = useProductionIndex;
|
364
|
}
|
365
|
}
|