Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {ProjectInfo} from '../../utils/entities/projectInfo';
5
import {OpenaireProperties} from '../../utils/properties/openaireProperties';
6
import 'rxjs/add/observable/of';
7
import 'rxjs/add/operator/do';
8
import 'rxjs/add/operator/share';
9

    
10
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
11

    
12
@Injectable()
13
export class ProjectService {
14

    
15
    constructor(private http: Http ) {
16
      this.parsingFunctions = new ParsingFunctions();
17
    }
18

    
19
    public parsingFunctions: ParsingFunctions;
20
    projectInfo: ProjectInfo;
21

    
22
    getProjectInfo (id: string):any {
23
        console.info("getProjectInfo in service");
24

    
25
        let url = OpenaireProperties. getSearchAPIURLLast() + 'projects/'+id+"?format=json";
26
        let key = url;
27

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

    
36
    }
37

    
38
    /*
39
    get project strtDate and endDate
40
    */
41
    getProjectDates (id: string):any {
42

    
43
        let url = OpenaireProperties. getSearchAPIURLLast()+'projects/'+id+"?format=json";
44
        let key = url+'_projectDates';
45

    
46
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
47
                    .map(res => <any> res.json())
48
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
49
                    .map(res => [res,
50
                              res['fundingtree'],
51
                              res['rels']['rel']])
52
                    .map(res => this.parseProjectDates(id,res))
53

    
54
    }
55

    
56
    getHTMLInfo(id: string): any {
57
        let url = OpenaireProperties. getSearchAPIURLLast() + 'projects/'+id+"?format=json";
58
        let key = url;
59

    
60
        return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
61
                    .map(res => <any> res.json())
62
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
63
                    .map(res => this.parseHTMLInfo(res));
64
    }
65

    
66
    private handleError (error: Response) {
67
    // in a real world app, we may send the error to some remote logging infrastructure
68
    // instead of just logging it to the console
69
        console.log(error);
70
        return Observable.throw(error  || 'Server error');
71
    }
72

    
73
    parseHTMLInfo (data: any):any {
74
        let htmlInfo: {"title": string, "acronym": string, "callIdentifier": string};
75

    
76
        if(data != null) {
77
            htmlInfo = {"title": data.title, "acronym": data.acronym, "callIdentifier": data.callidentifier};
78
        }
79
        return htmlInfo;
80
    }
81

    
82
    parseProjectInfo (data: any):any {
83
        this.projectInfo = new ProjectInfo();
84

    
85
        if(data[0] != null) {
86
            this.projectInfo.acronym = data[0].acronym;
87
            if(Array.isArray(data[0]['title'])) {
88
                this.projectInfo.title = data[0].title[0];
89
            } else {
90
                this.projectInfo.title = data[0].title;
91
            }
92
            this.projectInfo.callIdentifier = data[0].callidentifier;
93
            this.projectInfo.contractNum = data[0].code;
94
            this.projectInfo.startDate = data[0].startdate;
95
            this.projectInfo.endDate = data[0].enddate;
96
            this.projectInfo.openAccessMandate = data[0].oamandatepublications;
97
            this.projectInfo.specialClause39 = data[0].ecsc39;
98
        }
99
        if(data[1] != null) {
100
          /*
101
            if(data[1]['funder'] != null) {
102
                this.projectInfo.funder = data[1]['funder'].shortname;
103
            }
104

    
105
            let funding;
106
            this.projectInfo.funding = "";
107

    
108
            if(data[1]['funding_level_2'] != null) {
109
                funding = data[1]['funding_level_2'].id;
110
            } else if(data[1]['funding_level_1'] != null) {
111
                funding = data[1]['funding_level_1'].id;
112
            } else if(data[1]['funding_level_0'] != null) {
113
                funding = data[1]['funding_level_0'].id;
114
            }
115

    
116
            if(funding != undefined) {
117
                funding = funding.split("::");
118
                for(let i=1; i<funding.length; i++) {
119
                    if(this.projectInfo.funding != "") {
120
                        this.projectInfo.funding += " | ";
121
                    }
122
                    this.projectInfo.funding += funding[i];
123
                }
124
            }
125
            */
126

    
127
            let funding: {"funderName": string, "funderShortname": string, "stream": string};
128
            funding = this.parsingFunctions.parseFundingTrees(data[1]);
129

    
130
            // if(funding.funderName) {
131
            //   this.publicationInfo.fundedByProjects[counter]['funderName'] = funding.funderName;
132
            // }
133
            if(funding.funderShortname) {
134
              this.projectInfo.funder = funding.funderShortname;
135
            }
136
            if(funding.stream) {
137
              this.projectInfo['funding'] = funding.stream;
138
            }
139
        }
140

    
141
        if(data[2] != null) {
142
            this.projectInfo.organizations = [];//new Map<string, string>();
143

    
144
            let acronym: string = "";
145
            let name: string = "";
146
            let id: string = "";
147

    
148
            if(!Array.isArray(data[2])) {
149
                if(data[2].hasOwnProperty("legalshortname")) {
150
                  acronym = data[2].legalshortname;
151
                }
152
                if(data[2].hasOwnProperty("legalname")) {
153
                  name = data[2].legalname;
154
                }
155
                if(!acronym && !name){
156
                  // acronym is displayed with link and name only in tooltip
157
                  acronym = "[no title available]";
158
                }
159

    
160
                if(data[2].hasOwnProperty("to")) {
161
                    id = data[2]['to'].content;
162
                }
163

    
164
                this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
165
            } else {
166
                for(let i=0; i<data[2].length; i++) {
167
                    if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
168
                        if(data[2][i].hasOwnProperty("legalshortname")) {
169
                          acronym = data[2][i].legalshortname;
170
                        }
171
                        if(data[2][i].hasOwnProperty("legalname")) {
172
                          name = data[2][i].legalname;
173
                        }
174
                        if(!acronym && !name){
175
                          acronym = "[no title available]";
176
                        }
177

    
178
                        if(data[2][i].hasOwnProperty("to")) {
179
                            id = data[2][i]['to'].content;
180
                        }
181

    
182
                        this.projectInfo.organizations.push({"acronym": acronym, "name": name, "id": id});
183
                    }
184
                }
185
            }
186
        }
187

    
188
        if(this.projectInfo.funder == "EC") {
189
            this.projectInfo.url = OpenaireProperties.getCordisURL()+this.projectInfo.contractNum;
190
            this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
191
        }
192

    
193
        return this.projectInfo;
194

    
195
    }
196

    
197
    parseProjectDates (id: string, data: any):any {
198
        var project = { id: id, startDate: "", endDate: ""};
199
        if(data[0] != null) {
200
            project.startDate = data[0].startdate;
201
            project.endDate = data[0].enddate;
202
        }
203
        return project;
204

    
205
    }
206

    
207
}
(5-5/6)