Project

General

Profile

1
import {Component, ElementRef, Input, ViewChild} from '@angular/core';
2
import {ActivatedRoute, Router} from '@angular/router';
3
import {Meta, Title} from '@angular/platform-browser';
4
import {OrganizationService} from '../../services/organization.service';
5
import {OrganizationInfo} from '../../utils/entities/organizationInfo';
6
import {ReportsService} from '../../services/reports.service';
7
import {FetchProjects} from '../../utils/fetchEntitiesClasses/fetchProjects.class';
8
import {SearchResearchResultsService} from '../../services/searchResearchResults.service';
9
import {SearchDataprovidersService} from '../../services/searchDataproviders.service';
10
import {SearchProjectsService} from '../../services/searchProjects.service';
11
import {ErrorCodes} from '../../utils/properties/errorCodes';
12
import {ProjectsInModalComponent} from '../landing-utils/projects-in-modal.component';
13
import {RouterHelper} from '../../utils/routerHelper.class';
14

    
15
import {ModalLoading} from '../../utils/modal/loading.component';
16
import {PiwikService} from '../../utils/piwik/piwik.service';
17
import {StringUtils} from '../../utils/string-utils.class';
18
import {EnvProperties} from '../../utils/properties/env-properties';
19
import {SEOService} from '../../sharedComponents/SEO/SEO.service';
20
import {HelperFunctions} from "../../utils/HelperFunctions.class";
21
import {HelperService} from "../../utils/helper/helper.service";
22
import {Location} from "@angular/common";
23

    
24
interface Total {
25
  publications: number;
26
  datasets: number;
27
  software: number;
28
  other: number;
29
  results: number;
30
  dataproviders: number;
31
}
32

    
33
@Component({
34
  selector: 'organization',
35
  templateUrl: 'organization.component.html',
36
})
37
export class OrganizationComponent {
38
  @Input() piwikSiteId = null;
39
  @Input() communityId = null;
40
  
41
  public organizationInfo: OrganizationInfo;
42
  public organizationId: string;
43
  
44
  // Message variables
45
  public warningMessage = "";
46
  public errorMessage = "";
47
  public showLoading: boolean = true;
48
  
49
  // CSV variables
50
  public downloadURLAPI: string;
51
  public csvProjectParamsHead: string;
52
  public csvPublicationParamsHead: string;
53
  public csvParamsTail: string;
54
  
55
  // Active tab variable for responsiveness
56
  public activeTab: string = "Publications";
57
  
58
  // Variables for publications, research data, projects, dataproviders tabs
59
  public total: Total = {
60
    publications: 0,
61
    datasets: 0,
62
    software: 0,
63
    other: 0,
64
    results: 0,
65
    dataproviders: 0
66
  };
67
  public fetchProjects: FetchProjects;
68
  // Variables for projects query (query results only if projects tab is clicked)
69
  @ViewChild(ProjectsInModalComponent) projectsInModalComponent: ProjectsInModalComponent;
70
  
71
  @ViewChild(ModalLoading) loading: ModalLoading;
72
  // Alert box when CSV: Project Publications for a funder is requested
73
  @ViewChild('AlertModalApplyAll') alertApplyAll;
74
  // Alert box when something is wrong with CSV requests
75
  @ViewChild('AlertModalCsvError') alertCsvError;
76
  
77
  public routerHelper: RouterHelper = new RouterHelper();
78
  public errorCodes: ErrorCodes = new ErrorCodes();
79
  public pageContents = null;
80
  public divContents = null;
81
  
82
  // Helper variables to specify funder in downloadPublicationsFile function
83
  public contentTypes: [string,string][] =[
84
    ['results', 'all research outcomes'],
85
    ['publications', 'publications'],
86
    ['datasets', 'research data'],
87
    ['software', 'software'],
88
    ['other', 'other research products'],
89
  ];
90
  public funderContentType: string = '';
91
  public funder: any = "";
92
  private funderId: string;
93
  private funderCount: number;
94
  sub: any;
95
  infoSub: any;
96
  piwiksub: any;
97
  downloadFileSub: any;
98
  downloadFilePiwikSub: any;
99
  countProjectsSub: any;
100
  countPublSub: any;
101
  downloadProjectPublSub: any;
102
  properties: EnvProperties;
103
  public indexUpdateDate: Date;
104
  public showFeedback: boolean = false;
105
  public feedbackFields: string [] = ['Name', 'Country', 'Other'];
106
  
107
  @ViewChild('AlertModalDeletedByInference') alertModalDeletedByInference;
108
  @ViewChild('projectsModal') projectsModal;
109
  public deleteByInferenceOpened: boolean = false;
110
  
111
  //private ngUnsubscribe: Subject<void> = new Subject<void>();
112
  
113
  constructor(private element: ElementRef,
114
              private _organizationService: OrganizationService,
115
              private _piwikService: PiwikService,
116
              private  route: ActivatedRoute,
117
              private _searchDataprovidersService: SearchDataprovidersService,
118
              private _reportsService: ReportsService,
119
              private _searchResearchResultsService: SearchResearchResultsService,
120
              // private _searchDatasetsService: SearchDatasetsService,
121
              private _searchProjectsService: SearchProjectsService,
122
              private _meta: Meta,
123
              private _title: Title,
124
              private _router: Router,
125
              private helper: HelperService,
126
              private seoService: SEOService,
127
              private _location: Location) {
128
    this.fetchProjects = new FetchProjects(this._searchProjectsService);
129
  }
130
  
131
  ngOnInit() {
132
    this.route.data
133
      .subscribe((data: { envSpecific: EnvProperties }) => {
134
        this.properties = data.envSpecific;
135
        if(this.properties.lastIndexUpdate) {
136
          this.indexUpdateDate = new Date(this.properties.lastIndexUpdate);
137
        }
138
        //this.getDivContents();
139
        this.getPageContents();
140
        this.updateUrl(data.envSpecific.baseLink + this._router.url);
141
      });
142
    this.sub = this.route.queryParams.subscribe(params => {
143
      this.organizationInfo = null;
144
      this.updateTitle("Organization");
145
      this.updateDescription("");
146
      
147
      this.organizationId = params['organizationId'];
148
      
149
      if (this.organizationId) {
150
        this.getOrganizationInfo();
151
      } else {
152
        this.showLoading = false;
153
        this._router.navigate(['/error'], {
154
          queryParams: {
155
            "page": this._location.path(true),
156
            "page_type": "organization"
157
          }
158
        });
159
        //this.warningMessage = "No valid organization id";
160
      }
161
      
162
      HelperFunctions.scroll();
163
      
164
      this.csvParamsTail = '" and relorganizationid exact "' + this.organizationId + '" ))';
165
      
166
    });
167
    
168
    this.downloadURLAPI = this.properties.csvAPIURL;
169
    //this.csvAffiliatedPublications = this.downloadURLAPI + "?format=csv&type=publications&fq=(((oaftype exact result) and (resulttypeid exact publication)) and (relorganizationid exact \"" + this.organizationId + "\"))";
170
    this.csvProjectParamsHead = 'format=csv&type=projects&fq=((funder exact "';
171
    //this.csvPublicationParamsHead = 'format=csv-special&type=publications&page=0&query=((((oaftype exact result) and (resulttypeid exact publication)) and (funderid exact ';
172
  }
173
  
174
  private getPageContents() {
175
    this.helper.getPageHelpContents(this._router.url, this.properties, this.communityId).subscribe(contents => {
176
      this.pageContents = contents;
177
    })
178
  }
179
  
180
  private getDivContents() {
181
    this.helper.getDivHelpContents(this._router.url, this.properties, this.communityId).subscribe(contents => {
182
      this.divContents = contents;
183
    })
184
  }
185
  
186
  
187
  ngOnDestroy() {
188
    if (this.sub) {
189
      this.sub.unsubscribe();
190
    }
191
    if (this.piwiksub) {
192
      this.piwiksub.unsubscribe();
193
    }
194
    if (this.infoSub) {
195
      this.infoSub.unsubscribe();
196
    }
197
    if (this.downloadFileSub) {
198
      this.downloadFileSub.unsubscribe();
199
    }
200
    if (this.downloadFilePiwikSub) {
201
      this.downloadFilePiwikSub.unsubscribe();
202
    }
203
    if (this.countProjectsSub) {
204
      this.countProjectsSub.unsubscribe();
205
    }
206
    if (this.countPublSub) {
207
      this.countPublSub.unsubscribe();
208
    }
209
    if (this.downloadProjectPublSub) {
210
      this.downloadProjectPublSub.unsubscribe();
211
    }
212
    
213
    /*
214
    this.ngUnsubscribe.next();
215
    this.ngUnsubscribe.complete();
216
    */
217
  }
218
  
219
  private getTotalResearchResults() {
220
    this._searchResearchResultsService.numOfEntityResults('publication',
221
      this.organizationInfo.objIdentifier, 'organization', this.properties).subscribe(total => {
222
      this.total.publications = total;
223
      this.total.results += total;
224
    });
225
    this._searchResearchResultsService.numOfEntityResults('dataset',
226
      this.organizationInfo.objIdentifier, 'organization', this.properties).subscribe(total => {
227
      this.total.datasets = total;
228
      this.total.results += total;
229
    });
230
    this._searchResearchResultsService.numOfEntityResults('software',
231
      this.organizationInfo.objIdentifier, 'organization', this.properties).subscribe(total => {
232
      this.total.software = total;
233
      this.total.results += total;
234
    });
235
    this._searchResearchResultsService.numOfEntityResults('other',
236
      this.organizationInfo.objIdentifier, 'organization', this.properties).subscribe(total => {
237
      this.total.other = total;
238
      this.total.results += total;
239
    });
240
  }
241
  
242
  private getTotalDataproviders() {
243
    this._searchDataprovidersService.numOfEntityDataproviders(
244
      this.organizationInfo.objIdentifier,'organization', this.properties).subscribe(
245
      total => {
246
        this.total.dataproviders = total;
247
      });
248
  }
249
  
250
  private getOrganizationInfo() {
251
    
252
    this.warningMessage = '';
253
    this.errorMessage = ""
254
    this.showLoading = true;
255
    
256
    this.organizationInfo = null;
257
    
258
    this.infoSub = this._organizationService.getOrganizationInfo(this.organizationId, this.properties).subscribe(
259
      data => {
260
        if (data == null) {
261
          this.showLoading = false;
262
          this._router.navigate(['/error'], {
263
            queryParams: {
264
              "page": this._location.path(true),
265
              "page_type": "organization"
266
            }
267
          });
268
          this.errorMessage = 'No organization found';
269
        } else {
270
          this.organizationInfo = data;
271
          this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this.properties.searchLinkToOrganization + this.organizationInfo.objIdentifier);
272
          this.updateTitle(this.organizationInfo.title.name);
273
          this.updateDescription("Organization, country, " + this.organizationInfo.title.name + ((this.organizationInfo.title.name && this.organizationInfo.title.name != this.organizationInfo.name) ? (", " + this.organizationInfo.name) : ""));
274
          if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
275
            this.piwiksub = this._piwikService.trackView(this.properties, this.organizationInfo.title.name, this.piwikSiteId).subscribe();
276
          }
277
          console.log(this.organizationInfo);
278
          var refineFields: string [] = ["funder"];
279
          this.getTotalResearchResults();
280
          this.getTotalDataproviders();
281
          this.fetchProjects.getResultsForOrganizations(this.organizationId, "", 1, 0, refineFields, this.properties);
282
          this.showLoading = false;
283
        }
284
      },
285
      err => {
286
        //console.log(err)
287
        this.handleError("Error getting organization for id: " + this.organizationId, err);
288
        if (err.status == 404) {
289
          this._router.navigate(['/error'], {
290
            queryParams: {
291
              "page": this._location.path(true),
292
              "page_type": "organization"
293
            }
294
          });
295
        }
296
        this.seoService.createLinkForCanonicalURL(this.properties.baseLink + this.properties.searchLinkToOrganizations);
297
        //this.errorMessage = 'No organization found';
298
        this.showLoading = false;
299
      }
300
    );
301
  }
302
  
303
  
304
  /*public searchDataproviders(page: number = 1) {
305
    this.fetchDataproviders.getResultsForEntity("organization", this.organizationId, page, 1, this.properties);
306
  }*/
307
  
308
  public downloadFile(url: string, filename: string) {
309
    this.openLoading();
310
    this.setMessageLoading("Downloading CSV file");
311
    
312
    this.downloadFileSub = this._reportsService.downloadCSVFile(url).subscribe(
313
      data => {
314
        this.closeLoading();
315
        
316
        var url = window.URL.createObjectURL(data);
317
        var a = window.document.createElement('a');
318
        window.document.body.appendChild(a);
319
        a.setAttribute('style', 'display: none');
320
        a.href = url;
321
        a.download = filename + ".csv";
322
        a.click();
323
        window.URL.revokeObjectURL(url);
324
        a.remove(); // remove the element
325
        
326
        //window.open(window.URL.createObjectURL(data));
327
        if (this.properties.enablePiwikTrack && (typeof document !== 'undefined')) {
328
          this.downloadFilePiwikSub = this._piwikService.trackDownload(this.properties, url, this.piwikSiteId).subscribe();
329
        }
330
      },
331
      err => {
332
        //console.log("Error downloading the file.");
333
        this.handleError("Error downloading file: " + filename + ".csv", err);
334
        
335
        this.closeLoading();
336
        this.confirmOpenCsvError();
337
      }/*,
338
           () => console.log('Completed file download.')*/
339
    );
340
  }
341
  
342
  private downloadFileByFunder() {
343
    
344
    this.openLoading();
345
    this.setMessageLoading("Downloading CSV file");
346
    
347
    let response: string[] = [];
348
    let totalResponse: string = "";
349
    let projects = [];
350
    let counter: number = this.funderCount;
351
    let title: boolean = false;
352
    let title_index: number = 0;
353
    
354
    let filename: string = 'funder-project-' + this.funderContentType + '-report';
355
    
356
    this.countProjectsSub = this._searchProjectsService.getProjectsForOrganizations(this.organizationId, ' and (funder exact "' + this.encodeURI(this.funderId) + '" ) ', 1, this.funderCount, [], this.properties).subscribe(
357
      data => {
358
        projects = data[1];
359
        for (let index = 0; index < projects.length; index++) {
360
          this.countPublSub = this._searchResearchResultsService.numOfEntityResults(this.funderContentType, projects[index].id, "project", this.properties).subscribe(
361
            data => {
362
              //if(data == 0 && title) {   // if no publications for this project
363
              if (data == 0 && (counter > 1 || title)) {   // if no publications for this project
364
                counter--;
365
                response[index] = "";
366
                //console.info("index: "+index, "counter: "+counter, "id:"+projects[index].id, response[index]);
367
                
368
                if (counter == 0) {
369
                  //for(let i=count-1; i>=0; i--) {
370
                  for (let i = 0; i < projects.length; i++) {
371
                    if (response[i] != "") {
372
                      if (i == title_index) {
373
                        totalResponse = response[i] + totalResponse;
374
                      } else {
375
                        totalResponse += response[i];
376
                      }
377
                    }
378
                  }
379
                  this.closeLoading();
380
                  
381
                  var csvurl = window.URL.createObjectURL(new Blob([totalResponse], {type: 'text/csv'}));
382
                  var a = window.document.createElement('a');
383
                  window.document.body.appendChild(a);
384
                  a.setAttribute('style', 'display: none');
385
                  a.href = csvurl;
386
                  a.download = filename + ".csv";
387
                  a.click();
388
                  window.URL.revokeObjectURL(csvurl);
389
                  a.remove(); // remove the element
390
                }
391
              } else {
392
                let url: string;
393
                if (!title) {
394
                  title_index = index;
395
                  //console.info(title_index);
396
                  url = this.downloadURLAPI + '?format=csv-special&' + this.getTypeParam(this.funderContentType)+ '&fq=((relprojectid exact "' + projects[index].id + '"))';
397
                } else {
398
                  url = this.downloadURLAPI + '?format=csv-special-notitle&' + this.getTypeParam(this.funderContentType)+ '&fq=((relprojectid exact "' + projects[index].id + '"))';
399
                }
400
                title = true;
401
                
402
                this.downloadProjectPublSub = this._reportsService.getCSVResponse(url).subscribe(
403
                  data => {
404
                    counter--;
405
                    response[index] = data;
406
                    //console.info("index: "+index, "counter: "+counter, "id:"+projects[index].id, response[index]);
407
                    
408
                    if (counter == 0) {
409
                      //for(let i=count-1; i>=0; i--) {
410
                      for (let i = 0; i < projects.length; i++) {
411
                        if (response[i] != "") {
412
                          if (i == title_index) {
413
                            totalResponse = response[i] + totalResponse;
414
                          } else {
415
                            totalResponse += response[i];
416
                          }
417
                        }
418
                      }
419
                      this.closeLoading();
420
                      
421
                      var csvurl = window.URL.createObjectURL(new Blob([totalResponse], {type: 'text/csv'}));
422
                      var a = window.document.createElement('a');
423
                      window.document.body.appendChild(a);
424
                      a.setAttribute('style', 'display: none');
425
                      a.href = csvurl;
426
                      a.download = filename + ".csv";
427
                      a.click();
428
                      window.URL.revokeObjectURL(csvurl);
429
                      a.remove(); // remove the element
430
                    }
431
                  },
432
                  err => {
433
                    this.handleError("Error downloading file: " + filename, err);
434
                    
435
                    this.closeLoading();
436
                    this.confirmOpenCsvError();
437
                  }/*,
438
                            () => console.log('Completed file download.')*/
439
                );
440
              }
441
            },
442
            err => {
443
              this.handleError("Error getting number of publications for project with id: " + projects[index].id, err);
444
            });
445
        }
446
      },
447
      err => {
448
        this.handleError("Error getting projects for organization with id: " + this.organizationId, err);
449
        
450
        this.closeLoading();
451
        this.confirmOpenCsvError();
452
      }
453
    );
454
  }
455
  
456
  private updateDescription(description: string) {
457
    this._meta.updateTag({content: description.substring(0, 160)}, "name='description'");
458
    this._meta.updateTag({content: description.substring(0, 160)}, "property='og:description'");
459
  }
460
  
461
  private updateTitle(title: string) {
462
    var _prefix = "";
463
    // if(!this.communityId) {
464
    //   _prefix = "OpenAIRE | ";
465
    // }
466
    // var _title = _prefix + ((title.length > 50) ? title.substring(0, 50) : title);
467
    this._title.setTitle(title);
468
    this._meta.updateTag({content: title}, "property='og:title'");
469
  }
470
  
471
  private updateUrl(url: string) {
472
    this._meta.updateTag({content: url}, "property='og:url'");
473
  }
474
  
475
  private openLoading() {
476
    if (this.loading) {
477
      this.loading.open();
478
    }
479
  }
480
  
481
  private closeLoading() {
482
    if (this.loading) {
483
      this.loading.close();
484
    }
485
  }
486
  
487
  private setMessageLoading(message: string) {
488
    if (this.loading) {
489
      this.loading.message = message;
490
    }
491
  }
492
  
493
  public confirmOpenApplyAll(contentType: string) {
494
    this.alertApplyAll.cancelButton = true;
495
    this.alertApplyAll.okButton = true;
496
    this.alertApplyAll.alertTitle = "CSV FILE";
497
    this.alertApplyAll.message = "Do you wish to download a CSV file? Note that this process may take a while.";
498
    this.alertApplyAll.okButtonText = "Yes";
499
    this.alertApplyAll.cancelButtonText = "No";
500
    this.alertApplyAll.open();
501
    this.funderId = this.funder.id;
502
    this.funderCount = this.funder.number;
503
    this.funderContentType = contentType;
504
  }
505
  
506
  public confirmCloseApplyAll() {
507
    this.downloadFileByFunder();
508
  }
509
  
510
  public confirmOpenCsvError() {
511
    this.alertCsvError.cancelButton = false;
512
    this.alertCsvError.okButton = true;
513
    this.alertCsvError.alertTitle = "ERROR DOWNLOADING CSV FILE";
514
    this.alertCsvError.message = "There was an error in csv downloading. Please try again later.";
515
    this.alertCsvError.okButtonText = "OK";
516
    this.alertCsvError.open();
517
  }
518
  
519
  encodeURI(input: string): string {
520
    return StringUtils.URIEncode(input);
521
  }
522
  
523
  private handleError(message: string, error) {
524
    console.error("Organizaton Landing Page: " + message, error);
525
  }
526
  
527
  openDeletedByInference() {
528
    this.deleteByInferenceOpened = true;
529
    this.alertModalDeletedByInference.cancelButton = false;
530
    this.alertModalDeletedByInference.okButton = false;
531
    this.alertModalDeletedByInference.alertTitle = "Other versions of";
532
    this.alertModalDeletedByInference.open();
533
  }
534
  
535
  openProjectsModal() {
536
    this.projectsInModalComponent.pageChange({value: 1});
537
    this.projectsModal.cancelButton = false;
538
    this.projectsModal.okButton = false;
539
    this.projectsModal.alertTitle = "Projects of";
540
    this.projectsModal.open();
541
  }
542
  
543
  public getTypeParam(type: string): string {
544
    if(type == 'results') {
545
      type = 'publications&type=datasets&type=software&type=other';
546
    }
547
    return 'type='+type;
548
  }
549
  
550
  getCSVAffiliated(contentType: string): string {
551
    return this.downloadURLAPI + '?format=csv&' + this.getTypeParam(contentType) + '&fq=(relorganizationid exact "' + this.organizationId + '")';
552
  }
553
  
554
  getFunderProjects(): string {
555
    return this.downloadURLAPI + '?'+this.csvProjectParamsHead+encodeURI(this.funder.id)+this.csvParamsTail;
556
  }
557
}
(3-3/4)