Project

General

Profile

1
import {Component, Input, Output, EventEmitter} from '@angular/core';
2
import { ActivatedRoute } from '@angular/router';
3
import {SearchCrossrefService} from '../claim-utils/service/searchCrossref.service';
4
import {SearchOrcidService} from '../claim-utils/service/searchOrcid.service';
5
import {SearchPublicationsService} from '../../services/searchPublications.service';
6
import { SearchDataciteService } from '../claim-utils/service/searchDatacite.service';
7
import {SearchDatasetsService} from '../../services/searchDatasets.service';
8
import {SearchSoftwareService} from '../../services/searchSoftware.service';
9

    
10
import { ErrorCodes} from '../../utils/properties/openaireProperties';
11
import {ClaimResult} from '../claim-utils/claimEntities.class';
12
import{DOI} from '../../utils/string-utils.class';
13
import{EnvProperties} from '../../utils/properties/env-properties';
14

    
15
declare var UIkit:any;
16

    
17
@Component({
18
    selector: 'claim-result-search-form',
19
    templateUrl: 'claimResultSearchForm.component.html',
20

    
21
})
22
export class ClaimResultSearchFormComponent {
23
  constructor (private _searchDataciteService: SearchDataciteService, private _searchDatasetsService:SearchDatasetsService, private _searchSoftwareService:SearchSoftwareService,
24
    private _searchCrossrefService: SearchCrossrefService,private _searchOrcidService: SearchOrcidService, private _searchPublicationsService: SearchPublicationsService,
25
    private route: ActivatedRoute) {
26
    var myDate = new Date();
27
    this.todayDate = myDate.getFullYear()+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
28
    this.nextDate =  (myDate.getFullYear()+100)+ "-" +(myDate.getMonth() + 1) + "-" + myDate.getDate() ;
29

    
30
  }
31
  ngOnInit() {
32
    console.log("Init of ClaimResultSearchFormComponent");
33
    console.log(this.properties);
34
    if(this.keyword !=null && this.keyword.length > 0){
35
     this.search(false);
36
    }
37
}
38

    
39

    
40
  page : number = 1;
41
  size:number = 5;
42
  navigateTo: string = "Search";
43
  source: string = "datacite";
44
  type : string = "dataset";
45
  showSearchResults:boolean=false;
46
  // searchType ="publication";
47
  @Input() public select:boolean = true ;
48
  @Input() public keyword:string = '';
49
  @Input() public selectedResults:ClaimResult[];
50
  @Input() public properties:EnvProperties;
51

    
52
  // @Output() datasetsChange = new EventEmitter();
53
  // @Output() publicationsChange = new EventEmitter();
54

    
55
  // @Output() resultsChange = new EventEmitter();
56

    
57
    public errorCodes:ErrorCodes = new ErrorCodes();
58

    
59
    dataciteResults=[];
60
    dataciteResultsNum:number = null;
61
    // dataciteResultsNum : Observable<number> = null;
62
    dataciteStatus = this.errorCodes.NONE;
63
    datacitePage : number = 1;
64

    
65
    openaireData=[];
66
    openaireDataNum:number = 0 ;
67
    openaireDataStatus = this.errorCodes.NONE;
68
    openaireDataPage : number = 1;
69
    openaireSoftware=[];
70
    openaireSoftwareNum:number = 0 ;
71
    openaireSoftwareStatus = this.errorCodes.NONE;
72
    openaireSoftwarePage : number = 1;
73

    
74
    public warningMessage = "";
75
    public infoMessage = "";
76

    
77
    public todayDate = '';
78
    public nextDate = '';
79
    public DOIs:string[] = [];
80
    sub: any;
81
    public searchSource:string = "openaire"
82
    public activeTab:string = "openairePub"
83

    
84

    
85
    crossrefResults=[];
86
    crossrefResultsNum : number = null;
87
    crossrefPage : number = 1;
88
    crossrefStatus:number = this.errorCodes.NONE;
89

    
90
    openairePubs = [];
91
    openairePubsNum: number ;
92
    openairePubsPage : number = 1;
93
    openairePubsStatus:number = this.errorCodes.NONE;
94

    
95
    orcidResults: string[];
96
    orcidResultsNum: number ;
97
    totalPages: number;
98
    orcidResultsToShow: string[];
99
    orcidPage : number = 1;
100
    orcidStatus:number = this.errorCodes.NONE;
101
    authorId: string;
102
    selectAuthorId: string = "0";
103
    authorGivenName: string;
104
    authorFamilyName: string;
105

    
106
    authorIds: string[];
107
    authorGivenNames: string[];
108
    authorFamilyNames: string[];
109

    
110
    authorsNum : number ;
111

    
112
    reloadOpenaire:boolean = true;
113
    reloadCrossref:boolean = false;
114
    reloadDatacite:boolean = false;
115
    reloadOrcid:boolean = false;
116

    
117

    
118

    
119

    
120
  search(sourceChanged){
121
    this.warningMessage = "";
122
    this.infoMessage = "";
123
    if(!sourceChanged){
124
      this.DOIs = DOI.getDOIsFromString(this.keyword);
125
      this.reloadOpenaire = true;
126
      this.reloadCrossref = true;
127
      this.reloadDatacite = true;
128
      this.reloadOrcid = true;
129
    }
130
    if((this.searchSource == "all" || this.searchSource == "openaire") && this.reloadOpenaire){
131
      this.searchOpenairePubs(this.keyword, this.size, 1);
132
      this.searchOpenaireData(this.keyword,this.size,1);
133
      this.searchOpenaireSoftware(this.keyword,this.size,1);
134
      this.reloadOpenaire = false;
135
    }
136
    if((this.searchSource == "all" || this.searchSource == "crossref")&&this.reloadCrossref){
137
      this.getCrossrefResults(this.keyword, this.size,1);
138
      this.reloadCrossref = false;
139
    }
140
    if((this.searchSource == "all" || this.searchSource == "datacite")&& this.reloadDatacite){
141
      this.searchDatacite(this.keyword,this.size,1);
142
      this.reloadDatacite = false;
143
    }
144
    if((this.searchSource == "all" || this.searchSource == "orcid")&& this.reloadOrcid){
145
      this.searchOrcid(this.keyword);
146
      this.reloadOrcid = false;
147
    }
148
    this.showSearchResults = true;
149

    
150
  }
151

    
152
private   getCrossrefResults (term: string, size : number, page : number)  {
153
    this.crossrefStatus = this.errorCodes.LOADING;
154
    if( this.DOIs.length > 0 ){
155
      this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs, this.properties.searchCrossrefAPIURL).subscribe(
156
        data => {
157
            if(data != null) {
158
                this.crossrefResults = data.items;
159
                this.crossrefPage=page;
160
                this.crossrefResultsNum = data['total-results'];
161
                if(data.items == 0){
162
                    this._searchCrossrefService.searchCrossrefResults(term, size, page, this.properties.searchCrossrefAPIURL).subscribe(
163
                        data => {
164
                            if(data != null) {
165
                                this.crossrefResults = data.items;
166
                                this.crossrefPage=page;
167
                                this.crossrefResultsNum = data['total-results'];
168
                                this.crossrefStatus = this.errorCodes.DONE;
169

    
170
                            }else{
171
                              this.crossrefStatus = this.errorCodes.ERROR;
172
                            }
173
                        },
174
                        err =>{
175
                           console.log(err.status);
176
                           this.crossrefStatus = this.errorCodes.ERROR;
177
                         }
178

    
179
                    );
180
                }else{
181
                  this.crossrefStatus = this.errorCodes.DONE;
182
                }
183
            }
184
         },
185
        err => {
186
          //console.log(err);
187
          this._searchCrossrefService.searchCrossrefResults(term, size, page, this.properties.searchCrossrefAPIURL).subscribe(
188
            data => {
189
              this.crossrefResults = data.items;
190
              this.crossrefPage=page;
191
              this.crossrefResultsNum = data['total-results'];
192
              this.crossrefStatus = this.errorCodes.DONE;
193

    
194
             },
195
            err => {
196
              console.log(err.status);
197
              this.crossrefStatus = this.errorCodes.ERROR;
198
            }
199

    
200
          );
201
        }
202
      );
203

    
204
    }else{
205

    
206

    
207
      this._searchCrossrefService.searchCrossrefResults(term, size, page, this.properties.searchCrossrefAPIURL).subscribe(
208
        data => {
209
            if(data != null) {
210
                this.crossrefResults = data.items;
211
                this.crossrefPage=page;
212
                this.crossrefResultsNum = data['total-results'];
213
                this.crossrefStatus = this.errorCodes.DONE;
214

    
215
            }else{
216
              this.crossrefStatus = this.errorCodes.ERROR;
217
            }
218

    
219
         },
220
        err => {
221
          console.log(err.status);
222
          this.crossrefStatus = this.errorCodes.ERROR;
223
          }
224
      );
225
    }
226
  }
227
  private searchOpenairePubs(term: string, size : number, page : number)  {
228

    
229
          if(this.DOIs.length > 0 ){
230
            this.openairePubsStatus = this.errorCodes.LOADING;
231
            this._searchPublicationsService.searchPublicationsByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
232
              data => {
233
                  if(data != null) {
234
                      this.openairePubsPage=page;
235
                      this.openairePubsNum = data[0];
236
                      this.openairePubs = data[1];
237
                      this.openairePubsStatus = this.errorCodes.DONE;
238
                      if(this.openairePubsNum == 0){
239
                        this.openairePubsStatus = this.errorCodes.NONE;
240
                      }
241
                  }else {
242
                    this.openairePubsStatus = this.errorCodes.ERROR;
243
                  }
244
               },
245
              err => {
246
                this.openairePubsStatus = this.errorCodes.ERROR;
247
                console.log(err.status);
248
              }
249
            );
250
          }else{
251
            this.openairePubsStatus = this.errorCodes.LOADING;
252
            this._searchPublicationsService.searchPublications('q='+term, null, page, size, [], this.properties).subscribe(
253
              data => {
254
                  if(data != null) {
255
                      this.openairePubsPage=page;
256
                      this.openairePubsNum = data[0];
257
                      this.openairePubs = data[1];
258
                      this.openairePubsStatus = this.errorCodes.DONE;
259
                      if(this.openairePubsNum == 0){
260
                        this.openairePubsStatus = this.errorCodes.NONE;
261
                      }
262
                  }else {
263
                    this.openairePubsStatus = this.errorCodes.ERROR;
264
                  }
265
               },
266
              err => {
267
                this.openairePubsStatus = this.errorCodes.ERROR;
268
                console.log(err.status);
269
              }
270
            );
271
          }
272
  }
273

    
274
  private searchOrcid (term: string) {
275
    if(this.DOIs.length > 0){
276
      this.orcidStatus = this.errorCodes.NONE;
277
      return;
278
    }
279
    this.orcidStatus = this.errorCodes.LOADING;
280
    this.authorIds = new Array<string>();
281
    this.authorGivenNames = new Array<string>();
282
    this.authorFamilyNames = new Array<string>();
283

    
284
    this.getOrcidAuthor(term);
285

    
286
    console.info('searchOrcid in searchOrcid file');
287
   }
288

    
289
  private readData(data: any) {
290
    this.authorIds.push(data[2].path);
291

    
292
    if(data[0] != null) {
293
      this.authorGivenNames.push(data[0].value);
294
    } else {
295
      this.authorGivenNames.push("");
296
    }
297
    if(data[1] != null) {
298
      this.authorFamilyNames.push(data[1].value);
299
    } else {
300
      this.authorFamilyNames.push("");
301
    }
302
  }
303

    
304
  private  getOrcidAuthor (term: string)  {
305
    this.orcidResultsNum = null;
306

    
307
    //passing structures in order to fill them in service
308
    this._searchOrcidService.searchOrcidAuthor(term, this.authorIds,
309
      this.authorGivenNames, this.authorFamilyNames, this.properties).subscribe(
310
        data => {
311
          if(data != null && data == true) {
312
            this.getOrcidResultsByIndex(0);
313
          }
314

    
315
          this.orcidStatus = this.errorCodes.NONE;
316

    
317
        },
318
        err => this.errorHandler(err, term)
319

    
320
      );
321
    }
322

    
323
    private  errorHandler(err: any, term: string) {
324
      if(err.status == 404){
325
        this.getOrcidAuthors(term);
326
      } else {
327
        this.orcidStatus = this.errorCodes.ERROR;
328
        console.log(err.status);
329

    
330
      }
331
    }
332

    
333
    private  getOrcidAuthors (term: string)  {
334
      this.orcidResultsNum = null;
335
      this.selectAuthorId = "0";
336
          this.orcidStatus = this.errorCodes.LOADING;
337
      //passing structures in order to fill them in service
338
      this._searchOrcidService.searchOrcidAuthors(term, this.authorIds,
339
        this.authorGivenNames, this.authorFamilyNames, this.properties).subscribe(
340
          data => {
341
            if(data != null && data == true) {
342
              this.getOrcidResultsByIndex(0);
343
            }else{
344
              this.orcidStatus = this.errorCodes.ERROR;
345
            }
346

    
347
          },
348
          err => {
349
            this.orcidStatus = this.errorCodes.ERROR;
350
            console.log(err.status);
351
          }
352
        );
353
      }
354

    
355

    
356
      private    getOrcidResultsByIndex (index:number) {
357
        if(this.authorIds.length > index) {
358
          this.orcidStatus = this.errorCodes.LOADING;
359
          let id = this.authorIds[index];
360
          this.authorGivenName = this.authorGivenNames[index];
361
          this.authorFamilyName = this.authorFamilyNames[index];
362
          this.getOrcidResultsById(id);
363
        }
364
      }
365
      private    getOrcidResultsById (id:string) {
366
          if(id=="0"){
367
            return;
368
          }
369
          var index = this.authorIds.indexOf(id);
370
          this.authorGivenName = this.authorGivenNames[index];
371
          this.authorFamilyName = this.authorFamilyNames[index];
372
          this.authorId = id;
373
          console.info("getOrcidResultsById: "+id);
374
            this.orcidStatus = this.errorCodes.LOADING;
375
          this._searchOrcidService.searchOrcidPublications(id, this.properties).subscribe(
376
            data => {
377
              if(data != null) {
378
                this.orcidResults=data['orcid-work'];
379
                this.orcidResultsNum = data['orcid-work'].length;
380
                this.orcidPage = 1;
381
                if((this.orcidResultsNum % this.size) == 0){
382
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
383
                } else{
384
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
385
                }
386

    
387
                this.orcidResultsToShow = this.orcidResults.slice(0,10);
388

    
389
                this.orcidStatus = this.errorCodes.DONE;
390
                if(this.orcidResultsNum == 0){
391
                  this.orcidStatus = this.errorCodes.NONE;
392
                }
393
              } else {
394
                this.orcidResultsNum = 0;
395
                this.totalPages=0;
396
                this.orcidStatus = this.errorCodes.NONE;
397
              }
398

    
399
            },
400
            err => {
401
              console.log(err.status);
402
              this.orcidStatus = this.errorCodes.ERROR;
403
            }
404
          );
405

    
406

    
407
      }
408

    
409
/*
410
Is it USED???
411
private  remove(item){
412
     this.warningMessage = "";
413
     this.infoMessage = "";
414
        var index:number =this.selectedResults.indexOf(item);
415
        item.selected=false;
416
         if (index > -1) {
417
            this.selectedResults.splice(index, 1);
418
            // this.publicationsChange.emit({
419
            //   value: this.selectedResults
420
            // });
421
        }
422

    
423
   }*/
424
private crossrefPageChange($event) {
425
     this.crossrefPage=$event.value;
426
     this.crossrefResults=[];
427
     console.log("Crossref chaenged "+this.crossrefPage);
428
     this.getCrossrefResults(this.keyword,this.size,this.crossrefPage);
429
}
430
private orcidPageChange($event) {
431
     this.orcidPage=$event.value;
432
     this.orcidResultsToShow=[];
433
     this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
434
}
435
private openairePubsPageChange($event) {
436
     this.openairePubsPage=$event.value;
437
     this.searchOpenairePubs(this.keyword,this.size,this.openairePubsPage);
438
}
439
datacitePageChange($event) {
440
  this.datacitePage=$event.value;
441
  this.dataciteResults=[];
442
  this.searchDatacite(this.keyword,10,this.datacitePage);
443
  this.warningMessage = "";
444
  this.infoMessage = "";
445

    
446
}
447
openaireDataPageChange($event) {
448
  this.openaireDataPage=$event.value;
449
  this.openaireData=[];
450
  this.searchOpenaireData(this.keyword,10,this.openaireDataPage);
451
  this.warningMessage = "";
452
  this.infoMessage = "";
453

    
454
}
455
openaireSoftwarePageChange($event) {
456
  this.openaireSoftwarePage=$event.value;
457
  this.openaireSoftware=[];
458
  this.searchOpenaireSoftware(this.keyword,10,this.openaireSoftwarePage);
459
  this.warningMessage = "";
460
  this.infoMessage = "";
461

    
462
}
463

    
464
  private   isSelected(id:string){
465

    
466
      var found:boolean = false;
467
      this.warningMessage = "";
468
      for (var _i = 0; _i < this.selectedResults.length; _i++) {
469
          let item = this.selectedResults[_i];
470
          if(item.id == id){
471
                found=true;
472
                this.warningMessage = "Publication already in selected list";
473
          }
474
       }
475
       return found;
476

    
477

    
478
    }
479
    // isSelected(id:string){
480
    //
481
    //   var found:boolean = false;
482
    //   this.warningMessage = "";
483
    //   for (var _i = 0; _i < this.selectedResults.length; _i++) {
484
    //       let item = this.selectedResults[_i];
485
    //       if(item.id == id){
486
    //             found=true;
487
    //             break;
488
    //        }
489
    //    }
490
    //    return found;
491
    // }
492
    private searchDatacite (term: string, size : number, page : number) {
493
      this.getDataciteResults(term,size,page);
494
      this.warningMessage = "";
495
      this.infoMessage = "";
496

    
497
     }
498
     private searchOpenaireData (term: string, size : number, page : number) {
499
       if(this.DOIs.length > 0 ){
500
         this.openaireDataStatus = this.errorCodes.LOADING;
501
         this._searchDatasetsService.searchDatasetsByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
502
           data => {
503
               if(data != null) {
504
                   this.openaireDataPage=page;
505
                   this.openaireDataNum = data[0];
506
                   this.openaireData = data[1];
507
                   this.openaireDataStatus = this.errorCodes.DONE;
508
                   if(this.openaireDataNum == 0){
509
                      this.openaireDataStatus = this.errorCodes.NONE;
510
                   }
511
               }
512
            },
513
           err => {
514
             this.openaireDataStatus = this.errorCodes.ERROR;
515
             console.log(err.status);
516
           }
517
         );
518
       }else{
519
         this._searchDatasetsService.searchDatasets('q='+term+'', null, page, size, [], this.properties).subscribe(
520
                     data => {
521
                         if(data != null) {
522
                             this.openaireDataPage=page;
523
                             this.openaireDataNum = data[0];
524
                             this.openaireData = data[1];
525
                             this.openaireDataStatus = this.errorCodes.DONE;
526
                             if(this.openaireDataNum == 0){
527
                                this.openaireDataStatus = this.errorCodes.NONE;
528
                             }
529
                         }
530
                      },
531
                     err => {
532
                       this.openaireDataStatus = this.errorCodes.ERROR;
533
                       console.log(err.status);
534
                     }
535
                   );
536
                }
537
       this.warningMessage = "";
538
       this.infoMessage = "";
539

    
540
      }
541
      private searchOpenaireSoftware (term: string, size : number, page : number) {
542
        if(this.DOIs.length > 0 ){
543
          this.openaireSoftwareStatus = this.errorCodes.LOADING;
544
          this._searchSoftwareService.searchSoftwareByDois(this.DOIs, null, page, size, [], this.properties).subscribe(
545
            data => {
546
                if(data != null) {
547
                    this.openaireSoftwarePage=page;
548
                    this.openaireSoftwareNum = data[0];
549
                    this.openaireSoftware = data[1];
550
                    this.openaireSoftwareStatus = this.errorCodes.DONE;
551
                    if(this.openaireSoftwareNum == 0){
552
                       this.openaireSoftwareStatus = this.errorCodes.NONE;
553
                    }
554
                }
555
             },
556
            err => {
557
              this.openaireSoftwareStatus = this.errorCodes.ERROR;
558
              console.log(err.status);
559
            }
560
          );
561
        }else{
562
          this._searchSoftwareService.searchSoftware('q='+term+'', null, page, size, [], this.properties).subscribe(
563
                      data => {
564
                          if(data != null) {
565
                              this.openaireSoftwarePage=page;
566
                              this.openaireSoftwareNum = data[0];
567
                              this.openaireSoftware = data[1];
568
                              this.openaireSoftwareStatus = this.errorCodes.DONE;
569
                              if(this.openaireSoftwareNum == 0){
570
                                 this.openaireSoftwareStatus = this.errorCodes.NONE;
571
                              }
572
                          }
573
                       },
574
                      err => {
575
                        this.openaireSoftwareStatus = this.errorCodes.ERROR;
576
                        console.log(err.status);
577
                      }
578
                    );
579
                 }
580
        this.warningMessage = "";
581
        this.infoMessage = "";
582

    
583
       }
584
   private getDataciteResults (term: string, size : number, page : number)  {
585
     console.log("getDataciteResults  + "+this.properties.searchDataciteAPIURL)
586
       this._searchDataciteService.searchDataciteResults(term, size, page, this.properties).subscribe(
587
        data => {
588
          this.dataciteResults = data.data;
589
          this.datacitePage=page;
590
          this.dataciteResultsNum = data.meta.total;
591
          this.dataciteStatus = this.errorCodes.DONE;
592

    
593

    
594
        },
595
        err => {
596
          this.dataciteStatus = this.errorCodes.ERROR;
597
          console.log(err);
598
        }
599

    
600
      );
601
      }
602

    
603
   add(item, itemId,itemSource,itemType, itemUrl, itemTitle, date, accessmode){
604

    
605
     console.log(' adding ' + itemType + " From " + itemSource+"  "+ itemTitle);
606
     var result: ClaimResult = ClaimResult.generateResult(item, itemId,itemSource,itemType, itemUrl, itemTitle, date, accessmode);
607

    
608

    
609
    if (itemSource == 'orcid'){
610
        if(result.authors.length ==0 ){
611
          result.authors.push(this.authorGivenName + ', '+ this.authorFamilyName);
612
        }
613
      }
614
      var found:boolean = this.isSelected( result.id);
615

    
616
      this.warningMessage = "";
617
       if (!found) {
618
        this.selectedResults.push(result);
619

    
620
               UIkit.notification({
621
                  message : 'A new research result is selected.',
622
                  status  : 'primary',
623
                  timeout : 1000,
624
                  pos     : 'top-center'
625
              });
626

    
627

    
628
      }else{
629
        this.warningMessage = "Research Data already in selected list";
630
        UIkit.notification({
631
            message : 'The  research result is already on your list.',
632
            status  : 'warning',
633
            timeout : 1000,
634
            pos     : 'top-center'
635
        });
636
      }
637

    
638
    }
639

    
640
    public searchSourceChanged(source){
641
      this.searchSource = source;
642
      this.activeTab = (source == "openaire" || source == "all")?"openairePub":source;
643
      if(this.keyword && this.keyword.length > 0){
644
        this.search(true);
645
      }
646
    }
647
    public clickTab(tab){
648
       this.activeTab = tab;
649
    }
650
}
(8-8/11)