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
import { CacheService  } from '../../shared/cache.service';
10
@Injectable()
11
export class ProjectService {
12

    
13
    constructor(private http: Http, public _cache: CacheService) {}
14

    
15
    projectInfo: ProjectInfo;
16

    
17
    getProjectInfo (id: string):any {
18
        console.info("getProjectInfo in service");
19

    
20
        let url = OpenaireProperties. getSearchAPIURLLast() + 'projects/'+id+"?format=json";
21
        let key = url;
22
        if (this._cache.has(key)) {
23
          return Observable.of(this._cache.get(key))
24
                  .map(res => this.parseProjectInfo(res));
25
        }
26
        return this.http.get(url)
27
                    .map(res => <any> res.json())
28
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
29
                    .map(res => [res,
30
                                 res['fundingtree'],
31
                                 res['rels']['rel']])
32
                    .do(res => {
33
                       this._cache.set(key, res);
34
                    })
35
                    .map(res => this.parseProjectInfo(res));
36

    
37
    }
38

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

    
44
        let url = OpenaireProperties. getSearchAPIURLLast()+'projects/'+id+"?format=json";
45
        let key = url+'_projectDates';
46
        if (this._cache.has(key)) {
47
          return Observable.of(this._cache.get(key))
48
                              .map(res => [res,
49
                                           res['fundingtree'],
50
                                           res['rels']['rel']])
51
                              .map(res => this.parseProjectDates(id,res))
52
        }
53
        return this.http.get(url)
54
                    .map(res => <any> res.json())
55
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
56
                                 .do(res => {
57
                                   this._cache.set(key, res);
58
                                 })
59
                    .map(res => [res,
60
                              res['fundingtree'],
61
                              res['rels']['rel']])
62
                    .map(res => this.parseProjectDates(id,res))
63

    
64
    }
65

    
66
    getHTMLInfo(id: string): any {
67
        console.info("getHTMLInfo in service");
68

    
69
        let url = OpenaireProperties. getSearchAPIURLLast() + 'projects/'+id+"?format=json";
70
        let key = url;
71
        if (this._cache.has(key)) {
72
          return Observable.of(this._cache.get(key))
73
                  .map(res => this.parseHTMLInfo(res));
74
        }
75
        return this.http.get(url)
76
                    .map(res => <any> res.json())
77
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:project'])
78
                    .do(res => {
79
                       this._cache.set(key, res);
80
                    })
81
                    .map(res => this.parseHTMLInfo(res));
82
    }
83

    
84
    private handleError (error: Response) {
85
    // in a real world app, we may send the error to some remote logging infrastructure
86
    // instead of just logging it to the console
87
        console.log(error);
88
        return Observable.throw(error  || 'Server error');
89
    }
90

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

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

    
101
    parseProjectInfo (data: any):any {
102
        this.projectInfo = new ProjectInfo();
103

    
104
        if(data[0] != null) {
105
            this.projectInfo.acronym = data[0].acronym;
106
            if(Array.isArray(data[0]['title'])) {
107
                this.projectInfo.title = data[0].title[0];
108
            } else {
109
                this.projectInfo.title = data[0].title;
110
            }
111
            this.projectInfo.callIdentifier = data[0].callidentifier;
112
            this.projectInfo.contractNum = data[0].code;
113
            this.projectInfo.startDate = data[0].startdate;
114
            this.projectInfo.endDate = data[0].enddate;
115
            this.projectInfo.openAccessMandate = data[0].oamandatepublications;
116
            this.projectInfo.specialClause39 = data[0].ecsc39;
117
        }
118
        if(data[1] != null) {
119
            if(data[1]['funder'] != null) {
120
                this.projectInfo.funder = data[1]['funder'].shortname;
121
            }
122

    
123
            let funding;
124
            this.projectInfo.funding = "";
125

    
126
            if(data[1]['funding_level_2'] != null) {
127
                funding = data[1]['funding_level_2'].id;
128
            } else if(data[1]['funding_level_1'] != null) {
129
                funding = data[1]['funding_level_1'].id;
130
            } else if(data[1]['funding_level_0'] != null) {
131
                funding = data[1]['funding_level_0'].id;
132
            }
133

    
134
            if(funding != undefined) {
135
                funding = funding.split("::");
136
                for(let i=1; i<funding.length; i++) {
137
                    if(this.projectInfo.funding != "") {
138
                        this.projectInfo.funding += " | ";
139
                    }
140
                    this.projectInfo.funding += funding[i];
141
                }
142
            }
143
        }
144

    
145
        if(data[2] != null) {
146
            this.projectInfo.organizations = [];//new Map<string, string>();
147

    
148
            let name = "";
149
            let id = "";
150

    
151
            if(!Array.isArray(data[2])) {
152
                if(data[2].hasOwnProperty("legalshortname")) {
153
                    name = data[2].legalshortname;
154
                } else if(data[2].hasOwnProperty("legalname")) {
155
                    name = data[2].legalname;
156
                }
157

    
158
                if(data[2].hasOwnProperty("to") && name != "") {
159
                    id = /*OpenaireProperties.getsearchLinkToOrganization()+*/data[2]['to'].content;
160
                }
161
                if(name != "") {
162
                    this.projectInfo.organizations.push({"name": name, "id": id});
163
                }
164
            } else {
165
                for(let i=0; i<data[2].length; i++) {
166
                    if(data[2][i].hasOwnProperty("to") && data[2][i]['to'].class == "hasParticipant") {
167
                        if(data[2][i].hasOwnProperty("legalshortname")) {
168
                            name = data[2][i].legalshortname;
169
                        } else if(data[2][i].hasOwnProperty("legalname")) {
170
                            name = data[2][i].legalname;
171
                        }
172

    
173
                        if(data[2][i].hasOwnProperty("to") && name!="") {
174
                            id = /*OpenaireProperties.getsearchLinkToOrganization()+*/data[2][i]['to'].content;
175
                        }
176

    
177
                        if(name != "") {
178
                            this.projectInfo.organizations.push({"name": name, "id": id});
179
                        }
180
                    }
181
                }
182
            }
183
        }
184

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

    
190
        return this.projectInfo;
191

    
192
    }
193

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

    
202
    }
203

    
204
}
(5-5/6)