Project

General

Profile

1
import {Component, Input} from '@angular/core';
2
import {ChangeDetectionStrategy} from '@angular/core';
3
import {ViewEncapsulation} from '@angular/core';
4
import {ActivatedRoute, Router} from '@angular/router';
5
import {Location} from '@angular/common';
6
import {Title, Meta} from '@angular/platform-browser';
7

    
8
import {FetchResearchResults} from '../../utils/fetchEntitiesClasses/fetchResearchResults.class';
9
import {FetchDataproviders} from '../../utils/fetchEntitiesClasses/fetchDataproviders.class';
10
import {FetchProjects} from '../../utils/fetchEntitiesClasses/fetchProjects.class';
11
import {FetchOrganizations} from '../../utils/fetchEntitiesClasses/fetchOrganizations.class';
12

    
13
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
14
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
15
import {SearchProjectsService} from '../../services/searchProjects.service';
16
import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
17

    
18
import {SearchFields} from '../../utils/properties/searchFields';
19
import {ErrorCodes} from '../../utils/properties/errorCodes';
20
import {RouterHelper} from '../../utils/routerHelper.class';
21
import {RefineFieldResultsService} from '../../services/refineFieldResults.service';
22
import {PiwikService} from '../../utils/piwik/piwik.service';
23
import {ConfigurationService} from '../../utils/configuration/configuration.service';
24
import {EnvProperties} from '../../utils/properties/env-properties';
25
import {SEOService} from '../../sharedComponents/SEO/SEO.service';
26
import {StringUtils} from '../../utils/string-utils.class';
27
import {SearchCustomFilter} from "../searchUtils/searchUtils.class";
28
import {Observable, Subscription} from "rxjs";
29

    
30
@Component({
31
  changeDetection: ChangeDetectionStrategy.Default,
32
  encapsulation: ViewEncapsulation.Emulated,
33
  selector: 'search-find',
34
  templateUrl: 'search.component.html'
35
})
36
export class SearchComponent {
37
  public sub: any;
38
  piwiksub: any;
39
  public reloadPublications: boolean;
40
  public reloadDatasets: boolean;
41
  public reloadSoftware: boolean;
42
  public reloadOrps: boolean;
43
  public reloadProjects: boolean;
44
  public reloadDataproviders: boolean;
45
  public reloadOrganizations: boolean;
46

    
47

    
48
  public pageTitle = "Search in OpenAIRE"
49
  public keyword: string = "";
50
  public publications: string[];
51
  public datasets: string[];
52
  public software: string[];
53
  public orps: string[];
54
  public projectsTab: string[];
55
  public dataproviders: string[];
56
  public organizations: string[];
57

    
58
  public activeTab = "publications";
59
  public linkToSearchPublications = "";
60
  public linkToSearchProjects = "";
61
  public linkToSearchDataproviders = "";
62
  public linkToSearchDatasets = "";
63
  public linkToSearchSoftware = "";
64
  public linkToSearchOrps = "";
65
  public linkToSearchOrganizations = "";
66

    
67
  public fetchPublications: FetchResearchResults;
68
  public fetchDataproviders: FetchDataproviders;
69
  public fetchProjects: FetchProjects;
70
  public fetchDatasets: FetchResearchResults;
71
  public fetchSoftware: FetchResearchResults;
72
  public fetchOrps: FetchResearchResults;
73
  public fetchOrganizations: FetchOrganizations;
74

    
75
  public searchFields: SearchFields = new SearchFields();
76
  public errorCodes: ErrorCodes = new ErrorCodes();
77
  public routerHelper: RouterHelper = new RouterHelper();
78

    
79
  public publicationsSize: any = null;
80
  public datasetsSize: any = null;
81
  public softwareSize: any = null;
82
  public oprsSize: any = null;
83
  public fundersSize: any = null;
84
  public projectsSize: any = null;
85
  public datasourcesSize: any = null;
86
  showPublications: boolean = false;
87
  showDatasets: boolean = false;
88
  showSoftware: boolean = false;
89
  showOrps: boolean = false;
90
  showProjects: boolean = false;
91
  showDataProviders: boolean = false;
92
  showOrganizations: boolean = false;
93
  advancedSearchLink: string = "/search/advanced/publications";
94
  properties: EnvProperties;
95
  @Input() logoURL;
96
  @Input() name;
97
  @Input() customFilter: SearchCustomFilter = null;
98
  @Input() piwikSiteId = null;
99
  @Input() formPlaceholderText = "Search for research results, projects, content providers & organizations in OpenAIRE";
100

    
101

    
102
  public subPub;
103
  public subData;
104
  public subSoftware;
105
  public subOrps;
106
  public subProjects;
107
  public subOrg;
108
  public subDataPr;
109

    
110
  constructor(private route: ActivatedRoute,
111
              private _router: Router,
112
              private _searchResearchResultsService: SearchResearchResultsService,
113
              private _searchDataprovidersService: SearchDataprovidersService,
114
              private _searchProjectsService: SearchProjectsService,
115
              private _searchOrganizationsService: SearchOrganizationsService,
116
              private _refineFieldResultsService: RefineFieldResultsService,
117
              private location: Location,
118
              private _meta: Meta,
119
              private _title: Title,
120
              private _piwikService: PiwikService,
121
              private config: ConfigurationService,
122
              private seoService: SEOService) {
123
    this.fetchPublications = new FetchResearchResults(this._searchResearchResultsService);
124
    this.fetchDataproviders = new FetchDataproviders(this._searchDataprovidersService);
125
    this.fetchProjects = new FetchProjects(this._searchProjectsService);
126
    this.fetchDatasets = new FetchResearchResults(this._searchResearchResultsService);
127
    this.fetchSoftware = new FetchResearchResults(this._searchResearchResultsService);
128
    this.fetchOrps = new FetchResearchResults(this._searchResearchResultsService);
129
    this.fetchOrganizations = new FetchOrganizations(this._searchOrganizationsService);
130

    
131
  }
132

    
133
  public ngOnInit() {
134
    this.route.data
135
      .subscribe((data: { envSpecific: EnvProperties }) => {
136

    
137
        var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects ";
138
        var title = "OpenAIRE | Search publications, research data, projects... | OpenAIRE";
139
        this.properties = data.envSpecific;
140
        var url = data.envSpecific.baseLink + this._router.url;
141
        this._title.setTitle(title);
142
        this._meta.updateTag({content: description}, "name='description'");
143
        this._meta.updateTag({content: description}, "property='og:description'");
144
        this._meta.updateTag({content: title}, "property='og:title'");
145
        this._meta.updateTag({content: url}, "property='og:url'");
146
        this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this._router.url, false);
147
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
148
          this.piwiksub = this._piwikService.trackView(this.properties, "OpenAIRE |Search publications, research data, projects...", this.piwikSiteId).subscribe();
149

    
150
        }
151

    
152

    
153
        if ((this.customFilter && this.customFilter.queryFieldName == "communityId") || this.properties.adminToolsCommunity) {
154
          this.config.getCommunityInformation(this.properties, (this.customFilter && this.customFilter.queryFieldName == "communityId") ? this.customFilter.valueId : this.properties.adminToolsCommunity).subscribe(data => {
155
            var showEntity = {};
156
            for (var i = 0; i < data['entities'].length; i++) {
157

    
158
              showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
159
            }
160
            this.showPublications = showEntity["publication"];
161
            this.showDatasets = showEntity["dataset"];
162
            this.showProjects = showEntity["project"];
163
            this.showOrganizations = showEntity["organization"];
164
            this.showDataProviders = showEntity["datasource"];
165
            this.showSoftware = showEntity["software"];
166
            this.showOrps = showEntity["orp"];
167
            if (this.customFilter && this.customFilter.queryFieldName == "communityId") {
168
              this.showProjects = false;
169
              this.showOrganizations = false;
170
              this.showDataProviders = false;
171
            }
172
            this.loadAll();
173

    
174
          });
175
        } else {
176
          if ((this.customFilter && this.customFilter.queryFieldName == "country")) {
177
            this.showPublications =true;
178
            this.showDatasets = true;
179
            this.showDataProviders = true;
180
            this.showSoftware =  true;
181
            this.showOrps = true;
182
            this.showProjects = true;
183
            this.showOrganizations = true;
184
            this.showDataProviders = true;
185
          }
186
        }
187
        this.loadAll();
188
      });
189
  }
190

    
191
  loadAll() {
192
    this.sub = this.route.queryParams.subscribe(params => {
193

    
194
      this.reloadTabs();
195
      this.keyword = (params['keyword']) ? params['keyword'] : "";
196
      if (this.keyword != null && this.keyword.length > 0) {
197
        //if showPublications == false will setValues another entity as the first
198
        if (this.showPublications) {
199
          this.activeTab = "publications";
200
          this.searchPublications();
201
        } else if (this.showDatasets) {
202
          this.activeTab = "research data";
203
          this.searchDatasets();
204
        } else if (this.showSoftware) {
205
          this.activeTab = "software";
206
          this.searchSoftware();
207
        } else if (this.showOrps) {
208
          this.activeTab = "other research products";
209
          this.searchOrps();
210
        } else if (this.showProjects) {
211
          this.activeTab = "projects";
212
          this.searchProjects();
213
        } else if (this.showDataProviders) {
214
          this.activeTab = "content providers";
215
          this.searchDataProviders();
216
        } else if (this.showOrganizations) {
217
          this.activeTab = "organizations";
218
          this.searchOrganizations();
219
        }
220
        this.count();
221
      }
222
    });
223
  }
224

    
225

    
226
  public ngOnDestroy() {
227
    if (this.sub) {
228
      this.sub.unsubscribe();
229
    }
230
    if (this.piwiksub) {
231
      this.piwiksub.unsubscribe();
232
    }
233
    if (this.keyword != null && this.keyword.length > 0) {
234
      if (this.subPub) {
235
        this.subPub.unsubscribe();
236
      }
237
      if (this.subData) {
238
        this.subData.unsubscribe();
239
      }
240
      if (this.subSoftware) {
241
        this.subSoftware.unsubscribe();
242
      }
243
      if (this.subOrps) {
244
        this.subOrps.unsubscribe();
245
      }
246
      if (this.subProjects) {
247
        this.subProjects.unsubscribe();
248
      }
249
      if (this.subOrg) {
250
        this.subOrg.unsubscribe();
251
      }
252
      if (this.subDataPr) {
253
        this.subDataPr.unsubscribe();
254
      }
255

    
256
    }
257
  }
258

    
259
  public searchPublications() {
260
    this.activeTab = "publications";
261
    this.advancedSearchLink = this.properties.searchLinkToAdvancedPublications;//"/search/advanced/publications";
262
    if (this.reloadPublications &&
263
      this.fetchPublications.searchUtils.status != this.errorCodes.NONE /*&&
264
        this.fetchPublications.searchUtils.status != this.errorCodes.ERROR*/
265
      && (this.keyword != null && this.keyword.length > 0)) {
266
      this.reloadPublications = false;
267
      this.fetchPublications.getResultsByKeyword("publication", this.keyword, 1, 10, this.properties, this.customFilter);
268
      this.linkToSearchPublications = this.properties.searchLinkToPublications;// + "?keyword="  + this.keyword;
269
    }
270
  }
271

    
272
  public searchDatasets() {
273
    this.activeTab = "research data";
274
    this.advancedSearchLink = this.properties.searchLinkToAdvancedDatasets;//"/search/advanced/datasets";
275
    if (this.reloadDatasets &&
276
      this.fetchDatasets.searchUtils.status != this.errorCodes.NONE /*&&
277
        this.fetchDatasets.searchUtils.status != this.errorCodes.ERROR*/
278
      && (this.keyword != null && this.keyword.length > 0)) {
279
      this.reloadDatasets = false;
280
      this.fetchDatasets.getResultsByKeyword("dataset", this.keyword, 1, 10, this.properties, this.customFilter);
281
      this.linkToSearchDatasets = this.properties.searchLinkToDatasets;// + "?keyword="  + this.keyword;
282
    }
283
  }
284

    
285
  public searchSoftware() {
286

    
287
    this.activeTab = "software";
288
    this.advancedSearchLink = this.properties.searchLinkToAdvancedSoftware;//"/search/advanced/software";
289
    if (this.reloadSoftware
290
      && this.fetchSoftware.searchUtils.status != this.errorCodes.NONE/* &&
291
        ( this.fetchSoftware.searchUtils.status == this.errorCodes.LOADING ||
292
          this.fetchSoftware.searchUtils.status == this.errorCodes.DONE )*/
293
      && (this.keyword != null && this.keyword.length > 0)) {
294
      this.reloadSoftware = false;
295
      this.fetchSoftware.getResultsByKeyword("software", this.keyword, 1, 10, this.properties, this.customFilter);
296
      this.linkToSearchSoftware = this.properties.searchLinkToSoftware;// + "?keyword="  + this.keyword;
297
    }
298
  }
299

    
300
  public searchOrps() {
301
    this.activeTab = "other research products";
302
    this.advancedSearchLink = this.properties.searchLinkToAdvancedOrps;//"/search/advanced/other";
303
    if (this.reloadOrps &&
304
      this.fetchOrps.searchUtils.status != this.errorCodes.NONE /*&&
305
        this.fetchOrps.searchUtils.status != this.errorCodes.ERROR*/
306
      && (this.keyword != null && this.keyword.length > 0)) {
307
      this.reloadOrps = false;
308
      this.fetchOrps.getResultsByKeyword("other", this.keyword, 1, 10, this.properties, this.customFilter);
309
      this.linkToSearchOrps = this.properties.searchLinkToOrps;
310
    }
311
  }
312

    
313
  public searchProjects() {
314
    this.activeTab = "projects";
315
    this.advancedSearchLink = this.properties.searchLinkToAdvancedProjects;//"/search/advanced/projects";
316
    if (this.reloadProjects &&
317
      this.fetchProjects.searchUtils.status != this.errorCodes.NONE /*&&
318
        this.fetchProjects.searchUtils.status != this.errorCodes.ERROR*/
319
      && (this.keyword != null && this.keyword.length > 0)) {
320
      this.reloadProjects = false;
321
      this.fetchProjects.getResultsByKeyword(this.keyword, 1, 10, this.properties, this.customFilter);
322
      this.linkToSearchProjects = this.properties.searchLinkToProjects;// + "?keyword="  + this.keyword;
323
    }
324
  }
325

    
326
  public searchDataProviders() {
327
    this.activeTab = "content providers";
328
    this.advancedSearchLink = this.properties.searchLinkToAdvancedDataProviders;//"/search/advanced/dataproviders";
329
    if (this.reloadDataproviders &&
330
      this.fetchDataproviders.searchUtils.status != this.errorCodes.NONE /*&&
331
         this.fetchDataproviders.searchUtils.status != this.errorCodes.ERROR*/
332
      && (this.keyword != null && this.keyword.length > 0)) {
333
      this.reloadDataproviders = false;
334
      this.fetchDataproviders.getResultsByKeyword(this.keyword, 1, 10, this.properties, this.customFilter);
335
      this.linkToSearchDataproviders = this.properties.searchLinkToDataProviders;// + "?keyword="  + this.keyword;
336
    }
337
  }
338

    
339
  public searchOrganizations() {
340
    this.activeTab = "organizations";
341
    this.advancedSearchLink = this.properties.searchLinkToAdvancedOrganizations;//"/search/advanced/organizations";
342
    if (this.reloadOrganizations &&
343
      this.fetchOrganizations.searchUtils.status != this.errorCodes.NONE /*&&
344
         this.fetchOrganizations.searchUtils.status != this.errorCodes.ERROR*/
345
      && (this.keyword != null && this.keyword.length > 0)) {
346
      this.reloadOrganizations = false;
347
      this.fetchOrganizations.getResultsByKeyword(this.keyword, 1, 10, this.properties, this.customFilter);
348
      this.linkToSearchOrganizations = this.properties.searchLinkToOrganizations;// + "?keyword="  + this.keyword;
349
    }
350
  }
351

    
352

    
353
  public keywordChanged($event) {
354

    
355
    this.keyword = $event.value;
356
    //console.info("Search Find: search with keyword \"" +  this.keyword + "\"" );
357
    this.location.go(location.pathname, "?keyword=" + this.keyword);
358
    this.reloadTabs();
359
    if (this.activeTab == "publications") {
360
      this.searchPublications();
361
    }
362
    if (this.activeTab == "projects") {
363
      this.searchProjects();
364
    }
365
    if (this.activeTab == "content providers") {
366
      this.searchDataProviders();
367
    }
368
    if (this.activeTab == "research data") {
369
      this.searchDatasets();
370
    }
371
    if (this.activeTab == "software") {
372
      this.searchSoftware();
373
    }
374
    if (this.activeTab == "other research products") {
375
      this.searchOrps();
376
    }
377
    if (this.activeTab == "organizations") {
378
      this.searchOrganizations();
379
    }
380

    
381
    this.count();
382
  }
383

    
384
  private count() {
385
    var refineParams = null;
386
    if (this.customFilter) {
387
      refineParams = (refineParams ? (refineParams + '&') : '') + "&fq=" + StringUtils.URIEncode(this.customFilter.queryFieldName + " exact " + StringUtils.quote((this.customFilter.valueId)));
388
    }
389
    if (this.activeTab != "publications" && this.showPublications) {
390
      this.fetchPublications.searchUtils.status = this.errorCodes.LOADING;
391
      this.fetchPublications.results = [];
392

    
393
      this.subPub = this.numOfSearchResults("publication", this.fetchPublications, refineParams);
394
    }
395
    if (this.activeTab != "research data" && this.showDatasets) {
396
      this.fetchDatasets.searchUtils.status = this.errorCodes.LOADING;
397
      this.fetchDatasets.results = [];
398
      this.subData = this.numOfSearchResults("dataset", this.fetchDatasets, refineParams);
399
    }
400
    if (this.activeTab != "software" && this.showSoftware) {
401
      this.fetchSoftware.searchUtils.status = this.errorCodes.LOADING;
402
      this.fetchSoftware.results = [];
403
      this.subSoftware = this.numOfSearchResults("software", this.fetchSoftware, refineParams);
404
    }
405
    if (this.activeTab != "other research products" && this.showOrps) {
406
      this.fetchOrps.searchUtils.status = this.errorCodes.LOADING;
407
      this.fetchOrps.results = [];
408
      this.subOrps = this.numOfSearchResults("other", this.fetchOrps, refineParams);
409
    }
410
    if (this.activeTab != "projects" && this.showProjects) {
411
      this.fetchProjects.searchUtils.status = this.errorCodes.LOADING;
412
      this.fetchProjects.results = [];
413
      this.subProjects = this._searchProjectsService.numOfSearchProjects(this.keyword, this.properties, refineParams).subscribe(
414
        data => {
415
          this.fetchProjects.searchUtils.totalResults = data;
416
          this.fetchProjects.searchUtils.status = this.errorCodes.DONE;
417
          if (this.fetchProjects.searchUtils.totalResults == 0) {
418
            this.fetchProjects.searchUtils.status = this.errorCodes.NONE;
419
          }
420
        },
421
        err => {
422
          //console.log(err);
423
          this.handleError("Error getting number of Projects", err);
424
          this.fetchProjects.searchUtils.status = this.errorCodes.ERROR;
425
        }
426
      );
427
    }
428
    if (this.activeTab != "content providers" && this.showDataProviders) {
429
      this.fetchDataproviders.results = [];
430
      this.fetchDataproviders.getNumForSearch(this.keyword, this.properties, refineParams);
431
    }
432
    if (this.activeTab != "organizations" && this.showOrganizations) {
433
      this.fetchOrganizations.searchUtils.status = this.errorCodes.LOADING;
434
      this.fetchOrganizations.results = [];
435
      this.subOrg = this._searchOrganizationsService.numOfSearchOrganizations(this.keyword, this.properties, refineParams).subscribe(
436
        data => {
437
          this.fetchOrganizations.searchUtils.totalResults = data;
438
          this.fetchOrganizations.searchUtils.status = this.errorCodes.DONE;
439
          if (this.fetchOrganizations.searchUtils.totalResults == 0) {
440
            this.fetchOrganizations.searchUtils.status = this.errorCodes.NONE;
441
          }
442
        },
443
        err => {
444
          //console.log(err);
445
          this.handleError("Error getting number of Organizations", err);
446
          this.fetchOrganizations.searchUtils.status = this.errorCodes.ERROR;
447

    
448
        }
449
      );
450
    }
451

    
452

    
453
  }
454

    
455
  private numOfSearchResults(resultType: string, fetchClass: FetchResearchResults, refineParams) {
456
    return this._searchResearchResultsService.numOfSearchResults(resultType, this.keyword, this.properties, refineParams).subscribe(
457
      data => {
458
        fetchClass.searchUtils.totalResults = data;
459
        fetchClass.searchUtils.status = this.errorCodes.DONE;
460
        if (fetchClass.searchUtils.totalResults == 0) {
461
          fetchClass.searchUtils.status = this.errorCodes.NONE;
462
        }
463
      },
464
      err => {
465
        this.handleError("Error getting number of "+this.getEntityName(resultType, true, true), err);
466
        fetchClass.searchUtils.status = this.errorCodes.ERROR;
467
      }
468
    );
469
  }
470

    
471
  private reloadTabs() {
472
    this.reloadPublications = true;
473
    this.reloadDatasets = true;
474
    this.reloadSoftware = true;
475
    this.reloadOrps = true;
476
    this.reloadProjects = true;
477
    this.reloadDataproviders = true;
478
    this.reloadOrganizations = true;
479
    this.fetchOrganizations.searchUtils.status = null;
480
    this.fetchDataproviders.searchUtils.status = null;
481
    this.fetchProjects.searchUtils.status = null;
482
    this.fetchPublications.searchUtils.status = null;
483
    this.fetchDatasets.searchUtils.status = null;
484
    this.fetchSoftware.searchUtils.status = null;
485
    this.fetchOrps.searchUtils.status = null;
486
  }
487

    
488
  private handleError(message: string, error) {
489
    console.error("General Search Page: " + message, error);
490
  }
491

    
492
  private getEntityName (entityType:string, plural:boolean, full:boolean): string {
493
    if(entityType == "publication") {
494
      return "publication" + (plural ? "s" : "");
495
    } else if(entityType == "dataset") {
496
      return (full ? "research data" : ("dataset" + (plural ? "s" : "")));
497
    } else if(entityType == "software") {
498
      return "software";
499
    } else if(entityType == "other") {
500
      return (full ? ("other research product" + (plural ? "s" : "")) : "other");
501
    }
502
  }
503
}
(3-3/6)