Project

General

Profile

1
import {Component, OnInit} from "@angular/core";
2
import {RepositoryService} from "../../services/repository.service";
3
import {ActivatedRoute, Router} from "@angular/router";
4
import {AuthenticationService} from "../../services/authentication.service";
5
import {Repository} from "../../domain/typeScriptClasses";
6
import {Observable} from "rxjs/Observable";
7
import {ReportResponseWrapper} from "../../domain/usageStatsClasses";
8
import {loadingRepoMessage} from "../../domain/shared-messages";
9

    
10

    
11
@Component({
12
  selector: 'metrics-usagestats-report',
13
  templateUrl: 'metrics-usagestats-report.component.html'
14
})
15

    
16
export class MetricsUsagestatsReportComponent implements OnInit {
17

    
18
  errorMessage: string;
19
  loadingMessage: string;
20
  title: string;
21

    
22
  repo: Repository;
23
  repoId: string;
24
  shownRepoId: string;
25
  shownOpenaireId: string;
26
  issnToShow: string = '';
27
  chosen_report: string;
28

    
29
  userEmail: string;
30
  beginDate: string = '';
31
  endDate: string = '';
32
  itemIdentifier: string = '';
33
  itemDataType: string = '';
34
  granularity: string = 'Monthly';
35
  pretty: boolean = true;
36

    
37
  constructor(private repoService: RepositoryService,
38
              private route: ActivatedRoute,
39
              private router: Router,
40
              private authService: AuthenticationService) {}
41

    
42
  ngOnInit() {
43
    this.getParams();
44
    this.getUserEmail();
45
    this.getRepo();
46
  }
47

    
48
  getParams() {
49
    this.repoId = this.route.snapshot.paramMap.get('id');
50
    this.chosen_report = this.route.snapshot.paramMap.get('reportID');
51
    this.shownRepoId = this.convertToDisplayedFormat(this.repoId);
52
    console.log(`shownRepoId is ${this.repoId}`);
53
    this.title = `${this.chosen_report} report`;
54
  }
55

    
56
  convertToDisplayedFormat(input: string) {
57
    const tempArray = this.repoId.split('____::');
58
    return tempArray[0] + ':' + tempArray[1];
59
  }
60

    
61
  getUserEmail() {
62
    this.userEmail = this.authService.getUserEmail();
63
  }
64

    
65
  getRepo() {
66
    this.loadingMessage = loadingRepoMessage;
67
    this.repoService.getRepositoryById(this.repoId).subscribe(
68
      repo => this.repo = repo,
69
      error => {
70
        console.log(error);
71
        this.loadingMessage = '';
72
          this.errorMessage = 'The repository could not be retrieved';
73
      },
74
      () => {
75
        this.loadingMessage = '';
76
        if (this.repo.piwikInfo) {
77
          this.shownOpenaireId = this.convertToDisplayedFormat(this.repo.piwikInfo.openaireId);
78
        }
79
        if (this.repo.issn){
80
          this.issnToShow = this.repo.issn.slice(0, 4)+ '-' + this.repo.issn.toString().slice(4);
81
        }
82
        this.title = `${this.chosen_report} report for ${this.repo.officialName}`;
83
        if ( this.authService.getUserEmail() !== this.repo.registeredBy ) {
84
          this.router.navigateByUrl('/403-forbidden', { skipLocationChange: true });
85
        }
86
      }
87
    );
88
  }
89

    
90
  updateBeginDate(event: any) {
91
    this.beginDate = event.target.value;
92
  }
93

    
94
  updateEndDate(event: any) {
95
    this.endDate = event.target.value;
96
  }
97

    
98
  updateItemDataType(event: any) {
99
    this.itemDataType = event.target.value;
100
  }
101

    
102
  updateItemIdentifier(event: any) {
103
    this.itemIdentifier = event.target.value;
104
  }
105

    
106
  updateGranularity(event: any) {
107
    this.granularity = event.target.value;
108
  }
109

    
110
  updatePretty(event: any) {
111
    this.pretty = !this.pretty;
112
  }
113

    
114
  goToReport() {
115
    /* additional field: */
116
    /*itemIdentifier: this.repo.piwikInfo.openaireId,*/
117
    /*this.router.navigate([`/getImpact/${this.repoId}/${this.chosen_report}/results'], {
118
      queryParams: {
119
        report: this.chosen_report,
120
        beginDate: this.beginDate,
121
        endDate: this.endDate,
122
        repoId: this.shownRepoId,
123
        itemDataType: this.itemDataType,
124
        itemIdentifier: this.itemIdentifier,
125
        granularity: this.granularity,
126
        pretty: this.pretty
127
      }
128
    });*/
129

    
130
    const params = new URLSearchParams();
131

    
132
    params.append('Report', this.chosen_report);
133
    params.append('Release', '4');
134
    params.append('RequestorID', this.authService.getUserEmail());
135
    params.append('BeginDate', this.beginDate);
136
    params.append('EndDate', this.endDate);
137
    params.append('RepositoryIdentifier', this.shownRepoId);
138
    if (this.itemIdentifier) {
139
      params.append('ItemIdentifier', this.itemIdentifier);
140
    }
141
    if (this.itemDataType) {
142
      params.append('ItemDataType', this.itemDataType);
143
    }
144
    params.append('Granularity', this.granularity);
145
    if (this.pretty && this.pretty === true) {
146
      params.append('Pretty', 'Pretty');
147
    }
148

    
149
    let url = `http://beta.services.openaire.eu/usagestats/sushilite/GetReport/?${params}`;
150
    console.log(`going to: ${url}`);
151

    
152
    window.location.href = url;
153
  }
154

    
155
}
(10-10/16)