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
    getMetrics (id: string):any {
40
        console.info("getProjectsMetrics in service");
41
        //let url = OpenaireProperties. getSearchAPIURLLast() + 'publications/' +id+"?format=json";
42
        let url = OpenaireProperties.getMetricsAPIURL()+"projects/"+id+"/clicks";
43
        let key = url;
44
        if (this._cache.has(key)) {
45
          return Observable.of(this._cache.get(key));
46
        }
47
        return this.http.get(url)
48
                    .map(res => <any> res.json())
49
                    .map(res => res['views'])
50
                    .do(res => {
51
                      this._cache.set(key, res);
52
                    });
53
    }
54

    
55
    /*
56
    get project strtDate and endDate
57
    */
58
    getProjectDates (id: string):any {
59

    
60
        let url = OpenaireProperties. getSearchAPIURLLast()+'projects/'+id+"?format=json";
61
        let key = url+'_projectDates';
62
        if (this._cache.has(key)) {
63
          return Observable.of(this._cache.get(key)).map(res => this.parseProjectDates(id,res))
64
        }
65
        return this.http.get(url)
66
                    .map(res => <any> res.json())
67
                    .map(res => [res['result']['metadata']['oaf:entity']['oaf:project'],
68
                                 res['result']['metadata']['oaf:entity']['oaf:project']['fundingtree'],
69
                                 res['result']['metadata']['oaf:entity']['oaf:project']['rels']['rel']])
70
                                 .do(res => {
71
                                   this._cache.set(key, res);
72
                                 })
73
                    .map(res => this.parseProjectDates(id,res))
74

    
75
    }
76
    private handleError (error: Response) {
77
    // in a real world app, we may send the error to some remote logging infrastructure
78
    // instead of just logging it to the console
79
        console.log(error);
80
        return Observable.throw(error  || 'Server error');
81
    }
82

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

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

    
101
            let funding;
102
            this.projectInfo.funding = "";
103

    
104
            if(data[1]['funding_level_2'] != null) {
105
                funding = data[1]['funding_level_2'].id;
106
            } else if(data[1]['funding_level_1'] != null) {
107
                funding = data[1]['funding_level_1'].id;
108
            } else if(data[1]['funding_level_0'] != null) {
109
                funding = data[1]['funding_level_0'].id;
110
            }
111

    
112
            if(funding != undefined) {
113
                funding = funding.split("::");
114
                for(let i=1; i<funding.length; i++) {
115
                    if(this.projectInfo.funding != "") {
116
                        this.projectInfo.funding += " | ";
117
                    }
118
                    this.projectInfo.funding += funding[i];
119
                }
120
            }
121
        }
122

    
123
        if(data[2] != null) {
124
            this.projectInfo.organizations = new Map<string, string>();
125

    
126
            let name = "";
127
            let url = "";
128

    
129
            if(!Array.isArray(data[2])) {
130
                if(data[2].hasOwnProperty("legalshortname")) {
131
                    name = data[2].legalshortname;
132
                } else if(data[2].hasOwnProperty("legalname")) {
133
                    name = data[2].legalname;
134
                }
135

    
136
                if(data[2].hasOwnProperty("to") && name != "") {
137
                    url = OpenaireProperties.getsearchLinkToOrganization()+data[2]['to'].content;
138
                }
139
                if(name != "") {
140
                    this.projectInfo.organizations.set(name, url);
141
                }
142
            } else {
143
                for(let i=0; i<data[2].length; i++) {
144
                    if(data[2][i].hasOwnProperty("legalshortname")) {
145
                        name = data[2][i].legalshortname;
146
                    } else if(data[2][i].hasOwnProperty("legalname")) {
147
                        name = data[2][i].legalname;
148
                    }
149

    
150
                    if(data[2][i].hasOwnProperty("to") && name!="") {
151
                        url = OpenaireProperties.getsearchLinkToOrganization()+data[2][i]['to'].content;
152
                    }
153

    
154
                    if(name != "") {
155
                        this.projectInfo.organizations.set(name, url);
156
                    }
157
                }
158
            }
159
        }
160

    
161
        if(this.projectInfo.funder == "EC") {
162
            this.projectInfo.url = OpenaireProperties.getCordisURL()+this.projectInfo.contractNum;
163
            this.projectInfo.urlInfo = "Detailed project information (CORDIS)";
164
        }
165

    
166
        return this.projectInfo;
167

    
168
    }
169

    
170
    parseProjectDates (id: string, data: any):any {
171
        var project = { id: id, startDate: "", endDate: ""};
172
        if(data[0] != null) {
173
            project.startDate = data[0].startdate;
174
            project.endDate = data[0].enddate;
175
        }
176
        return project;
177

    
178
    }
179

    
180
}
(11-11/23)