Project

General

Profile

1
import {Component, Input, Output, EventEmitter, ViewChild, ChangeDetectionStrategy, ViewEncapsulation} from '@angular/core';
2
import {Observable}       from 'rxjs/Observable';
3
import {ActivatedRoute, Router} from '@angular/router';
4
import {Location} from '@angular/common';
5

    
6
import { Meta, MetaDefinition} from '../../../angular2-meta';
7

    
8
import { FetchPublications } from '../../utils/fetchEntitiesClasses/fetchPublications.class';
9
import { FetchDataproviders } from  '../../utils/fetchEntitiesClasses/fetchDataproviders.class';
10
import { FetchProjects } from  '../../utils/fetchEntitiesClasses/fetchProjects.class';
11
import { FetchDatasets } from '../../utils/fetchEntitiesClasses/fetchDatasets.class';
12
import { FetchSoftware } from '../../utils/fetchEntitiesClasses/fetchSoftware.class';
13
import { FetchOrganizations } from '../../utils/fetchEntitiesClasses/fetchOrganizations.class';
14

    
15
import {SearchPublicationsService} from '../../services/searchPublications.service';
16
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
17
import {SearchProjectsService} from '../../services/searchProjects.service';
18
import {SearchDatasetsService} from '../../services/searchDatasets.service';
19
import {SearchSoftwareService} from '../../services/searchSoftware.service';
20
import {SearchOrganizationsService} from '../../services/searchOrganizations.service';
21

    
22
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
23
import {SearchFields} from '../../utils/properties/searchFields';
24
import {ErrorCodes} from '../../utils/properties/openaireProperties';
25
import {RouterHelper} from '../../utils/routerHelper.class';
26
import {RefineFieldResultsService} from '../../services/refineFieldResults.service';
27
import {PiwikService} from '../../utils/piwik/piwik.service';
28
import { ConfigurationService } from '../../utils/configuration/configuration.service';
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; piwiksub: any;
38
  public reloadPublications: boolean;
39
  public reloadDatasets: boolean;
40
  public reloadSoftware: boolean;
41
  public reloadProjects: boolean;
42
  public reloadDataproviders: boolean;
43
  public reloadOrganizations: boolean;
44

    
45

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

    
55
  public activeTab = "publications";
56
  public linkToSearchPublications = "";
57
  public linkToSearchProjects = "";
58
  public linkToSearchDataproviders = "";
59
  public linkToSearchDatasets = "";
60
  public linkToSearchSoftware = "";
61
  public linkToSearchOrganizations = "";
62

    
63
  public fetchPublications : FetchPublications;
64
  public fetchDataproviders : FetchDataproviders;
65
  public fetchProjects : FetchProjects;
66
  public fetchDatasets: FetchDatasets;
67
  public fetchSoftware: FetchSoftware;
68
  public fetchOrganizations: FetchOrganizations;
69

    
70
  public searchFields:SearchFields = new SearchFields();
71
  public errorCodes:ErrorCodes = new ErrorCodes();
72
  public routerHelper:RouterHelper = new RouterHelper();
73

    
74
  public publicationsSize:any = null;
75
  public datasetsSize:any = null;
76
  public softwareSize:any = null;
77
  public fundersSize:any = null;
78
  public projectsSize:any = null;
79
  public datasourcesSize:any = null;
80
  showPublications:boolean= false;
81
  showDatasets:boolean= false;
82
  showSoftware:boolean=false;
83
  showProjects:boolean= false;
84
  showDataProviders:boolean= false;
85
  showOrganizations:boolean= false;
86
public subPub;public subData;public subProjects;public subOrg; public subDataPr;
87
  constructor ( private route: ActivatedRoute,
88
      private _router: Router,
89
      private _searchPublicationsService: SearchPublicationsService,
90
      private _searchDataprovidersService: SearchDataprovidersService,
91
      private _searchProjectsService: SearchProjectsService,
92
      private _searchDatasetsService: SearchDatasetsService,
93
      private _searchSoftwareService: SearchSoftwareService,
94
      private _searchOrganizationsService: SearchOrganizationsService,
95
      private _refineFieldResultsService:RefineFieldResultsService,
96
      private location: Location, private _meta: Meta,private _piwikService:PiwikService,
97
      private config: ConfigurationService ) {
98
          this.fetchPublications = new FetchPublications(this._searchPublicationsService);
99
          this.fetchDataproviders = new FetchDataproviders(this._searchDataprovidersService);
100
          this.fetchProjects = new FetchProjects(this._searchProjectsService);
101
          this.fetchDatasets = new FetchDatasets( this._searchDatasetsService);
102
          this.fetchSoftware = new FetchSoftware(this._searchSoftwareService);
103
          this.fetchOrganizations = new FetchOrganizations( this._searchOrganizationsService);
104

    
105
          var description = "open access, research, scientific publication, European Commission, EC, FP7, ERC, Horizon 2020, H2020, search, projects ";
106

    
107
          var title = "OpenAIRE | Search publications, research data, projects... | OpenAIRE";
108

    
109
          var url = OpenaireProperties.getBaseLink()+this._router.url;
110
          this._meta.setTitle(title);
111
          this._meta.updateMeta("description", description);
112
          this._meta.updateProperty("og:description", description);
113
          this._meta.updateProperty("og:title", title);
114
          this._meta.updateProperty("og:url", url);
115
          if(OpenaireProperties.isPiwikTrackEnabled() && (typeof document !== 'undefined')){
116
            this.piwiksub = this._piwikService.trackView("OpenAIRE |Search publications, research data, projects...").subscribe();
117

    
118
          }
119

    
120

    
121

    
122
  }
123

    
124
  public ngOnInit() {
125
    const entityOne = this.config.isEntityEnabled("publication");
126
    const entityTwo = this.config.isEntityEnabled("dataset");
127
    const entityThree = this.config.isEntityEnabled("project");
128
    const entityFour = this.config.isEntityEnabled("organization");
129
    const entityFive = this.config.isEntityEnabled("datasource");
130
    const entitySix = this.config.isEntityEnabled("software");
131
    const example = Observable.zip(entityOne,entityTwo,entityThree,entityFour,entityFive,entitySix);
132

    
133
//wait until all observables have emitted a value then emit all as an array
134
    this.sub =  this.route.queryParams.subscribe(params => {
135
      const subscribe = example.subscribe(data => {
136
         this.showPublications =  data[0];
137
         this.showDatasets =  data[1];
138
         this.showProjects =  data[2];
139
         this.showOrganizations =  data[3];
140
         this.showDataProviders =  data[4];
141
         this.showSoftware = data[5];
142
          console.log(data)
143
          this.keyword = (params['keyword'])?params['keyword']:"";
144
          if(this.keyword !=null && this.keyword.length > 0){
145
            this.reloadTabs();
146
            //if showPublications == false will set another entity as the first
147
            if(this.showPublications){
148
              this.activeTab = "publications";
149
              this.searchPublications();
150
            }else if(this.showDatasets){
151
              this.activeTab = "research data";
152
              this.searchDatasets();
153
            }else if(this.showSoftware){
154
              this.activeTab = "software";
155
              this.searchSoftware();
156
            }else if(this.showProjects){
157
              this.activeTab = "projects";
158
              this.searchProjects();
159
            }else if(this.showDataProviders){
160
              this.activeTab = "content providers";
161
              this.searchDataProviders();
162
            }else if(this.showOrganizations){
163
              this.activeTab = "organizations";
164
              this.searchOrganizations();
165
            }
166
            this.count();
167
          }
168
        });
169
      });
170

    
171
  }
172
  public ngOnDestroy() {
173
    this.sub.unsubscribe();
174
    if(this.piwiksub){
175
  this.piwiksub.unsubscribe();
176
}
177
    if(this.keyword !=null && this.keyword.length > 0){
178
        if(this.subPub){
179
          this.subPub.unsubscribe();
180
        }
181
        if(this.subData){
182
          this.subData.unsubscribe();
183
        }
184
        if(this.subProjects){
185
          this.subProjects.unsubscribe();
186
        }
187
        if(this.subOrg){
188
          this.subOrg.unsubscribe();
189
        }
190
        if(this.subDataPr){
191
          this.subDataPr.unsubscribe();
192
        }
193

    
194
    }
195
  }
196
  public searchPublications() {
197
    this.activeTab = "publications";
198
    if( this.reloadPublications &&
199
        this.fetchPublications.searchUtils.status != this.errorCodes.NONE &&
200
        this.fetchPublications.searchUtils.status != this.errorCodes.ERROR) {
201
      this.reloadPublications = false;
202
      this.fetchPublications.getResultsByKeyword(this.keyword,  1, 10);
203
      this.linkToSearchPublications = OpenaireProperties.getLinkToSearchPublications();// + "?keyword="  + this.keyword;
204
    }
205
   }
206
   public searchDatasets() {
207
     this.activeTab = "research data";
208
     if(this.reloadDatasets &&
209
        this.fetchDatasets.searchUtils.status != this.errorCodes.NONE &&
210
        this.fetchDatasets.searchUtils.status != this.errorCodes.ERROR) {
211
       this.reloadDatasets = false;
212
       this.fetchDatasets.getResultsByKeyword(this.keyword,  1, 10);
213
       this.linkToSearchDatasets = OpenaireProperties.getLinkToSearchDatasets();// + "?keyword="  + this.keyword;
214
     }
215
   }
216
   public searchSoftware() {
217
     this.activeTab = "software";
218
     if(this.reloadSoftware &&
219
        ( this.fetchSoftware.searchUtils.status == this.errorCodes.LOADING ||
220
          this.fetchSoftware.searchUtils.status == this.errorCodes.DONE )) {
221
       this.reloadSoftware = false;
222
       this.fetchSoftware.getResultsByKeyword(this.keyword,  1, 10);
223
       this.linkToSearchSoftware = OpenaireProperties.getLinkToSearchSoftware();// + "?keyword="  + this.keyword;
224
     }
225
   }
226
   public  searchProjects() {
227
     this.activeTab = "projects";
228
     if(this.reloadProjects &&
229
        this.fetchProjects.searchUtils.status != this.errorCodes.NONE &&
230
        this.fetchProjects.searchUtils.status != this.errorCodes.ERROR) {
231
       this.reloadProjects = false;
232
       this.fetchProjects.getResultsByKeyword(this.keyword,  1, 10);
233
       this.linkToSearchProjects = OpenaireProperties.getLinkToSearchProjects();// + "?keyword="  + this.keyword;
234
     }
235
   }
236
   public  searchDataProviders() {
237
     this.activeTab = "content providers";
238
     if( this.reloadDataproviders &&
239
         this.fetchDataproviders.searchUtils.status != this.errorCodes.NONE &&
240
         this.fetchDataproviders.searchUtils.status != this.errorCodes.ERROR) {
241
       this.reloadDataproviders = false;
242
       this.fetchDataproviders.getResultsByKeyword(this.keyword, 1, 10);
243
       this.linkToSearchDataproviders = OpenaireProperties.getLinkToSearchDataProviders();// + "?keyword="  + this.keyword;
244
     }
245
   }
246
   public searchOrganizations() {
247
     this.activeTab = "organizations";
248
     if( this.reloadOrganizations &&
249
         this.fetchOrganizations.searchUtils.status != this.errorCodes.NONE &&
250
         this.fetchOrganizations.searchUtils.status != this.errorCodes.ERROR) {
251
       this.reloadOrganizations = false;
252
       this.fetchOrganizations.getResultsByKeyword(this.keyword,  1, 10);
253
       this.linkToSearchOrganizations = OpenaireProperties.getLinkToSearchOrganizations();// + "?keyword="  + this.keyword;
254
     }
255
   }
256

    
257

    
258
   public keywordChanged($event){
259

    
260
    this.keyword = $event.value;
261
    console.info("Search Find: search with keyword \"" +  this.keyword + "\"" );
262
      this.location.go(location.pathname,"?keyword="  + this.keyword);
263
    this.reloadTabs();
264
    if(this.activeTab == "publications") {
265
      this.searchPublications();
266
    }
267
    if(this.activeTab == "projects") {
268
      this.searchProjects();
269
    }
270
    if(this.activeTab == "content providers") {
271
      this.searchDataProviders();
272
    }
273
    if(this.activeTab == "research data") {
274
      this.searchDatasets();
275
    }
276
    if(this.activeTab == "software") {
277
      this.searchSoftware();
278
    }
279
    if(this.activeTab == "organizations") {
280
      this.searchOrganizations();
281
    }
282

    
283
    this.count();
284
   }
285

    
286
   private count() {
287
     if(this.activeTab != "publications" && this.showPublications){
288
      this.fetchPublications.searchUtils.status = this.errorCodes.LOADING;
289
      this.fetchPublications.results = [];
290
      this.subPub = this._searchPublicationsService.numOfSearchPublications(this.keyword).subscribe(
291
             data => {
292
                console.log("Count results: "+data);
293
                 this.fetchPublications.searchUtils.totalResults = data;
294
                 this.fetchPublications.searchUtils.status = this.errorCodes.DONE;
295
                 if(this.fetchPublications.searchUtils.totalResults == 0) {
296
                   this.fetchPublications.searchUtils.status = this.errorCodes.NONE;
297
                 }
298
             },
299
             err => {
300
       		    console.log(err);
301
              this.fetchPublications.searchUtils.status = this.errorCodes.ERROR;
302
       		 }
303
      );
304
    }
305
    if(this.activeTab != "research data" && this.showDatasets){
306
       this.fetchDatasets.searchUtils.status = this.errorCodes.LOADING;
307
       this.fetchDatasets.results = [];
308
       this.subData = this._searchDatasetsService.numOfSearchDatasets(this.keyword).subscribe(
309
             data => {
310
                 this.fetchDatasets.searchUtils.totalResults = data;
311
                 this.fetchDatasets.searchUtils.status = this.errorCodes.DONE;
312
                 if(this.fetchDatasets.searchUtils.totalResults == 0) {
313
                   this.fetchDatasets.searchUtils.status = this.errorCodes.NONE;
314
                 }
315
             },
316
             err => {
317
                 console.log(err);
318
                 this.fetchDatasets.searchUtils.status = this.errorCodes.ERROR;
319
              }
320
       );
321
    }
322
    if(this.activeTab != "software" && this.showSoftware){
323
       this.fetchSoftware.searchUtils.status = this.errorCodes.LOADING;
324
       this.fetchSoftware.results = [];
325
       this.subData = this._searchSoftwareService.numOfSearchSoftware(this.keyword).subscribe(
326
             data => {
327
                 this.fetchSoftware.searchUtils.totalResults = data;
328
                 this.fetchSoftware.searchUtils.status = this.errorCodes.DONE;
329
                 if(this.fetchSoftware.searchUtils.totalResults == 0) {
330
                   this.fetchSoftware.searchUtils.status = this.errorCodes.NONE;
331
                 }
332
             },
333
             err => {
334
                 console.log(err);
335
                 if(err.status == '404') {
336
                   this.fetchSoftware.searchUtils.status = this.errorCodes.NOT_FOUND;
337
                 } else if(err.status == '500') {
338
                   this.fetchSoftware.searchUtils.status = this.errorCodes.ERROR;
339
                 } else {
340
                   this.fetchSoftware.searchUtils.status = this.errorCodes.NOT_AVAILABLE;
341
                 }
342
                 //this.fetchSoftware.searchUtils.status = this.errorCodes.ERROR;
343
              }
344
       );
345
    }
346
    if(this.activeTab != "projects" && this.showProjects){
347
      this.fetchProjects.searchUtils.status = this.errorCodes.LOADING;
348
      this.fetchProjects.results = [];
349
      this.subProjects = this._searchProjectsService.numOfSearchProjects(this.keyword).subscribe(
350
             data => {
351
                 this.fetchProjects.searchUtils.totalResults = data;
352
                 this.fetchProjects.searchUtils.status = this.errorCodes.DONE;
353
                 if(this.fetchProjects.searchUtils.totalResults == 0) {
354
                   this.fetchProjects.searchUtils.status = this.errorCodes.NONE;
355
                 }
356
             },
357
             err => {
358
                 console.log(err);
359
                 this.fetchProjects.searchUtils.status = this.errorCodes.ERROR;
360
              }
361
       );
362
     }
363
     if(this.activeTab != "content providers" && this.showDataProviders){
364
       this.fetchDataproviders.results = [];
365
       this.fetchDataproviders.getNumForSearch(this.keyword);
366
     }
367
      if(this.activeTab != "organizations" && this.showOrganizations){
368
       this.fetchOrganizations.searchUtils.status = this.errorCodes.LOADING;
369
       this.fetchOrganizations.results = [];
370
       this.subOrg = this._searchOrganizationsService.numOfSearchOrganizations(this.keyword).subscribe(
371
             data => {
372
                 this.fetchOrganizations.searchUtils.totalResults = data;
373
                 this.fetchOrganizations.searchUtils.status = this.errorCodes.DONE;
374
                 if(this.fetchOrganizations.searchUtils.totalResults == 0) {
375
                   this.fetchOrganizations.searchUtils.status = this.errorCodes.NONE;
376
                 }
377
             },
378
             err => {
379
                 console.log(err);
380
                 this.fetchOrganizations.searchUtils.status = this.errorCodes.ERROR;
381

    
382
              }
383
         );
384
       }
385

    
386

    
387
   }
388

    
389
   private reloadTabs() {
390
       this.reloadPublications = true;
391
       this.reloadDatasets = true;
392
       this.reloadSoftware = true;
393
       this.reloadProjects = true;
394
       this.reloadDataproviders = true;
395
       this.reloadOrganizations = true;
396
   }
397

    
398

    
399

    
400
}
(4-4/4)