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

    
9
import { ErrorCodes} from '../../utils/properties/openaireProperties';
10
import {ClaimResult} from '../claim-utils/claimEntities.class';
11
import{DOI} from '../../utils/string-utils.class';
12
declare var UIkit:any;
13

    
14
@Component({
15
    selector: 'claim-result-search-form',
16
    templateUrl: 'claimResultSearchForm.component.html',
17

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

    
27
  }
28
  ngOnInit() {
29
    if(this.keyword !=null && this.keyword.length > 0){
30
     this.search(false);
31
    }
32
}
33

    
34

    
35
  page : number = 1;
36
  size:number = 5;
37
  navigateTo: string = "Search";
38
  source: string = "datacite";
39
  type : string = "dataset";
40
  showSearchResults:boolean=false;
41
  // searchType ="publication";
42
  @Input() public select:boolean = true ;
43
  @Input() public keyword:string = '';
44
  @Input() public selectedResults:ClaimResult[];
45
  // @Output() datasetsChange = new EventEmitter();
46
  // @Output() publicationsChange = new EventEmitter();
47

    
48
  // @Output() resultsChange = new EventEmitter();
49

    
50
    public errorCodes:ErrorCodes = new ErrorCodes();
51

    
52
    dataciteResults=[];
53
    dataciteResultsNum:number = null;
54
    // dataciteResultsNum : Observable<number> = null;
55
    dataciteStatus = this.errorCodes.NONE;
56
    datacitePage : number = 1;
57

    
58
    openaireData=[];
59
    openaireDataNum:number = 0 ;
60
    openaireDataStatus = this.errorCodes.NONE;
61
    openaireDataPage : number = 1;
62

    
63
    public warningMessage = "";
64
    public infoMessage = "";
65

    
66
    public todayDate = '';
67
    public nextDate = '';
68
    public DOIs:string[] = [];
69
    sub: any;
70
    public searchSource:string = "openaire"
71
    public activeTab:string = "openairePub"
72

    
73

    
74
    crossrefResults=[];
75
    crossrefResultsNum : number = null;
76
    crossrefPage : number = 1;
77
    crossrefStatus:number = this.errorCodes.NONE;
78

    
79
    openairePubs = [];
80
    openairePubsNum: number ;
81
    openairePubsPage : number = 1;
82
    openairePubsStatus:number = this.errorCodes.NONE;
83

    
84
    orcidResults: string[];
85
    orcidResultsNum: number ;
86
    totalPages: number;
87
    orcidResultsToShow: string[];
88
    orcidPage : number = 1;
89
    orcidStatus:number = this.errorCodes.NONE;
90
    authorId: string;
91
    selectAuthorId: string = "0";
92
    authorGivenName: string;
93
    authorFamilyName: string;
94

    
95
    authorIds: string[];
96
    authorGivenNames: string[];
97
    authorFamilyNames: string[];
98

    
99
    authorsNum : number ;
100

    
101
    reloadOpenaire:boolean = true;
102
    reloadCrossref:boolean = false;
103
    reloadDatacite:boolean = false;
104
    reloadOrcid:boolean = false;
105

    
106

    
107

    
108

    
109
  search(sourceChanged){
110
    this.warningMessage = "";
111
    this.infoMessage = "";
112
    if(!sourceChanged){
113
      this.DOIs = DOI.getDOIsFromString(this.keyword);
114
      this.reloadOpenaire = true;
115
      this.reloadCrossref = true;
116
      this.reloadDatacite = true;
117
      this.reloadOrcid = true;
118
    }
119
    if((this.searchSource == "all" || this.searchSource == "openaire") && this.reloadOpenaire){
120
      this.searchOpenairePubs(this.keyword, this.size, 1);
121
      this.searchOpenaireData(this.keyword,this.size,1);
122
      this.reloadOpenaire = false;
123
    }
124
    if((this.searchSource == "all" || this.searchSource == "crossref")&&this.reloadCrossref){
125
      this.getCrossrefResults(this.keyword, this.size,1);
126
      this.reloadCrossref = false;
127
    }
128
    if((this.searchSource == "all" || this.searchSource == "datacite")&& this.reloadDatacite){
129
      this.searchDatacite(this.keyword,this.size,1);
130
      this.reloadDatacite = false;
131
    }
132
    if((this.searchSource == "all" || this.searchSource == "orcid")&& this.reloadOrcid){
133
      this.searchOrcid(this.keyword);
134
      this.reloadOrcid = false;
135
    }
136
    this.showSearchResults = true;
137

    
138
  }
139

    
140
private   getCrossrefResults (term: string, size : number, page : number)  {
141
    this.crossrefStatus = this.errorCodes.LOADING;
142
    if( this.DOIs.length > 0 ){
143
      this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs).subscribe(
144
        data => {
145
            if(data != null) {
146
                this.crossrefResults = data.items;
147
                this.crossrefPage=page;
148
                this.crossrefResultsNum = data['total-results'];
149
                if(data.items == 0){
150
                    this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
151
                        data => {
152
                            if(data != null) {
153
                                this.crossrefResults = data.items;
154
                                this.crossrefPage=page;
155
                                this.crossrefResultsNum = data['total-results'];
156
                                this.crossrefStatus = this.errorCodes.DONE;
157

    
158
                            }else{
159
                              this.crossrefStatus = this.errorCodes.ERROR;
160
                            }
161
                        },
162
                        err =>{
163
                           console.log(err.status);
164
                           this.crossrefStatus = this.errorCodes.ERROR;
165
                         }
166

    
167
                    );
168
                }else{
169
                  this.crossrefStatus = this.errorCodes.DONE;
170
                }
171
            }
172
         },
173
        err => {
174
          //console.log(err);
175
          this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
176
            data => {
177
              this.crossrefResults = data.items;
178
              this.crossrefPage=page;
179
              this.crossrefResultsNum = data['total-results'];
180
              this.crossrefStatus = this.errorCodes.DONE;
181

    
182
             },
183
            err => {
184
              console.log(err.status);
185
              this.crossrefStatus = this.errorCodes.ERROR;
186
            }
187

    
188
          );
189
        }
190
      );
191

    
192
    }else{
193

    
194

    
195
      this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
196
        data => {
197
            if(data != null) {
198
                this.crossrefResults = data.items;
199
                this.crossrefPage=page;
200
                this.crossrefResultsNum = data['total-results'];
201
                this.crossrefStatus = this.errorCodes.DONE;
202

    
203
            }else{
204
              this.crossrefStatus = this.errorCodes.ERROR;
205
            }
206

    
207
         },
208
        err => {
209
          console.log(err.status);
210
          this.crossrefStatus = this.errorCodes.ERROR;
211
          }
212
      );
213
    }
214
  }
215
  private searchOpenairePubs(term: string, size : number, page : number)  {
216

    
217
          if(this.DOIs.length > 0 ){
218
            this.openairePubsStatus = this.errorCodes.LOADING;
219
            this._searchPublicationsService.searchPublicationsByDois(this.DOIs, null, page, size, []).subscribe(
220
              data => {
221
                  if(data != null) {
222
                      this.openairePubsPage=page;
223
                      this.openairePubsNum = data[0];
224
                      this.openairePubs = data[1];
225
                      this.openairePubsStatus = this.errorCodes.DONE;
226
                      if(this.openairePubsNum == 0){
227
                        this.openairePubsStatus = this.errorCodes.NONE;
228
                      }
229
                  }else {
230
                    this.openairePubsStatus = this.errorCodes.ERROR;
231
                  }
232
               },
233
              err => {
234
                this.openairePubsStatus = this.errorCodes.ERROR;
235
                console.log(err.status);
236
              }
237
            );
238
          }else{
239
            this.openairePubsStatus = this.errorCodes.LOADING;
240
            this._searchPublicationsService.searchPublications('q='+term, null, page, size, []).subscribe(
241
              data => {
242
                  if(data != null) {
243
                      this.openairePubsPage=page;
244
                      this.openairePubsNum = data[0];
245
                      this.openairePubs = data[1];
246
                      this.openairePubsStatus = this.errorCodes.DONE;
247
                      if(this.openairePubsNum == 0){
248
                        this.openairePubsStatus = this.errorCodes.NONE;
249
                      }
250
                  }else {
251
                    this.openairePubsStatus = this.errorCodes.ERROR;
252
                  }
253
               },
254
              err => {
255
                this.openairePubsStatus = this.errorCodes.ERROR;
256
                console.log(err.status);
257
              }
258
            );
259
          }
260
  }
261

    
262
  private searchOrcid (term: string) {
263
    if(this.DOIs.length > 0){
264
      this.orcidStatus = this.errorCodes.NONE;
265
      return;
266
    }
267
    this.orcidStatus = this.errorCodes.LOADING;
268
    this.authorIds = new Array<string>();
269
    this.authorGivenNames = new Array<string>();
270
    this.authorFamilyNames = new Array<string>();
271

    
272
    this.getOrcidAuthor(term);
273

    
274
    console.info('searchOrcid in searchOrcid file');
275
   }
276

    
277
  private readData(data: any) {
278
    this.authorIds.push(data[2].path);
279

    
280
    if(data[0] != null) {
281
      this.authorGivenNames.push(data[0].value);
282
    } else {
283
      this.authorGivenNames.push("");
284
    }
285
    if(data[1] != null) {
286
      this.authorFamilyNames.push(data[1].value);
287
    } else {
288
      this.authorFamilyNames.push("");
289
    }
290
  }
291

    
292
  private  getOrcidAuthor (term: string)  {
293
    this.orcidResultsNum = null;
294

    
295
    //passing structures in order to fill them in service
296
    this._searchOrcidService.searchOrcidAuthor(term, this.authorIds,
297
      this.authorGivenNames, this.authorFamilyNames).subscribe(
298
        data => {
299
          if(data != null && data == true) {
300
            this.getOrcidResultsByIndex(0);
301
          }
302

    
303
          this.orcidStatus = this.errorCodes.NONE;
304

    
305
        },
306
        err => this.errorHandler(err, term)
307

    
308
      );
309
    }
310

    
311
    private  errorHandler(err: any, term: string) {
312
      if(err.status == 404){
313
        this.getOrcidAuthors(term);
314
      } else {
315
        this.orcidStatus = this.errorCodes.ERROR;
316
        console.log(err.status);
317

    
318
      }
319
    }
320

    
321
    private  getOrcidAuthors (term: string)  {
322
      this.orcidResultsNum = null;
323
      this.selectAuthorId = "0";
324
          this.orcidStatus = this.errorCodes.LOADING;
325
      //passing structures in order to fill them in service
326
      this._searchOrcidService.searchOrcidAuthors(term, this.authorIds,
327
        this.authorGivenNames, this.authorFamilyNames).subscribe(
328
          data => {
329
            if(data != null && data == true) {
330
              this.getOrcidResultsByIndex(0);
331
            }else{
332
              this.orcidStatus = this.errorCodes.ERROR;
333
            }
334

    
335
          },
336
          err => {
337
            this.orcidStatus = this.errorCodes.ERROR;
338
            console.log(err.status);
339
          }
340
        );
341
      }
342

    
343

    
344
      private    getOrcidResultsByIndex (index:number) {
345
        if(this.authorIds.length > index) {
346
          this.orcidStatus = this.errorCodes.LOADING;
347
          let id = this.authorIds[index];
348
          this.authorGivenName = this.authorGivenNames[index];
349
          this.authorFamilyName = this.authorFamilyNames[index];
350
          this.getOrcidResultsById(id);
351
        }
352
      }
353
      private    getOrcidResultsById (id:string) {
354
          if(id=="0"){
355
            return;
356
          }
357
          var index = this.authorIds.indexOf(id);
358
          this.authorGivenName = this.authorGivenNames[index];
359
          this.authorFamilyName = this.authorFamilyNames[index];
360
          this.authorId = id;
361
          console.info("getOrcidResultsById: "+id);
362
            this.orcidStatus = this.errorCodes.LOADING;
363
          this._searchOrcidService.searchOrcidPublications(id).subscribe(
364
            data => {
365
              if(data != null) {
366
                this.orcidResults=data['orcid-work'];
367
                this.orcidResultsNum = data['orcid-work'].length;
368
                this.orcidPage = 1;
369
                if((this.orcidResultsNum % this.size) == 0){
370
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
371
                } else{
372
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
373
                }
374

    
375
                this.orcidResultsToShow = this.orcidResults.slice(0,10);
376

    
377
                this.orcidStatus = this.errorCodes.DONE;
378
                if(this.orcidResultsNum == 0){
379
                  this.orcidStatus = this.errorCodes.NONE;
380
                }
381
              } else {
382
                this.orcidResultsNum = 0;
383
                this.totalPages=0;
384
                this.orcidStatus = this.errorCodes.NONE;
385
              }
386

    
387
            },
388
            err => {
389
              console.log(err.status);
390
              this.orcidStatus = this.errorCodes.ERROR;
391
            }
392
          );
393

    
394

    
395
      }
396

    
397
/*
398
Is it USED???
399
private  remove(item){
400
     this.warningMessage = "";
401
     this.infoMessage = "";
402
        var index:number =this.selectedResults.indexOf(item);
403
        item.selected=false;
404
         if (index > -1) {
405
            this.selectedResults.splice(index, 1);
406
            // this.publicationsChange.emit({
407
            //   value: this.selectedResults
408
            // });
409
        }
410

    
411
   }*/
412
private crossrefPageChange($event) {
413
     this.crossrefPage=$event.value;
414
     this.crossrefResults=[];
415
     console.log("Crossref chaenged "+this.crossrefPage);
416
     this.getCrossrefResults(this.keyword,this.size,this.crossrefPage);
417
}
418
private orcidPageChange($event) {
419
     this.orcidPage=$event.value;
420
     this.orcidResultsToShow=[];
421
     this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
422
}
423
private openairePubsPageChange($event) {
424
     this.openairePubsPage=$event.value;
425
     this.searchOpenairePubs(this.keyword,this.size,this.openairePubsPage);
426
}
427
datacitePageChange($event) {
428
  this.datacitePage=$event.value;
429
  this.dataciteResults=[];
430
  this.searchDatacite(this.keyword,10,this.datacitePage);
431
  this.warningMessage = "";
432
  this.infoMessage = "";
433

    
434
}
435
openaireDataPageChange($event) {
436
  this.openaireDataPage=$event.value;
437
  this.openaireData=[];
438
  this.searchOpenaireData(this.keyword,10,this.openaireDataPage);
439
  this.warningMessage = "";
440
  this.infoMessage = "";
441

    
442
}
443

    
444

    
445
  private   isSelected(id:string){
446

    
447
      var found:boolean = false;
448
      this.warningMessage = "";
449
      for (var _i = 0; _i < this.selectedResults.length; _i++) {
450
          let item = this.selectedResults[_i];
451
          if(item.id == id){
452
                found=true;
453
                this.warningMessage = "Publication already in selected list";
454
          }
455
       }
456
       return found;
457

    
458

    
459
    }
460
    // isSelected(id:string){
461
    //
462
    //   var found:boolean = false;
463
    //   this.warningMessage = "";
464
    //   for (var _i = 0; _i < this.selectedResults.length; _i++) {
465
    //       let item = this.selectedResults[_i];
466
    //       if(item.id == id){
467
    //             found=true;
468
    //             break;
469
    //        }
470
    //    }
471
    //    return found;
472
    // }
473
    private searchDatacite (term: string, size : number, page : number) {
474
      this.getDataciteResults(term,size,page);
475
      this.warningMessage = "";
476
      this.infoMessage = "";
477

    
478
     }
479
     private searchOpenaireData (term: string, size : number, page : number) {
480
       if(this.DOIs.length > 0 ){
481
         this.openaireDataStatus = this.errorCodes.LOADING;
482
         this._searchDatasetsService.searchDatasetsByDois(this.DOIs, null, page, size, []).subscribe(
483
           data => {
484
               if(data != null) {
485
                   this.openaireDataPage=page;
486
                   this.openaireDataNum = data[0];
487
                   this.openaireData = data[1];
488
                   this.openaireDataStatus = this.errorCodes.DONE;
489
                   if(this.openaireDataNum == 0){
490
                      this.openaireDataStatus = this.errorCodes.NONE;
491
                   }
492
               }
493
            },
494
           err => {
495
             this.openaireDataStatus = this.errorCodes.ERROR;
496
             console.log(err.status);
497
           }
498
         );
499
       }else{
500
         this._searchDatasetsService.searchDatasets('q='+term+'', null, page, size, []).subscribe(
501
                     data => {
502
                         if(data != null) {
503
                             this.openaireDataPage=page;
504
                             this.openaireDataNum = data[0];
505
                             this.openaireData = data[1];
506
                             this.openaireDataStatus = this.errorCodes.DONE;
507
                             if(this.openaireDataNum == 0){
508
                                this.openaireDataStatus = this.errorCodes.NONE;
509
                             }
510
                         }
511
                      },
512
                     err => {
513
                       this.openaireDataStatus = this.errorCodes.ERROR;
514
                       console.log(err.status);
515
                     }
516
                   );
517
                }
518
       this.warningMessage = "";
519
       this.infoMessage = "";
520

    
521
      }
522
   private getDataciteResults (term: string, size : number, page : number)  {
523
       this._searchDataciteService.searchDataciteResults(term, size, page).subscribe(
524
        data => {
525
          this.dataciteResults = data.data;
526
          this.datacitePage=page;
527
          this.dataciteResultsNum = data.meta.total;
528
          this.dataciteStatus = this.errorCodes.DONE;
529

    
530

    
531
        },
532
        err => {
533
          this.dataciteStatus = this.errorCodes.ERROR;
534
          console.log(err);
535
        }
536

    
537
      );
538
      }
539

    
540
   add(item, itemId,itemSource,itemType, itemUrl, itemTitle, date, accessmode){
541

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

    
545

    
546
    if (itemSource == 'orcid'){
547
        if(result.authors.length ==0 ){
548
          result.authors.push(this.authorGivenName + ', '+ this.authorFamilyName);
549
        }
550
      }
551
      var found:boolean = this.isSelected( result.id);
552

    
553
      this.warningMessage = "";
554
       if (!found) {
555
        this.selectedResults.push(result);
556

    
557
               UIkit.notification({
558
                  message : 'A new research result is selected.',
559
                  status  : 'info',
560
                  timeout : 1000,
561
                  pos     : 'top-center'
562
              });
563

    
564

    
565
      }else{
566
        this.warningMessage = "Research Data already in selected list";
567
        UIkit.notification({
568
            message : 'The  research result is already on your list.',
569
            status  : 'warning',
570
            timeout : 1000,
571
            pos     : 'top-center'
572
        });
573
      }
574

    
575
    }
576

    
577
    public searchSourceChanged(source){
578
      this.searchSource = source;
579
      this.activeTab = (source == "openaire" || source == "all")?"openairePub":source;
580
      if(this.keyword && this.keyword.length > 0){
581
        this.search(true);
582
      }
583
    }
584
    public clickTab(tab){
585
       this.activeTab = tab;
586
    }
587
}
(8-8/11)