Project

General

Profile

1 61381 k.triantaf
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 {forkJoin, Observable, Subscriber} from 'rxjs';
12
import {NewSearchPageComponent} from "../../searchPages/searchUtils/newSearchPage.component";
13
import {RangeFilter} from "../../utils/rangeFilter/rangeFilterHelperClasses.class";
14
import {SearchFields} from "../../utils/properties/searchFields";
15
@Component({
16
  selector: 'claim-result-search-form',
17
  templateUrl: 'claimResultSearchForm.component.html',
18
19
})
20
export class ClaimResultSearchFormComponent {
21
  page: number = 1;
22
  size: number = 5;
23
  source: string = "datacite";
24
  type: string = "dataset";
25
  showSearchResults: boolean = false;
26
  @Input() public select: boolean = true;
27
  @Input() public keyword: string = '';
28
  @Input() public selectedResults: ClaimEntity[];
29
  @Input() public properties: EnvProperties;
30
  @Input() localStoragePrefix: string = "";
31
  @Input() basketLimit;
32
33
  public errorCodes: ErrorCodes = new ErrorCodes();
34
35
  dataciteResults: ClaimEntity[] = [];
36
  dataciteResultsNum: number = null;
37
  dataciteStatus = this.errorCodes.NONE;
38
  datacitePage: number = 1;
39
40
  public warningMessage = "";
41
  public infoMessage = "";
42
43
  public todayDate = '';
44
  public nextDate = '';
45
  public DOIs: string[] = [];
46
  sub: any;
47
  public activeTab: string = "openaire";
48
49
50
  crossrefResults: ClaimEntity[] = [];
51
  crossrefResultsNum: number = null;
52
  crossrefPage: number = 1;
53
  crossrefStatus: number = this.errorCodes.NONE;
54
55
  openaireTypeValues = [
56
    {title: "Publications", value: "publication", count: null},
57
    {title: "Research Data", value: "dataset", count: null},
58
    {title: "Software", value: "software", count: null},
59
    {title: "Other research products", value: "other", count: null}];
60
  totalOpenaireCount:number = 0;
61
  openaireResultsfilters = [];
62
  openaireResultsPrevFilters = [];
63
  openaireYear = "";
64
65
  // public openaireRefineFields: string[] = ["relfunder",  "relproject", "resultbestaccessright", "instancetypename", "resultlanguagename", "community"];
66
  // public openaireRefineFieldsQuery: string = "&refine=true&fields=relfunder&fields=relproject&fields=resultacceptanceyear&fields=resultbestaccessright&fields=instancetypename&fields=resultlanguagename&fields=community";
67
  public openaireRefineFields: string[] = ["resulttypeid","relfunder",   "resultbestaccessright", "instancetypename"];
68
  public openaireRefineFieldsQuery: string = "&refine=true&fields=resulttypeid&fields=relfunder&fields=resultbestaccessright&fields=instancetypename";
69
70
  // public fieldIdsMap=this.searchFields.RESULT_FIELDS;
71
  openaireResults: ClaimEntity[] = [];
72
  openaireResultsNum: number;
73
  openaireResultsPage: number = 1;
74
  openaireResultsStatus: number = this.errorCodes.NONE;
75
  // openaireResultsType = "publication";
76
77
  orcidResults: ClaimEntity[];
78
  orcidResultsNum: number;
79
  totalPages: number;
80
  orcidResultsToShow: ClaimEntity[];
81
  orcidPage: number = 1;
82
  orcidStatus: number = this.errorCodes.NONE;
83
  authorId: string;
84
  selectAuthorId: string = "0";
85
  authorGivenName: string;
86
  authorFamilyName: string;
87
88
  authorIds: string[];
89
  authors = [];
90
91
  reloadOpenaire: boolean = true;
92
  reloadCrossref: boolean = false;
93
  reloadDatacite: boolean = false;
94
  reloadOrcid: boolean = false;
95
96
  //new search
97
  quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string } = {
98
    filter: null,
99
    selected: true,
100
    filterId: "resultbestaccessright",
101
    value: "Open Access"
102
  };
103
104
  resultTypes = {publication: true, dataset: true, software: true, other: true};
105
  public rangeFilters: RangeFilter[] = [];
106
  public searchFields: SearchFields = new SearchFields();
107
  public rangeFields:string[][] = this.searchFields.RESULT_RANGE_FIELDS;
108
  constructor(private _searchDataciteService: SearchDataciteService,
109
              private _searchCrossrefService: SearchCrossrefService,
110
              private _searchOrcidService: SearchOrcidService,
111
              private _searchResearchResultsService: SearchResearchResultsService) {
112
    const myDate = new Date();
113
    this.todayDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
114
    this.nextDate = (myDate.getFullYear() + 100) + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
115
    this.rangeFilters = RangeFilter.parse(this.rangeFields,"result");
116
  }
117
118
  ngOnInit() {
119
    if (this.keyword != null && this.keyword.length > 0) {
120
      this.search(false);
121
    }
122
  }
123
  subscriptions = [];
124
  ngOnDestroy() {
125
    this.subscriptions.forEach(subscription => {
126
      if (subscription instanceof Subscriber) {
127
        subscription.unsubscribe();
128
      }
129
    });
130
  }
131
  search(keywordChanged) {
132
    //uncomment to disable search with no keyword
133
    if (this.keyword.length == 0) {
134
      this.totalOpenaireCount = 0;
135
      this.crossrefResultsNum = null;
136
      this.dataciteResultsNum = null;
137
      this.openaireResultsNum = null;
138
      this.orcidResultsNum = null;
139
      this.openaireResults = [];
140
      this.crossrefResults = [];
141
      this.dataciteResults = [];
142
      this.orcidResultsToShow = [];
143
      this.showSearchResults = false;
144
      return;
145
    }
146
    this.warningMessage = "";
147
    this.infoMessage = "";
148
    this.showSearchResults = false;
149
    if (keywordChanged) {
150
      // this.activeTab = "openaire";
151
      this.DOIs = DOI.getDOIsFromString(this.keyword);
152
      this.reloadOpenaire = true;
153
      this.reloadCrossref = true;
154
      this.reloadDatacite = true;
155
      this.reloadOrcid = true;
156
      this.crossrefResultsNum = null;
157
      this.dataciteResultsNum = null;
158
      this.openaireResultsNum = null;
159
      this.orcidResultsNum = null;
160
      // this.openaireResultsType = "publication";
161
    }
162
163
    if (this.reloadOpenaire) {
164
      this.openaireResults = [];
165
      this.searchOpenaire(this.keyword, this.size, 1, keywordChanged);
166
      this.reloadOpenaire = false;
167
    }
168
    if (this.reloadCrossref) {
169
      this.crossrefResults = [];
170
      this.getCrossrefResults(this.keyword, this.size, 1);
171
      this.reloadCrossref = false;
172
    }
173
    if (this.reloadDatacite) {
174
      this.dataciteResults = [];
175
      this.searchDatacite(this.keyword, this.size, 1);
176
      this.reloadDatacite = false;
177
    }
178
    if (this.reloadOrcid) {
179
      this.orcidResultsToShow = [];
180
      this.searchOrcid(this.keyword);
181
      this.reloadOrcid = false;
182
    }
183
    this.showSearchResults = true;
184
185
  }
186
187
  searchOpenaire(keyword, size, page, keywordChanged) {
188
    this.searchResearchResults("publication", keyword, size, page, true, "publication");
189
  }
190
191
  private getCrossrefResults(term: string, size: number, page: number) {
192
    this.crossrefStatus = this.errorCodes.LOADING;
193
    // this.crossrefResultsNum = null;
194
    if (this.DOIs.length > 0) {
195
      this.subscriptions.push(this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs, this.properties, true).subscribe(
196
        data => {
197
          if (data != null) {
198
            this.crossrefResults = data[1];
199
            this.crossrefPage = page;
200
            this.crossrefResultsNum = data[0];
201
            if (this.crossrefResultsNum == 0) {
202
              this.subscriptions.push(this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
203
                data => {
204
                  if (data != null) {
205
                    this.crossrefResults = data[1];
206
                    this.crossrefPage = page;
207
                    this.crossrefResultsNum = data[0];
208
                    this.crossrefStatus = this.errorCodes.DONE;
209
                    // console.log("Crossref " + this.crossrefResultsNum);
210
                    if (this.crossrefResultsNum == 0) {
211
                      this.crossrefStatus = this.errorCodes.NONE;
212
                    }
213
214
                  } else {
215
                    this.crossrefStatus = this.errorCodes.ERROR;
216
                  }
217
                },
218
                err => {
219
                  //console.log(err.status);
220
                  ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
221
                  this.crossrefStatus = this.errorCodes.ERROR;
222
                }
223
              ));
224
            } else {
225
              this.crossrefStatus = this.errorCodes.DONE;
226
            }
227
          }
228
        },
229
        err => {
230
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + JSON.stringify(this.DOIs), err);
231
232
          this.subscriptions.push(this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
233
            data => {
234
              this.crossrefResults = data[1];
235
              this.crossrefPage = page;
236
              this.crossrefResultsNum = data[0];
237
              this.crossrefStatus = this.errorCodes.DONE;
238
239
            },
240
            err => {
241
              //console.log(err.status);
242
              ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
243
              this.crossrefStatus = this.errorCodes.ERROR;
244
            }
245
          ));
246
        }
247
      ));
248
249
    } else {
250
251
252
      this.subscriptions.push(this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
253
        data => {
254
          if (data != null) {
255
            this.crossrefResults = data[1];
256
            this.crossrefPage = page;
257
            this.crossrefResultsNum = data[0];
258
            this.crossrefStatus = this.errorCodes.DONE;
259
            if (this.crossrefResultsNum == 0) {
260
              this.crossrefStatus = this.errorCodes.NONE;
261
            }
262
          } else {
263
            this.crossrefStatus = this.errorCodes.ERROR;
264
          }
265
266
        },
267
        err => {
268
          //console.log(err.status);
269
          ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
270
          this.crossrefStatus = this.errorCodes.ERROR;
271
        }
272
      ));
273
    }
274
  }
275
276
  /*private callOpenaireService(term: string, size: number, page: number, mainResults: boolean, type) {
277
    if (mainResults) {
278
      this.openaireResultsStatus = this.errorCodes.LOADING;
279
      this.openaireResultsPrevFilters = this.openaireResultsfilters;
280
    }
281
    if((mainResults && page==1 )||!mainResults){
282
      this.setOpenaireResultsCount(type, null);
283
      this.totalOpenaireCount = 0;
284
    }
285
    if (type == "dataset") {
286
      this.searchResearchResults("dataset", term, size, page, mainResults, type);
287
    } else if (type == "software") {
288
      this.searchResearchResults("software", term, size, page, mainResults, type);
289
    } else if (type == "other") {
290
      this.searchResearchResults("other", term, size, page, mainResults, type);
291
    } else {
292
      this.searchResearchResults("publication", term, size, page, mainResults, type);
293
    }
294
295
296
  }*/
297
298
  public searchResearchResults(resultType: string, term: string, size: number, page: number, mainResults: boolean, type) {
299
    this.openaireResultsStatus = this.errorCodes.LOADING;
300
    this.openaireResultsPrevFilters = this.openaireResultsfilters;
301
    this.openaireResultsNum = 0;
302
    this.subscriptions.push(this._searchResearchResultsService.advancedSearchResults('publication', this.createOpenaireQueryParams(), page, size, null, this.properties, this.createOpenaireRefineQueryParams(),  (page==1 ?this.openaireRefineFields:[]), (page==1 ?this.openaireRefineFieldsQuery:null)).subscribe(
303
      data => {
304
        this.setOpenaireResults(data, mainResults, page, type);
305
      },
306
      err => {
307
        this.setOpenaireResultsError(mainResults, term, err);
308
      }
309
    ));
310
  }
311
312
  public setOpenaireResults(data, mainResults: boolean, page, type) {
313
    if (data != null) {
314
315
        this.openaireResultsPage = page;
316
        this.openaireResultsNum = data[0];
317
        // this.setOpenaireResultsCount(type, this.openaireResultsNum);
318
        if(data[2] && data[2].length > 0){
319
          this.openaireResultsfilters =  this.checkSelectedFilters(data[2], this.openaireResultsPrevFilters)
320
        }
321
        this.openaireResults = ClaimResultSearchFormComponent.openaire2ClaimResults(data[1], this.properties);
322
        this.openaireResultsStatus = this.errorCodes.DONE;
323
        if (this.openaireResultsNum == 0) {
324
          this.openaireResultsStatus = this.errorCodes.NONE;
325
        }
326
    } else {
327
      if (mainResults) {
328
        this.openaireResultsStatus = this.errorCodes.ERROR;
329
      }
330
    }
331
  }
332
333
  public setOpenaireResultsError(mainResults: boolean, term, err) {
334
    if (mainResults) {
335
      this.openaireResultsStatus = this.errorCodes.ERROR;
336
    }
337
    ClaimResultSearchFormComponent.handleError("Error getting publications for term: " + term, err);
338
  }
339
340
  setOpenaireResultsCount(type, count) {
341
    if (type == "dataset") {
342
      this.openaireTypeValues[1]['count'] = count;
343
    } else if (type == "software") {
344
      this.openaireTypeValues[2]['count'] = count;
345
    } else if (type == "other") {
346
      this.openaireTypeValues[3]['count'] = count;
347
    } else {
348
      this.openaireTypeValues[0]['count'] = count;
349
    }
350
351
  }
352
353
  private searchOrcid(term: string) {
354
    if (this.DOIs.length > 0 || term.length == 0) {
355
      this.orcidStatus = this.errorCodes.NONE;
356
      this.orcidResultsNum = -1;
357
      return;
358
    }
359
    this.orcidStatus = this.errorCodes.LOADING;
360
    this.authorIds = new Array<string>();
361
    this.authors = [];
362
363
    this.getOrcidAuthor(term, true);
364
365
  }
366
367
  /*private readData(data: any) {
368
    this.authorIds.push(data[2].path);
369
    var author = {};
370
    author['id'] = data[2].path;
371
    if (data[0] != null) {
372
      author['authorGivenName'] = data[0].value;
373
    } else {
374
      author['authorGivenName'] = "";
375
    }
376
    if (data[1] != null) {
377
      author['authorFamilyName'] = data[1].value;
378
    } else {
379
      author['authorFamilyName'] = "";
380
    }
381
    this.authors.push(author);
382
  }*/
383
384
  private getOrcidAuthor(term: string, addId) {
385
    this.orcidResultsNum = null;
386
    //passing structures in order to fill them in service
387
    this.subscriptions.push(this._searchOrcidService.searchOrcidAuthor(StringUtils.URIEncode(term.replace(/\s/g, "")), this.authorIds,
388
      this.authors, this.properties, addId).subscribe(
389
      data => {
390
        if (data != null && data == true && addId) {
391
          this.getOrcidResults(0);
392
        }
393
394
395
        // this.orcidStatus = this.errorCodes.NONE;
396
397
      },
398
      err => this.errorHandler(err, term)
399
    ));
400
  }
401
402
  private errorHandler(err: any, term: string) {
403
    if (err.status == 404) {
404
      this.getOrcidAuthors(term);
405
    } else {
406
      this.orcidStatus = this.errorCodes.ERROR;
407
      //console.log(err.status);
408
      ClaimResultSearchFormComponent.handleError("Error getting orcid author for term: " + term, err);
409
    }
410
  }
411
412
  private getOrcidAuthors(term: string) {
413
    this.orcidResultsNum = null;
414
    this.selectAuthorId = "0";
415
    this.orcidStatus = this.errorCodes.LOADING;
416
    //passing structures in order to fill them in service
417
    this.subscriptions.push(this._searchOrcidService.searchOrcidAuthors(StringUtils.URIEncode(term), this.properties).subscribe(
418
      data => {
419
        this.authorIds = data;
420
        if (data != null) {
421
          for (let i = 0; i < this.authorIds.length; i++) {
422
            this.getOrcidAuthor(this.authorIds[i], (i == 0));
423
          }
424
          if (this.authorIds.length == 0) {
425
            this.orcidStatus = this.errorCodes.NONE;
426
            this.orcidResultsNum = -1;
427
          }
428
        } else {
429
          this.orcidStatus = this.errorCodes.ERROR;
430
        }
431
432
      },
433
      err => {
434
        this.orcidStatus = this.errorCodes.ERROR;
435
        //console.log(err.status);
436
        ClaimResultSearchFormComponent.handleError("Error getting orcid authors for term: " + term + " and ids: " + JSON.stringify(this.authorIds), err);
437
      }
438
    ));
439
  }
440
441
442
  private getOrcidResultsById(id) {
443
    for (let i = 0; i < this.authors.length; i++) {
444
      if (this.authors[i].id == id) {
445
        this.getOrcidResults(i);
446
      }
447
    }
448
  }
449
450
  private getOrcidResults(index) {
451
    this.authorGivenName = this.authors[index].authorGivenName;
452
    this.authorFamilyName = this.authors[index].authorFamilyName;
453
    this.authorId = this.authors[index].id;
454
    this.orcidStatus = this.errorCodes.LOADING;
455
    this.subscriptions.push(this._searchOrcidService.searchOrcidPublications(this.authors[index].id, this.properties, true).subscribe(
456
      data => {
457
        if (data != null) {
458
          this.orcidResults = data;
459
          this.orcidResultsNum = data.length;
460
          this.orcidPage = 1;
461
          if ((this.orcidResultsNum % this.size) == 0) {
462
            this.totalPages = parseInt('' + (this.orcidResultsNum / this.size));
463
          } else {
464
            this.totalPages = parseInt('' + (this.orcidResultsNum / this.size + 1));
465
          }
466
467
          this.orcidResultsToShow = this.orcidResults.slice(0, 10);
468
469
          this.orcidStatus = this.errorCodes.DONE;
470
          if (this.orcidResultsNum == 0) {
471
            this.orcidStatus = this.errorCodes.NONE;
472
          }
473
          for (let i = 0; i < this.orcidResults.length; i++) {
474
            const entity: ClaimEntity = this.orcidResults[i];
475
            if (!entity.result.authors) {
476
              entity.result.authors = [];
477
            }
478
            entity.result.authors.push(this.authorFamilyName + ', ' + this.authorGivenName);
479
            if (entity.result.DOI != null) {
480
              this.enhanceInfoFromDOI(entity);
481
            }
482
          }
483
        } else {
484
          this.orcidResultsNum = 0;
485
          this.totalPages = 0;
486
          this.orcidStatus = this.errorCodes.NONE;
487
        }
488
489
      },
490
      err => {
491
        //console.log(err.status);
492
        ClaimResultSearchFormComponent.handleError("Error getting orcid publications for author id: " + this.authors[index].id, err);
493
        this.orcidStatus = this.errorCodes.ERROR;
494
      }
495
    ));
496
497
498
  }
499
500
  private enhanceInfoFromDOI(entity: ClaimEntity) {
501
502
    if (entity.result.DOI != null) {
503
      this.subscriptions.push(this._searchCrossrefService.searchCrossrefByDOIs([entity.result.DOI], this.properties, true).subscribe(
504
        data => {
505
          if (data != null && data[0] > 0 && data[1]) {
506
            let crossrefResult: ClaimEntity = data[1][0];
507
            if (entity.title == null || entity.title == "") {
508
              entity.title = crossrefResult.title;
509
            }
510
            if (crossrefResult.result.authors) {
511
              entity.result.authors = [];
512
              for (let i = 0; i < crossrefResult.result.authors.length; i++) {
513
                entity.result.authors.push(crossrefResult.result.authors[i]);
514
              }
515
            }
516
            if (entity.result.journal == null || entity.result.journal == "") {
517
              entity.result.journal = crossrefResult.result.journal;
518
            }
519
            if (entity.result.publisher == null || entity.result.publisher == "") {
520
              entity.result.publisher = crossrefResult.result.publisher;
521
            }
522
            if (entity.result.date == null || entity.result.date == "") {
523
              entity.result.date = crossrefResult.result.date;
524
            }
525
526
          } else {
527
          }
528
        },
529
        err => {
530
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + entity['DOI'], err);
531
        }
532
      ));
533
    }
534
  }
535
536
  private crossrefPageChange($event) {
537
    this.crossrefPage = $event.value;
538
    this.crossrefResults = [];
539
    this.getCrossrefResults(this.keyword, this.size, this.crossrefPage);
540
  }
541
542
  private orcidPageChange($event) {
543
    this.orcidPage = $event.value;
544
    this.orcidResultsToShow = [];
545
    this.orcidResultsToShow = this.orcidResults.slice(($event.value - 1) * this.size, $event.value * this.size);
546
  }
547
548
  private openaireResultsPageChange($event) {
549
    this.openaireResultsPage = $event.value;
550
    this.openaireResults = [];
551
    this.searchOpenaire(this.keyword, this.size, this.openaireResultsPage, false);
552
  }
553
554
  datacitePageChange($event) {
555
    this.datacitePage = $event.value;
556
    this.dataciteResults = [];
557
    this.searchDatacite(this.keyword, 10, this.datacitePage);
558
    this.warningMessage = "";
559
    this.infoMessage = "";
560
561
  }
562
563
564
  private searchDatacite(term: string, size: number, page: number) {
565
    this.getDataciteResults(term, size, page);
566
    this.warningMessage = "";
567
    this.infoMessage = "";
568
569
  }
570
571
572
573
574
  createOpenaireQueryParams():string {
575
    let query = "";
576
/*    if (this.DOIs.length > 0) {
577
      let doisParams = "";
578
      for (let i = 0; i < this.DOIs.length; i++) {
579
        doisParams += (doisParams.length > 0 ? "&" : "") + 'doi="' + this.DOIs[i] + '"';
580
      }
581
      query += doisParams;
582
    } else if(this.keyword.length > 0){
583
      query += "q=" + StringUtils.quote(StringUtils.URIEncode(this.keyword));
584
    }*/
585
  if(this.keyword.length>0){
586
    query+=NewSearchPageComponent.createKeywordQuery("result",this.keyword,"q","=");
587
  }
588
    return query;
589
  }
590
  createOpenaireRefineQueryParams():string {
591
    let allFqs = "";
592
    if(this.openaireYear.length > 0 ){
593
      allFqs+='&fq=resultacceptanceyear exact \"'+this.openaireYear+'\"'
594
    }
595
    for (let filter of this.openaireResultsfilters){
596
      if(filter.countSelectedValues > 0){
597
        let count_selected=0;
598
        let fq = "";
599
        for (let value of filter.values){
600
          if(value.selected == true){
601
            count_selected++;
602
            fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
603
          }
604
        }
605
        if(count_selected > 0){
606
          fq="&fq="+StringUtils.URIEncode(fq);
607
          allFqs += fq;
608
        }
609
      }
610
    }
611
    for (let i=0; i<this.rangeFilters.length; i++){
612
      let filter = this.rangeFilters[i];
613
      //selectedFromValue, selectedToValue, equalityOp, equalityOpFrom, equalityOpTo, filterOp ){
614
      allFqs+= NewSearchPageComponent.createRangeFilterQuery(this.rangeFields[i],filter.selectedFromValue, filter.selectedToValue, " within ", ">=" ,"<=", "and" )
615
    }
616
617
    if(this.resultTypes.publication && this.resultTypes.dataset && this.resultTypes.software && this.resultTypes.other){
618
      allFqs += "&type=results";
619
    }else{
620
        if(this.resultTypes.publication) {
621
          allFqs += "&type=publications";
622
        }
623
        if(this.resultTypes.dataset) {
624
          allFqs += "&type=datasets";
625
        }
626
        if(this.resultTypes.software) {
627
          allFqs += "&type=software";
628
        }
629
        if(this.resultTypes.other) {
630
          allFqs += "&type=other";
631
        }
632
633
    }
634
    return allFqs;
635
636
  }
637
638
  public static openaire2ClaimResults(data, properties): ClaimEntity[] {
639
    const claimResults = [];
640
    for (let i = 0; i < data.length; i++) {
641
      let item = data[i];
642
      let entity: ClaimEntity = new ClaimEntity();
643
      entity.result = new ClaimResult();
644
      entity.result.publisher = (item.publisher && item.publisher != "") ? item.publisher : null;
645
      entity.result.journal = null;
646
      entity.result.DOI = null;
647
      entity.id = item.id;
648
      entity.title = item.title.name;
649
      let prefixUrl = "";
650
      if(item.entityType == "publication"){
651
        prefixUrl = properties.searchLinkToPublication;
652
      }else if(item.entityType == "dataset"){
653
        prefixUrl = properties.searchLinkToDataset;
654
      }else if(item.entityType == "software"){
655
        prefixUrl = properties.searchLinkToSoftwareLanding;
656
      }else if(item.entityType == "other"){
657
        prefixUrl = properties.searchLinkToOrp;
658
      }
659
      entity.result.url = prefixUrl + entity.id;
660
      entity.result.source = String("openaire");
661
      entity.result.date = (item.year && item.year != "") ? item.year : null;
662
      entity.result.accessRights = (item.title && item.title.accessMode) ? String(item.title.accessMode) : "";
663
664
      entity.type = item.entityType;
665
      entity.result.embargoEndDate = (item.embargoEndDate && item.embargoEndDate != "") ? item.embargoEndDate : entity.result.embargoEndDate;
666
      if (item.publisher && item.publisher.length > 0) {
667
        entity.result.publisher = item.publisher;
668
      }
669
      entity.result.record = item;
670
      if (item.authors) {
671
        entity.result.authors = [];
672
        for (let author of item.authors) {
673
          entity.result.authors.push(author.fullName);
674
        }
675
      }
676
      claimResults.push(entity);
677
    }
678
    return claimResults;
679
680
  }
681
682
  private getDataciteResults(term: string, size: number, page: number) {
683
    this.dataciteStatus = this.errorCodes.LOADING;
684
    if (this.DOIs.length > 0) {
685
      let doiObservables: Array<Observable<any>> = new Array();
686
      for(let doi of this.DOIs){
687
        let ob = this._searchDataciteService.getDataciteResultByDOI(doi, this.properties, true);
688
        doiObservables.push(ob);
689
690
      }
691
      this.subscriptions.push(forkJoin(doiObservables).subscribe(
692
        data => {
693
          //if DOI not found or an error occured the result will be null -- remove null values
694
          for(let result of data){
695
            if(result){
696
              this.dataciteResults.push(result)
697
            }
698
          }
699
          this.datacitePage = page;
700
          this.dataciteResultsNum = this.dataciteResults.length;
701
          this.dataciteStatus = this.errorCodes.DONE;
702
          if (this.dataciteResultsNum == 0) {
703
            this.subscriptions.push(this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
704
              data => {
705
                this.dataciteResults = data[1];
706
                this.datacitePage = page;
707
                this.dataciteResultsNum = data[0];
708
                this.dataciteStatus = this.errorCodes.DONE;
709
                if (this.dataciteResultsNum == 0) {
710
                  this.dataciteStatus = this.errorCodes.NONE;
711
                }
712
713
              },
714
              err => {
715
716
                this.dataciteStatus = this.errorCodes.ERROR;
717
718
                console.log(err);
719
                ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
720
              }));
721
          }
722
        }
723
724
        ));
725
    } else {
726
727
728
      this.subscriptions.push(this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
729
        data => {
730
          this.dataciteResults = data[1];
731
          this.datacitePage = page;
732
          this.dataciteResultsNum = data[0];
733
          // console.log("Datacite " + this.dataciteResultsNum);
734
          this.dataciteStatus = this.errorCodes.DONE;
735
          if (this.dataciteResultsNum == 0) {
736
            this.dataciteStatus = this.errorCodes.NONE;
737
          }
738
739
        },
740
        err => {
741
          this.dataciteStatus = this.errorCodes.ERROR;
742
          //console.log(err);
743
          ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
744
        }
745
      ));
746
    }
747
  }
748
749
  public openaireTypeChanged() {
750
    // this.openaireResultsType = type;
751
    this.reloadOpenaire = true;
752
    this.openaireYear = "";
753
    this.openaireResultsfilters = [];
754
    this.search(false);
755
756
  }
757
  public yearChanged() {
758
    this.reloadOpenaire = true;
759
    this.search(false);
760
761
  }
762
763
  public clickTab(tab) {
764
    this.activeTab = tab;
765
    // this.search(false);
766
  }
767
768
  private static handleError(message: string, error) {
769
    console.error("Claim Result Search Form (component): " + message, error);
770
  }
771
772
  //OpenaireFilters:
773
774
  getSelectedValues(filter): any {
775
    var selected = [];
776
    if (filter.countSelectedValues > 0) {
777
      for (var i = 0; i < filter.values.length; i++) {
778
        if (filter.values[i].selected) {
779
          selected.push(filter.values[i]);
780
        }
781
      }
782
    }
783
    return selected;
784
785
  }
786
787
  private removeFilter(value: Value, filter: Filter) {
788
    filter.countSelectedValues--;
789
    if (value.selected == true) {
790
      value.selected = false;
791
    }
792
    // this.search(false);
793
    this.filterChanged(null);
794
  }
795
796
  public countFilters(): number {
797
    let filters = 0;
798
    for (let filter of this.openaireResultsfilters) {
799
      if (filter.countSelectedValues > 0) {
800
        filters += filter.countSelectedValues;
801
      }
802
    }
803
    for (let filter of this.rangeFilters) {
804
      if (filter.selectedFromValue || filter.selectedToValue) {
805
        filters += 1;
806
      }
807
    }
808
    return filters;
809
  }
810
811
  private clearFilters() {
812
    for (let i = 0; i < this.openaireResultsfilters.length; i++) {
813
      for (let j = 0; j < this.openaireResultsfilters[i].countSelectedValues; j++) {
814
        if (this.openaireResultsfilters[i].values[j].selected) {
815
          this.openaireResultsfilters[i].values[j].selected = false;
816
        }
817
        this.openaireResultsfilters[i].countSelectedValues = 0;
818
      }
819
    }
820
    for(let filter of this.rangeFilters){
821
      this.removeRangeFilter(filter);
822
    }
823
    this.filterChanged(null);
824
  }
825
  dateFilterChanged(filter:RangeFilter) {
826
    if (filter.selectedFromValue && filter.selectedToValue) {
827
      filter.selectedFromAndToValues = filter.selectedFromValue + "-" + filter.selectedToValue;
828
    } else if (filter.selectedFromValue) {
829
      filter.selectedFromAndToValues = "From " + filter.selectedFromValue;
830
    } else if (filter.selectedToValue) {
831
      filter.selectedFromAndToValues = "Until " + filter.selectedToValue;
832
    }
833
    this.filterChanged(null);
834
  }
835
  filterChanged($event) {
836
    // console.log("filterChanged");
837
    this.reloadOpenaire = true;
838
    this.search(false);
839
840
  }
841
  public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
842
    for(let i=0; i< filters.length ; i++){
843
      let filter:Filter = filters[i];
844
      filter.countSelectedValues = 0;
845
      let prevFilterSelectedValues:string[] = [];
846
      for(let j=0; j< prevFilters.length ; j++){
847
        if(filters[i].filterId == prevFilters[j].filterId){
848
          if(prevFilters[j].countSelectedValues >0){
849
            for(let filterValue of prevFilters[j].values) {
850
              if(filterValue.selected){
851
                prevFilterSelectedValues.push(filterValue.id);
852
              }
853
            }
854
855
          }
856
          break;
857
        }
858
      }
859
      for(let filterValue of filter.values) {
860
        if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
861
          filterValue.selected = true;
862
          filter.countSelectedValues++;
863
        }
864
865
      }
866
867
    }
868
    return filters;
869
  }
870
  countTotalPages(totalResults: number): number {
871
    let totalPages:any = totalResults/(this.size);
872
    if(!(Number.isInteger(totalPages))) {
873
      totalPages = (parseInt(totalPages, 10) + 1);
874
    }
875
    return totalPages;
876
  }
877
/*  getTotalOpenaireNumber():number{
878
    let count = 0;
879
    for(let i=0; i<this.openaireResultsType.length; i++){
880
      if(this.openaireResultsType[i]["count]"]!=null){
881
        count+= this.openaireResultsType[i]["count]"];
882
      }
883
    }
884
    return count;
885
  }*/
886
/*  public _formatName(value, number){
887
    let maxLineLength = 29;
888
    //1 space after checkbox
889
    //3 space before number + parenthesis
890
    if(number && value.length+number.toLocaleString().length +1 +3> maxLineLength){
891
      return value.substr(0, (maxLineLength- 3 -4 - number.toLocaleString().length))+'...';
892
    }else if(!number && value.length + 1 > maxLineLength){
893
      return value.substr(0, (maxLineLength- 3 -1 ))+'...';
894
    }
895
    return value;
896
  }*/
897
898
  public removeRangeFilter(filter: RangeFilter) {
899
    filter.selectedFromValue = null;
900
    filter.selectedToValue = null;
901
    filter.selectedFromAndToValues = null;
902
    this.filterChanged(null);
903
  }
904
905
}