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, Subscriber} 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 {SearchFields} from "../../utils/properties/searchFields";
16
@Component({
17
  selector: 'claim-result-search-form',
18
  templateUrl: 'claimResultSearchForm.component.html',
19

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

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

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

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

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

    
50

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

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

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

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

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

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

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

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

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

    
119
  ngOnInit() {
120
    if (this.keyword != null && this.keyword.length > 0) {
121
      this.search(false);
122
    }
123
  }
124
  subscriptions = [];
125
  ngOnDestroy() {
126
    this.subscriptions.forEach(subscription => {
127
      if (subscription instanceof Subscriber) {
128
        subscription.unsubscribe();
129
      }
130
    });
131
  }
132
  search(keywordChanged) {
133
    //uncomment to disable search with no keyword
134
    if (this.keyword.length == 0) {
135
      this.totalOpenaireCount = 0;
136
      this.crossrefResultsNum = null;
137
      this.dataciteResultsNum = null;
138
      this.openaireResultsNum = null;
139
      this.orcidResultsNum = null;
140
      this.openaireResults = [];
141
      this.crossrefResults = [];
142
      this.dataciteResults = [];
143
      this.orcidResultsToShow = [];
144
      this.showSearchResults = false;
145
      return;
146
    }
147
    this.warningMessage = "";
148
    this.infoMessage = "";
149
    this.showSearchResults = false;
150
    if (keywordChanged) {
151
      // this.activeTab = "openaire";
152
      this.DOIs = DOI.getDOIsFromString(this.keyword);
153
      this.reloadOpenaire = true;
154
      this.reloadCrossref = true;
155
      this.reloadDatacite = true;
156
      this.reloadOrcid = true;
157
      this.crossrefResultsNum = null;
158
      this.dataciteResultsNum = null;
159
      this.openaireResultsNum = null;
160
      this.orcidResultsNum = null;
161
      // this.openaireResultsType = "publication";
162
    }
163

    
164
    if (this.reloadOpenaire) {
165
      this.openaireResults = [];
166
      this.searchOpenaire(this.keyword, this.size, 1, keywordChanged);
167
      this.reloadOpenaire = false;
168
    }
169
    if (this.reloadCrossref) {
170
      this.crossrefResults = [];
171
      this.getCrossrefResults(this.keyword, this.size, 1);
172
      this.reloadCrossref = false;
173
    }
174
    if (this.reloadDatacite) {
175
      this.dataciteResults = [];
176
      this.searchDatacite(this.keyword, this.size, 1);
177
      this.reloadDatacite = false;
178
    }
179
    if (this.reloadOrcid) {
180
      this.orcidResultsToShow = [];
181
      this.searchOrcid(this.keyword);
182
      this.reloadOrcid = false;
183
    }
184
    this.showSearchResults = true;
185

    
186
  }
187

    
188
  searchOpenaire(keyword, size, page, keywordChanged) {
189
    this.searchResearchResults("publication", keyword, size, page, true, "publication");
190
  }
191

    
192
  private getCrossrefResults(term: string, size: number, page: number) {
193
    this.crossrefStatus = this.errorCodes.LOADING;
194
    // this.crossrefResultsNum = null;
195
    if (this.DOIs.length > 0) {
196
      this.subscriptions.push(this._searchCrossrefService.searchCrossrefByDOIs(this.DOIs, this.properties, true).subscribe(
197
        data => {
198
          if (data != null) {
199
            this.crossrefResults = data[1];
200
            this.crossrefPage = page;
201
            this.crossrefResultsNum = data[0];
202
            if (this.crossrefResultsNum == 0) {
203
              this.subscriptions.push(this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
204
                data => {
205
                  if (data != null) {
206
                    this.crossrefResults = data[1];
207
                    this.crossrefPage = page;
208
                    this.crossrefResultsNum = data[0];
209
                    this.crossrefStatus = this.errorCodes.DONE;
210
                    // console.log("Crossref " + this.crossrefResultsNum);
211
                    if (this.crossrefResultsNum == 0) {
212
                      this.crossrefStatus = this.errorCodes.NONE;
213
                    }
214

    
215
                  } else {
216
                    this.crossrefStatus = this.errorCodes.ERROR;
217
                  }
218
                },
219
                err => {
220
                  //console.log(err.status);
221
                  ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
222
                  this.crossrefStatus = this.errorCodes.ERROR;
223
                }
224
              ));
225
            } else {
226
              this.crossrefStatus = this.errorCodes.DONE;
227
            }
228
          }
229
        },
230
        err => {
231
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + JSON.stringify(this.DOIs), err);
232

    
233
          this.subscriptions.push(this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
234
            data => {
235
              this.crossrefResults = data[1];
236
              this.crossrefPage = page;
237
              this.crossrefResultsNum = data[0];
238
              this.crossrefStatus = this.errorCodes.DONE;
239

    
240
            },
241
            err => {
242
              //console.log(err.status);
243
              ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
244
              this.crossrefStatus = this.errorCodes.ERROR;
245
            }
246
          ));
247
        }
248
      ));
249

    
250
    } else {
251

    
252

    
253
      this.subscriptions.push(this._searchCrossrefService.searchCrossrefResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
254
        data => {
255
          if (data != null) {
256
            this.crossrefResults = data[1];
257
            this.crossrefPage = page;
258
            this.crossrefResultsNum = data[0];
259
            this.crossrefStatus = this.errorCodes.DONE;
260
            if (this.crossrefResultsNum == 0) {
261
              this.crossrefStatus = this.errorCodes.NONE;
262
            }
263
          } else {
264
            this.crossrefStatus = this.errorCodes.ERROR;
265
          }
266

    
267
        },
268
        err => {
269
          //console.log(err.status);
270
          ClaimResultSearchFormComponent.handleError("Error getting crossref results for term: " + term, err);
271
          this.crossrefStatus = this.errorCodes.ERROR;
272
        }
273
      ));
274
    }
275
  }
276

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

    
296

    
297
  }*/
298

    
299
  public searchResearchResults(resultType: string, term: string, size: number, page: number, mainResults: boolean, type) {
300
    this.openaireResultsStatus = this.errorCodes.LOADING;
301
    this.openaireResultsPrevFilters = this.openaireResultsfilters;
302
    this.openaireResultsNum = 0;
303
    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(
304
      data => {
305
        this.setOpenaireResults(data, mainResults, page, type);
306
      },
307
      err => {
308
        this.setOpenaireResultsError(mainResults, term, err);
309
      }
310
    ));
311
  }
312

    
313
  public setOpenaireResults(data, mainResults: boolean, page, type) {
314
    if (data != null) {
315

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

    
334
  public setOpenaireResultsError(mainResults: boolean, term, err) {
335
    if (mainResults) {
336
      this.openaireResultsStatus = this.errorCodes.ERROR;
337
    }
338
    ClaimResultSearchFormComponent.handleError("Error getting publications for term: " + term, err);
339
  }
340

    
341
  setOpenaireResultsCount(type, count) {
342
    if (type == "dataset") {
343
      this.openaireTypeValues[1]['count'] = count;
344
    } else if (type == "software") {
345
      this.openaireTypeValues[2]['count'] = count;
346
    } else if (type == "other") {
347
      this.openaireTypeValues[3]['count'] = count;
348
    } else {
349
      this.openaireTypeValues[0]['count'] = count;
350
    }
351

    
352
  }
353

    
354
  private searchOrcid(term: string) {
355
    if (this.DOIs.length > 0 || term.length == 0) {
356
      this.orcidStatus = this.errorCodes.NONE;
357
      this.orcidResultsNum = -1;
358
      return;
359
    }
360
    this.orcidStatus = this.errorCodes.LOADING;
361
    this.authorIds = new Array<string>();
362
    this.authors = [];
363

    
364
    this.getOrcidAuthor(term, true);
365

    
366
  }
367

    
368
  /*private readData(data: any) {
369
    this.authorIds.push(data[2].path);
370
    var author = {};
371
    author['id'] = data[2].path;
372
    if (data[0] != null) {
373
      author['authorGivenName'] = data[0].value;
374
    } else {
375
      author['authorGivenName'] = "";
376
    }
377
    if (data[1] != null) {
378
      author['authorFamilyName'] = data[1].value;
379
    } else {
380
      author['authorFamilyName'] = "";
381
    }
382
    this.authors.push(author);
383
  }*/
384

    
385
  private getOrcidAuthor(term: string, addId) {
386
    this.orcidResultsNum = null;
387
    //passing structures in order to fill them in service
388
    this.subscriptions.push(this._searchOrcidService.searchOrcidAuthor(StringUtils.URIEncode(term.replace(/\s/g, "")), this.authorIds,
389
      this.authors, this.properties, addId).subscribe(
390
      data => {
391
        if (data != null && data == true && addId) {
392
          this.getOrcidResults(0);
393
        }
394

    
395

    
396
        // this.orcidStatus = this.errorCodes.NONE;
397

    
398
      },
399
      err => this.errorHandler(err, term)
400
    ));
401
  }
402

    
403
  private errorHandler(err: any, term: string) {
404
    if (err.status == 404) {
405
      this.getOrcidAuthors(term);
406
    } else {
407
      this.orcidStatus = this.errorCodes.ERROR;
408
      //console.log(err.status);
409
      ClaimResultSearchFormComponent.handleError("Error getting orcid author for term: " + term, err);
410
    }
411
  }
412

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

    
433
      },
434
      err => {
435
        this.orcidStatus = this.errorCodes.ERROR;
436
        //console.log(err.status);
437
        ClaimResultSearchFormComponent.handleError("Error getting orcid authors for term: " + term + " and ids: " + JSON.stringify(this.authorIds), err);
438
      }
439
    ));
440
  }
441

    
442

    
443
  private getOrcidResultsById(id) {
444
    for (let i = 0; i < this.authors.length; i++) {
445
      if (this.authors[i].id == id) {
446
        this.getOrcidResults(i);
447
      }
448
    }
449
  }
450

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

    
468
          this.orcidResultsToShow = this.orcidResults.slice(0, 10);
469

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

    
490
      },
491
      err => {
492
        //console.log(err.status);
493
        ClaimResultSearchFormComponent.handleError("Error getting orcid publications for author id: " + this.authors[index].id, err);
494
        this.orcidStatus = this.errorCodes.ERROR;
495
      }
496
    ));
497

    
498

    
499
  }
500

    
501
  private enhanceInfoFromDOI(entity: ClaimEntity) {
502

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

    
527
          } else {
528
          }
529
        },
530
        err => {
531
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + entity['DOI'], err);
532
        }
533
      ));
534
    }
535
  }
536

    
537
  private crossrefPageChange($event) {
538
    this.crossrefPage = $event.value;
539
    this.crossrefResults = [];
540
    this.getCrossrefResults(this.keyword, this.size, this.crossrefPage);
541
  }
542

    
543
  private orcidPageChange($event) {
544
    this.orcidPage = $event.value;
545
    this.orcidResultsToShow = [];
546
    this.orcidResultsToShow = this.orcidResults.slice(($event.value - 1) * this.size, $event.value * this.size);
547
  }
548

    
549
  private openaireResultsPageChange($event) {
550
    this.openaireResultsPage = $event.value;
551
    this.openaireResults = [];
552
    this.searchOpenaire(this.keyword, this.size, this.openaireResultsPage, false);
553
  }
554

    
555
  datacitePageChange($event) {
556
    this.datacitePage = $event.value;
557
    this.dataciteResults = [];
558
    this.searchDatacite(this.keyword, 10, this.datacitePage);
559
    this.warningMessage = "";
560
    this.infoMessage = "";
561

    
562
  }
563

    
564

    
565
  private searchDatacite(term: string, size: number, page: number) {
566
    this.getDataciteResults(term, size, page);
567
    this.warningMessage = "";
568
    this.infoMessage = "";
569

    
570
  }
571

    
572

    
573

    
574

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

    
618
    if(this.resultTypes.publication && this.resultTypes.dataset && this.resultTypes.software && this.resultTypes.other){
619
      allFqs += "&type=results";
620
    }else{
621
        if(this.resultTypes.publication) {
622
          allFqs += "&type=publications";
623
        }
624
        if(this.resultTypes.dataset) {
625
          allFqs += "&type=datasets";
626
        }
627
        if(this.resultTypes.software) {
628
          allFqs += "&type=software";
629
        }
630
        if(this.resultTypes.other) {
631
          allFqs += "&type=other";
632
        }
633

    
634
    }
635
    return allFqs;
636

    
637
  }
638

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

    
665
      entity.type = item.entityType;
666
      entity.result.embargoEndDate = (item.embargoEndDate && item.embargoEndDate != "") ? item.embargoEndDate : entity.result.embargoEndDate;
667
      if (item.publisher && item.publisher.length > 0) {
668
        entity.result.publisher = item.publisher;
669
      }
670
      entity.result.record = item;
671
      if (item.authors) {
672
        entity.result.authors = [];
673
        for (let author of item.authors) {
674
          entity.result.authors.push(author.fullName);
675
        }
676
      }
677
      claimResults.push(entity);
678
    }
679
    return claimResults;
680

    
681
  }
682

    
683
  private getDataciteResults(term: string, size: number, page: number) {
684
    this.dataciteStatus = this.errorCodes.LOADING;
685
    if (this.DOIs.length > 0) {
686
      let doiObservables: Array<Observable<any>> = new Array();
687
      for(let doi of this.DOIs){
688
        let ob = this._searchDataciteService.getDataciteResultByDOI(doi, this.properties, true);
689
        doiObservables.push(ob);
690

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

    
714
              },
715
              err => {
716

    
717
                this.dataciteStatus = this.errorCodes.ERROR;
718

    
719
                console.log(err);
720
                ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
721
              }));
722
          }
723
        }
724

    
725
        ));
726
    } else {
727

    
728

    
729
      this.subscriptions.push(this._searchDataciteService.searchDataciteResults(StringUtils.URIEncode(term), size, page, this.properties, true).subscribe(
730
        data => {
731
          this.dataciteResults = data[1];
732
          this.datacitePage = page;
733
          this.dataciteResultsNum = data[0];
734
          // console.log("Datacite " + this.dataciteResultsNum);
735
          this.dataciteStatus = this.errorCodes.DONE;
736
          if (this.dataciteResultsNum == 0) {
737
            this.dataciteStatus = this.errorCodes.NONE;
738
          }
739

    
740
        },
741
        err => {
742
          this.dataciteStatus = this.errorCodes.ERROR;
743
          //console.log(err);
744
          ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
745
        }
746
      ));
747
    }
748
  }
749

    
750
  public openaireTypeChanged() {
751
    // this.openaireResultsType = type;
752
    this.reloadOpenaire = true;
753
    this.openaireYear = "";
754
    this.openaireResultsfilters = [];
755
    this.search(false);
756

    
757
  }
758
  public yearChanged() {
759
    this.reloadOpenaire = true;
760
    this.search(false);
761

    
762
  }
763

    
764
  public clickTab(tab) {
765
    this.activeTab = tab;
766
    // this.search(false);
767
  }
768

    
769
  private static handleError(message: string, error) {
770
    console.error("Claim Result Search Form (component): " + message, error);
771
  }
772

    
773
  //OpenaireFilters:
774

    
775
  getSelectedValues(filter): any {
776
    var selected = [];
777
    if (filter.countSelectedValues > 0) {
778
      for (var i = 0; i < filter.values.length; i++) {
779
        if (filter.values[i].selected) {
780
          selected.push(filter.values[i]);
781
        }
782
      }
783
    }
784
    return selected;
785

    
786
  }
787

    
788
  private removeFilter(value: Value, filter: Filter) {
789
    filter.countSelectedValues--;
790
    if (value.selected == true) {
791
      value.selected = false;
792
    }
793
    // this.search(false);
794
    this.filterChanged(null);
795
  }
796

    
797
  public countFilters(): number {
798
    let filters = 0;
799
    for (let filter of this.openaireResultsfilters) {
800
      if (filter.countSelectedValues > 0) {
801
        filters += filter.countSelectedValues;
802
      }
803
    }
804
    for (let filter of this.rangeFilters) {
805
      if (filter.selectedFromValue || filter.selectedToValue) {
806
        filters += 1;
807
      }
808
    }
809
    return filters;
810
  }
811

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

    
841
  }
842
  public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
843
    for(let i=0; i< filters.length ; i++){
844
      let filter:Filter = filters[i];
845
      filter.countSelectedValues = 0;
846
      let prevFilterSelectedValues:string[] = [];
847
      for(let j=0; j< prevFilters.length ; j++){
848
        if(filters[i].filterId == prevFilters[j].filterId){
849
          if(prevFilters[j].countSelectedValues >0){
850
            for(let filterValue of prevFilters[j].values) {
851
              if(filterValue.selected){
852
                prevFilterSelectedValues.push(filterValue.id);
853
              }
854
            }
855

    
856
          }
857
          break;
858
        }
859
      }
860
      for(let filterValue of filter.values) {
861
        if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
862
          filterValue.selected = true;
863
          filter.countSelectedValues++;
864
        }
865

    
866
      }
867

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

    
899
  public removeRangeFilter(filter: RangeFilter) {
900
    filter.selectedFromValue = null;
901
    filter.selectedToValue = null;
902
    filter.selectedFromAndToValues = null;
903
    this.filterChanged(null);
904
  }
905

    
906
}
(10-10/15)