Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5
import {ProjectInfo} from '../../utils/entities/projectInfo';
6

    
7

    
8

    
9
import{EnvProperties} from '../../utils/properties/env-properties';
10

    
11
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
12
import {map} from "rxjs/operators";
13

    
14
@Injectable()
15
export class ProjectService {
16

    
17
    constructor(private http: HttpClient ) {
18
      this.parsingFunctions = new ParsingFunctions();
19
    }
20

    
21
    public parsingFunctions: ParsingFunctions;
22
    projectInfo: ProjectInfo;
23

    
24
    getProjectInfo (id: string, properties:EnvProperties):any {
25

    
26
        let url = properties.searchAPIURLLAst + 'projects/'+id+"?format=json";
27
        let key = url;
28

    
29
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
30
                    //.map(res => <any> res.json())
31
                    .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
32
                    .pipe(map(res => [res,
33
                                 res['fundingtree'],
34
                                 res['rels']['rel']]))
35
                    .pipe(map(res => this.parseProjectInfo(res, properties)));
36

    
37
    }
38

    
39
    getProjectInfoByGrantId (grant: string, funder:string, properties:EnvProperties):any {
40

    
41
      let url =properties.searchAPIURLLAst+ 'resources?query=(oaftype exact project)'+
42
      'and (projectcode_nt exact "'+grant+'" ) and (fundershortname exact '+'"'+funder+ '"'+')'+"&format=json&size=1";
43

    
44

    
45
         let key = url;
46

    
47
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
48
                    //.map(res => <any> res.json())
49
                    .pipe(map(res => res['results'][0]))
50
                    .pipe(map(res => [res['result']['metadata']['oaf:entity']['oaf:project'],res['result']['header']['dri:objIdentifier']]))
51
                    .pipe(map(res => [res[0],
52
                                 res[0]['fundingtree'],
53
                                 res[0]['rels']['rel'],res[1]]))
54
                    .pipe(map(res => this.parseProjectInfo(res, properties)));
55

    
56
    }
57
    /*
58
    get project strtDate and endDate
59
    */
60
    getProjectDates (id: string, properties:EnvProperties):any {
61

    
62
        let url = properties.searchAPIURLLAst+'projects/'+id+"?format=json";
63
        let key = url+'_projectDates';
64

    
65
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
66
                    //.map(res => <any> res.json())
67
                    .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
68
                    .pipe(map(res => [res,
69
                              res['fundingtree'],
70
                              res['rels']['rel']]))
71
                    .pipe(map(res => this.parseProjectDates(id,res)))
72

    
73
    }
74

    
75
    getHTMLInfo(id: string, properties:EnvProperties): any {
76
        let url = properties.searchAPIURLLAst + 'projects/'+id+"?format=json";
77
        let key = url;
78

    
79
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
80
                    //.map(res => <any> res.json())
81
                    .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:project']))
82
                    .pipe(map(res => this.parseHTMLInfo(res)));
83
    }
84

    
85
    private handleError (error: HttpErrorResponse) {
86
        console.log(error);
87
        return throwError(error  || 'Server error');
88
    }
89

    
90
    parseHTMLInfo (data: any):any {
91
        let htmlInfo: {"title": string, "acronym": string, "callIdentifier": string};
92

    
93
        if(data != null) {
94
            htmlInfo = {"title": data.title, "acronym": data.acronym, "callIdentifier": data.callidentifier};
95
        }
96
        return htmlInfo;
97
    }
98

    
99
    parseProjectInfo (data: any, properties:EnvProperties):any {
100
        this.projectInfo = new ProjectInfo();
101
        this.projectInfo.funding = {funderName: "", funderShortName: "", code: "", callIdentifier: "",
102
                                    fundingStream: "", budget: "", contribution: "", currency: ""};
103

    
104
        // ['result']['header']['dri:objIdentifier']
105
        if(data[3] != null) {
106
          this.projectInfo.id = data[3];
107
        }
108

    
109
      // ['result']['metadata']['oaf:entity']['oaf:project']['fundingtree']
110
      if(data[1] != null) {
111
            let funding: {"funderName": string, "funderShortname": string, "stream": string};
112
            funding = this.parsingFunctions.parseFundingTrees(data[1]);
113
            if(funding.funderName) {
114
              this.projectInfo.funding.funderName = funding.funderName;
115
            }
116
            if(funding.funderShortname) {
117
              this.projectInfo.funding.funderShortName = funding.funderShortname;
118
            }
119
            if(funding.stream) {
120
              this.projectInfo.funding.fundingStream = funding.stream;
121
            }
122
      }
123

    
124
      // ['result']['metadata']['oaf:entity']['oaf:project']
125
      if(data[0] != null) {
126
        this.projectInfo.acronym = data[0].acronym;
127
        this.projectInfo.title = Array.isArray(data[0]['title']) ? data[0].title[0] : data[0].title;
128
        this.projectInfo.funding.code = data[0].code;
129
        if(data[0].startdate) {
130
          let date: number = Date.parse(data[0].startdate);
131
          this.projectInfo.startDate = (date ? date : null);
132
        }
133
        if(data[0].enddate) {
134
          let date: number = Date.parse(data[0].enddate);
135
          this.projectInfo.endDate = (date ? date : null);
136
        }
137
        if(this.projectInfo.endDate || this.projectInfo.startDate) {
138
          let todayDate = Date.parse(new Date().toString());
139
          this.projectInfo.currentDate = todayDate;
140
          if(this.projectInfo.startDate) {
141
            let startDate = +(this.projectInfo.startDate);
142
            if (todayDate < startDate) {
143
              this.projectInfo.status = "Not started";
144
            }
145
          }
146
          if(this.projectInfo.endDate && !this.projectInfo.status) {
147
            let endDate = +(this.projectInfo.endDate);
148
            if (todayDate <= endDate) {
149
              this.projectInfo.status = "On going";
150
            } else {
151
              this.projectInfo.status = "Closed";
152
            }
153
          }
154
        }
155
        if(this.projectInfo.funding) {
156
          if(this.projectInfo.funding.funderShortName == "EC") {
157
            this.projectInfo.openAccessMandatePublications = data[0].oamandatepublications;
158
            this.projectInfo.specialClause39 = data[0].ecsc39;
159
            this.projectInfo.openAccessMandateDatasets = data[0].ecarticle29_3;
160
            this.projectInfo.funding.callIdentifier = data[0].callidentifier;
161
          }
162
          this.projectInfo.funding.budget = data[0].totalcost;//"10000";
163
          this.projectInfo.funding.contribution = data[0].fundedamount;//"200100";
164
          this.projectInfo.funding.currency = data[0].currency;//"EUR";
165
        }
166
        if(!Array.isArray(data[0]['summary'])) {
167
          this.projectInfo.description = (data[0]['summary']) ? String(data[0]['summary']) : "";
168
        } else {
169
          this.projectInfo.description = (data[0]['summary'][0]) ? String(data[0]['summary'][0]) : "";
170
        }
171
      }
172

    
173
      // ['result']['metadata']['oaf:entity']['oaf:project']['rels']['rel']
174
      if(data[2] != null) {
175
            this.projectInfo.organizations = [];//new Map<string, string>();
176

    
177
            if(!Array.isArray(data[2])) {
178
              if(data[2].hasOwnProperty("to") && data[2]['to'].class == "hasParticipant") {
179
                let acronym: string = "";
180
                let name: string = "";
181
                let id: string = "";
182
                if (data[2].hasOwnProperty("legalshortname")) {
183
                  acronym = data[2].legalshortname;
184
                }
185
                if (data[2].hasOwnProperty("legalname")) {
186
                  name = data[2].legalname;
187
                }
188
                if (!acronym && !name) {
189
                  // acronym is displayed with link and name only in tooltip
190
                  acronym = "[no title available]";
191
                }
192

    
193
                if (data[2].hasOwnProperty("to")) {
194
                  id = data[2]['to'].content;
195
                }
196

    
197
                this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
198
              }
199
            } else {
200
                for(let i=0; i<data[2].length; i++) {
201
                  let acronym: string = "";
202
                  let name: string = "";
203
                  let id: string = "";
204
                    if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
205
                        if(data[2][i].hasOwnProperty("legalshortname")) {
206
                          acronym = data[2][i].legalshortname;
207
                        }
208
                        if(data[2][i].hasOwnProperty("legalname")) {
209
                          name = data[2][i].legalname;
210
                        }
211
                        if(!acronym && !name){
212
                          acronym = "[no title available]";
213
                        }
214

    
215
                        if(data[2][i].hasOwnProperty("to")) {
216
                            id = data[2][i]['to'].content;
217
                        }
218

    
219
                        this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
220
                    }
221
                }
222
            }
223
        }
224

    
225
        if(this.projectInfo.funding && this.projectInfo.funding.funderShortName == "EC") {
226
            this.projectInfo.url = properties.cordisURL+this.projectInfo.funding.code;
227
            this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
228
        }
229

    
230
        return this.projectInfo;
231

    
232
    }
233

    
234
    parseProjectDates (id: string, data: any):any {
235
        let project = { id: id, startDate: "", endDate: ""};
236
        if(data[0] != null) {
237
            project.startDate = data[0].startdate;
238
            project.endDate = data[0].enddate;
239
        }
240
        return project;
241

    
242
    }
243

    
244
}
(4-4/5)