Project

General

Profile

1 61381 k.triantaf
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3
import {throwError} from 'rxjs';
4
import {ProjectInfo} from '../../utils/entities/projectInfo';
5
6
7
8
import{EnvProperties} from '../../utils/properties/env-properties';
9
10
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
11
import {map} from "rxjs/operators";
12
13
@Injectable()
14
export class ProjectService {
15
16
    constructor(private http: HttpClient ) {
17
      this.parsingFunctions = new ParsingFunctions();
18
    }
19
20
    public parsingFunctions: ParsingFunctions;
21
    projectInfo: ProjectInfo;
22
23
    getProjectInfo (id: string, properties:EnvProperties):any {
24
25
        let url = properties.searchAPIURLLAst + 'projects/'+id+"?format=json";
26
        let key = url;
27
28
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
29
                    //.map(res => <any> res.json())
30
                    .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
31
                    .pipe(map(res => [res,
32
                                 res['fundingtree'],
33
                                 res['rels']['rel']]))
34
                    .pipe(map(res => this.parseProjectInfo(res, properties)));
35
36
    }
37
38
    getProjectInfoByGrantId (grant: string, funder:string, properties:EnvProperties):any {
39
40
      let url =properties.searchAPIURLLAst+ 'resources?query=(oaftype exact project)'+
41
      'and (projectcode_nt exact "'+grant+'" ) and (fundershortname exact '+'"'+funder+ '"'+')'+"&format=json&size=1";
42
43
44
         let key = url;
45
46
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
47
                    //.map(res => <any> res.json())
48
                    .pipe(map(res => res['results'][0]))
49
                    .pipe(map(res => [res['result']['metadata']['oaf:entity']['oaf:project'],res['result']['header']['dri:objIdentifier']]))
50
                    .pipe(map(res => [res[0],
51
                                 res[0]['fundingtree'],
52
                                 res[0]['rels']['rel'],res[1]]))
53
                    .pipe(map(res => this.parseProjectInfo(res, properties)));
54
55
    }
56
    /*
57
    get project strtDate and endDate
58
    */
59
    getProjectDates (id: string, properties:EnvProperties):any {
60
61
        let url = properties.searchAPIURLLAst+'projects/'+id+"?format=json";
62
        let key = url+'_projectDates';
63
64
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
65
                    //.map(res => <any> res.json())
66
                    .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
67
                    .pipe(map(res => [res,
68
                              res['fundingtree'],
69
                              res['rels']['rel']]))
70
                    .pipe(map(res => this.parseProjectDates(id,res)))
71
72
    }
73
74
    getHTMLInfo(id: string, properties:EnvProperties): any {
75
        let url = properties.searchAPIURLLAst + 'projects/'+id+"?format=json";
76
        let key = url;
77
78
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
79
                    //.map(res => <any> res.json())
80
                    .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
81
                    .pipe(map(res => this.parseHTMLInfo(res)));
82
    }
83
84
    private handleError (error: HttpErrorResponse) {
85
        console.log(error);
86
        return throwError(error  || 'Server error');
87
    }
88
89
    parseHTMLInfo (data: any):any {
90
        let htmlInfo: {"title": string, "acronym": string, "callIdentifier": string};
91
92
        if(data != null) {
93
            htmlInfo = {"title": data.title, "acronym": data.acronym, "callIdentifier": data.callidentifier};
94
        }
95
        return htmlInfo;
96
    }
97
98
    parseProjectInfo (data: any, properties:EnvProperties):any {
99
        this.projectInfo = new ProjectInfo();
100
        this.projectInfo.funding = {funderName: "", funderShortName: "", code: "", callIdentifier: "",
101
                                    fundingStream: "", budget: "", contribution: "", currency: ""};
102
103
        // ['result']['header']['dri:objIdentifier']
104
        if(data[3] != null) {
105
          this.projectInfo.id = data[3];
106
        }
107
108
      // ['result']['metadata']['oaf:entity']['oaf:project']['fundingtree']
109
      if(data[1] != null) {
110
            let funding: {"funderName": string, "funderShortname": string, "stream": string};
111
            funding = this.parsingFunctions.parseFundingTrees(data[1]);
112
            if(funding.funderName) {
113
              this.projectInfo.funding.funderName = funding.funderName;
114
            }
115
            if(funding.funderShortname) {
116
              this.projectInfo.funding.funderShortName = funding.funderShortname;
117
            }
118
            if(funding.stream) {
119
              this.projectInfo.funding.fundingStream = funding.stream;
120
            }
121
      }
122
123
      // ['result']['metadata']['oaf:entity']['oaf:project']
124
      if(data[0] != null) {
125
        this.projectInfo.acronym = data[0].acronym;
126
        this.projectInfo.title = Array.isArray(data[0]['title']) ? data[0].title[0] : data[0].title;
127
        this.projectInfo.funding.code = data[0].code;
128
        if(data[0].startdate) {
129
          let date: number = Date.parse(data[0].startdate);
130
          this.projectInfo.startDate = (date ? date : null);
131
        }
132
        if(data[0].enddate) {
133
          let date: number = Date.parse(data[0].enddate);
134
          this.projectInfo.endDate = (date ? date : null);
135
        }
136
        if(this.projectInfo.endDate || this.projectInfo.startDate) {
137
          let todayDate = Date.parse(new Date().toString());
138
          this.projectInfo.currentDate = todayDate;
139
          if(this.projectInfo.startDate) {
140
            let startDate = +(this.projectInfo.startDate);
141
            if (todayDate < startDate) {
142
              this.projectInfo.status = "Not started";
143
            }
144
          }
145
          if(this.projectInfo.endDate && !this.projectInfo.status) {
146
            let endDate = +(this.projectInfo.endDate);
147
            if (todayDate <= endDate) {
148
              this.projectInfo.status = "On going";
149
            } else {
150
              this.projectInfo.status = "Closed";
151
            }
152
          }
153
        }
154
        if(this.projectInfo.funding) {
155
          if(this.projectInfo.funding.funderShortName == "EC") {
156
            this.projectInfo.openAccessMandatePublications = data[0].oamandatepublications;
157
            this.projectInfo.specialClause39 = data[0].ecsc39;
158
            this.projectInfo.openAccessMandateDatasets = data[0].ecarticle29_3;
159
            this.projectInfo.funding.callIdentifier = data[0].callidentifier;
160
          }
161
          this.projectInfo.funding.budget = data[0].totalcost;//"10000";
162
          this.projectInfo.funding.contribution = data[0].fundedamount;//"200100";
163
          this.projectInfo.funding.currency = data[0].currency;//"EUR";
164
        }
165
        if(!Array.isArray(data[0]['summary'])) {
166
          this.projectInfo.description = (data[0]['summary']) ? String(data[0]['summary']) : "";
167
        } else {
168
          this.projectInfo.description = (data[0]['summary'][0]) ? String(data[0]['summary'][0]) : "";
169
        }
170
      }
171
172
      // ['result']['metadata']['oaf:entity']['oaf:project']['rels']['rel']
173
      if(data[2] != null) {
174
            this.projectInfo.organizations = [];//new Map<string, string>();
175
176
            if(!Array.isArray(data[2])) {
177
              if(data[2].hasOwnProperty("to") && data[2]['to'].class == "hasParticipant") {
178
                let acronym: string = "";
179
                let name: string = "";
180
                let id: string = "";
181
                if (data[2].hasOwnProperty("legalshortname")) {
182
                  acronym = data[2].legalshortname;
183
                }
184
                if (data[2].hasOwnProperty("legalname")) {
185
                  name = data[2].legalname;
186
                }
187
                if (!acronym && !name) {
188
                  // acronym is displayed with link and name only in tooltip
189
                  acronym = "[no title available]";
190
                }
191
192
                if (data[2].hasOwnProperty("to")) {
193
                  id = data[2]['to'].content;
194
                }
195
196
                this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
197
              }
198
            } else {
199
                for(let i=0; i<data[2].length; i++) {
200
                  let acronym: string = "";
201
                  let name: string = "";
202
                  let id: string = "";
203
                    if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
204
                        if(data[2][i].hasOwnProperty("legalshortname")) {
205
                          acronym = data[2][i].legalshortname;
206
                        }
207
                        if(data[2][i].hasOwnProperty("legalname")) {
208
                          name = data[2][i].legalname;
209
                        }
210
                        if(!acronym && !name){
211
                          acronym = "[no title available]";
212
                        }
213
214
                        if(data[2][i].hasOwnProperty("to")) {
215
                            id = data[2][i]['to'].content;
216
                        }
217
218
                        this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
219
                    }
220
                }
221
            }
222
        }
223
224
        if(this.projectInfo.funding && this.projectInfo.funding.funderShortName == "EC") {
225
            this.projectInfo.url = properties.cordisURL+this.projectInfo.funding.code;
226
            this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
227
        }
228
229
        return this.projectInfo;
230
231
    }
232
233
    parseProjectDates (id: string, data: any):any {
234
        let project = { id: id, startDate: "", endDate: ""};
235
        if(data[0] != null) {
236
            project.startDate = data[0].startdate;
237
            project.endDate = data[0].enddate;
238
        }
239
        return project;
240
241
    }
242
243
}