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 {SearchPublicationsService} from '../../services/searchPublications.service';
5
import {SearchDataciteService} from './service/searchDatacite.service';
6
import {SearchDatasetsService} from '../../services/searchDatasets.service';
7
import {SearchSoftwareService} from '../../services/searchSoftware.service';
8
import {SearchOrpsService} from '../../services/searchOrps.service';
9

    
10
import {ErrorCodes} from '../../utils/properties/errorCodes';
11
import {ClaimEntity, ClaimResult} from './claimHelper.class';
12
import {DOI, StringUtils} from '../../utils/string-utils.class';
13
import {EnvProperties} from '../../utils/properties/env-properties';
14
import {Filter, Value} from "../../searchPages/searchUtils/searchHelperClasses.class";
15

    
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

    
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[] = ["relfunder",   "resultbestaccessright", "instancetypename"];
68
  public openaireRefineFieldsQuery: string = "&refine=true&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
  constructor(private _searchDataciteService: SearchDataciteService, private _searchDatasetsService: SearchDatasetsService, private _searchSoftwareService: SearchSoftwareService,
97
              private _searchCrossrefService: SearchCrossrefService, private _searchOrcidService: SearchOrcidService, private _searchPublicationsService: SearchPublicationsService,
98
              private _searchORPService: SearchOrpsService) {
99
    const myDate = new Date();
100
    this.todayDate = myDate.getFullYear() + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
101
    this.nextDate = (myDate.getFullYear() + 100) + "-" + (myDate.getMonth() + 1) + "-" + myDate.getDate();
102

    
103
  }
104

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

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

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

    
165
  }
166

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

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

    
177
    }
178
  }
179

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

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

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

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

    
238
    } else {
239

    
240

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

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

    
265
  private callOpenaireService(term: string, size: number, page: number, mainResults: boolean, type) {
266
    if (mainResults) {
267
      this.openaireResultsStatus = this.errorCodes.LOADING;
268
      this.openaireResultsPrevFilters = this.openaireResultsfilters;
269
    }
270
    if((mainResults && page==1 )||!mainResults){
271
      this.setOpenaireResultsCount(type, null);
272
      this.totalOpenaireCount = 0;
273
    }
274
    if (type == "dataset") {
275
      this._searchDatasetsService.searchDatasets(this.createOpenaireQueryParams(), (mainResults && page==1 ?this.openaireRefineFieldsQuery:null), (mainResults?page:1), (mainResults?size:0), "", (mainResults && page==1 ?this.openaireRefineFields:[]), this.properties).subscribe(
276
        data => {
277
          this.setOpenaireResults(data, mainResults, page, type);
278
        },
279
        err => {
280
          this.setOpenaireResultsError(mainResults, term, err);
281
        }
282
      );
283
    } else if (type == "software") {
284
      this._searchSoftwareService.searchSoftware(this.createOpenaireQueryParams(), (mainResults && page==1 ?this.openaireRefineFieldsQuery:null), (mainResults?page:1), (mainResults?size:0), "", (mainResults && page==1 ?this.openaireRefineFields:[]), this.properties).subscribe(
285
        data => {
286
          this.setOpenaireResults(data, mainResults, page, type);
287
        },
288
        err => {
289
          this.setOpenaireResultsError(mainResults, term, err);
290
        }
291
      );
292
    } else if (type == "other") {
293
      this._searchORPService.searchOrps(this.createOpenaireQueryParams(), (mainResults && page==1 ?this.openaireRefineFieldsQuery:null), (mainResults?page:1), (mainResults?size:0), "", (mainResults && page==1 ?this.openaireRefineFields:[]), this.properties).subscribe(
294
        data => {
295
          this.setOpenaireResults(data, mainResults, page, type);
296
        },
297
        err => {
298
          this.setOpenaireResultsError(mainResults, term, err);
299
        }
300
      );
301
    } else {
302
      this._searchPublicationsService.searchPublications(this.createOpenaireQueryParams(), (mainResults && page==1 ?this.openaireRefineFieldsQuery:null), (mainResults?page:1), (mainResults?size:0), "", (mainResults && page==1 ?this.openaireRefineFields:[]), this.properties).subscribe(
303
        data => {
304
          this.setOpenaireResults(data, mainResults, page, type);
305
        },
306
        err => {
307
          this.setOpenaireResultsError(mainResults, term, err);
308
        }
309
      );
310
    }
311

    
312

    
313
  }
314

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

    
334
    } else {
335
      if (mainResults) {
336
        this.openaireResultsStatus = this.errorCodes.ERROR;
337
      }
338
    }
339
  }
340

    
341
  public setOpenaireResultsError(mainResults: boolean, term, err) {
342
    if (mainResults) {
343
      this.openaireResultsStatus = this.errorCodes.ERROR;
344
    }
345
    ClaimResultSearchFormComponent.handleError("Error getting publications for term: " + term, err);
346
  }
347

    
348
  setOpenaireResultsCount(type, count) {
349
    if (type == "dataset") {
350
      this.openaireTypeValues[1]['count'] = count;
351
    } else if (type == "software") {
352
      this.openaireTypeValues[2]['count'] = count;
353
    } else if (type == "other") {
354
      this.openaireTypeValues[3]['count'] = count;
355
    } else {
356
      this.openaireTypeValues[0]['count'] = count;
357
    }
358

    
359
  }
360

    
361
  private searchOrcid(term: string) {
362
    if (this.DOIs.length > 0 || term.length == 0) {
363
      this.orcidStatus = this.errorCodes.NONE;
364
      this.orcidResultsNum = -1;
365
      return;
366
    }
367
    this.orcidStatus = this.errorCodes.LOADING;
368
    this.authorIds = new Array<string>();
369
    this.authors = [];
370

    
371
    this.getOrcidAuthor(term, true);
372

    
373
  }
374

    
375
  /*private readData(data: any) {
376
    this.authorIds.push(data[2].path);
377
    var author = {};
378
    author['id'] = data[2].path;
379
    if (data[0] != null) {
380
      author['authorGivenName'] = data[0].value;
381
    } else {
382
      author['authorGivenName'] = "";
383
    }
384
    if (data[1] != null) {
385
      author['authorFamilyName'] = data[1].value;
386
    } else {
387
      author['authorFamilyName'] = "";
388
    }
389
    this.authors.push(author);
390
  }*/
391

    
392
  private getOrcidAuthor(term: string, addId) {
393
    this.orcidResultsNum = null;
394
    //passing structures in order to fill them in service
395
    this._searchOrcidService.searchOrcidAuthor(term.replace(/\s/g, ""), this.authorIds,
396
      this.authors, this.properties, addId).subscribe(
397
      data => {
398
        if (data != null && data == true && addId) {
399
          this.getOrcidResults(0);
400
        }
401

    
402

    
403
        // this.orcidStatus = this.errorCodes.NONE;
404

    
405
      },
406
      err => this.errorHandler(err, term)
407
    );
408
  }
409

    
410
  private errorHandler(err: any, term: string) {
411
    if (err.status == 404) {
412
      this.getOrcidAuthors(term);
413
    } else {
414
      this.orcidStatus = this.errorCodes.ERROR;
415
      //console.log(err.status);
416
      ClaimResultSearchFormComponent.handleError("Error getting orcid author for term: " + term, err);
417
    }
418
  }
419

    
420
  private getOrcidAuthors(term: string) {
421
    this.orcidResultsNum = null;
422
    this.selectAuthorId = "0";
423
    this.orcidStatus = this.errorCodes.LOADING;
424
    //passing structures in order to fill them in service
425
    this._searchOrcidService.searchOrcidAuthors(term, this.properties).subscribe(
426
      data => {
427
        this.authorIds = data;
428
        if (data != null) {
429
          for (let i = 0; i < this.authorIds.length; i++) {
430
            this.getOrcidAuthor(this.authorIds[i], (i == 0));
431
          }
432
          if (this.authorIds.length == 0) {
433
            this.orcidStatus = this.errorCodes.NONE;
434
            this.orcidResultsNum = -1;
435
          }
436
        } else {
437
          this.orcidStatus = this.errorCodes.ERROR;
438
        }
439

    
440
      },
441
      err => {
442
        this.orcidStatus = this.errorCodes.ERROR;
443
        //console.log(err.status);
444
        ClaimResultSearchFormComponent.handleError("Error getting orcid authors for term: " + term + " and ids: " + JSON.stringify(this.authorIds), err);
445
      }
446
    );
447
  }
448

    
449

    
450
  private getOrcidResultsById(id) {
451
    for (let i = 0; i < this.authors.length; i++) {
452
      if (this.authors[i].id == id) {
453
        this.getOrcidResults(i);
454
      }
455
    }
456
  }
457

    
458
  private getOrcidResults(index) {
459
    this.authorGivenName = this.authors[index].authorGivenName;
460
    this.authorFamilyName = this.authors[index].authorFamilyName;
461
    this.authorId = this.authors[index].id;
462
    this.orcidStatus = this.errorCodes.LOADING;
463
    this._searchOrcidService.searchOrcidPublications(this.authors[index].id, this.properties, true).subscribe(
464
      data => {
465
        if (data != null) {
466
          this.orcidResults = data;
467
          this.orcidResultsNum = data.length;
468
          this.orcidPage = 1;
469
          if ((this.orcidResultsNum % this.size) == 0) {
470
            this.totalPages = parseInt('' + (this.orcidResultsNum / this.size));
471
          } else {
472
            this.totalPages = parseInt('' + (this.orcidResultsNum / this.size + 1));
473
          }
474

    
475
          this.orcidResultsToShow = this.orcidResults.slice(0, 10);
476

    
477
          this.orcidStatus = this.errorCodes.DONE;
478
          if (this.orcidResultsNum == 0) {
479
            this.orcidStatus = this.errorCodes.NONE;
480
          }
481
          for (let i = 0; i < this.orcidResults.length; i++) {
482
            const entity: ClaimEntity = this.orcidResults[i];
483
            if (!entity.result.authors) {
484
              entity.result.authors = [];
485
            }
486
            entity.result.authors.push(this.authorFamilyName + ', ' + this.authorGivenName);
487
            if (entity.result.DOI != null) {
488
              this.enhanceInfoFromDOI(entity);
489
            }
490
          }
491
        } else {
492
          this.orcidResultsNum = 0;
493
          this.totalPages = 0;
494
          this.orcidStatus = this.errorCodes.NONE;
495
        }
496

    
497
      },
498
      err => {
499
        //console.log(err.status);
500
        ClaimResultSearchFormComponent.handleError("Error getting orcid publications for author id: " + this.authors[index].id, err);
501
        this.orcidStatus = this.errorCodes.ERROR;
502
      }
503
    );
504

    
505

    
506
  }
507

    
508
  private enhanceInfoFromDOI(entity: ClaimEntity) {
509

    
510
    if (entity.result.DOI != null) {
511
      this._searchCrossrefService.searchCrossrefByDOIs([entity.result.DOI], this.properties.searchCrossrefAPIURL, true).subscribe(
512
        data => {
513
          if (data != null && data[0] > 0 && data[1]) {
514
            let crossrefResult: ClaimEntity = data[1][0];
515
            if (entity.title == null || entity.title == "") {
516
              entity.title = crossrefResult.title;
517
            }
518
            if (crossrefResult.result.authors) {
519
              entity.result.authors = [];
520
              for (let i = 0; i < crossrefResult.result.authors.length; i++) {
521
                entity.result.authors.push(crossrefResult.result.authors[i]);
522
              }
523
            }
524
            if (entity.result.journal == null || entity.result.journal == "") {
525
              entity.result.journal = crossrefResult.result.journal;
526
            }
527
            if (entity.result.publisher == null || entity.result.publisher == "") {
528
              entity.result.publisher = crossrefResult.result.publisher;
529
            }
530
            if (entity.result.date == null || entity.result.date == "") {
531
              entity.result.date = crossrefResult.result.date;
532
            }
533

    
534
          } else {
535
          }
536
        },
537
        err => {
538
          ClaimResultSearchFormComponent.handleError("Error getting crossref by DOIs: " + entity['DOI'], err);
539
        }
540
      );
541
    }
542
  }
543

    
544
  private crossrefPageChange($event) {
545
    this.crossrefPage = $event.value;
546
    this.crossrefResults = [];
547
    this.getCrossrefResults(this.keyword, this.size, this.crossrefPage);
548
  }
549

    
550
  private orcidPageChange($event) {
551
    this.orcidPage = $event.value;
552
    this.orcidResultsToShow = [];
553
    this.orcidResultsToShow = this.orcidResults.slice(($event.value - 1) * this.size, $event.value * this.size);
554
  }
555

    
556
  private openaireResultsPageChange($event) {
557
    this.openaireResultsPage = $event.value;
558
    this.openaireResults = [];
559
    this.searchOpenaire(this.keyword, this.size, this.openaireResultsPage, false);
560
  }
561

    
562
  datacitePageChange($event) {
563
    this.datacitePage = $event.value;
564
    this.dataciteResults = [];
565
    this.searchDatacite(this.keyword, 10, this.datacitePage);
566
    this.warningMessage = "";
567
    this.infoMessage = "";
568

    
569
  }
570

    
571

    
572
  private searchDatacite(term: string, size: number, page: number) {
573
    this.getDataciteResults(term, size, page);
574
    this.warningMessage = "";
575
    this.infoMessage = "";
576

    
577
  }
578

    
579

    
580

    
581

    
582
  createOpenaireQueryParams():string {
583
    let query = "";
584
    if (this.DOIs.length > 0) {
585
      let doisParams = "";
586
      for (let i = 0; i < this.DOIs.length; i++) {
587
        doisParams += (doisParams.length > 0 ? "&" : "") + 'doi="' + this.DOIs[i] + '"';
588
      }
589
      query += doisParams;
590
    } else if(this.keyword.length > 0){
591
      query += "q=" + StringUtils.quote(StringUtils.URIEncode(this.keyword));
592
    }
593
    if(this.openaireYear.length > 0 ){
594
      query+='&fq=resultacceptanceyear exact \"'+this.openaireYear+'\"'
595
    }
596
    let allFqs = "";
597
    for (let filter of this.openaireResultsfilters){
598
      if(filter.countSelectedValues > 0){
599
        let count_selected=0;
600
        let fq = "";
601
        for (let value of filter.values){
602
          if(value.selected == true){
603
            count_selected++;
604
            fq+=(fq.length > 0 ? " " + filter.filterOperator + " ":"" ) + filter.filterId + " exact " + (StringUtils.quote(value.id));
605
          }
606
        }
607
        if(count_selected > 0){
608
          fq="&fq="+StringUtils.URIEncode(fq);
609
          allFqs += fq;
610
        }
611
      }
612
    }
613
    return query+allFqs;
614

    
615
  }
616

    
617
  public static openaire2ClaimResults(data, type): ClaimEntity[] {
618
    const claimResults = [];
619
    for (let i = 0; i < data.length; i++) {
620
      let item = data[i];
621
      let entity: ClaimEntity = new ClaimEntity();
622
      entity.result = new ClaimResult();
623
      entity.result.publisher = (item.publisher && item.publisher != "") ? item.publisher : null;
624
      entity.result.journal = null;
625
      entity.result.DOI = null;
626
      entity.id = item.id;
627
      entity.title = item.title.name;
628
      entity.result.url = item.title.url;
629
      entity.result.source = String("openaire");
630
      entity.result.date = (item.year && item.year != "") ? item.year : null;
631
      entity.result.accessRights = String(item.title.accessMode);
632

    
633
      entity.type = type;
634
      entity.result.embargoEndDate = (item.embargoEndDate && item.embargoEndDate != "") ? item.embargoEndDate : entity.result.embargoEndDate;
635
      if (item.publisher && item.publisher.length > 0) {
636
        entity.result.publisher = item.publisher;
637
      }
638
      entity.result.record = item;
639
      if (item.authors) {
640
        entity.result.authors = [];
641
        for (let author of item.authors) {
642
          entity.result.authors.push(author.fullName);
643
        }
644
      }
645
      claimResults.push(entity);
646
    }
647
    return claimResults;
648

    
649
  }
650

    
651
  private getDataciteResults(term: string, size: number, page: number) {
652
    this.dataciteStatus = this.errorCodes.LOADING;
653
    if (this.DOIs.length > 0) {
654
      this._searchDataciteService.getDataciteResultByDOI(term, this.properties, true).subscribe(
655
        data => {
656
          this.dataciteResults = data[1];
657
          this.datacitePage = page;
658
          this.dataciteResultsNum = data[0];
659
          this.dataciteStatus = this.errorCodes.DONE;
660
          if (this.dataciteResultsNum == 0) {
661
            this._searchDataciteService.searchDataciteResults(term, size, page, this.properties, true).subscribe(
662
              data => {
663
                this.dataciteResults = data[1];
664
                this.datacitePage = page;
665
                this.dataciteResultsNum = data[0];
666
                this.dataciteStatus = this.errorCodes.DONE;
667
                if (this.dataciteResultsNum == 0) {
668
                  this.dataciteStatus = this.errorCodes.NONE;
669
                }
670

    
671
              },
672
              err => {
673

    
674
                this.dataciteStatus = this.errorCodes.ERROR;
675

    
676
                console.log(err);
677
                ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
678
              });
679
          }
680

    
681
        },
682
        err => {
683
          if (err.status == "404") {
684
            this._searchDataciteService.searchDataciteResults(term, size, page, this.properties, true).subscribe(
685
              data => {
686
                this.dataciteResults = data[1];
687
                this.datacitePage = page;
688
                this.dataciteResultsNum = data[0];
689
                this.dataciteStatus = this.errorCodes.DONE;
690
                if (this.dataciteResultsNum == 0) {
691
                  this.dataciteStatus = this.errorCodes.NONE;
692
                }
693

    
694
              },
695
              err => {
696

    
697
                this.dataciteStatus = this.errorCodes.ERROR;
698

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

    
708

    
709
         });
710
    } else {
711

    
712

    
713
      this._searchDataciteService.searchDataciteResults(term, size, page, this.properties, true).subscribe(
714
        data => {
715
          this.dataciteResults = data[1];
716
          this.datacitePage = page;
717
          this.dataciteResultsNum = data[0];
718
          // console.log("Datacite " + this.dataciteResultsNum);
719
          this.dataciteStatus = this.errorCodes.DONE;
720
          if (this.dataciteResultsNum == 0) {
721
            this.dataciteStatus = this.errorCodes.NONE;
722
          }
723

    
724
        },
725
        err => {
726
          this.dataciteStatus = this.errorCodes.ERROR;
727
          //console.log(err);
728
          ClaimResultSearchFormComponent.handleError("Error getting datacite results for term: " + term, err);
729
        }
730
      );
731
    }
732
  }
733

    
734
  public openaireTypeChanged(type) {
735
    this.openaireResultsType = type;
736
    this.reloadOpenaire = true;
737
    this.openaireYear = "";
738
    this.openaireResultsfilters = [];
739
    this.search(false);
740

    
741
  }
742
  public yearChanged() {
743
    this.reloadOpenaire = true;
744
    this.search(false);
745

    
746
  }
747

    
748
  public clickTab(tab) {
749
    this.activeTab = tab;
750
    // this.search(false);
751
  }
752

    
753
  private static handleError(message: string, error) {
754
    console.error("Claim Result Search Form (component): " + message, error);
755
  }
756

    
757
  //OpenaireFilters:
758

    
759
  getSelectedValues(filter): any {
760
    var selected = [];
761
    if (filter.countSelectedValues > 0) {
762
      for (var i = 0; i < filter.values.length; i++) {
763
        if (filter.values[i].selected) {
764
          selected.push(filter.values[i]);
765
        }
766
      }
767
    }
768
    return selected;
769

    
770
  }
771

    
772
  private removeFilter(value: Value, filter: Filter) {
773
    filter.countSelectedValues--;
774
    if (value.selected == true) {
775
      value.selected = false;
776
    }
777
    this.search(false);
778

    
779
  }
780

    
781
  public countFilters(): number {
782
    let filters = 0;
783
    for (let filter of this.openaireResultsfilters) {
784
      if (filter.countSelectedValues > 0) {
785
        filters += filter.countSelectedValues;
786
      }
787
    }
788
    if (this.keyword.length > 0) {
789
      filters++;
790
    }
791

    
792
    return filters;
793
  }
794

    
795
  private clearFilters() {
796
    for (let i = 0; i < this.openaireResultsfilters.length; i++) {
797
      for (let j = 0; j < this.openaireResultsfilters[i].countSelectedValues; j++) {
798
        if (this.openaireResultsfilters[i].values[j].selected) {
799
          this.openaireResultsfilters[i].values[j].selected = false;
800
        }
801
        this.openaireResultsfilters[i].countSelectedValues = 0;
802
      }
803
    }
804

    
805
  }
806

    
807
  filterChanged($event) {
808
    // console.log("filterChanged");
809
    this.reloadOpenaire = true;
810
    this.search(false);
811

    
812
  }
813
  public checkSelectedFilters(filters:Filter[], prevFilters:Filter[]){
814
    for(let i=0; i< filters.length ; i++){
815
      let filter:Filter = filters[i];
816
      filter.countSelectedValues = 0;
817
      let prevFilterSelectedValues:string[] = [];
818
      for(let j=0; j< prevFilters.length ; j++){
819
        if(filters[i].filterId == prevFilters[j].filterId){
820
          if(prevFilters[j].countSelectedValues >0){
821
            for(let filterValue of prevFilters[j].values) {
822
              if(filterValue.selected){
823
                prevFilterSelectedValues.push(filterValue.id);
824
              }
825
            }
826

    
827
          }
828
          break;
829
        }
830
      }
831
      for(let filterValue of filter.values) {
832
        if(prevFilterSelectedValues.indexOf(filterValue.id) > -1) {
833
          filterValue.selected = true;
834
          filter.countSelectedValues++;
835
        }
836

    
837
      }
838

    
839
    }
840
    return filters;
841
  }
842
  countTotalPages(totalResults: number): number {
843
    let totalPages:any = totalResults/(this.size);
844
    if(!(Number.isInteger(totalPages))) {
845
      totalPages = (parseInt(totalPages, 10) + 1);
846
    }
847
    return totalPages;
848
  }
849
  getTotalOpenaireNumber():number{
850
    let count = 0;
851
    for(let i=0; i<this.openaireResultsType.length; i++){
852
      if(this.openaireResultsType[i]["count]"]!=null){
853
        count+= this.openaireResultsType[i]["count]"];
854
      }
855
    }
856
    return count;
857
  }
858
}
(10-10/15)