Project

General

Profile

1
package eu.dnetlib.data.claims.migration;
2

    
3
import eu.dnetlib.data.claims.migration.entity.*;
4
import eu.dnetlib.data.claims.migration.handler.*;
5
import eu.dnetlib.data.claims.migration.parser.OafParser;
6
import eu.dnetlib.data.claimsDemo.ClaimUtils;
7
import eu.dnetlib.data.claimsDemo.SQLStoreException;
8
import org.apache.log4j.BasicConfigurator;
9
import org.apache.log4j.Level;
10
import org.apache.log4j.Logger;
11
import org.springframework.context.ApplicationContext;
12
import org.springframework.context.support.ClassPathXmlApplicationContext;
13
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
14
import org.xml.sax.SAXException;
15

    
16
import javax.xml.parsers.ParserConfigurationException;
17
import javax.xml.transform.TransformerException;
18
import javax.xml.xpath.XPathExpressionException;
19
import java.io.*;
20
import java.math.BigInteger;
21
import java.security.MessageDigest;
22
import java.sql.Timestamp;
23
import java.util.ArrayList;
24
import java.util.Date;
25
import java.util.List;
26

    
27
/**
28
 * Created by kiatrop on 4/2/2016.
29
 */
30

    
31
public class Migration {
32
    private static final Logger logger = Logger.getLogger(Migration.class);
33

    
34
    RelationHandler relationHandler = null;
35
    ContextRelationHandler contextRelationHandler = null;
36
    ProjectHandler projectHandler = null;
37
    DMFContextHandler dmfContextHandler = null;
38
    DMFResultHandler dmfResultHandler = null;
39
    IndexResultHandler indexResultHandler = null;
40
    ExternalRecordHandler externalRecordHandler = null;
41
    ResultHandler resultHandler = null;
42

    
43
    ClaimHandler claimHandler = null;
44
    ClaimValidation claimValidation = new ClaimValidation();
45

    
46

    
47
    public Migration() {
48
        ApplicationContext context = new ClassPathXmlApplicationContext("eu/dnetlib/data/claims/migration/springContext-claimsDemo.xml");
49
        relationHandler = context.getBean(RelationHandler.class);
50
        contextRelationHandler = context.getBean(ContextRelationHandler.class);
51
        projectHandler = context.getBean(ProjectHandler.class);
52
        dmfContextHandler = context.getBean(DMFContextHandler.class);
53
        dmfResultHandler = context.getBean(DMFResultHandler.class);
54
        externalRecordHandler = context.getBean(ExternalRecordHandler.class);
55
        indexResultHandler = context.getBean(IndexResultHandler.class);
56
        resultHandler = context.getBean(ResultHandler.class);
57
        claimHandler = context.getBean(ClaimHandler.class);
58
        claimValidation = context.getBean(ClaimValidation.class);
59

    
60
    }
61

    
62

    
63
    public List<Claim> createRelationsClaims() throws Exception, SQLStoreException {
64

    
65

    
66
        List<Relation> relations = relationHandler.fetchAllRelations();
67
        List<Claim> claims = new ArrayList<Claim>();
68
        for (Relation relation : relations) {
69

    
70
            Claim claim= new Claim();
71

    
72
            claim.setUserMail(relation.getClaimedBy());
73
            claim.setDate(relation.getClaimDate());
74
            claim.setId(relation.getClaimId());
75
            System.out.println("Claim id:" + claim.getId());
76
            OpenaireEntity source = null;
77
            //first work with the Source
78
            if (relation.getSourceType().equals(ClaimUtils.PROJECT)) {
79
                source= buildProject(relation.getSourceId(),relation.getClaimId());
80
                claim.setSourceType(ClaimUtils.PROJECT);
81
            } else if (relation.getSourceType().equals(ClaimUtils.CONTEXT)) {
82
                Context context = dmfContextHandler.fetchContextByIdFromDmf(relation.getSourceId(), false);
83
                source = context;
84
                claim.setSourceType(ClaimUtils.CONTEXT);
85
            } else if (relation.getSourceType().equals(ClaimUtils.DATASET) || relation.getSourceType().equals(ClaimUtils.PUBLICATION)) {
86
                Result result = buildResult(relation.getDmf(), relation.getCollectedFrom(), relation.getSourceId(), relation.getClaimId());
87
                source = result;
88
                claim.setSourceType(result.getResultType());
89
            }
90
            claim.setSource(source);
91

    
92
            Result target = null;
93
            if (relation.getTargetType().equals(ClaimUtils.DATASET) || relation.getTargetType().equals(ClaimUtils.PUBLICATION)) {
94
                target = buildResult(relation.getDmf(), relation.getCollectedFrom(), relation.getTargetId(), relation.getClaimId());
95

    
96
            }
97
            if( target != null) {
98
                claim.setTarget(target);
99
                claim.setTargetType(target.getResultType());
100
            }
101
            if(claimValidation.validateClaim(claim)){ // is valid
102
//                System.out.println(claim);
103
                claims.add(claim);
104
            }
105

    
106
        }
107
        return claims;
108

    
109
    }
110
    public void createAndSaveRelationsClaims(boolean save) throws Exception, SQLStoreException {
111
                    /*for stats*/
112
        List<Claim> claimsOp= new ArrayList<Claim>();
113
        List<Claim> claimsCr= new ArrayList<Claim>();
114
        List<Claim> claimsDat= new ArrayList<Claim>();
115
        List<Claim> claimsOrc= new ArrayList<Claim>();
116
        Integer targetOp=0;
117
        Integer targetCr=0;
118
        Integer targetDat=0;
119
        Integer targetOrc=0;
120
        Integer sourceRes=0;
121
        Integer sourceOp=0;
122
        Integer sourceCr=0;
123
        Integer sourceDat=0;
124
        Integer sourceOrc=0;
125
        Integer projects=0;
126
        Integer contexts=0;
127
        Integer totalClaims=0;
128

    
129

    
130
        List<Relation> relations = relationHandler.fetchAllRelations();
131
        List<Claim> claims = new ArrayList<Claim>();
132
        logger.info("Relation size: "+relations.size());
133
        for (Relation relation : relations) {
134
            try {
135
                Claim claim = new Claim();
136

    
137
                claim.setUserMail(relation.getClaimedBy());
138
                claim.setDate(relation.getClaimDate());
139
                claim.setId(relation.getClaimId());
140
                logger.info("Claim id:" + claim.getId()+" "+claim.getDate());
141
                OpenaireEntity source = null;
142
                //first work with the Source
143
                if (relation.getSourceType().equals(ClaimUtils.PROJECT)) {
144
                    source = buildProject(relation.getSourceId(), relation.getClaimId());
145
                    claim.setSourceType(ClaimUtils.PROJECT);
146
                } else if (relation.getSourceType().equals(ClaimUtils.CONTEXT)) {
147
                    Context context = dmfContextHandler.fetchContextByIdFromDmf(relation.getSourceId(), false);
148
                    source = context;
149
                    claim.setSourceType(ClaimUtils.CONTEXT);
150
                } else if (relation.getSourceType().equals(ClaimUtils.DATASET) || relation.getSourceType().equals(ClaimUtils.PUBLICATION)) {
151
                    Result result = buildResult(relation.getDmf(), relation.getCollectedFrom(), relation.getSourceId(), relation.getClaimId());
152
                    source = result;
153
                    claim.setSourceType(result.getResultType());
154
                }
155

    
156
                claim.setSource(source);
157

    
158
                Result target = null;
159
                if (relation.getTargetType().equals(ClaimUtils.DATASET) || relation.getTargetType().equals(ClaimUtils.PUBLICATION)) {
160
                    target = buildResult(relation.getDmf(), relation.getCollectedFrom(), relation.getTargetId(), relation.getClaimId());
161

    
162
                }
163
                if (target != null) {
164
                    claim.setTarget(target);
165
                    claim.setTargetType(target.getResultType());
166
                }
167

    
168
                if (claimValidation.validateClaim(claim)) { // is valid
169
                    if(save) {
170
                        claim = claimHandler.exportMedatataForClaim(claim);
171
                        claimHandler.saveClaim(claim);
172
                    }
173
                    /*for stats*/
174
                    if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
175
                        targetCr++;
176
                    }else if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
177
                        targetDat++;
178
                    }else if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID)){
179
                        targetOrc++;
180
                    }else {
181
                        targetOp++;
182
                    }
183
                    if(claim.getSourceType().equals(ClaimUtils.PUBLICATION)||claim.getSourceType().equals(ClaimUtils.DATASET)){
184
                        sourceRes++;
185
                        if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
186
                            sourceCr++;
187
                        }else if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
188
                            sourceDat++;
189
                        }else if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID)){
190
                            sourceOrc++;
191
                        }else {
192
                            sourceOp++;
193
                        }
194
                    }else if(claim.getSourceType().equals(ClaimUtils.PROJECT)) {
195
                        projects++;
196
                    }else if(claim.getSourceType().equals(ClaimUtils.CONTEXT)){
197
                        contexts++;
198
                    }
199

    
200
                }else{
201
                    claimHandler.saveOrphanClaimId(claim.getId());
202
                }
203
            }catch (Exception e){
204
                e.printStackTrace();
205
                logger.error("Exception: "+e.getMessage());
206

    
207
            }
208
        }
209

    
210
                    /*for stats*/
211
        System.out.println("\n\nTargets:\n\nCrossref : "+targetCr+ " DataCite :"+targetDat+" Orcid : "+targetOrc+" OpenAire : "+targetOp);
212
        System.out.println("\n\nSources:\n\nAll : "+sourceRes+" Crossref : "+sourceCr+ " DataCite :"+sourceDat+" Orcid : "+sourceOrc+" OpenAire : "+sourceOp);
213
        System.out.println("\n\nTotalClaims: "+totalClaims+" Projects: "+projects+ " Contexts :"+contexts);
214

    
215

    
216
}
217
    public List<Claim> createContextRelationClaims() throws Exception, SQLStoreException {
218

    
219

    
220
        List<ContextRelation> relations = contextRelationHandler.fetchAllConceptRelations();
221
        List<Claim> claims = new ArrayList<Claim>();
222
        for (ContextRelation relation : relations) {
223

    
224
            Claim claim= new Claim();
225
            claim.setId(relation.getClaimId());
226
            claim.setUserMail(relation.getClaimedBy());
227
            claim.setDate(relation.getClaimDate());
228
            System.out.println("Claim id:" + claim.getId());
229

    
230
            //first work with the Source
231
            Context context = dmfContextHandler.extractContextFromDMF(relation.getContextDmf(), false);
232
            claim.setSourceType(ClaimUtils.CONTEXT);
233
            claim.setSource(context);
234

    
235
            Result target =  buildResult(relation.getResultDmf(), relation.getCollectedFrom(), relation.getResultId(), relation.getClaimId());
236
            if( target != null){
237
                claim.setTarget(target);
238
                claim.setTargetType(target.getResultType());
239
            }
240
            if(claimValidation.validateClaim(claim)){ // is valid
241
                claims.add(claim);
242
            }
243

    
244
        }
245
        return claims;
246

    
247

    
248
    }
249
    public void createAndSaveContextRelationClaims(Boolean save) throws Exception, SQLStoreException {
250
                    /*for stats*/
251
        List<Claim> claimsOp= new ArrayList<Claim>();
252
        List<Claim> claimsCr= new ArrayList<Claim>();
253
        List<Claim> claimsDat= new ArrayList<Claim>();
254
        List<Claim> claimsOrc= new ArrayList<Claim>();
255
        Integer targetOp=0;
256
        Integer targetCr=0;
257
        Integer targetDat=0;
258
        Integer targetOrc=0;
259
        Integer sourceRes=0;
260
        Integer sourceOp=0;
261
        Integer sourceCr=0;
262
        Integer sourceDat=0;
263
        Integer sourceOrc=0;
264
        Integer projects=0;
265
        Integer contexts=0;
266
        Integer totalClaims=0;
267

    
268
        List<ContextRelation> relations = contextRelationHandler.fetchAllConceptRelations();
269
//        List<Claim> claims = new ArrayList<Claim>();
270
        for (ContextRelation relation : relations) {
271

    
272
            Claim claim= new Claim();
273
            claim.setId(relation.getClaimId());
274
            claim.setUserMail(relation.getClaimedBy());
275
            claim.setDate(relation.getClaimDate());
276
            logger.info("Claim id:" + claim.getId());
277

    
278
            //first work with the Source
279
            Context context = dmfContextHandler.extractContextFromDMF(relation.getContextDmf(),false);
280
            claim.setSourceType(ClaimUtils.CONTEXT);
281
            claim.setSource(context);
282

    
283
            Result target =  buildResult(relation.getResultDmf(), relation.getCollectedFrom(), relation.getResultId(), relation.getClaimId());
284
            if( target != null){
285
                claim.setTarget(target);
286
                claim.setTargetType(target.getResultType());
287
            }
288
            if(claimValidation.validateClaim(claim)){ // is valid
289
                if(save){
290
                    claim = claimHandler.exportMedatataForClaim(claim);
291
                    claimHandler.saveClaim(claim);
292
                }
293

    
294
                 /*for stats*/
295
                if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
296
                    targetCr++;
297
                }else if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
298
                    targetDat++;
299
                }else if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID)){
300
                    targetOrc++;
301
                }else {
302
                    targetOp++;
303
                }
304
                if(claim.getSourceType().equals(ClaimUtils.PUBLICATION)||claim.getSourceType().equals(ClaimUtils.DATASET)){
305
                    sourceRes++;
306
                    if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
307
                        sourceCr++;
308
                    }else if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
309
                        sourceDat++;
310
                    }else if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID)){
311
                        sourceOrc++;
312
                    }else {
313
                        sourceOp++;
314
                    }
315
                }else if(claim.getSourceType().equals(ClaimUtils.PROJECT)) {
316
                    projects++;
317
                }else if(claim.getSourceType().equals(ClaimUtils.CONTEXT)){
318
                    contexts++;
319
                }
320
            }else{
321
                if(save){
322
                    claimHandler.saveOrphanClaimId(claim.getId());
323
                }
324

    
325
            }
326

    
327

    
328
        }
329

    
330
             /*for stats*/
331
        System.out.println("\n\nTargets:\n\nCrossref : "+targetCr+ " DataCite :"+targetDat+" Orcid : "+targetOrc+" OpenAire : "+targetOp);
332
        System.out.println("\n\nSources:\n\nAll : "+sourceRes+" Crossref : "+sourceCr+ " DataCite :"+sourceDat+" Orcid : "+sourceOrc+" OpenAire : "+sourceOp);
333
        System.out.println("\n\nTotalClaims: "+totalClaims+" Projects: "+projects+ " Contexts :"+contexts);
334

    
335
    }
336

    
337
    /**
338
     *Returns a project from Openaire.
339
     * In case the project id has a "welcometrust" prefix changes it to "wt__________", before search for it in the index.
340
     *
341
     * @param projectId
342
     * @param claimId
343
     * @return Project
344
     * @throws Exception
345
     */
346
    public Project buildProject(String projectId, String claimId) throws Exception {
347
        String id = projectId;
348
        if(projectId != null && projectId.contains("welcometrust")){
349
            id=projectId.replace("welcometrust","wt__________");
350
        }
351
        Project project = projectHandler.fetchProjectByID(id, false);
352
        if (project == null) {
353
         project = projectHandler.fetchProjectByID(id, true);
354
        }
355
        if (project == null) {
356
            try{
357
                PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(claimValidation.getPathToSaveReport()+"missing_projects.txt", true)));
358
                Date date= new java.util.Date();
359
                out.println(new Timestamp(date.getTime())+"  ProjectId: "+id+ " ClaimId: "+claimId);
360
                out.close();
361
            }catch (IOException e) {
362
                logger.error("Couldn't write to file " + "missing_projects.txt",e);
363
            }
364
        }
365
        return project;
366

    
367
    }
368
    /**
369
     * *If there is a dmf, the result is extracted from the external sources (crossRef, Datacite, Orcid)
370
     * Otherwise Result is extracted from Openaire
371
     * @param dmf
372
     * @param collectedFrom
373
     * @param resultId
374
     * @param claimId
375
     * @return Result or null
376
     */
377
    private Result buildResult(String dmf, String collectedFrom, String resultId, String claimId){
378
        Result result = null;
379
        String external_id= null; // testing
380
        if((dmf !=null && collectedFrom != null)&& !collectedFrom.equals(ClaimUtils.COLLECTED_FROM_OPENAIRE)){
381

    
382
            if (collectedFrom.equals(ClaimUtils.COLLECTED_FROM_CROSSREF)) {
383
                try {
384
                    String doi= dmfResultHandler.fetchDoiByDMF(dmf);
385
                    external_id=doi;
386
                    if(doi != null) {
387
                        result = externalRecordHandler.fetchResultfromCrossref(doi);
388
                    }
389
                } catch (Exception e) {
390
                    logger.error("Error fetching result from Crossref",e);
391
                }
392

    
393
            } else if (collectedFrom.equals(ClaimUtils.COLLECTED_FROM_ORCID)) {
394
                try {
395
                    String orcidwork= dmfResultHandler.fetchOrcidWorkByDMF(dmf);
396
                    external_id=orcidwork;
397
                     if(orcidwork != null) {
398
                        result = externalRecordHandler.fetchResultfromOrcid(orcidwork);
399
                    }
400
                } catch (Exception e) {
401
                    logger.error("Error fetching result from Orcid",e);
402
                }
403
            } else if (collectedFrom.equals(ClaimUtils.COLLECTED_FROM_DATACITE)) {
404
                try {
405
                    String doi= dmfResultHandler.fetchDoiByDMF(dmf);
406
                    external_id=doi;
407
                    if(doi != null) {
408
                        result = externalRecordHandler.fetchResultfromDatacite(doi);
409
                    }
410
                } catch (Exception e) {
411
                    logger.error("Error fetching result from Datacite",e);
412
                }
413
            }
414
            if(result == null){
415
                //report missing external results
416
                try{
417
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(claimValidation.getPathToSaveReport()+"missing_results_external_sources.txt", true)));
418
                    Date date= new java.util.Date();
419
                    out.println(new Timestamp(date.getTime())+"  OpenaireId: "+resultId+ " collectedFrom: "+collectedFrom +" externalId: "+external_id+" claimId: "+claimId);
420
                    out.close();
421
                }catch (IOException e) {
422
                    logger.error("Couldn't write to file " + "missing_results_external_sources.txt",e);
423
                }
424
                //give a second chance - search index
425
                result = buildOpenaireResult(resultId,claimId);
426
                if(result!=null) {
427
                    try {
428
                        PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(claimValidation.getPathToSaveReport()+"external_results_found_inOpenaire.txt", true)));
429
                        Date date = new java.util.Date();
430
                        out.println(new Timestamp(date.getTime()) + "  OpenaireId: " + resultId + " collectedFrom: " + collectedFrom + " externalId: " + external_id + " claimId: " + claimId);
431
                        out.close();
432
                    } catch (IOException e) {
433
                        logger.error("Couldn't write to file " + "external_results_found_inOpenaire.txt",e);
434
                    }
435
                }
436

    
437
            }
438
            if(result!=null){
439
                // if result found enriched with access rights/ embargo date from DMF
440
                try {
441
                    result.setAccessRights(dmfResultHandler.fetchAccessRights(dmf));
442
                    result.setEmbargoEndDate(dmfResultHandler.fetchEmbargoEndDateByDMF(dmf));
443
                } catch (Exception e) {
444
                    logger.error("Error fetching Access or Embargo end date from DMF",e);
445
                }
446
            }
447
        } else { //If dmf/ collecteFrom  is null or is collected from openaire search in the  index
448
            result = buildOpenaireResult(resultId,claimId);
449
        }
450

    
451

    
452
        return result;
453
    }
454

    
455
    /**
456
     *
457
     * @param resultId
458
     * @param claimId
459
     * @return target Result from Openaire or null
460
     */
461
    public Result buildOpenaireResult(String resultId, String claimId  ){
462

    
463
        Result result = null;
464
        try {
465
           result = indexResultHandler.fetchPublicationById(resultId, false);
466
        } catch (Exception e) {
467
            logger.error("Error fetching result from Openaire",e);
468
            System.out.println("Error fetching result from Openaire");
469
        }
470

    
471
        //If it is not found in the index
472
        if (result == null) {
473
           //give a second chance as dedup
474
//            try {
475
//                result = indexResultHandler.fetchDedupResultById(resultId);
476
//            } catch (Exception e) {
477
//                logger.error("Error fetching dedup Result",e);
478
//            }
479
            //give a second chance in production
480
            try {
481
                result = indexResultHandler.fetchPublicationById(resultId, true);
482
            } catch (Exception e) {
483
                logger.error("Error fetching result from Openaire",e);
484
                System.out.println("Error fetching result from Openaire");
485
            }
486
            if (result == null) {
487
                try {
488
                    result = indexResultHandler.fetchDatasetById(resultId, false);
489
                } catch (Exception e) {
490
                    logger.error("Error fetching result from Openaire",e);
491
                    System.out.println("Error fetching result from Openaire");
492
                }
493
            }
494
            if (result == null) {
495
                try {
496
                    result = indexResultHandler.fetchDatasetById(resultId, true);
497
                } catch (Exception e) {
498
                    logger.error("Error fetching result from Openaire",e);
499
                    System.out.println("Error fetching result from Openaire");
500
                }
501
            }
502
            if (result == null) {
503
                try {
504
                    result = indexResultHandler.fetchDedupById(resultId, false);
505
                } catch (Exception e) {
506
                    logger.error("Error fetching result from Openaire",e);
507
                    System.out.println("Error fetching result from Openaire");
508
                }
509
            }
510
            if (result == null) {
511
                try {
512
                    result = indexResultHandler.fetchDedupById(resultId, true);
513
                } catch (Exception e) {
514
                    logger.error("Error fetching result from Openaire",e);
515
                    System.out.println("Error fetching result from Openaire");
516
                }
517
            }
518
            if (result != null) {
519
                try {
520
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(claimValidation.getPathToSaveReport()+"dedup_results_found.txt", true)));
521
                    Date date = new java.util.Date();
522
                    out.println(new Timestamp(date.getTime()) + "  OpenaireId: " + resultId + " claimId: " + claimId);
523
                    out.close();
524
                } catch (IOException e) {
525
                    logger.error("Couldn't write to file " + "external_results_found_inOpenaire.txt",e);
526
                }
527
            }else {
528
                //report it as missing
529
                try {
530
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(claimValidation.getPathToSaveReport()+"missing_results.txt", true)));
531
                    Date date = new java.util.Date();
532
                    out.println(new Timestamp(date.getTime()) + "  OpenaireId: " + resultId + " ClaimId: " + claimId);
533
                    out.close();
534
                } catch (IOException e) {
535
                    logger.error("Couldn't write to file " + "missing_results.txt",e);
536
                }
537
            }
538

    
539
        }else{
540
            //FOUND report results that came from datacite but are publications
541
            //TODO deal with them as externals?
542

    
543
            if(result.getProvenanceaction() != null && result.getProvenanceaction().equals("user:claim:datacite")&&result.getOai()!=null){
544
                try{
545
                    PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(claimValidation.getPathToSaveReport()+"datacite_claim_results.txt", true)));
546
                    Date date= new java.util.Date();
547
                    out.println(new Timestamp(date.getTime())+"  openaireId:"+resultId+" aoi: " +result.getOai() +" provenanceactions: "+result.getProvenanceaction()+" type: "+result.getResultType()+ " ClaimId: "+claimId);
548
                    out.close();
549
                    try {
550
                        result= externalRecordHandler.fetchResultfromDatacite(result.getOai());
551
                        result.setOpenaireId(resultId);
552
                    }catch(Exception e){
553
                        logger.error("Couldn't get Result from Datacite "+result.getOai(),e);
554
                    }
555
                }catch (IOException e) {
556
                    logger.error("Couldn't write to file " + "datacite_claim_results.txt",e);
557
                }
558

    
559
            }
560
        }
561

    
562
        return result;
563
    }
564

    
565

    
566
    /**
567
     * For each claim of the list
568
     * if there are results in the relation exports their metadaa file
569
     * Save the claim in DB
570
     * @param claims
571
     * @throws Exception
572
     */
573
    private void saveClaims(List<Claim> claims) throws Exception, SQLStoreException {
574
        for(Claim claim : claims){
575
           /* if(claim.getTargetType().equals(ClaimUtils.DATASET)||claim.getTargetType().equals(ClaimUtils.PUBLICATION)){
576
                String path = resultHandler.exportMetadataFileForResult((Result)claim.getTarget());
577
                ((Result) claim.getTarget()).setRecordPath(path);
578
            }
579
            if(claim.getSourceType().equals(ClaimUtils.DATASET)||claim.getSourceType().equals(ClaimUtils.PUBLICATION)){
580
                String path = resultHandler.exportMetadataFileForResult((Result)claim.getSource());
581
                ((Result) claim.getSource()).setRecordPath(path);
582
            }*/
583
            claim = claimHandler.exportMedatataForClaim(claim);
584
            claimHandler.saveClaim(claim);
585
        }
586

    
587
    }
588

    
589

    
590

    
591
    public static void main(String[] args) throws IOException {
592
        BasicConfigurator.configure();
593
        logger.setLevel(Level.DEBUG);
594
        Migration migration = new Migration();
595
        try {
596
//            migration.claimHandler.getQueryGenerator().setMigrationTable("claims_view");
597
//            migration.createAndSaveRelationsClaims(true);
598
//            migration.createAndSaveContextRelationClaims(true);
599

    
600
            migration.claimHandler.getQueryGenerator().setMigrationTable("claims_clean");
601
            migration.createAndSaveRelationsClaims(true);
602
            migration.createAndSaveContextRelationClaims(true);
603

    
604

    
605
        } catch (Exception e) {
606
            logger.error("Error in migration",e);
607
        } catch (SQLStoreException e) {
608
            e.printStackTrace();
609
        }
610

    
611
    }
612
    public void printStatistics(List<Claim> claims){
613

    
614
        //testing
615
        List<Claim> claimsOp= new ArrayList<Claim>();
616
        List<Claim> claimsCr= new ArrayList<Claim>();
617
        List<Claim> claimsDat= new ArrayList<Claim>();
618
        List<Claim> claimsOrc= new ArrayList<Claim>();
619
        Integer targetOp=0;
620
        Integer targetCr=0;
621
        Integer targetDat=0;
622
        Integer targetOrc=0;
623
        Integer sourceRes=0;
624
        Integer sourceOp=0;
625
        Integer sourceCr=0;
626
        Integer sourceDat=0;
627
        Integer sourceOrc=0;
628
        Integer projects=0;
629
        Integer contexts=0;
630
        Integer totalClaims=0;
631
        for(Claim claim: claims){
632
//            System.out.println(claim.toString());
633
            if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
634
                targetCr++;
635
            }else if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
636
                targetDat++;
637
            }else if(((Result)claim.getTarget()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID)){
638
                targetOrc++;
639
            }else {
640
                targetOp++;
641
            }
642
            if(claim.getSourceType().equals(ClaimUtils.PUBLICATION)||claim.getSourceType().equals(ClaimUtils.DATASET)){
643
                sourceRes++;
644
                if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_CROSSREF)){
645
                    sourceCr++;
646
                }else if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_DATACITE)){
647
                    sourceDat++;
648
                }else if(((Result)claim.getSource()).getCollectedFrom().equals(ClaimUtils.COLLECTED_FROM_ORCID)){
649
                    sourceOrc++;
650
                }else {
651
                    sourceOp++;
652
                }
653
            }else if(claim.getSourceType().equals(ClaimUtils.PROJECT)) {
654
                projects++;
655
            }else if(claim.getSourceType().equals(ClaimUtils.CONTEXT)){
656
                contexts++;
657
            }
658
        }
659
        totalClaims=claims.size();
660
        System.out.println("\n\nTargets:\n\nCrossref : "+targetCr+ " DataCite :"+targetDat+" Orcid : "+targetOrc+" OpenAire : "+targetOp);
661
        System.out.println("\n\nSources:\n\nAll : "+sourceRes+" Crossref : "+sourceCr+ " DataCite :"+sourceDat+" Orcid : "+sourceOrc+" OpenAire : "+sourceOp);
662
        System.out.println("\n\nTotalClaims: "+totalClaims+" Projects: "+projects+ " Contexts :"+contexts);
663

    
664
    }
665

    
666

    
667
}
(5-5/6)