Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {SearchCrossrefService} from './service/searchCrossref.service';
3
import {SearchOrcidService} from './service/searchOrcid.service';
4
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
5
import {SearchDataciteService} from './service/searchDatacite.service';
6
import {ErrorCodes} from '../../utils/properties/errorCodes';
7
import {ClaimEntity, ClaimResult} from './claimHelper.class';
8
import {DOI, StringUtils} from '../../utils/string-utils.class';
9
import {EnvProperties} from '../../utils/properties/env-properties';
10
import {Filter, Value} from "../../searchPages/searchUtils/searchHelperClasses.class";
11
import {Observable, forkJoin} from "rxjs";
12
import 'rxjs/add/observable/forkJoin'
13
@Component({
14
  selector: 'claim-result-search-form',
15
  templateUrl: 'claimResultSearchForm.component.html',
16

    
17
})
18
export class ClaimResultSearchFormComponent {
19
  page: number = 1;
20
  size: number = 5;
21
  source: string = "datacite";
22
  type: string = "dataset";
23
  showSearchResults: boolean = false;
24
  @Input() public select: boolean = true;
25
  @Input() public keyword: string = '';
26
  @Input() public selectedResults: ClaimEntity[];
27
  @Input() public properties: EnvProperties;
28
  @Input() localStoragePrefix: string = "";
29
  @Input() basketLimit;
30

    
31
  public errorCodes: ErrorCodes = new ErrorCodes();
32

    
33
  dataciteResults: ClaimEntity[] = [];
34
  dataciteResultsNum: number = null;
35
  dataciteStatus = this.errorCodes.NONE;
36
  datacitePage: number = 1;
37

    
38
  public warningMessage = "";
39
  public infoMessage = "";
40

    
41
  public todayDate = '';
42
  public nextDate = '';
43
  public DOIs: string[] = [];
44
  sub: any;
45
  public activeTab: string = "openaire";
46

    
47

    
48
  crossrefResults: ClaimEntity[] = [];
49
  crossrefResultsNum: number = null;
50
  crossrefPage: number = 1;
51
  crossrefStatus: number = this.errorCodes.NONE;
52

    
53
  openaireTypeValues = [
54
    {title: "Publications", value: "publication", count: null},
55
    {title: "Research Data", value: "dataset", count: null},
56
    {title: "Software", value: "software", count: null},
57
    {title: "Other research products", value: "other", count: null}];
58
  totalOpenaireCount:number = 0;
59
  openaireResultsfilters = [];
60
  openaireResultsPrevFilters = [];
61
  openaireYear = "";
62

    
63
  // public openaireRefineFields: string[] = ["relfunder",  "relproject", "resultbestaccessright", "instancetypename", "resultlanguagename", "community"];
64
  // public openaireRefineFieldsQuery: string = "&refine=true&fields=relfunder&fields=relproject&fields=resultacceptanceyear&fields=resultbestaccessright&fields=instancetypename&fields=resultlanguagename&fields=community";
65
  public openaireRefineFields: string[] = ["relfunder",   "resultbestaccessright", "instancetypename"];
66
  public openaireRefineFieldsQuery: string = "&refine=true&fields=relfunder&fields=resultbestaccessright&fields=instancetypename";
67

    
68
  // public fieldIdsMap=this.searchFields.RESULT_FIELDS;
69
  openaireResults: ClaimEntity[] = [];
70
  openaireResultsNum: number;
71
  openaireResultsPage: number = 1;
72
  openaireResultsStatus: number = this.errorCodes.NONE;
73
  openaireResultsType = "publication";
74

    
75
  orcidResults: ClaimEntity[];
76
  orcidResultsNum: number;
77
  totalPages: number;
78
  orcidResultsToShow: ClaimEntity[];
79
  orcidPage: number = 1;
80
  orcidStatus: number = this.errorCodes.NONE;
81
  authorId: string;
82
  selectAuthorId: string = "0";
83
  authorGivenName: string;
84
  authorFamilyName: string;
85

    
86
  authorIds: string[];
87
  authors = [];
88

    
89
  reloadOpenaire: boolean = true;
90
  reloadCrossref: boolean = false;
91
  reloadDatacite: boolean = false;
92
  reloadOrcid: boolean = false;
93

    
94
  constructor(private _searchDataciteService: SearchDataciteService,
95
              private _searchCrossrefService: SearchCrossrefService,
96
              private _searchOrcidService: SearchOrcidService,
97
              private _searchResearchResultsService: SearchResearchResultsService) {
98
    const myDate = new Date();
99
    this.todayDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
100
    this.nextDate = (myDate.getFullYear() + 100) + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
101

    
102
  }
103

    
104
  ngOnInit() {
105
    if (this.keyword != null && this.keyword.length > 0) {
106
      this.search(false);
107
    }
108
  }
109

    
110
  search(keywordChanged) {
111
    //uncomment to disable search with no keyword
112
    if (this.keyword.length == 0) {
113
      this.totalOpenaireCount = 0;
114
      this.crossrefResultsNum = null;
115
      this.dataciteResultsNum = null;
116
      this.openaireResultsNum = null;
117
      this.orcidResultsNum = null;
118
      this.openaireResults = [];
119
      this.crossrefResults = [];
120
      this.dataciteResults = [];
121
      this.orcidResultsToShow = [];
122
      this.showSearchResults = false;
123
      return;
124
    }
125
    this.warningMessage = "";
126
    this.infoMessage = "";
127
    this.showSearchResults = false;
128
    if (keywordChanged) {
129
      // this.activeTab = "openaire";
130
      this.DOIs = DOI.getDOIsFromString(this.keyword);
131
      this.reloadOpenaire = true;
132
      this.reloadCrossref = true;
133
      this.reloadDatacite = true;
134
      this.reloadOrcid = true;
135
      this.crossrefResultsNum = null;
136
      this.dataciteResultsNum = null;
137
      this.openaireResultsNum = null;
138
      this.orcidResultsNum = null;
139
      // this.openaireResultsType = "publication";
140
    }
141

    
142
    if (this.reloadOpenaire) {
143
      this.openaireResults = [];
144
      this.searchOpenaire(this.keyword, this.size, 1, keywordChanged);
145
      this.reloadOpenaire = false;
146
    }
147
    if (this.reloadCrossref) {
148
      this.crossrefResults = [];
149
      this.getCrossrefResults(this.keyword, this.size, 1);
150
      this.reloadCrossref = false;
151
    }
152
    if (this.reloadDatacite) {
153
      this.dataciteResults = [];
154
      this.searchDatacite(this.keyword, this.size, 1);
155
      this.reloadDatacite = false;
156
    }
157
    if (this.reloadOrcid) {
158
      this.orcidResultsToShow = [];
159
      this.searchOrcid(this.keyword);
160
      this.reloadOrcid = false;
161
    }
162
    this.showSearchResults = true;
163

    
164
  }
165

    
166
  searchOpenaire(keyword, size, page, keywordChanged) {
167

    
168
    if (keywordChanged) {
169
      this.callOpenaireService(keyword, size, page, (this.openaireResultsType == "publication"),"publication");
170
      this.callOpenaireService(keyword, size, page, (this.openaireResultsType == "dataset"),"dataset");
171
      this.callOpenaireService(keyword, size, page, (this.openaireResultsType == "software"),"software");
172
      this.callOpenaireService(keyword, size, page, (this.openaireResultsType == "other"),"other");
173
    } else {
174
      this.callOpenaireService(keyword, size, page, (this.openaireResultsType == this.openaireResultsType),this.openaireResultsType );
175

    
176
    }
177
  }
178

    
179
  private getCrossrefResults(term: string, size: number, page: number) {
180
    this.crossrefStatus = this.errorCodes.LOADING;
181
    // this.crossrefResultsNum = null;
182
    if (this.DOIs.length > 0) {
183
      this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs, this.properties, true).subscribe(
184
        data => {
185
          if (data != null) {
186
            this.crossrefResults = data[1];
187
            this.crossrefPage = page;
188
            this.crossrefResultsNum = data[0];
189
            if (this.crossrefResultsNum == 0) {
190
              this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
191
                data => {
192
                  if (data != null) {
193
                    this.crossrefResults = data[1];
194
                    this.crossrefPage = page;
195
                    this.crossrefResultsNum = data[0];
196
                    this.crossrefStatus = this.errorCodes.DONE;
197
                    // console.log("Crossref " + this.crossrefResultsNum);
198
                    if (this.crossrefResultsNum == 0) {
199
                      this.crossrefStatus = this.errorCodes.NONE;
200
                    }
201

    
202
                  } else {
203
                    this.crossrefStatus = this.errorCodes.ERROR;
204
                  }
205
                },
206
                err => {
207
                  //console.log(err.status);
208
                  ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
209
                  this.crossrefStatus = this.errorCodes.ERROR;
210
                }
211
              );
212
            } else {
213
              this.crossrefStatus = this.errorCodes.DONE;
214
            }
215
          }
216
        },
217
        err => {
218
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + JSON.stringify(this.DOIs), err);
219

    
220
          this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
221
            data => {
222
              this.crossrefResults = data[1];
223
              this.crossrefPage = page;
224
              this.crossrefResultsNum = data[0];
225
              this.crossrefStatus = this.errorCodes.DONE;
226

    
227
            },
228
            err => {
229
              //console.log(err.status);
230
              ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
231
              this.crossrefStatus = this.errorCodes.ERROR;
232
            }
233
          );
234
        }
235
      );
236

    
237
    } else {
238

    
239

    
240
      this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
241
        data => {
242
          if (data != null) {
243
            this.crossrefResults = data[1];
244
            this.crossrefPage = page;
245
            this.crossrefResultsNum = data[0];
246
            this.crossrefStatus = this.errorCodes.DONE;
247
            if (this.crossrefResultsNum == 0) {
248
              this.crossrefStatus = this.errorCodes.NONE;
249
            }
250
          } else {
251
            this.crossrefStatus = this.errorCodes.ERROR;
252
          }
253

    
254
        },
255
        err => {
256
          //console.log(err.status);
257
          ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
258
          this.crossrefStatus = this.errorCodes.ERROR;
259
        }
260
      );
261
    }
262
  }
263

    
264
  private callOpenaireService(term: string, size: number, page: number, mainResults: boolean, type) {
265
    if (mainResults) {
266
      this.openaireResultsStatus = this.errorCodes.LOADING;
267
      this.openaireResultsPrevFilters = this.openaireResultsfilters;
268
    }
269
    if((mainResults && page==1 )||!mainResults){
270
      this.setOpenaireResultsCount(type, null);
271
      this.totalOpenaireCount = 0;
272
    }
273
    if (type == "dataset") {
274
      this.searchResearchResults("dataset", term, size, page, mainResults, type);
275
    } else if (type == "software") {
276
      this.searchResearchResults("software", term, size, page, mainResults, type);
277
    } else if (type == "other") {
278
      this.searchResearchResults("other", term, size, page, mainResults, type);
279
    } else {
280
      this.searchResearchResults("publication", term, size, page, mainResults, type);
281
    }
282

    
283

    
284
  }
285

    
286
  public searchResearchResults(resultType: string, term: string, size: number, page: number, mainResults: boolean, type) {
287
    this._searchResearchResultsService.search(resultType, this.createOpenaireQueryParams(), (mainResults && page==1 ?this.openaireRefineFieldsQuery:null), (mainResults?page:1), (mainResults?size:0), "", (mainResults && page==1 ?this.openaireRefineFields:[]), this.properties).subscribe(
288
      data => {
289
        this.setOpenaireResults(data, mainResults, page, type);
290
      },
291
      err => {
292
        this.setOpenaireResultsError(mainResults, term, err);
293
      }
294
    );
295
  }
296

    
297
  public setOpenaireResults(data, mainResults: boolean, page, type) {
298
    if (data != null) {
299
      if (mainResults) {
300
        this.openaireResultsPage = page;
301
        this.openaireResultsNum = data[0];
302
        this.setOpenaireResultsCount(type, this.openaireResultsNum);
303
        if(data[2] && data[2].length > 0){
304
          this.openaireResultsfilters =  this.checkSelectedFilters(data[2], this.openaireResultsPrevFilters)
305
        }
306
        this.openaireResults = ClaimResultSearchFormComponent.openaire2ClaimResults(data[1], type, this.properties);
307
        this.openaireResultsStatus = this.errorCodes.DONE;
308
        if (this.openaireResultsNum == 0) {
309
          this.openaireResultsStatus = this.errorCodes.NONE;
310
        }
311
      } else {
312
        this.setOpenaireResultsCount(type, data[0]);
313
      }
314
      this.totalOpenaireCount +=+data[0];
315

    
316
    } else {
317
      if (mainResults) {
318
        this.openaireResultsStatus = this.errorCodes.ERROR;
319
      }
320
    }
321
  }
322

    
323
  public setOpenaireResultsError(mainResults: boolean, term, err) {
324
    if (mainResults) {
325
      this.openaireResultsStatus = this.errorCodes.ERROR;
326
    }
327
    ClaimResultSearchFormComponent.handleError("Error getting publications for term: " + term, err);
328
  }
329

    
330
  setOpenaireResultsCount(type, count) {
331
    if (type == "dataset") {
332
      this.openaireTypeValues[1]['count'] = count;
333
    } else if (type == "software") {
334
      this.openaireTypeValues[2]['count'] = count;
335
    } else if (type == "other") {
336
      this.openaireTypeValues[3]['count'] = count;
337
    } else {
338
      this.openaireTypeValues[0]['count'] = count;
339
    }
340

    
341
  }
342

    
343
  private searchOrcid(term: string) {
344
    if (this.DOIs.length > 0 || term.length == 0) {
345
      this.orcidStatus = this.errorCodes.NONE;
346
      this.orcidResultsNum = -1;
347
      return;
348
    }
349
    this.orcidStatus = this.errorCodes.LOADING;
350
    this.authorIds = new Array<string>();
351
    this.authors = [];
352

    
353
    this.getOrcidAuthor(term, true);
354

    
355
  }
356

    
357
  /*private readData(data: any) {
358
    this.authorIds.push(data[2].path);
359
    var author = {};
360
    author['id'] = data[2].path;
361
    if (data[0] != null) {
362
      author['authorGivenName'] = data[0].value;
363
    } else {
364
      author['authorGivenName'] = "";
365
    }
366
    if (data[1] != null) {
367
      author['authorFamilyName'] = data[1].value;
368
    } else {
369
      author['authorFamilyName'] = "";
370
    }
371
    this.authors.push(author);
372
  }*/
373

    
374
  private getOrcidAuthor(term: string, addId) {
375
    this.orcidResultsNum = null;
376
    //passing structures in order to fill them in service
377
    this._searchOrcidService.searchOrcidAuthor(StringUtils.URIEncode(term.replace(/\s/g, "")), this.authorIds,
378
      this.authors, this.properties, addId).subscribe(
379
      data => {
380
        if (data != null && data == true && addId) {
381
          this.getOrcidResults(0);
382
        }
383

    
384

    
385
        // this.orcidStatus = this.errorCodes.NONE;
386

    
387
      },
388
      err => this.errorHandler(err, term)
389
    );
390
  }
391

    
392
  private errorHandler(err: any, term: string) {
393
    if (err.status == 404) {
394
      this.getOrcidAuthors(term);
395
    } else {
396
      this.orcidStatus = this.errorCodes.ERROR;
397
      //console.log(err.status);
398
      ClaimResultSearchFormComponent.handleError("Error getting orcid author for term: " + term, err);
399
    }
400
  }
401

    
402
  private getOrcidAuthors(term: string) {
403
    this.orcidResultsNum = null;
404
    this.selectAuthorId = "0";
405
    this.orcidStatus = this.errorCodes.LOADING;
406
    //passing structures in order to fill them in service
407
    this._searchOrcidService.searchOrcidAuthors(StringUtils.URIEncode(term), this.properties).subscribe(
408
      data => {
409
        this.authorIds = data;
410
        if (data != null) {
411
          for (let i = 0; i < this.authorIds.length; i++) {
412
            this.getOrcidAuthor(this.authorIds[i], (i == 0));
413
          }
414
          if (this.authorIds.length == 0) {
415
            this.orcidStatus = this.errorCodes.NONE;
416
            this.orcidResultsNum = -1;
417
          }
418
        } else {
419
          this.orcidStatus = this.errorCodes.ERROR;
420
        }
421

    
422
      },
423
      err => {
424
        this.orcidStatus = this.errorCodes.ERROR;
425
        //console.log(err.status);
426
        ClaimResultSearchFormComponent.handleError("Error getting orcid authors for term: " + term + " and ids: " + JSON.stringify(this.authorIds), err);
427
      }
428
    );
429
  }
430

    
431

    
432
  private getOrcidResultsById(id) {
433
    for (let i = 0; i < this.authors.length; i++) {
434
      if (this.authors[i].id == id) {
435
        this.getOrcidResults(i);
436
      }
437
    }
438
  }
439

    
440
  private getOrcidResults(index) {
441
    this.authorGivenName = this.authors[index].authorGivenName;
442
    this.authorFamilyName = this.authors[index].authorFamilyName;
443
    this.authorId = this.authors[index].id;
444
    this.orcidStatus = this.errorCodes.LOADING;
445
    this._searchOrcidService.searchOrcidPublications(this.authors[index].id, this.properties, true).subscribe(
446
      data => {
447
        if (data != null) {
448
          this.orcidResults = data;
449
          this.orcidResultsNum = data.length;
450
          this.orcidPage = 1;
451
          if ((this.orcidResultsNum % this.size) == 0) {
452
            this.totalPages = parseInt('' + (this.orcidResultsNum / this.size));
453
          } else {
454
            this.totalPages = parseInt('' + (this.orcidResultsNum / this.size + 1));
455
          }
456

    
457
          this.orcidResultsToShow = this.orcidResults.slice(0, 10);
458

    
459
          this.orcidStatus = this.errorCodes.DONE;
460
          if (this.orcidResultsNum == 0) {
461
            this.orcidStatus = this.errorCodes.NONE;
462
          }
463
          for (let i = 0; i < this.orcidResults.length; i++) {
464
            const entity: ClaimEntity = this.orcidResults[i];
465
            if (!entity.result.authors) {
466
              entity.result.authors = [];
467
            }
468
            entity.result.authors.push(this.authorFamilyName + ', ' + this.authorGivenName);
469
            if (entity.result.DOI != null) {
470
              this.enhanceInfoFromDOI(entity);
471
            }
472
          }
473
        } else {
474
          this.orcidResultsNum = 0;
475
          this.totalPages = 0;
476
          this.orcidStatus = this.errorCodes.NONE;
477
        }
478

    
479
      },
480
      err => {
481
        //console.log(err.status);
482
        ClaimResultSearchFormComponent.handleError("Error getting orcid publications for author id: " + this.authors[index].id, err);
483
        this.orcidStatus = this.errorCodes.ERROR;
484
      }
485
    );
486

    
487

    
488
  }
489

    
490
  private enhanceInfoFromDOI(entity: ClaimEntity) {
491

    
492
    if (entity.result.DOI != null) {
493
      this._searchCrossrefService.searchCrossrefByDOIs([entity.result.DOI], this.properties, true).subscribe(
494
        data => {
495
          if (data != null && data[0] > 0 && data[1]) {
496
            let crossrefResult: ClaimEntity = data[1][0];
497
            if (entity.title == null || entity.title == "") {
498
              entity.title = crossrefResult.title;
499
            }
500
            if (crossrefResult.result.authors) {
501
              entity.result.authors = [];
502
              for (let i = 0; i < crossrefResult.result.authors.length; i++) {
503
                entity.result.authors.push(crossrefResult.result.authors[i]);
504
              }
505
            }
506
            if (entity.result.journal == null || entity.result.journal == "") {
507
              entity.result.journal = crossrefResult.result.journal;
508
            }
509
            if (entity.result.publisher == null || entity.result.publisher == "") {
510
              entity.result.publisher = crossrefResult.result.publisher;
511
            }
512
            if (entity.result.date == null || entity.result.date == "") {
513
              entity.result.date = crossrefResult.result.date;
514
            }
515

    
516
          } else {
517
          }
518
        },
519
        err => {
520
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + entity['DOI'], err);
521
        }
522
      );
523
    }
524
  }
525

    
526
  private crossrefPageChange($event) {
527
    this.crossrefPage = $event.value;
528
    this.crossrefResults = [];
529
    this.getCrossrefResults(this.keyword, this.size, this.crossrefPage);
530
  }
531

    
532
  private orcidPageChange($event) {
533
    this.orcidPage = $event.value;
534
    this.orcidResultsToShow = [];
535
    this.orcidResultsToShow = this.orcidResults.slice(($event.value - 1) * this.size, $event.value * this.size);
536
  }
537

    
538
  private openaireResultsPageChange($event) {
539
    this.openaireResultsPage = $event.value;
540
    this.openaireResults = [];
541
    this.searchOpenaire(this.keyword, this.size, this.openaireResultsPage, false);
542
  }
543

    
544
  datacitePageChange($event) {
545
    this.datacitePage = $event.value;
546
    this.dataciteResults = [];
547
    this.searchDatacite(this.keyword, 10, this.datacitePage);
548
    this.warningMessage = "";
549
    this.infoMessage = "";
550

    
551
  }
552

    
553

    
554
  private searchDatacite(term: string, size: number, page: number) {
555
    this.getDataciteResults(term, size, page);
556
    this.warningMessage = "";
557
    this.infoMessage = "";
558

    
559
  }
560

    
561

    
562

    
563

    
564
  createOpenaireQueryParams():string {
565
    let query = "";
566
    if (this.DOIs.length > 0) {
567
      let doisParams = "";
568
      for (let i = 0; i < this.DOIs.length; i++) {
569
        doisParams += (doisParams.length > 0 ? "&" : "") + 'doi="' + this.DOIs[i] + '"';
570
      }
571
      query += doisParams;
572
    } else if(this.keyword.length > 0){
573
      query += "q=" + StringUtils.quote(StringUtils.URIEncode(this.keyword));
574
    }
575
    if(this.openaireYear.length > 0 ){
576
      query+='&fq=resultacceptanceyear exact \"'+this.openaireYear+'\"'
577
    }
578
    let allFqs = "";
579
    for (let filter of this.openaireResultsfilters){
580
      if(filter.countSelectedValues > 0){
581
        let count_selected=0;
582
        let fq = "";
583
        for (let value of filter.values){
584
          if(value.selected == true){
585
            count_selected++;
586
            fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
587
          }
588
        }
589
        if(count_selected > 0){
590
          fq="&fq="+StringUtils.URIEncode(fq);
591
          allFqs += fq;
592
        }
593
      }
594
    }
595
    return query+allFqs;
596

    
597
  }
598

    
599
  public static openaire2ClaimResults(data, type, properties): ClaimEntity[] {
600
    const claimResults = [];
601
    for (let i = 0; i < data.length; i++) {
602
      let item = data[i];
603
      let entity: ClaimEntity = new ClaimEntity();
604
      entity.result = new ClaimResult();
605
      entity.result.publisher = (item.publisher && item.publisher != "") ? item.publisher : null;
606
      entity.result.journal = null;
607
      entity.result.DOI = null;
608
      entity.id = item.id;
609
      entity.title = item.title.name;
610
      let prefixUrl = "";
611
      if(type == "publication"){
612
        prefixUrl = properties.searchLinkToPublication;
613
      }else if(type == "dataset"){
614
        prefixUrl = properties.searchLinkToDataset;
615
      }else if(type == "software"){
616
        prefixUrl = properties.searchLinkToSoftwareLanding;
617
      }else if(type == "other"){
618
        prefixUrl = properties.searchLinkToOrp;
619
      }
620
      entity.result.url = prefixUrl + entity.id;
621
      entity.result.source = String("openaire");
622
      entity.result.date = (item.year && item.year != "") ? item.year : null;
623
      entity.result.accessRights = (item.title && item.title.accessMode) ? String(item.title.accessMode) : "";
624

    
625
      entity.type = type;
626
      entity.result.embargoEndDate = (item.embargoEndDate && item.embargoEndDate != "") ? item.embargoEndDate : entity.result.embargoEndDate;
627
      if (item.publisher && item.publisher.length > 0) {
628
        entity.result.publisher = item.publisher;
629
      }
630
      entity.result.record = item;
631
      if (item.authors) {
632
        entity.result.authors = [];
633
        for (let author of item.authors) {
634
          entity.result.authors.push(author.fullName);
635
        }
636
      }
637
      claimResults.push(entity);
638
    }
639
    return claimResults;
640

    
641
  }
642

    
643
  private getDataciteResults(term: string, size: number, page: number) {
644
    this.dataciteStatus = this.errorCodes.LOADING;
645
    if (this.DOIs.length > 0) {
646
      let doiObservables: Array<Observable<any>> = new Array();
647
      for(let doi of this.DOIs){
648
        let ob = this._searchDataciteService.getDataciteResultByDOI(doi, this.properties, true);
649
        doiObservables.push(ob);
650

    
651
      }
652
      Observable.forkJoin(doiObservables).subscribe(
653
        data => {
654
          //if DOI not found or an error occured the result will be null -- remove null values
655
          for(let result of data){
656
            if(result){
657
              this.dataciteResults.push(result)
658
            }
659
          }
660
          this.datacitePage = page;
661
          this.dataciteResultsNum = this.dataciteResults.length;
662
          this.dataciteStatus = this.errorCodes.DONE;
663
          if (this.dataciteResultsNum == 0) {
664
            this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
665
              data => {
666
                this.dataciteResults = data[1];
667
                this.datacitePage = page;
668
                this.dataciteResultsNum = data[0];
669
                this.dataciteStatus = this.errorCodes.DONE;
670
                if (this.dataciteResultsNum == 0) {
671
                  this.dataciteStatus = this.errorCodes.NONE;
672
                }
673

    
674
              },
675
              err => {
676

    
677
                this.dataciteStatus = this.errorCodes.ERROR;
678

    
679
                console.log(err);
680
                ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
681
              });
682
          }
683
        }
684

    
685
        );
686
    } else {
687

    
688

    
689
      this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
690
        data => {
691
          this.dataciteResults = data[1];
692
          this.datacitePage = page;
693
          this.dataciteResultsNum = data[0];
694
          // console.log("Datacite " + this.dataciteResultsNum);
695
          this.dataciteStatus = this.errorCodes.DONE;
696
          if (this.dataciteResultsNum == 0) {
697
            this.dataciteStatus = this.errorCodes.NONE;
698
          }
699

    
700
        },
701
        err => {
702
          this.dataciteStatus = this.errorCodes.ERROR;
703
          //console.log(err);
704
          ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
705
        }
706
      );
707
    }
708
  }
709

    
710
  public openaireTypeChanged(type) {
711
    this.openaireResultsType = type;
712
    this.reloadOpenaire = true;
713
    this.openaireYear = "";
714
    this.openaireResultsfilters = [];
715
    this.search(false);
716

    
717
  }
718
  public yearChanged() {
719
    this.reloadOpenaire = true;
720
    this.search(false);
721

    
722
  }
723

    
724
  public clickTab(tab) {
725
    this.activeTab = tab;
726
    // this.search(false);
727
  }
728

    
729
  private static handleError(message: string, error) {
730
    console.error("Claim Result Search Form (component): " + message, error);
731
  }
732

    
733
  //OpenaireFilters:
734

    
735
  getSelectedValues(filter): any {
736
    var selected = [];
737
    if (filter.countSelectedValues > 0) {
738
      for (var i = 0; i < filter.values.length; i++) {
739
        if (filter.values[i].selected) {
740
          selected.push(filter.values[i]);
741
        }
742
      }
743
    }
744
    return selected;
745

    
746
  }
747

    
748
  private removeFilter(value: Value, filter: Filter) {
749
    filter.countSelectedValues--;
750
    if (value.selected == true) {
751
      value.selected = false;
752
    }
753
    this.search(false);
754

    
755
  }
756

    
757
  public countFilters(): number {
758
    let filters = 0;
759
    for (let filter of this.openaireResultsfilters) {
760
      if (filter.countSelectedValues > 0) {
761
        filters += filter.countSelectedValues;
762
      }
763
    }
764
    if (this.keyword.length > 0) {
765
      filters++;
766
    }
767

    
768
    return filters;
769
  }
770

    
771
  private clearFilters() {
772
    for (let i = 0; i < this.openaireResultsfilters.length; i++) {
773
      for (let j = 0; j < this.openaireResultsfilters[i].countSelectedValues; j++) {
774
        if (this.openaireResultsfilters[i].values[j].selected) {
775
          this.openaireResultsfilters[i].values[j].selected = false;
776
        }
777
        this.openaireResultsfilters[i].countSelectedValues = 0;
778
      }
779
    }
780

    
781
  }
782

    
783
  filterChanged($event) {
784
    // console.log("filterChanged");
785
    this.reloadOpenaire = true;
786
    this.search(false);
787

    
788
  }
789
  public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
790
    for(let i=0; i< filters.length ; i++){
791
      let filter:Filter = filters[i];
792
      filter.countSelectedValues = 0;
793
      let prevFilterSelectedValues:string[] = [];
794
      for(let j=0; j< prevFilters.length ; j++){
795
        if(filters[i].filterId == prevFilters[j].filterId){
796
          if(prevFilters[j].countSelectedValues >0){
797
            for(let filterValue of prevFilters[j].values) {
798
              if(filterValue.selected){
799
                prevFilterSelectedValues.push(filterValue.id);
800
              }
801
            }
802

    
803
          }
804
          break;
805
        }
806
      }
807
      for(let filterValue of filter.values) {
808
        if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
809
          filterValue.selected = true;
810
          filter.countSelectedValues++;
811
        }
812

    
813
      }
814

    
815
    }
816
    return filters;
817
  }
818
  countTotalPages(totalResults: number): number {
819
    let totalPages:any = totalResults/(this.size);
820
    if(!(Number.isInteger(totalPages))) {
821
      totalPages = (parseInt(totalPages, 10) + 1);
822
    }
823
    return totalPages;
824
  }
825
  getTotalOpenaireNumber():number{
826
    let count = 0;
827
    for(let i=0; i<this.openaireResultsType.length; i++){
828
      if(this.openaireResultsType[i]["count]"]!=null){
829
        count+= this.openaireResultsType[i]["count]"];
830
      }
831
    }
832
    return count;
833
  }
834
  public _formatName(value, number){
835
    let maxLineLength = 29;
836
    //1 space after checkbox
837
    //3 space before number + parenthesis
838
    if(number && value.length+number.toLocaleString().length +1 +3> maxLineLength){
839
      return value.substr(0, (maxLineLength- 3 -4 - number.toLocaleString().length))+'...';
840
    }else if(!number && value.length + 1 > maxLineLength){
841
      return value.substr(0, (maxLineLength- 3 -1 ))+'...';
842
    }
843
    return value;
844
  }
845
}
(10-10/15)