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
  subs: Subscription[] = [];
38

    
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 outcomes, projects, content providers & organizations in OpenAIRE";
100

    
101
  constructor(private route: ActivatedRoute,
102
              private _router: Router,
103
              private _searchResearchResultsService: SearchResearchResultsService,
104
              private _searchDataprovidersService: SearchDataprovidersService,
105
              private _searchProjectsService: SearchProjectsService,
106
              private _searchOrganizationsService: SearchOrganizationsService,
107
              private _refineFieldResultsService: RefineFieldResultsService,
108
              private location: Location,
109
              private _meta: Meta,
110
              private _title: Title,
111
              private _piwikService: PiwikService,
112
              private config: ConfigurationService,
113
              private seoService: SEOService) {
114
    this.fetchPublications = new FetchResearchResults(this._searchResearchResultsService);
115
    this.fetchDataproviders = new FetchDataproviders(this._searchDataprovidersService);
116
    this.fetchProjects = new FetchProjects(this._searchProjectsService);
117
    this.fetchDatasets = new FetchResearchResults(this._searchResearchResultsService);
118
    this.fetchSoftware = new FetchResearchResults(this._searchResearchResultsService);
119
    this.fetchOrps = new FetchResearchResults(this._searchResearchResultsService);
120
    this.fetchOrganizations = new FetchOrganizations(this._searchOrganizationsService);
121

    
122
  }
123

    
124
  public ngOnInit() {
125
    this.subs.push(this.route.data
126
      .subscribe((data: { envSpecific: EnvProperties }) => {
127

    
128
        var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects ";
129
        var title = "OpenAIRE | Search publications, research data, projects... | OpenAIRE";
130
        this.properties = data.envSpecific;
131
        var url = data.envSpecific.baseLink + this._router.url;
132
        this._title.setTitle(title);
133
        this._meta.updateTag({content: description}, "name='description'");
134
        this._meta.updateTag({content: description}, "property='og:description'");
135
        this._meta.updateTag({content: title}, "property='og:title'");
136
        this._meta.updateTag({content: url}, "property='og:url'");
137
        this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this._router.url, false);
138
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
139
          this.subs.push(this._piwikService.trackView(this.properties, "OpenAIRE |Search publications, research data, projects...", this.piwikSiteId).subscribe());
140

    
141
        }
142

    
143

    
144
        if ((this.customFilter && this.customFilter.queryFieldName == "communityId") || this.properties.adminToolsCommunity) {
145
          //this.subs.push(this.config.getCommunityInformation(this.properties, (this.customFilter && this.customFilter.queryFieldName == "communityId") ? this.customFilter.valueId : this.properties.adminToolsCommunity).subscribe(data => {
146
          this.subs.push(this.config.communityInformationState.subscribe(data => {
147
            if(data) {
148
              var showEntity = {};
149
              for (var i = 0; i < data['entities'].length; i++) {
150

    
151
                showEntity["" + data['entities'][i]["pid"] + ""] = data['entities'][i]["isEnabled"];
152
              }
153
              this.showPublications = showEntity["publication"];
154
              this.showDatasets = showEntity["dataset"];
155
              this.showProjects = showEntity["project"];
156
              this.showOrganizations = showEntity["organization"];
157
              this.showDataProviders = showEntity["datasource"];
158
              this.showSoftware = showEntity["software"];
159
              this.showOrps = showEntity["orp"];
160
              if (this.customFilter && this.customFilter.queryFieldName == "communityId") {
161
                this.showProjects = false;
162
                this.showOrganizations = false;
163
                this.showDataProviders = false;
164
              }
165
              this.loadAll();
166
            }
167
          }));
168
        } else {
169
          if ((this.customFilter && this.customFilter.queryFieldName == "country")) {
170
            this.showPublications =true;
171
            this.showDatasets = true;
172
            this.showDataProviders = true;
173
            this.showSoftware =  true;
174
            this.showOrps = true;
175
            this.showProjects = true;
176
            this.showOrganizations = true;
177
            this.showDataProviders = true;
178
          }
179
        }
180
        this.loadAll();
181
      }));
182
  }
183

    
184
  loadAll() {
185
    this.subs.push(this.route.queryParams.subscribe(params => {
186

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

    
218

    
219
  public ngOnDestroy() {
220
    for (let sub of this.subs) {
221
      sub.unsubscribe();
222
    }
223
  }
224

    
225
  public searchPublications() {
226
    this.activeTab = "publications";
227
    this.advancedSearchLink = this.properties.searchLinkToAdvancedPublications;//"/search/advanced/publications";
228
    if (this.reloadPublications &&
229
      this.fetchPublications.searchUtils.status != this.errorCodes.NONE /*&&
230
        this.fetchPublications.searchUtils.status != this.errorCodes.ERROR*/
231
      && (this.keyword != null && this.keyword.length > 0)) {
232
      this.reloadPublications = false;
233
      this.fetchPublications.getResultsByKeyword("publication", this.keyword, 1, 10, this.properties, this.customFilter);
234
      this.linkToSearchPublications = this.properties.searchLinkToPublications;// + "?keyword="  + this.keyword;
235
    }
236
  }
237

    
238
  public searchDatasets() {
239
    this.activeTab = "research data";
240
    this.advancedSearchLink = this.properties.searchLinkToAdvancedDatasets;//"/search/advanced/datasets";
241
    if (this.reloadDatasets &&
242
      this.fetchDatasets.searchUtils.status != this.errorCodes.NONE /*&&
243
        this.fetchDatasets.searchUtils.status != this.errorCodes.ERROR*/
244
      && (this.keyword != null && this.keyword.length > 0)) {
245
      this.reloadDatasets = false;
246
      this.fetchDatasets.getResultsByKeyword("dataset", this.keyword, 1, 10, this.properties, this.customFilter);
247
      this.linkToSearchDatasets = this.properties.searchLinkToDatasets;// + "?keyword="  + this.keyword;
248
    }
249
  }
250

    
251
  public searchSoftware() {
252

    
253
    this.activeTab = "software";
254
    this.advancedSearchLink = this.properties.searchLinkToAdvancedSoftware;//"/search/advanced/software";
255
    if (this.reloadSoftware
256
      && this.fetchSoftware.searchUtils.status != this.errorCodes.NONE/* &&
257
        ( this.fetchSoftware.searchUtils.status == this.errorCodes.LOADING ||
258
          this.fetchSoftware.searchUtils.status == this.errorCodes.DONE )*/
259
      && (this.keyword != null && this.keyword.length > 0)) {
260
      this.reloadSoftware = false;
261
      this.fetchSoftware.getResultsByKeyword("software", this.keyword, 1, 10, this.properties, this.customFilter);
262
      this.linkToSearchSoftware = this.properties.searchLinkToSoftware;// + "?keyword="  + this.keyword;
263
    }
264
  }
265

    
266
  public searchOrps() {
267
    this.activeTab = "other research products";
268
    this.advancedSearchLink = this.properties.searchLinkToAdvancedOrps;//"/search/advanced/other";
269
    if (this.reloadOrps &&
270
      this.fetchOrps.searchUtils.status != this.errorCodes.NONE /*&&
271
        this.fetchOrps.searchUtils.status != this.errorCodes.ERROR*/
272
      && (this.keyword != null && this.keyword.length > 0)) {
273
      this.reloadOrps = false;
274
      this.fetchOrps.getResultsByKeyword("other", this.keyword, 1, 10, this.properties, this.customFilter);
275
      this.linkToSearchOrps = this.properties.searchLinkToOrps;
276
    }
277
  }
278

    
279
  public searchProjects() {
280
    this.activeTab = "projects";
281
    this.advancedSearchLink = this.properties.searchLinkToAdvancedProjects;//"/search/advanced/projects";
282
    if (this.reloadProjects &&
283
      this.fetchProjects.searchUtils.status != this.errorCodes.NONE /*&&
284
        this.fetchProjects.searchUtils.status != this.errorCodes.ERROR*/
285
      && (this.keyword != null && this.keyword.length > 0)) {
286
      this.reloadProjects = false;
287
      this.fetchProjects.getResultsByKeyword(this.keyword, 1, 10, this.properties, this.customFilter);
288
      this.linkToSearchProjects = this.properties.searchLinkToProjects;// + "?keyword="  + this.keyword;
289
    }
290
  }
291

    
292
  public searchDataProviders() {
293
    this.activeTab = "content providers";
294
    this.advancedSearchLink = this.properties.searchLinkToAdvancedDataProviders;//"/search/advanced/dataproviders";
295
    if (this.reloadDataproviders &&
296
      this.fetchDataproviders.searchUtils.status != this.errorCodes.NONE /*&&
297
         this.fetchDataproviders.searchUtils.status != this.errorCodes.ERROR*/
298
      && (this.keyword != null && this.keyword.length > 0)) {
299
      this.reloadDataproviders = false;
300
      this.fetchDataproviders.getResultsByKeyword(this.keyword, 1, 10, this.properties, this.customFilter);
301
      this.linkToSearchDataproviders = this.properties.searchLinkToDataProviders;// + "?keyword="  + this.keyword;
302
    }
303
  }
304

    
305
  public searchOrganizations() {
306
    this.activeTab = "organizations";
307
    this.advancedSearchLink = this.properties.searchLinkToAdvancedOrganizations;//"/search/advanced/organizations";
308
    if (this.reloadOrganizations &&
309
      this.fetchOrganizations.searchUtils.status != this.errorCodes.NONE /*&&
310
         this.fetchOrganizations.searchUtils.status != this.errorCodes.ERROR*/
311
      && (this.keyword != null && this.keyword.length > 0)) {
312
      this.reloadOrganizations = false;
313
      this.fetchOrganizations.getResultsByKeyword(this.keyword, 1, 10, this.properties, this.customFilter);
314
      this.linkToSearchOrganizations = this.properties.searchLinkToOrganizations;// + "?keyword="  + this.keyword;
315
    }
316
  }
317

    
318

    
319
  public keywordChanged($event) {
320

    
321
    this.keyword = $event.value;
322
    //console.info("Search Find: search with keyword \"" +  this.keyword + "\"" );
323
    this.location.go(location.pathname, "?keyword=" + this.keyword);
324
    this.reloadTabs();
325
    if (this.activeTab == "publications") {
326
      this.searchPublications();
327
    }
328
    if (this.activeTab == "projects") {
329
      this.searchProjects();
330
    }
331
    if (this.activeTab == "content providers") {
332
      this.searchDataProviders();
333
    }
334
    if (this.activeTab == "research data") {
335
      this.searchDatasets();
336
    }
337
    if (this.activeTab == "software") {
338
      this.searchSoftware();
339
    }
340
    if (this.activeTab == "other research products") {
341
      this.searchOrps();
342
    }
343
    if (this.activeTab == "organizations") {
344
      this.searchOrganizations();
345
    }
346

    
347
    this.count();
348
  }
349

    
350
  private count() {
351
    var refineParams = null;
352
    if (this.customFilter) {
353
      refineParams = (refineParams ? (refineParams + '&') : '') + "&fq=" + StringUtils.URIEncode(this.customFilter.queryFieldName + " exact " + StringUtils.quote((this.customFilter.valueId)));
354
    }
355
    if (this.activeTab != "publications" && this.showPublications) {
356
      this.fetchPublications.searchUtils.status = this.errorCodes.LOADING;
357
      this.fetchPublications.results = [];
358

    
359
      this.subs.push(this.numOfSearchResults("publication", this.fetchPublications, refineParams));
360
    }
361
    if (this.activeTab != "research data" && this.showDatasets) {
362
      this.fetchDatasets.searchUtils.status = this.errorCodes.LOADING;
363
      this.fetchDatasets.results = [];
364
      this.subs.push(this.numOfSearchResults("dataset", this.fetchDatasets, refineParams));
365
    }
366
    if (this.activeTab != "software" && this.showSoftware) {
367
      this.fetchSoftware.searchUtils.status = this.errorCodes.LOADING;
368
      this.fetchSoftware.results = [];
369
      this.subs.push(this.numOfSearchResults("software", this.fetchSoftware, refineParams));
370
    }
371
    if (this.activeTab != "other research products" && this.showOrps) {
372
      this.fetchOrps.searchUtils.status = this.errorCodes.LOADING;
373
      this.fetchOrps.results = [];
374
      this.subs.push(this.numOfSearchResults("other", this.fetchOrps, refineParams));
375
    }
376
    if (this.activeTab != "projects" && this.showProjects) {
377
      this.fetchProjects.searchUtils.status = this.errorCodes.LOADING;
378
      this.fetchProjects.results = [];
379
      this.subs.push(this._searchProjectsService.numOfSearchProjects(this.keyword, this.properties, refineParams).subscribe(
380
        data => {
381
          this.fetchProjects.searchUtils.totalResults = data;
382
          this.fetchProjects.searchUtils.status = this.errorCodes.DONE;
383
          if (this.fetchProjects.searchUtils.totalResults == 0) {
384
            this.fetchProjects.searchUtils.status = this.errorCodes.NONE;
385
          }
386
        },
387
        err => {
388
          //console.log(err);
389
          this.handleError("Error getting number of Projects", err);
390
          this.fetchProjects.searchUtils.status = this.errorCodes.ERROR;
391
        }
392
      ));
393
    }
394
    if (this.activeTab != "content providers" && this.showDataProviders) {
395
      this.fetchDataproviders.results = [];
396
      this.fetchDataproviders.getNumForSearch(this.keyword, this.properties, refineParams);
397
    }
398
    if (this.activeTab != "organizations" && this.showOrganizations) {
399
      this.fetchOrganizations.searchUtils.status = this.errorCodes.LOADING;
400
      this.fetchOrganizations.results = [];
401
      this.subs.push(this._searchOrganizationsService.numOfSearchOrganizations(this.keyword, this.properties, refineParams).subscribe(
402
        data => {
403
          this.fetchOrganizations.searchUtils.totalResults = data;
404
          this.fetchOrganizations.searchUtils.status = this.errorCodes.DONE;
405
          if (this.fetchOrganizations.searchUtils.totalResults == 0) {
406
            this.fetchOrganizations.searchUtils.status = this.errorCodes.NONE;
407
          }
408
        },
409
        err => {
410
          //console.log(err);
411
          this.handleError("Error getting number of Organizations", err);
412
          this.fetchOrganizations.searchUtils.status = this.errorCodes.ERROR;
413

    
414
        }
415
      ));
416
    }
417

    
418

    
419
  }
420

    
421
  private numOfSearchResults(resultType: string, fetchClass: FetchResearchResults, refineParams) {
422
    return this._searchResearchResultsService.numOfSearchResults(resultType, this.keyword, this.properties, refineParams).subscribe(
423
      data => {
424
        fetchClass.searchUtils.totalResults = data;
425
        fetchClass.searchUtils.status = this.errorCodes.DONE;
426
        if (fetchClass.searchUtils.totalResults == 0) {
427
          fetchClass.searchUtils.status = this.errorCodes.NONE;
428
        }
429
      },
430
      err => {
431
        this.handleError("Error getting number of "+this.getEntityName(resultType, true, true), err);
432
        fetchClass.searchUtils.status = this.errorCodes.ERROR;
433
      }
434
    );
435
  }
436

    
437
  private reloadTabs() {
438
    this.reloadPublications = true;
439
    this.reloadDatasets = true;
440
    this.reloadSoftware = true;
441
    this.reloadOrps = true;
442
    this.reloadProjects = true;
443
    this.reloadDataproviders = true;
444
    this.reloadOrganizations = true;
445
    this.fetchOrganizations.searchUtils.status = null;
446
    this.fetchDataproviders.searchUtils.status = null;
447
    this.fetchProjects.searchUtils.status = null;
448
    this.fetchPublications.searchUtils.status = null;
449
    this.fetchDatasets.searchUtils.status = null;
450
    this.fetchSoftware.searchUtils.status = null;
451
    this.fetchOrps.searchUtils.status = null;
452
  }
453

    
454
  private handleError(message: string, error) {
455
    console.error("General Search Page: " + message, error);
456
  }
457

    
458
  private getEntityName (entityType:string, plural:boolean, full:boolean): string {
459
    if(entityType == "publication") {
460
      return "publication" + (plural ? "s" : "");
461
    } else if(entityType == "dataset") {
462
      return (full ? "research data" : ("dataset" + (plural ? "s" : "")));
463
    } else if(entityType == "software") {
464
      return "software";
465
    } else if(entityType == "other") {
466
      return (full ? ("other research product" + (plural ? "s" : "")) : "other");
467
    }
468
  }
469
}
(3-3/6)