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 'rxjs/add/observable/of';
6
import 'rxjs/add/operator/do';
7
import 'rxjs/add/operator/share';
8
import{EnvProperties} from '../utils/properties/env-properties';
9

    
10
@Injectable()
11
export class OrganizationService {
12

    
13
    constructor(private http: Http ) {}
14

    
15
    organizationInfo: OrganizationInfo;
16

    
17
    getOrganizationInfo (id: string, properties:EnvProperties):any {
18
        console.info("getOrganizationInfo in service");
19
         let url = properties.searchAPIURLLAst+'resources?format=json&query=( (oaftype exact organization) and (reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=*  or reldatasourcecompatibilityid = native)) and ( objIdentifier ='+id+')';
20
        //let url = "http://beta.services.openaire.eu:8480/search/rest/v2/api/resources?format=json&query=(%20(oaftype%20exact%20organization)%20and%20(reldatasourcecompatibilityid=driver%20or%20reldatasourcecompatibilityid=driver-openaire2.0%20or%20reldatasourcecompatibilityid=openaire2.0%20or%20reldatasourcecompatibilityid=openaire3.0%20or%20reldatasourcecompatibilityid=openaire2.0_data%20or%20reldatasourcecompatibilityid=hostedBy%20or%20relprojectid=*))%20and%20(%20objIdentifier%20=dedup_wf_001%3A%3Af1e63493def7cedfa5b497cdbea26faa)";
21

    
22

    
23
        return this.http.get((properties.useCache)? (properties.cacheUrl +encodeURIComponent(url)): url)
24
                    .map(res => <any> res.json())
25
                    .map(res => res['results'][0])
26
                    //.map(res => res[0]['result']['metadata']['oaf:entity']['oaf:organization'])
27
                    //.map(res => [res, res['rels']['rel']])
28
                    .map(res => this.parseOrganizationInfo(res));
29

    
30

    
31
    }
32

    
33
    private handleError (error: Response) {
34
    // in a real world app, we may send the error to some remote logging infrastructure
35
    // instead of just logging it to the console
36
        console.log(error);
37
        return Observable.throw(error  || 'Server error');
38
    }
39

    
40
    parseOrganizationInfo (data: any):any {
41
        console.info("parseOrganizationInfo");
42
        this.organizationInfo = new OrganizationInfo();
43

    
44
        let organization;
45
        let relations;
46

    
47
        if(data != null) {
48
            organization = data['result']['metadata']['oaf:entity']['oaf:organization'];
49
            relations = data['result']['metadata']['oaf:entity']['oaf:organization']['rels']['rel'];
50
        } else {
51
            return null;
52
        }
53

    
54
        if(organization != null) {
55

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

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

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

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

    
73
//Comment Parsing Projects info
74
/*
75
        if(data[1] != null) {
76

    
77
            let counter;
78
            let length = relations.length!=undefined ? relations.length : 1;
79

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

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

    
102
                                counter = this.organizationInfo.projects.get(relation['funding']['funder'].name).length;
103
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter] =
104
                                    { "name": "", "id": "", "code": "",
105
                                      "acronym": "", "funder": "", "funderId": "",
106
                                      "fundingStream": "", "fundingLevel1": "", "fundingLevel2": "",
107
                                      "sc39": "", "startDate": "", "endDate": "" };
108

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

    
119
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['funder'] = relation['funding']['funder'].shortname;
120
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['funderId'] = relation['funding']['funder'].id;
121
                                if(relation['funding'].hasOwnProperty("funding_level_0")) {
122
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['fundingStream'] = relation['funding']['funding_level_0'].name;
123
                                }
124
                                if(relation['funding'].hasOwnProperty("funding_level_1")) {
125
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['fundingLevel1'] = relation['funding']['funding_level_1'].name;
126
                                }
127
                                if(relation['funding'].hasOwnProperty("funding_level_2")) {
128
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['fundingLevel2'] = relation['funding']['funding_level_2'].name;
129
                                }
130
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['sc39'] =
131
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['startDate'] =
132
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['endDate'] =
133
                            }
134
                        }
135

    
136
                    }
137

    
138

    
139
                    } /*else if(relation['to'].class == "isProvidedBy") {
140

    
141
                        if(this.organizationInfo.dataProviders == undefined) {
142
                            this.organizationInfo.dataProviders = new Array<{ "name": string, "url": string, "type": string, "websiteUrl": string , "organizations": {"name": string, "url": string}[]}>();
143
                        }
144

    
145
                        counter = this.organizationInfo.dataProviders.length;
146
                        this.organizationInfo.dataProviders[counter] = { "name": "", "url": "", "type": "", "websiteUrl": "", "organizations": [] }
147

    
148
                        let url="";
149
                        if(relation['to'].content != null && relation['to'].content != "") {
150
                            url = OpenaireProperties.getsearchLinkToDataProvider()+relation['to'].content;
151
                        }
152
                        this.organizationInfo.dataProviders[counter]['name'] = relation.officialname;
153
                        this.organizationInfo.dataProviders[counter]['url'] = url;
154
                        if(relation.hasOwnProperty("datasourcetype")) {
155
                            this.organizationInfo.dataProviders[counter]['type'] = relation['datasourcetype'].classname;
156
                        }
157
                        this.organizationInfo.dataProviders[counter]['websiteUrl'] = relation.websiteurl;
158
                    }*/
159
/* ----->
160
                }
161
            }
162
        }
163
*/
164
        return this.organizationInfo;
165

    
166
    }
167

    
168
}
(4-4/21)