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

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

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

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

    
33

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

    
47
  @Output() resultsChange = new EventEmitter();
48

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

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

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

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

    
65
    public todayDate = '';
66
    public nextDate = '';
67
    public DOIs:string[] = [];
68
    sub: any;
69

    
70

    
71

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

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

    
82
    orcidResults: string[];
83
    orcidResultsNum: number ;
84
    totalPages: number;
85
    orcidResultsToShow: string[];
86
    orcidPage : number = 1;
87
    orcidStatus:number = this.errorCodes.NONE;
88
    authorId: string;
89
    authorGivenName: string;
90
    authorFamilyName: string;
91

    
92
    authorIds: string[];
93
    authorGivenNames: string[];
94
    authorFamilyNames: string[];
95

    
96
    authorsNum : number ;
97

    
98

    
99

    
100
  search(){
101
    this.warningMessage = "";
102
    this.infoMessage = "";
103
    this.DOIs = DOI.getDOIsFromString(this.keyword);
104
    this.getCrossrefResults(this.keyword, this.size,1);
105
    this.searchOrcid(this.keyword);
106
    this.searchOpenairePubs(this.keyword, this.size, 1);
107
    this.searchDatacite(this.keyword,10,1);
108
    this.searchOpenaireData(this.keyword,10,1);
109
    this.showSearchResults = true;
110

    
111
  }
112

    
113
private   getCrossrefResults (term: string, size : number, page : number)  {
114
    this.crossrefStatus = this.errorCodes.LOADING;
115
    if( this.DOIs.length > 0 ){
116
      this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs).subscribe(
117
        data => {
118
            if(data != null) {
119
                this.crossrefResults = data.items;
120
                this.crossrefPage=page;
121
                this.crossrefResultsNum = data['total-results'];
122
                if(data.items == 0){
123
                    this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
124
                        data => {
125
                            if(data != null) {
126
                                this.crossrefResults = data.items;
127
                                this.crossrefPage=page;
128
                                this.crossrefResultsNum = data['total-results'];
129
                                this.crossrefStatus = this.errorCodes.DONE;
130

    
131
                            }else{
132
                              this.crossrefStatus = this.errorCodes.ERROR;
133
                            }
134
                        },
135
                        err =>{
136
                           console.log(err.status);
137
                           this.crossrefStatus = this.errorCodes.ERROR;
138
                         }
139

    
140
                    );
141
                }else{
142
                  this.crossrefStatus = this.errorCodes.DONE;
143
                }
144
            }
145
         },
146
        err => {
147
          //console.log(err);
148
          this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
149
            data => {
150
              this.crossrefResults = data.items;
151
              this.crossrefPage=page;
152
              this.crossrefResultsNum = data['total-results'];
153
              this.crossrefStatus = this.errorCodes.DONE;
154

    
155
             },
156
            err => {
157
              console.log(err.status);
158
              this.crossrefStatus = this.errorCodes.ERROR;
159
            }
160

    
161
          );
162
        }
163
      );
164

    
165
    }else{
166

    
167

    
168
      this._searchCrossrefService.searchCrossrefResults(term, size, page).subscribe(
169
        data => {
170
            if(data != null) {
171
                this.crossrefResults = data.items;
172
                this.crossrefPage=page;
173
                this.crossrefResultsNum = data['total-results'];
174
                this.crossrefStatus = this.errorCodes.DONE;
175

    
176
            }else{
177
              this.crossrefStatus = this.errorCodes.ERROR;
178
            }
179

    
180
         },
181
        err => {
182
          console.log(err.status);
183
          this.crossrefStatus = this.errorCodes.ERROR;
184
          }
185
      );
186
    }
187
  }
188
  private searchOpenairePubs(term: string, size : number, page : number)  {
189

    
190
          if(this.DOIs.length > 0 ){
191
            this.openairePubsStatus = this.errorCodes.LOADING;
192
            this._searchPublicationsService.searchPublicationsByDois(this.DOIs, null, page, size, []).subscribe(
193
              data => {
194
                  if(data != null) {
195
                      this.openairePubsPage=page;
196
                      this.openairePubsNum = data[0];
197
                      this.openairePubs = data[1];
198
                      this.openairePubsStatus = this.errorCodes.DONE;
199
                      if(this.openairePubsNum == 0){
200
                        this.openairePubsStatus = this.errorCodes.NONE;
201
                      }
202
                  }else {
203
                    this.openairePubsStatus = this.errorCodes.ERROR;
204
                  }
205
               },
206
              err => {
207
                this.openairePubsStatus = this.errorCodes.ERROR;
208
                console.log(err.status);
209
              }
210
            );
211
          }else{
212
            this.openairePubsStatus = this.errorCodes.LOADING;
213
            this._searchPublicationsService.searchPublications('q='+term, null, page, size, []).subscribe(
214
              data => {
215
                  if(data != null) {
216
                      this.openairePubsPage=page;
217
                      this.openairePubsNum = data[0];
218
                      this.openairePubs = data[1];
219
                      this.openairePubsStatus = this.errorCodes.DONE;
220
                      if(this.openairePubsNum == 0){
221
                        this.openairePubsStatus = this.errorCodes.NONE;
222
                      }
223
                  }else {
224
                    this.openairePubsStatus = this.errorCodes.ERROR;
225
                  }
226
               },
227
              err => {
228
                this.openairePubsStatus = this.errorCodes.ERROR;
229
                console.log(err.status);
230
              }
231
            );
232
          }
233
  }
234

    
235
  private searchOrcid (term: string) {
236
    if(this.DOIs.length > 0){
237
      this.orcidStatus = this.errorCodes.NONE;
238
      return;
239
    }
240
    this.orcidStatus = this.errorCodes.LOADING;
241
    this.authorIds = new Array<string>();
242
    this.authorGivenNames = new Array<string>();
243
    this.authorFamilyNames = new Array<string>();
244

    
245
    this.getOrcidAuthor(term);
246

    
247
    console.info('searchOrcid in searchOrcid file');
248
   }
249

    
250
  private readData(data: any) {
251
    this.authorIds.push(data[2].path);
252

    
253
    if(data[0] != null) {
254
      this.authorGivenNames.push(data[0].value);
255
    } else {
256
      this.authorGivenNames.push("");
257
    }
258
    if(data[1] != null) {
259
      this.authorFamilyNames.push(data[1].value);
260
    } else {
261
      this.authorFamilyNames.push("");
262
    }
263
  }
264

    
265
  private  getOrcidAuthor (term: string)  {
266
    this.orcidResultsNum = null;
267

    
268
    //passing structures in order to fill them in service
269
    this._searchOrcidService.searchOrcidAuthor(term, this.authorIds,
270
      this.authorGivenNames, this.authorFamilyNames).subscribe(
271
        data => {
272
          if(data != null && data == true) {
273
            this.getOrcidResultsById(0);
274
          }
275

    
276
          this.orcidStatus = this.errorCodes.NONE;
277

    
278
        },
279
        err => this.errorHandler(err, term)
280

    
281
      );
282
    }
283

    
284
    private  errorHandler(err: any, term: string) {
285
      if(err.status == 404){
286
        this.getOrcidAuthors(term);
287
      } else {
288
        this.orcidStatus = this.errorCodes.ERROR;
289
        console.log(err.status);
290

    
291
      }
292
    }
293

    
294
    private  getOrcidAuthors (term: string)  {
295
      this.orcidResultsNum = null;
296

    
297
      //passing structures in order to fill them in service
298
      this._searchOrcidService.searchOrcidAuthors(term, this.authorIds,
299
        this.authorGivenNames, this.authorFamilyNames).subscribe(
300
          data => {
301
            if(data != null && data == true) {
302
              this.getOrcidResultsById(0);
303
            }else{
304
              this.orcidStatus = this.errorCodes.ERROR;
305
            }
306

    
307
          },
308
          err => {
309
            this.orcidStatus = this.errorCodes.ERROR;
310
            console.log(err.status);
311
          }
312
        );
313
      }
314

    
315

    
316
      private    getOrcidResultsById (index:number) {
317
        if(this.authorIds.length > index) {
318
          this.orcidStatus = this.errorCodes.LOADING;
319
          let id = this.authorIds[index];
320
          this.authorGivenName = this.authorGivenNames[index];
321
          this.authorFamilyName = this.authorFamilyNames[index];
322
          this.authorId = id;
323
          console.info("getOrcidResultsById: "+id);
324
          this._searchOrcidService.searchOrcidPublications(id).subscribe(
325
            data => {
326
              if(data != null) {
327
                this.orcidResults=data['orcid-work'];
328
                this.orcidResultsNum = data['orcid-work'].length;
329
                this.orcidPage = 1;
330
                if((this.orcidResultsNum % this.size) == 0){
331
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size));
332
                } else{
333
                  this.totalPages=parseInt(''+(this.orcidResultsNum/this.size+1));
334
                }
335

    
336
                this.orcidResultsToShow = this.orcidResults.slice(0,10);
337

    
338
                this.orcidStatus = this.errorCodes.DONE;
339
                if(this.orcidResultsNum == 0){
340
                  this.orcidStatus = this.errorCodes.NONE;
341
                }
342
              } else {
343
                this.orcidResultsNum = 0;
344
                this.totalPages=0;
345
                this.orcidStatus = this.errorCodes.NONE;
346
              }
347

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

    
355
        }
356
      }
357

    
358
/*
359
Is it USED???
360
private  remove(item){
361
     this.warningMessage = "";
362
     this.infoMessage = "";
363
        var index:number =this.selectedResults.indexOf(item);
364
        item.selected=false;
365
         if (index > -1) {
366
            this.selectedResults.splice(index, 1);
367
            // this.publicationsChange.emit({
368
            //   value: this.selectedResults
369
            // });
370
        }
371

    
372
   }*/
373
private crossrefPageChange($event) {
374
     this.crossrefPage=$event.value;
375
     this.crossrefResults=[];
376
     console.log("Crossref chaenged "+this.crossrefPage);
377
     this.getCrossrefResults(this.keyword,this.size,this.crossrefPage);
378
}
379
private orcidPageChange($event) {
380
     this.orcidPage=$event.value;
381
     this.orcidResultsToShow=[];
382
     this.orcidResultsToShow = this.orcidResults.slice(($event.value-1)*this.size, $event.value*this.size);
383
}
384
private openairePubsPageChange($event) {
385
     this.openairePubsPage=$event.value;
386
     this.searchOpenairePubs(this.keyword,this.size,this.openairePubsPage);
387
}
388
datacitePageChange($event) {
389
  this.datacitePage=$event.value;
390
  this.dataciteResults=[];
391
  this.searchDatacite(this.keyword,10,this.datacitePage);
392
  this.warningMessage = "";
393
  this.infoMessage = "";
394

    
395
}
396
openaireDataPageChange($event) {
397
  this.openaireDataPage=$event.value;
398
  this.openaireData=[];
399
  this.searchOpenaireData(this.keyword,10,this.openaireDataPage);
400
  this.warningMessage = "";
401
  this.infoMessage = "";
402

    
403
}
404

    
405

    
406
  private   isSelected(id:string){
407

    
408
      var found:boolean = false;
409
      this.warningMessage = "";
410
      for (var _i = 0; _i < this.selectedResults.length; _i++) {
411
          let item = this.selectedResults[_i];
412
          if(item.id == id){
413
                found=true;
414
                this.warningMessage = "Publication already in selected list";
415
          }
416
       }
417
       return found;
418

    
419

    
420
    }
421
    // isSelected(id:string){
422
    //
423
    //   var found:boolean = false;
424
    //   this.warningMessage = "";
425
    //   for (var _i = 0; _i < this.selectedResults.length; _i++) {
426
    //       let item = this.selectedResults[_i];
427
    //       if(item.id == id){
428
    //             found=true;
429
    //             break;
430
    //        }
431
    //    }
432
    //    return found;
433
    // }
434
    private searchDatacite (term: string, size : number, page : number) {
435
      this.getDataciteResults(term,size,page);
436
      this.warningMessage = "";
437
      this.infoMessage = "";
438

    
439
     }
440
     private searchOpenaireData (term: string, size : number, page : number) {
441
       if(this.DOIs.length > 0 ){
442
         this.openaireDataStatus = this.errorCodes.LOADING;
443
         this._searchDatasetsService.searchDatasetsByDois(this.DOIs, null, page, size, []).subscribe(
444
           data => {
445
               if(data != null) {
446
                   this.openaireDataPage=page;
447
                   this.openaireDataNum = data[0];
448
                   this.openaireData = data[1];
449
                   this.openaireDataStatus = this.errorCodes.DONE;
450
                   if(this.openaireDataNum == 0){
451
                      this.openaireDataStatus = this.errorCodes.NONE;
452
                   }
453
               }
454
            },
455
           err => {
456
             this.openaireDataStatus = this.errorCodes.ERROR;
457
             console.log(err.status);
458
           }
459
         );
460
       }else{
461
         this._searchDatasetsService.searchDatasets('q='+term+'', null, page, size, []).subscribe(
462
                     data => {
463
                         if(data != null) {
464
                             this.openaireDataPage=page;
465
                             this.openaireDataNum = data[0];
466
                             this.openaireData = data[1];
467
                             this.openaireDataStatus = this.errorCodes.DONE;
468
                             if(this.openaireDataNum == 0){
469
                                this.openaireDataStatus = this.errorCodes.NONE;
470
                             }
471
                         }
472
                      },
473
                     err => {
474
                       this.openaireDataStatus = this.errorCodes.ERROR;
475
                       console.log(err.status);
476
                     }
477
                   );
478
                }
479
       this.warningMessage = "";
480
       this.infoMessage = "";
481

    
482
      }
483
   private getDataciteResults (term: string, size : number, page : number)  {
484
       this._searchDataciteService.searchDataciteResults(term, size, page).subscribe(
485
        data => {
486
          this.dataciteResults = data.docs;
487
          this.datacitePage=page;
488
          this.dataciteResultsNum = data.numFound;
489
          this.dataciteStatus = this.errorCodes.DONE;
490

    
491

    
492
        },
493
        err => {
494
          this.dataciteStatus = this.errorCodes.ERROR;
495
          console.log(err);
496
        }
497

    
498
      );
499
      }
500

    
501
   add(item, itemId,itemSource,itemType, itemUrl, itemTitle, date, accessmode){
502
     console.log(' adding ' + itemType + " From " + itemSource+"  "+ itemTitle);
503
     var result: ClaimResult ;
504
      if(itemSource == 'datacite'){
505
        result = {id: itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date : date};
506
      }else if (itemSource == 'openaire'){
507
        //TODO put right access rights
508
        // result = {id:itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: accessMode, embargoEndDate: this.nextDate, date: date};
509
        result = {id:itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: accessmode, embargoEndDate: this.nextDate, date : date};
510
      }else if(itemSource == 'crossref'){
511
        date = (date == null) ? null : date.substring(0,10);
512
        result = {id: itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date: date};
513
      }else if (itemSource == 'orcid'){
514
        date = (date == null) ? null : date + "-01.-01"
515
          result = {id:itemId, type :itemType, source : itemSource, title: itemTitle,url: itemUrl, result: item, accessRights: 'OPEN', embargoEndDate: this.nextDate, date: date};
516
      }
517
      var found:boolean = this.isSelected( result.id);
518

    
519
      this.warningMessage = "";
520
       if (!found) {
521
        this.selectedResults.push(result);
522
        this.resultsChange.emit({
523
           value: this.selectedResults
524
         });
525
      }else{
526
        this.warningMessage = "Dataset already in selected list";
527
      }
528

    
529
    }
530

    
531

    
532
Pag
533
}
(8-8/9)