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
import {NewSearchPageComponent} from "../../searchPages/searchUtils/newSearchPage.component";
14
import {RangeFilter} from "../../utils/rangeFilter/rangeFilterHelperClasses.class";
15
import {SearchUtilsClass} from "../../searchPages/searchUtils/searchUtils.class";
16
import {SearchFields} from "../../utils/properties/searchFields";
17
@Component({
18
  selector: 'claim-result-search-form',
19
  templateUrl: 'claimResultSearchForm.component.html',
20

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

    
35
  public errorCodes: ErrorCodes = new ErrorCodes();
36

    
37
  dataciteResults: ClaimEntity[] = [];
38
  dataciteResultsNum: number = null;
39
  dataciteStatus = this.errorCodes.NONE;
40
  datacitePage: number = 1;
41

    
42
  public warningMessage = "";
43
  public infoMessage = "";
44

    
45
  public todayDate = '';
46
  public nextDate = '';
47
  public DOIs: string[] = [];
48
  sub: any;
49
  public activeTab: string = "openaire";
50

    
51

    
52
  crossrefResults: ClaimEntity[] = [];
53
  crossrefResultsNum: number = null;
54
  crossrefPage: number = 1;
55
  crossrefStatus: number = this.errorCodes.NONE;
56

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

    
67
  // public openaireRefineFields: string[] = ["relfunder",  "relproject", "resultbestaccessright", "instancetypename", "resultlanguagename", "community"];
68
  // public openaireRefineFieldsQuery: string = "&refine=true&fields=relfunder&fields=relproject&fields=resultacceptanceyear&fields=resultbestaccessright&fields=instancetypename&fields=resultlanguagename&fields=community";
69
  public openaireRefineFields: string[] = ["resulttypeid","relfunder",   "resultbestaccessright", "instancetypename"];
70
  public openaireRefineFieldsQuery: string = "&refine=true&fields=resulttypeid&fields=relfunder&fields=resultbestaccessright&fields=instancetypename";
71

    
72
  // public fieldIdsMap=this.searchFields.RESULT_FIELDS;
73
  openaireResults: ClaimEntity[] = [];
74
  openaireResultsNum: number;
75
  openaireResultsPage: number = 1;
76
  openaireResultsStatus: number = this.errorCodes.NONE;
77
  // openaireResultsType = "publication";
78

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

    
90
  authorIds: string[];
91
  authors = [];
92

    
93
  reloadOpenaire: boolean = true;
94
  reloadCrossref: boolean = false;
95
  reloadDatacite: boolean = false;
96
  reloadOrcid: boolean = false;
97

    
98
  //new search
99
  quickFilter: { filter: Filter, selected: boolean, filterId: string, value: string } = {
100
    filter: null,
101
    selected: true,
102
    filterId: "resultbestaccessright",
103
    value: "Open Access"
104
  };
105

    
106
  resultTypes = {publication: true, dataset: true, software: true, other: true};
107
  public rangeFilters: RangeFilter[] = [];
108
  public searchFields: SearchFields = new SearchFields();
109
  public rangeFields:string[][] = this.searchFields.RESULT_RANGE_FIELDS;
110
  constructor(private _searchDataciteService: SearchDataciteService,
111
              private _searchCrossrefService: SearchCrossrefService,
112
              private _searchOrcidService: SearchOrcidService,
113
              private _searchResearchResultsService: SearchResearchResultsService) {
114
    const myDate = new Date();
115
    this.todayDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
116
    this.nextDate = (myDate.getFullYear() + 100) + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
117
    this.rangeFilters = RangeFilter.parse(this.rangeFields,"result");
118
  }
119

    
120
  ngOnInit() {
121
    if (this.keyword != null && this.keyword.length > 0) {
122
      this.search(false);
123
    }
124
  }
125

    
126
  search(keywordChanged) {
127
    //uncomment to disable search with no keyword
128
    if (this.keyword.length == 0) {
129
      this.totalOpenaireCount = 0;
130
      this.crossrefResultsNum = null;
131
      this.dataciteResultsNum = null;
132
      this.openaireResultsNum = null;
133
      this.orcidResultsNum = null;
134
      this.openaireResults = [];
135
      this.crossrefResults = [];
136
      this.dataciteResults = [];
137
      this.orcidResultsToShow = [];
138
      this.showSearchResults = false;
139
      return;
140
    }
141
    this.warningMessage = "";
142
    this.infoMessage = "";
143
    this.showSearchResults = false;
144
    if (keywordChanged) {
145
      // this.activeTab = "openaire";
146
      this.DOIs = DOI.getDOIsFromString(this.keyword);
147
      this.reloadOpenaire = true;
148
      this.reloadCrossref = true;
149
      this.reloadDatacite = true;
150
      this.reloadOrcid = true;
151
      this.crossrefResultsNum = null;
152
      this.dataciteResultsNum = null;
153
      this.openaireResultsNum = null;
154
      this.orcidResultsNum = null;
155
      // this.openaireResultsType = "publication";
156
    }
157

    
158
    if (this.reloadOpenaire) {
159
      this.openaireResults = [];
160
      this.searchOpenaire(this.keyword, this.size, 1, keywordChanged);
161
      this.reloadOpenaire = false;
162
    }
163
    if (this.reloadCrossref) {
164
      this.crossrefResults = [];
165
      this.getCrossrefResults(this.keyword, this.size, 1);
166
      this.reloadCrossref = false;
167
    }
168
    if (this.reloadDatacite) {
169
      this.dataciteResults = [];
170
      this.searchDatacite(this.keyword, this.size, 1);
171
      this.reloadDatacite = false;
172
    }
173
    if (this.reloadOrcid) {
174
      this.orcidResultsToShow = [];
175
      this.searchOrcid(this.keyword);
176
      this.reloadOrcid = false;
177
    }
178
    this.showSearchResults = true;
179

    
180
  }
181

    
182
  searchOpenaire(keyword, size, page, keywordChanged) {
183
    this.searchResearchResults("publication", keyword, size, page, true, "publication");
184
  }
185

    
186
  private getCrossrefResults(term: string, size: number, page: number) {
187
    this.crossrefStatus = this.errorCodes.LOADING;
188
    // this.crossrefResultsNum = null;
189
    if (this.DOIs.length > 0) {
190
      this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs, 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
            if (this.crossrefResultsNum == 0) {
197
              this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
198
                data => {
199
                  if (data != null) {
200
                    this.crossrefResults = data[1];
201
                    this.crossrefPage = page;
202
                    this.crossrefResultsNum = data[0];
203
                    this.crossrefStatus = this.errorCodes.DONE;
204
                    // console.log("Crossref " + this.crossrefResultsNum);
205
                    if (this.crossrefResultsNum == 0) {
206
                      this.crossrefStatus = this.errorCodes.NONE;
207
                    }
208

    
209
                  } else {
210
                    this.crossrefStatus = this.errorCodes.ERROR;
211
                  }
212
                },
213
                err => {
214
                  //console.log(err.status);
215
                  ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
216
                  this.crossrefStatus = this.errorCodes.ERROR;
217
                }
218
              );
219
            } else {
220
              this.crossrefStatus = this.errorCodes.DONE;
221
            }
222
          }
223
        },
224
        err => {
225
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + JSON.stringify(this.DOIs), err);
226

    
227
          this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
228
            data => {
229
              this.crossrefResults = data[1];
230
              this.crossrefPage = page;
231
              this.crossrefResultsNum = data[0];
232
              this.crossrefStatus = this.errorCodes.DONE;
233

    
234
            },
235
            err => {
236
              //console.log(err.status);
237
              ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
238
              this.crossrefStatus = this.errorCodes.ERROR;
239
            }
240
          );
241
        }
242
      );
243

    
244
    } else {
245

    
246

    
247
      this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
248
        data => {
249
          if (data != null) {
250
            this.crossrefResults = data[1];
251
            this.crossrefPage = page;
252
            this.crossrefResultsNum = data[0];
253
            this.crossrefStatus = this.errorCodes.DONE;
254
            if (this.crossrefResultsNum == 0) {
255
              this.crossrefStatus = this.errorCodes.NONE;
256
            }
257
          } else {
258
            this.crossrefStatus = this.errorCodes.ERROR;
259
          }
260

    
261
        },
262
        err => {
263
          //console.log(err.status);
264
          ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
265
          this.crossrefStatus = this.errorCodes.ERROR;
266
        }
267
      );
268
    }
269
  }
270

    
271
  /*private callOpenaireService(term: string, size: number, page: number, mainResults: boolean, type) {
272
    if (mainResults) {
273
      this.openaireResultsStatus = this.errorCodes.LOADING;
274
      this.openaireResultsPrevFilters = this.openaireResultsfilters;
275
    }
276
    if((mainResults && page==1 )||!mainResults){
277
      this.setOpenaireResultsCount(type, null);
278
      this.totalOpenaireCount = 0;
279
    }
280
    if (type == "dataset") {
281
      this.searchResearchResults("dataset", term, size, page, mainResults, type);
282
    } else if (type == "software") {
283
      this.searchResearchResults("software", term, size, page, mainResults, type);
284
    } else if (type == "other") {
285
      this.searchResearchResults("other", term, size, page, mainResults, type);
286
    } else {
287
      this.searchResearchResults("publication", term, size, page, mainResults, type);
288
    }
289

    
290

    
291
  }*/
292

    
293
  public searchResearchResults(resultType: string, term: string, size: number, page: number, mainResults: boolean, type) {
294
    this.openaireResultsStatus = this.errorCodes.LOADING;
295
    this.openaireResultsPrevFilters = this.openaireResultsfilters;
296
    this.openaireResultsNum = 0;
297
    this._searchResearchResultsService.advancedSearchResults('publication', this.createOpenaireQueryParams(), page, size, null, this.properties, this.createOpenaireRefineQueryParams(),  (page==1 ?this.openaireRefineFields:[]), (page==1 ?this.openaireRefineFieldsQuery:null)).subscribe(
298
      data => {
299
        this.setOpenaireResults(data, mainResults, page, type);
300
      },
301
      err => {
302
        this.setOpenaireResultsError(mainResults, term, err);
303
      }
304
    );
305
  }
306

    
307
  public setOpenaireResults(data, mainResults: boolean, page, type) {
308
    if (data != null) {
309

    
310
        this.openaireResultsPage = page;
311
        this.openaireResultsNum = data[0];
312
        // this.setOpenaireResultsCount(type, this.openaireResultsNum);
313
        if(data[2] && data[2].length > 0){
314
          this.openaireResultsfilters =  this.checkSelectedFilters(data[2], this.openaireResultsPrevFilters)
315
        }
316
        this.openaireResults = ClaimResultSearchFormComponent.openaire2ClaimResults(data[1], this.properties);
317
        this.openaireResultsStatus = this.errorCodes.DONE;
318
        if (this.openaireResultsNum == 0) {
319
          this.openaireResultsStatus = this.errorCodes.NONE;
320
        }
321
    } else {
322
      if (mainResults) {
323
        this.openaireResultsStatus = this.errorCodes.ERROR;
324
      }
325
    }
326
  }
327

    
328
  public setOpenaireResultsError(mainResults: boolean, term, err) {
329
    if (mainResults) {
330
      this.openaireResultsStatus = this.errorCodes.ERROR;
331
    }
332
    ClaimResultSearchFormComponent.handleError("Error getting publications for term: " + term, err);
333
  }
334

    
335
  setOpenaireResultsCount(type, count) {
336
    if (type == "dataset") {
337
      this.openaireTypeValues[1]['count'] = count;
338
    } else if (type == "software") {
339
      this.openaireTypeValues[2]['count'] = count;
340
    } else if (type == "other") {
341
      this.openaireTypeValues[3]['count'] = count;
342
    } else {
343
      this.openaireTypeValues[0]['count'] = count;
344
    }
345

    
346
  }
347

    
348
  private searchOrcid(term: string) {
349
    if (this.DOIs.length > 0 || term.length == 0) {
350
      this.orcidStatus = this.errorCodes.NONE;
351
      this.orcidResultsNum = -1;
352
      return;
353
    }
354
    this.orcidStatus = this.errorCodes.LOADING;
355
    this.authorIds = new Array<string>();
356
    this.authors = [];
357

    
358
    this.getOrcidAuthor(term, true);
359

    
360
  }
361

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

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

    
389

    
390
        // this.orcidStatus = this.errorCodes.NONE;
391

    
392
      },
393
      err => this.errorHandler(err, term)
394
    );
395
  }
396

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

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

    
427
      },
428
      err => {
429
        this.orcidStatus = this.errorCodes.ERROR;
430
        //console.log(err.status);
431
        ClaimResultSearchFormComponent.handleError("Error getting orcid authors for term: " + term + " and ids: " + JSON.stringify(this.authorIds), err);
432
      }
433
    );
434
  }
435

    
436

    
437
  private getOrcidResultsById(id) {
438
    for (let i = 0; i < this.authors.length; i++) {
439
      if (this.authors[i].id == id) {
440
        this.getOrcidResults(i);
441
      }
442
    }
443
  }
444

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

    
462
          this.orcidResultsToShow = this.orcidResults.slice(0, 10);
463

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

    
484
      },
485
      err => {
486
        //console.log(err.status);
487
        ClaimResultSearchFormComponent.handleError("Error getting orcid publications for author id: " + this.authors[index].id, err);
488
        this.orcidStatus = this.errorCodes.ERROR;
489
      }
490
    );
491

    
492

    
493
  }
494

    
495
  private enhanceInfoFromDOI(entity: ClaimEntity) {
496

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

    
521
          } else {
522
          }
523
        },
524
        err => {
525
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + entity['DOI'], err);
526
        }
527
      );
528
    }
529
  }
530

    
531
  private crossrefPageChange($event) {
532
    this.crossrefPage = $event.value;
533
    this.crossrefResults = [];
534
    this.getCrossrefResults(this.keyword, this.size, this.crossrefPage);
535
  }
536

    
537
  private orcidPageChange($event) {
538
    this.orcidPage = $event.value;
539
    this.orcidResultsToShow = [];
540
    this.orcidResultsToShow = this.orcidResults.slice(($event.value - 1) * this.size, $event.value * this.size);
541
  }
542

    
543
  private openaireResultsPageChange($event) {
544
    this.openaireResultsPage = $event.value;
545
    this.openaireResults = [];
546
    this.searchOpenaire(this.keyword, this.size, this.openaireResultsPage, false);
547
  }
548

    
549
  datacitePageChange($event) {
550
    this.datacitePage = $event.value;
551
    this.dataciteResults = [];
552
    this.searchDatacite(this.keyword, 10, this.datacitePage);
553
    this.warningMessage = "";
554
    this.infoMessage = "";
555

    
556
  }
557

    
558

    
559
  private searchDatacite(term: string, size: number, page: number) {
560
    this.getDataciteResults(term, size, page);
561
    this.warningMessage = "";
562
    this.infoMessage = "";
563

    
564
  }
565

    
566

    
567

    
568

    
569
  createOpenaireQueryParams():string {
570
    let query = "";
571
/*    if (this.DOIs.length > 0) {
572
      let doisParams = "";
573
      for (let i = 0; i < this.DOIs.length; i++) {
574
        doisParams += (doisParams.length > 0 ? "&" : "") + 'doi="' + this.DOIs[i] + '"';
575
      }
576
      query += doisParams;
577
    } else if(this.keyword.length > 0){
578
      query += "q=" + StringUtils.quote(StringUtils.URIEncode(this.keyword));
579
    }*/
580
  if(this.keyword.length>0){
581
    query+=NewSearchPageComponent.createKeywordQuery("result",this.keyword,"q","=");
582
  }
583
    return query;
584
  }
585
  createOpenaireRefineQueryParams():string {
586
    let allFqs = "";
587
    if(this.openaireYear.length > 0 ){
588
      allFqs+='&fq=resultacceptanceyear exact \"'+this.openaireYear+'\"'
589
    }
590
    for (let filter of this.openaireResultsfilters){
591
      if(filter.countSelectedValues > 0){
592
        let count_selected=0;
593
        let fq = "";
594
        for (let value of filter.values){
595
          if(value.selected == true){
596
            count_selected++;
597
            fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
598
          }
599
        }
600
        if(count_selected > 0){
601
          fq="&fq="+StringUtils.URIEncode(fq);
602
          allFqs += fq;
603
        }
604
      }
605
    }
606
    for (let i=0; i<this.rangeFilters.length; i++){
607
      let filter = this.rangeFilters[i];
608
      //selectedFromValue, selectedToValue, equalityOp, equalityOpFrom, equalityOpTo, filterOp ){
609
      allFqs+= NewSearchPageComponent.createRangeFilterQuery(this.rangeFields[i],filter.selectedFromValue, filter.selectedToValue, " within ", ">=" ,"<=", "and" )
610
    }
611

    
612
    if(this.resultTypes.publication && this.resultTypes.dataset && this.resultTypes.software && this.resultTypes.other){
613
      allFqs += "&type=results";
614
    }else{
615
        if(this.resultTypes.publication) {
616
          allFqs += "&type=publications";
617
        }
618
        if(this.resultTypes.dataset) {
619
          allFqs += "&type=datasets";
620
        }
621
        if(this.resultTypes.software) {
622
          allFqs += "&type=software";
623
        }
624
        if(this.resultTypes.other) {
625
          allFqs += "&type=other";
626
        }
627

    
628
    }
629
    return allFqs;
630

    
631
  }
632

    
633
  public static openaire2ClaimResults(data, properties): ClaimEntity[] {
634
    const claimResults = [];
635
    for (let i = 0; i < data.length; i++) {
636
      let item = data[i];
637
      let entity: ClaimEntity = new ClaimEntity();
638
      entity.result = new ClaimResult();
639
      entity.result.publisher = (item.publisher && item.publisher != "") ? item.publisher : null;
640
      entity.result.journal = null;
641
      entity.result.DOI = null;
642
      entity.id = item.id;
643
      entity.title = item.title.name;
644
      let prefixUrl = "";
645
      if(item.entityType == "publication"){
646
        prefixUrl = properties.searchLinkToPublication;
647
      }else if(item.entityType == "dataset"){
648
        prefixUrl = properties.searchLinkToDataset;
649
      }else if(item.entityType == "software"){
650
        prefixUrl = properties.searchLinkToSoftwareLanding;
651
      }else if(item.entityType == "other"){
652
        prefixUrl = properties.searchLinkToOrp;
653
      }
654
      entity.result.url = prefixUrl + entity.id;
655
      entity.result.source = String("openaire");
656
      entity.result.date = (item.year && item.year != "") ? item.year : null;
657
      entity.result.accessRights = (item.title && item.title.accessMode) ? String(item.title.accessMode) : "";
658

    
659
      entity.type = item.entityType;
660
      entity.result.embargoEndDate = (item.embargoEndDate && item.embargoEndDate != "") ? item.embargoEndDate : entity.result.embargoEndDate;
661
      if (item.publisher && item.publisher.length > 0) {
662
        entity.result.publisher = item.publisher;
663
      }
664
      entity.result.record = item;
665
      if (item.authors) {
666
        entity.result.authors = [];
667
        for (let author of item.authors) {
668
          entity.result.authors.push(author.fullName);
669
        }
670
      }
671
      claimResults.push(entity);
672
    }
673
    return claimResults;
674

    
675
  }
676

    
677
  private getDataciteResults(term: string, size: number, page: number) {
678
    this.dataciteStatus = this.errorCodes.LOADING;
679
    if (this.DOIs.length > 0) {
680
      let doiObservables: Array<Observable<any>> = new Array();
681
      for(let doi of this.DOIs){
682
        let ob = this._searchDataciteService.getDataciteResultByDOI(doi, this.properties, true);
683
        doiObservables.push(ob);
684

    
685
      }
686
      Observable.forkJoin(doiObservables).subscribe(
687
        data => {
688
          //if DOI not found or an error occured the result will be null -- remove null values
689
          for(let result of data){
690
            if(result){
691
              this.dataciteResults.push(result)
692
            }
693
          }
694
          this.datacitePage = page;
695
          this.dataciteResultsNum = this.dataciteResults.length;
696
          this.dataciteStatus = this.errorCodes.DONE;
697
          if (this.dataciteResultsNum == 0) {
698
            this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
699
              data => {
700
                this.dataciteResults = data[1];
701
                this.datacitePage = page;
702
                this.dataciteResultsNum = data[0];
703
                this.dataciteStatus = this.errorCodes.DONE;
704
                if (this.dataciteResultsNum == 0) {
705
                  this.dataciteStatus = this.errorCodes.NONE;
706
                }
707

    
708
              },
709
              err => {
710

    
711
                this.dataciteStatus = this.errorCodes.ERROR;
712

    
713
                console.log(err);
714
                ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
715
              });
716
          }
717
        }
718

    
719
        );
720
    } else {
721

    
722

    
723
      this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
724
        data => {
725
          this.dataciteResults = data[1];
726
          this.datacitePage = page;
727
          this.dataciteResultsNum = data[0];
728
          // console.log("Datacite " + this.dataciteResultsNum);
729
          this.dataciteStatus = this.errorCodes.DONE;
730
          if (this.dataciteResultsNum == 0) {
731
            this.dataciteStatus = this.errorCodes.NONE;
732
          }
733

    
734
        },
735
        err => {
736
          this.dataciteStatus = this.errorCodes.ERROR;
737
          //console.log(err);
738
          ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
739
        }
740
      );
741
    }
742
  }
743

    
744
  public openaireTypeChanged() {
745
    // this.openaireResultsType = type;
746
    this.reloadOpenaire = true;
747
    this.openaireYear = "";
748
    this.openaireResultsfilters = [];
749
    this.search(false);
750

    
751
  }
752
  public yearChanged() {
753
    this.reloadOpenaire = true;
754
    this.search(false);
755

    
756
  }
757

    
758
  public clickTab(tab) {
759
    this.activeTab = tab;
760
    // this.search(false);
761
  }
762

    
763
  private static handleError(message: string, error) {
764
    console.error("Claim Result Search Form (component): " + message, error);
765
  }
766

    
767
  //OpenaireFilters:
768

    
769
  getSelectedValues(filter): any {
770
    var selected = [];
771
    if (filter.countSelectedValues > 0) {
772
      for (var i = 0; i < filter.values.length; i++) {
773
        if (filter.values[i].selected) {
774
          selected.push(filter.values[i]);
775
        }
776
      }
777
    }
778
    return selected;
779

    
780
  }
781

    
782
  private removeFilter(value: Value, filter: Filter) {
783
    filter.countSelectedValues--;
784
    if (value.selected == true) {
785
      value.selected = false;
786
    }
787
    // this.search(false);
788
    this.filterChanged(null);
789
  }
790

    
791
  public countFilters(): number {
792
    let filters = 0;
793
    for (let filter of this.openaireResultsfilters) {
794
      if (filter.countSelectedValues > 0) {
795
        filters += filter.countSelectedValues;
796
      }
797
    }
798
    for (let filter of this.rangeFilters) {
799
      if (filter.selectedFromValue || filter.selectedToValue) {
800
        filters += 1;
801
      }
802
    }
803
    return filters;
804
  }
805

    
806
  private clearFilters() {
807
    for (let i = 0; i < this.openaireResultsfilters.length; i++) {
808
      for (let j = 0; j < this.openaireResultsfilters[i].countSelectedValues; j++) {
809
        if (this.openaireResultsfilters[i].values[j].selected) {
810
          this.openaireResultsfilters[i].values[j].selected = false;
811
        }
812
        this.openaireResultsfilters[i].countSelectedValues = 0;
813
      }
814
    }
815
    for(let filter of this.rangeFilters){
816
      this.removeRangeFilter(filter);
817
    }
818
    this.filterChanged(null);
819
  }
820
  dateFilterChanged(filter:RangeFilter) {
821
    if (filter.selectedFromValue && filter.selectedToValue) {
822
      filter.selectedFromAndToValues = filter.selectedFromValue + "-" + filter.selectedToValue;
823
    } else if (filter.selectedFromValue) {
824
      filter.selectedFromAndToValues = "From " + filter.selectedFromValue;
825
    } else if (filter.selectedToValue) {
826
      filter.selectedFromAndToValues = "Until " + filter.selectedToValue;
827
    }
828
    this.filterChanged(null);
829
  }
830
  filterChanged($event) {
831
    // console.log("filterChanged");
832
    this.reloadOpenaire = true;
833
    this.search(false);
834

    
835
  }
836
  public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
837
    for(let i=0; i< filters.length ; i++){
838
      let filter:Filter = filters[i];
839
      filter.countSelectedValues = 0;
840
      let prevFilterSelectedValues:string[] = [];
841
      for(let j=0; j< prevFilters.length ; j++){
842
        if(filters[i].filterId == prevFilters[j].filterId){
843
          if(prevFilters[j].countSelectedValues >0){
844
            for(let filterValue of prevFilters[j].values) {
845
              if(filterValue.selected){
846
                prevFilterSelectedValues.push(filterValue.id);
847
              }
848
            }
849

    
850
          }
851
          break;
852
        }
853
      }
854
      for(let filterValue of filter.values) {
855
        if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
856
          filterValue.selected = true;
857
          filter.countSelectedValues++;
858
        }
859

    
860
      }
861

    
862
    }
863
    return filters;
864
  }
865
  countTotalPages(totalResults: number): number {
866
    let totalPages:any = totalResults/(this.size);
867
    if(!(Number.isInteger(totalPages))) {
868
      totalPages = (parseInt(totalPages, 10) + 1);
869
    }
870
    return totalPages;
871
  }
872
/*  getTotalOpenaireNumber():number{
873
    let count = 0;
874
    for(let i=0; i<this.openaireResultsType.length; i++){
875
      if(this.openaireResultsType[i]["count]"]!=null){
876
        count+= this.openaireResultsType[i]["count]"];
877
      }
878
    }
879
    return count;
880
  }*/
881
/*  public _formatName(value, number){
882
    let maxLineLength = 29;
883
    //1 space after checkbox
884
    //3 space before number + parenthesis
885
    if(number && value.length+number.toLocaleString().length +1 +3> maxLineLength){
886
      return value.substr(0, (maxLineLength- 3 -4 - number.toLocaleString().length))+'...';
887
    }else if(!number && value.length + 1 > maxLineLength){
888
      return value.substr(0, (maxLineLength- 3 -1 ))+'...';
889
    }
890
    return value;
891
  }*/
892

    
893
  public removeRangeFilter(filter: RangeFilter) {
894
    filter.selectedFromValue = null;
895
    filter.selectedToValue = null;
896
    filter.selectedFromAndToValues = null;
897
    this.filterChanged(null);
898
  }
899

    
900
}
(10-10/15)