Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import {OrganizationInfo} from '../utils/entities/organizationInfo';
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 OrganizationService {
12

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

    
15
    organizationInfo: OrganizationInfo;
16

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

    
20
        let url = OpenaireProperties. getSearchAPIURLLast()+'organizations/'+id+"?format=json";
21
        let key = url;
22
        // if (this._cache.has(key)) {
23
        //   console.log("HEre in req after put in cache "+this._cache.has(key));
24
        //   console.log("HEre in req get from cache " + this._cache.get(key).title.name);
25
        //   return Observable.of(this._cache.get(key));
26
        // }
27

    
28
        return this.http.get(url)
29
                    .map(res => <any> res.json())
30
                    .map(res => res['result']['metadata']['oaf:entity']['oaf:organization'])
31
                    .map(res => [res, res['rels']['rel']])
32
                    .map(res => this.parseOrganizationInfo(res))
33
                    .do(res => {
34
                      this._cache.set(key, res);
35
                    });
36

    
37

    
38
    }
39

    
40
    private handleError (error: Response) {
41
    // in a real world app, we may send the error to some remote logging infrastructure
42
    // instead of just logging it to the console
43
        console.error(error);
44
        return Observable.throw(error  || 'Server error');
45
    }
46

    
47
    parseOrganizationInfo (data: any):any {
48
        console.info("parseOrganizationInfo");
49
        this.organizationInfo = new OrganizationInfo();
50

    
51
        let mydata;
52

    
53
        if(data[0] != null) {
54
            mydata = data[0];
55

    
56
            if(mydata.hasOwnProperty("websiteurl")) {
57
                this.organizationInfo.title = {"name": mydata.legalshortname, "url": mydata.websiteurl};
58
            } else {
59
                this.organizationInfo.title = {"name": mydata.legalshortname, "url": ''};
60
            }
61

    
62
            this.organizationInfo.name = mydata.legalname;
63

    
64
            if(this.organizationInfo.title.name == '') {
65
                this.organizationInfo.title.name = this.organizationInfo.name;
66
            }
67

    
68
            if(mydata.hasOwnProperty("country")) {
69
                this.organizationInfo.country = mydata['country'].classname;
70
            }
71
        }
72

    
73
        if(data[1] != null) {
74
            let counter;
75
            let length = data[1].length!=undefined ? data[1].length : 1;
76

    
77
            for(let i=0; i<length; i++) {
78
                mydata = length > 1 ? data[1][i] : data[1];
79
                if(mydata.hasOwnProperty("to")) {
80
                    if(mydata['to'].class == "isParticipant") {
81
                        if(mydata.hasOwnProperty("funding")) {
82
                            if(mydata['funding'].hasOwnProperty("funder")) {
83
                                if(this.organizationInfo.projects == undefined) {
84
                                    this.organizationInfo.projects = new Map<string,
85
                                        { "name": string, "id": string, "url": string, "code": string,
86
                                          "acronym": string, "funder": string, "fundingStream": string,
87
                                          "fundingLevel1": string, "fundingLevel2": string,
88
                                          "sc39": string, "startDate": string, "endDate": string }[]>();
89
                                }
90

    
91
                                if(!this.organizationInfo.projects.has(mydata['funding']['funder'].name)) {
92
                                    this.organizationInfo.projects.set(mydata['funding']['funder'].name,
93
                                        new Array<{ "name": string, "id": string, "url": string, "code": string,
94
                                                    "acronym": string, "funder": string, "fundingStream": string,
95
                                                    "fundingLevel1": string, "fundingLevel2": string,
96
                                                    "sc39": string, "startDate": string, "endDate": string }>());
97
                                }
98

    
99
                                counter = this.organizationInfo.projects.get(mydata['funding']['funder'].name).length;
100
                                this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter] =
101
                                    { "name": "", "id": "", "url": "", "code": "",
102
                                      "acronym": "", "funder": "", "fundingStream": "",
103
                                      "fundingLevel1": "", "fundingLevel2": "",
104
                                      "sc39": "", "startDate": "", "endDate": "" };
105

    
106
                                let url = "";
107
                                if(mydata['to'].content != null && mydata['to'].content != "") {
108
                                    this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['id'] = mydata['to'].content;
109
                                    url = OpenaireProperties.getsearchLinkToProject()+mydata['to'].content;
110
                                }
111
                                this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['name'] = mydata.title;
112
                                this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['url'] = url;
113
                                this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['code'] = mydata.code;
114
                                this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['acronym'] = mydata.acronym;
115

    
116
                                this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['funder'] = mydata['funding']['funder'].shortname;
117
                                if(mydata['funding'].hasOwnProperty("funding_level_0")) {
118
                                    this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['fundingStream'] = mydata['funding']['funding_level_0'].name;
119
                                }
120
                                if(mydata['funding'].hasOwnProperty("funding_level_1")) {
121
                                    this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['fundingLevel1'] = mydata['funding']['funding_level_1'].name;
122
                                }
123
                                if(mydata['funding'].hasOwnProperty("funding_level_2")) {
124
                                    this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['fundingLevel2'] = mydata['funding']['funding_level_2'].name;
125
                                }
126
                                //this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['sc39'] =
127
                                //this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['startDate'] =
128
                                //this.organizationInfo.projects.get(mydata['funding']['funder'].name)[counter]['endDate'] =
129
                            }
130
                        }
131
                    } /*else if(mydata['to'].class == "isProvidedBy") {
132
                        if(this.organizationInfo.dataProviders == undefined) {
133
                            this.organizationInfo.dataProviders = new Array<{ "name": string, "url": string, "type": string, "websiteUrl": string , "organizations": {"name": string, "url": string}[]}>();
134
                        }
135

    
136
                        counter = this.organizationInfo.dataProviders.length;
137
                        this.organizationInfo.dataProviders[counter] = { "name": "", "url": "", "type": "", "websiteUrl": "", "organizations": [] }
138

    
139
                        let url="";
140
                        if(mydata['to'].content != null && mydata['to'].content != "") {
141
                            url = OpenaireProperties.getsearchLinkToDataProvider()+mydata['to'].content;
142
                        }
143
                        this.organizationInfo.dataProviders[counter]['name'] = mydata.officialname;
144
                        this.organizationInfo.dataProviders[counter]['url'] = url;
145
                        if(mydata.hasOwnProperty("datasourcetype")) {
146
                            this.organizationInfo.dataProviders[counter]['type'] = mydata['datasourcetype'].classname;
147
                        }
148
                        this.organizationInfo.dataProviders[counter]['websiteUrl'] = mydata.websiteurl;
149
                    }*/
150
                }
151
            }
152
        }
153

    
154
        return this.organizationInfo;
155

    
156
    }
157

    
158
}
(8-8/22)