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
        if(data[3] != null) {
102
          this.projectInfo.id = data[3];
103
        }
104
        if(data[0] != null) {
105
            this.projectInfo.acronym = data[0].acronym;
106
            this.projectInfo.title = Array.isArray(data[0]['title']) ? data[0].title[0] : data[0].title;
107
            this.projectInfo.callIdentifier = data[0].callidentifier;
108
            this.projectInfo.contractNum = data[0].code;
109
            this.projectInfo.startDate = data[0].startdate;
110
            this.projectInfo.endDate = data[0].enddate;
111
            this.projectInfo.openAccessMandatePublications = data[0].oamandatepublications;
112
            this.projectInfo.specialClause39 = data[0].ecsc39;
113
            this.projectInfo.openAccessMandateDatasets = data[0].ecarticle29_3;
114
        }
115
        if(data[1] != null) {
116
            let funding: {"funderName": string, "funderShortname": string, "stream": string};
117
            funding = this.parsingFunctions.parseFundingTrees(data[1]);
118
            if(funding.funderShortname) {
119
              this.projectInfo.funder = funding.funderShortname;
120
            }
121
            if(funding.stream) {
122
              this.projectInfo['funding'] = funding.stream;
123
            }
124
        }
125

    
126
        if(data[2] != null) {
127
            this.projectInfo.organizations = [];//new Map<string, string>();
128

    
129
            let acronym: string = "";
130
            let name: string = "";
131
            let id: string = "";
132

    
133
            if(!Array.isArray(data[2])) {
134
                if(data[2].hasOwnProperty("legalshortname")) {
135
                  acronym = data[2].legalshortname;
136
                }
137
                if(data[2].hasOwnProperty("legalname")) {
138
                  name = data[2].legalname;
139
                }
140
                if(!acronym && !name){
141
                  // acronym is displayed with link and name only in tooltip
142
                  acronym = "[no title available]";
143
                }
144

    
145
                if(data[2].hasOwnProperty("to")) {
146
                    id = data[2]['to'].content;
147
                }
148

    
149
                this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
150
            } else {
151
                for(let i=0; i<data[2].length; i++) {
152
                    if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
153
                        if(data[2][i].hasOwnProperty("legalshortname")) {
154
                          acronym = data[2][i].legalshortname;
155
                        }
156
                        if(data[2][i].hasOwnProperty("legalname")) {
157
                          name = data[2][i].legalname;
158
                        }
159
                        if(!acronym && !name){
160
                          acronym = "[no title available]";
161
                        }
162

    
163
                        if(data[2][i].hasOwnProperty("to")) {
164
                            id = data[2][i]['to'].content;
165
                        }
166

    
167
                        this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
168
                    }
169
                }
170
            }
171
        }
172

    
173
        if(this.projectInfo.funder == "EC") {
174
            this.projectInfo.url = properties.cordisURL+this.projectInfo.contractNum;
175
            this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
176
        }
177

    
178
        return this.projectInfo;
179

    
180
    }
181

    
182
    parseProjectDates (id: string, data: any):any {
183
        let project = { id: id, startDate: "", endDate: ""};
184
        if(data[0] != null) {
185
            project.startDate = data[0].startdate;
186
            project.endDate = data[0].enddate;
187
        }
188
        return project;
189

    
190
    }
191

    
192
}
(4-4/5)