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

    
104
  }
105

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

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

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

    
166
  }
167

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

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

    
178
    }
179
  }
180

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

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

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

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

    
239
    } else {
240

    
241

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

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

    
266
  private callOpenaireService(term: string, size: number, page: number, mainResults: boolean, type) {
267
    if (mainResults) {
268
      this.openaireResultsStatus = this.errorCodes.LOADING;
269
      this.openaireResultsPrevFilters = this.openaireResultsfilters;
270
    }
271
    if((mainResults && page==1 )||!mainResults){
272
      this.setOpenaireResultsCount(type, null);
273
      this.totalOpenaireCount = 0;
274
    }
275
    if (type == "dataset") {
276
      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(
277
        data => {
278
          this.setOpenaireResults(data, mainResults, page, type);
279
        },
280
        err => {
281
          this.setOpenaireResultsError(mainResults, term, err);
282
        }
283
      );
284
    } else if (type == "software") {
285
      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(
286
        data => {
287
          this.setOpenaireResults(data, mainResults, page, type);
288
        },
289
        err => {
290
          this.setOpenaireResultsError(mainResults, term, err);
291
        }
292
      );
293
    } else if (type == "other") {
294
      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(
295
        data => {
296
          this.setOpenaireResults(data, mainResults, page, type);
297
        },
298
        err => {
299
          this.setOpenaireResultsError(mainResults, term, err);
300
        }
301
      );
302
    } else {
303
      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(
304
        data => {
305
          this.setOpenaireResults(data, mainResults, page, type);
306
        },
307
        err => {
308
          this.setOpenaireResultsError(mainResults, term, err);
309
        }
310
      );
311
    }
312

    
313

    
314
  }
315

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

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

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

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

    
360
  }
361

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

    
372
    this.getOrcidAuthor(term, true);
373

    
374
  }
375

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

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

    
403

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

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

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

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

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

    
450

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

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

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

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

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

    
506

    
507
  }
508

    
509
  private enhanceInfoFromDOI(entity: ClaimEntity) {
510

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

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

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

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

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

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

    
570
  }
571

    
572

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

    
578
  }
579

    
580

    
581

    
582

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

    
616
  }
617

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

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

    
650
  }
651

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

    
672
              },
673
              err => {
674

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

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

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

    
695
              },
696
              err => {
697

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

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

    
709

    
710
         });
711
    } else {
712

    
713

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

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

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

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

    
747
  }
748

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

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

    
758
  //OpenaireFilters:
759

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

    
771
  }
772

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

    
780
  }
781

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

    
793
    return filters;
794
  }
795

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

    
806
  }
807

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

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

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

    
838
      }
839

    
840
    }
841
    return filters;
842
  }
843
  countTotalPages(totalResults: number): number {
844
    let totalPages:any = totalResults/(this.size);
845
    if(!(Number.isInteger(totalPages))) {
846
      totalPages = (parseInt(totalPages, 10) + 1);
847
    }
848
    return totalPages;
849
  }
850
  getTotalOpenaireNumber():number{
851
    let count = 0;
852
    for(let i=0; i<this.openaireResultsType.length; i++){
853
      if(this.openaireResultsType[i]["count]"]!=null){
854
        count+= this.openaireResultsType[i]["count]"];
855
      }
856
    }
857
    return count;
858
  }
859
  public _formatName(value, number){
860
    let maxLineLength = 29;
861
    //1 space after checkbox
862
    //3 space before number + parenthesis
863
    if(number && value.length+number.toLocaleString().length +1 +3> maxLineLength){
864
      return value.substr(0, (maxLineLength- 3 -4 - number.toLocaleString().length))+'...';
865
    }else if(!number && value.length + 1 > maxLineLength){
866
      return value.substr(0, (maxLineLength- 3 -1 ))+'...';
867
    }
868
    return value;
869
  }
870
}
(10-10/15)