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: "", fundingStream: ""};
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 && this.projectInfo.funding.funderShortName == "EC") {
155
          this.projectInfo.openAccessMandatePublications = data[0].oamandatepublications;
156
          this.projectInfo.specialClause39 = data[0].ecsc39;
157
          this.projectInfo.openAccessMandateDatasets = data[0].ecarticle29_3;
158
        }
159
        this.projectInfo.description = data[0]['summary'];
160
      }
161

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

    
166
            let acronym: string = "";
167
            let name: string = "";
168
            let id: string = "";
169

    
170
            if(!Array.isArray(data[2])) {
171
                if(data[2].hasOwnProperty("legalshortname")) {
172
                  acronym = data[2].legalshortname;
173
                }
174
                if(data[2].hasOwnProperty("legalname")) {
175
                  name = data[2].legalname;
176
                }
177
                if(!acronym && !name){
178
                  // acronym is displayed with link and name only in tooltip
179
                  acronym = "[no title available]";
180
                }
181

    
182
                if(data[2].hasOwnProperty("to")) {
183
                    id = data[2]['to'].content;
184
                }
185

    
186
                this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
187
            } else {
188
                for(let i=0; i<data[2].length; i++) {
189
                    if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
190
                        if(data[2][i].hasOwnProperty("legalshortname")) {
191
                          acronym = data[2][i].legalshortname;
192
                        }
193
                        if(data[2][i].hasOwnProperty("legalname")) {
194
                          name = data[2][i].legalname;
195
                        }
196
                        if(!acronym && !name){
197
                          acronym = "[no title available]";
198
                        }
199

    
200
                        if(data[2][i].hasOwnProperty("to")) {
201
                            id = data[2][i]['to'].content;
202
                        }
203

    
204
                        this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
205
                    }
206
                }
207
            }
208
        }
209

    
210
        if(this.projectInfo.funding && this.projectInfo.funding.funderShortName == "EC") {
211
            this.projectInfo.url = properties.cordisURL+this.projectInfo.funding.code;
212
            this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
213
        }
214

    
215
        return this.projectInfo;
216

    
217
    }
218

    
219
    parseProjectDates (id: string, data: any):any {
220
        let project = { id: id, startDate: "", endDate: ""};
221
        if(data[0] != null) {
222
            project.startDate = data[0].startdate;
223
            project.endDate = data[0].enddate;
224
        }
225
        return project;
226

    
227
    }
228

    
229
}
(4-4/5)