Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3
import {throwError} from 'rxjs';
4
import {OrganizationInfo} from '../utils/entities/organizationInfo';
5

    
6

    
7

    
8
import{EnvProperties} from '../utils/properties/env-properties';
9
import {map} from "rxjs/operators";
10
import {ParsingFunctions} from "../landingPages/landing-utils/parsingFunctions.class";
11

    
12
@Injectable()
13
export class OrganizationService {
14

    
15
    constructor(private http: HttpClient ) {}
16

    
17
    organizationInfo: OrganizationInfo;
18

    
19
    getOrganizationInfo (id: string, properties:EnvProperties):any {
20
         let url = properties.searchAPIURLLAst+'organizations/'+id+'?format=json';
21
           //'&query=( (oaftype exact organization) and (reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire4.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=*  or reldatasourcecompatibilityid = native)) and ( objIdentifier ='+id+')';
22

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

    
30

    
31
    }
32

    
33
    getOrganizationNameAndUrlById(id: string, properties: EnvProperties): any {
34
      let url = properties.searchAPIURLLAst+"organizations/"+id+"?format=json";
35

    
36
      return this.http.get((properties.useCache) ? (properties.cacheUrl+encodeURIComponent(url)): url)
37
                  //.map(res => <any> res.json())
38
                  .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:organization']))
39
                  .pipe(map(res => this.parseOrganizationNameAndUrl(res)));
40
    }
41

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

    
49
    parseOrganizationInfo (data: any):any {
50
        this.organizationInfo = new OrganizationInfo();
51
        this.organizationInfo.relcanId = ParsingFunctions.parseRelCanonicalId(data,"organization");
52
        let organization;
53
        let relations;
54
        if(data != null) {
55
            organization = data['result']['metadata']['oaf:entity']['oaf:organization'];
56
            this.organizationInfo.objIdentifier = data["result"]["header"]["dri:objIdentifier"];
57
            relations = data['result']['metadata']['oaf:entity']['oaf:organization']['rels']['rel'];
58
        } else {
59
            return null;
60
        }
61

    
62
        if(organization != null) {
63

    
64
            if(organization.hasOwnProperty("websiteurl")) {
65
                this.organizationInfo.title = {"name": organization.legalshortname, "url": organization.websiteurl};
66
            } else {
67
                this.organizationInfo.title = {"name": organization.legalshortname, "url": ''};
68
            }
69

    
70
            this.organizationInfo.name = organization.legalname;
71

    
72
            if(!this.organizationInfo.title.name || this.organizationInfo.title.name == '') {
73
                this.organizationInfo.title.name = this.organizationInfo.name;
74
            }
75

    
76
            if(organization.hasOwnProperty("country")) {
77
                this.organizationInfo.country = organization['country'].classname;
78
            }
79

    
80
            if (organization.hasOwnProperty("children")) {
81
              let children = organization['children'];
82
              if( children.hasOwnProperty("organization")) {
83
                this.organizationInfo.deletedByInferenceIds = [];
84
                let length = Array.isArray(children['organization']) ? children['organization'].length : 1;
85

    
86
                for (let i = 0; i < length; i++) {
87
                  let result = Array.isArray(children['organization']) ? children['organization'][i] : children['organization'];
88
                  this.organizationInfo.deletedByInferenceIds.push(result.objidentifier);
89
                }
90
              }
91
            }
92
        }
93

    
94
//Comment Parsing Projects info
95
/*
96
        if(data[1] != null) {
97

    
98
            let counter;
99
            let length = relations.length!=undefined ? relations.length : 1;
100

    
101
            for(let i=0; i<length; i++) {
102
                let relation = relations.length!=undefined ? relations[i] : relations;
103
                if(relation.hasOwnProperty("to")) {
104
                    if(relation['to'].class == "isParticipant") {
105
                        if(relation.hasOwnProperty("funding")) {
106
                            if(relation['funding'].hasOwnProperty("funder")) {
107
                                if(this.organizationInfo.projects == undefined) {
108
                                    this.organizationInfo.projects = new Map<string,
109
                                        { "name": string, "id": string, "code": string,
110
                                          "acronym": string, "funder": string, "funderId": string,
111
                                          "fundingStream": string, "fundingLevel1": string, "fundingLevel2": string,
112
                                          "sc39": string, "startDate": string, "endDate": string }[]>();
113
                                }
114

    
115
                                if(!this.organizationInfo.projects.has(relation['funding']['funder'].name)) {
116
                                    this.organizationInfo.projects.setValues(relation['funding']['funder'].name,
117
                                        new Array<{ "name": string, "id": string, "code": string,
118
                                                    "acronym": string, "funder": string, "funderId": string,
119
                                                    "fundingStream": string, "fundingLevel1": string, "fundingLevel2": string,
120
                                                    "sc39": string, "startDate": string, "endDate": string }>());
121
                                }
122

    
123
                                counter = this.organizationInfo.projects.get(relation['funding']['funder'].name).length;
124
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter] =
125
                                    { "name": "", "id": "", "code": "",
126
                                      "acronym": "", "funder": "", "funderId": "",
127
                                      "fundingStream": "", "fundingLevel1": "", "fundingLevel2": "",
128
                                      "sc39": "", "startDate": "", "endDate": "" };
129

    
130
                                //let url = "";
131
                                if(relation['to'].content != null && relation['to'].content != "") {
132
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['id'] = relation['to'].content;
133
                                    //url = OpenaireProperties.getsearchLinkToProject()+relation['to'].content;
134
                                }
135
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['name'] = relation.title;
136
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['url'] = url;
137
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['code'] = relation.code;
138
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['acronym'] = relation.acronym;
139

    
140
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['funder'] = relation['funding']['funder'].shortname;
141
                                this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['funderId'] = relation['funding']['funder'].id;
142
                                if(relation['funding'].hasOwnProperty("funding_level_0")) {
143
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['fundingStream'] = relation['funding']['funding_level_0'].name;
144
                                }
145
                                if(relation['funding'].hasOwnProperty("funding_level_1")) {
146
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['fundingLevel1'] = relation['funding']['funding_level_1'].name;
147
                                }
148
                                if(relation['funding'].hasOwnProperty("funding_level_2")) {
149
                                    this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['fundingLevel2'] = relation['funding']['funding_level_2'].name;
150
                                }
151
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['sc39'] =
152
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['startDate'] =
153
                                //this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['endDate'] =
154
                            }
155
                        }
156

    
157
                    }
158

    
159

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

    
162
                        if(this.organizationInfo.dataProviders == undefined) {
163
                            this.organizationInfo.dataProviders = new Array<{ "name": string, "url": string, "type": string, "websiteUrl": string , "organizations": {"name": string, "url": string}[]}>();
164
                        }
165

    
166
                        counter = this.organizationInfo.dataProviders.length;
167
                        this.organizationInfo.dataProviders[counter] = { "name": "", "url": "", "type": "", "websiteUrl": "", "organizations": [] }
168

    
169
                        let url="";
170
                        if(relation['to'].content != null && relation['to'].content != "") {
171
                            url = OpenaireProperties.getsearchLinkToDataProvider()+relation['to'].content;
172
                        }
173
                        this.organizationInfo.dataProviders[counter]['name'] = relation.officialname;
174
                        this.organizationInfo.dataProviders[counter]['url'] = url;
175
                        if(relation.hasOwnProperty("datasourcetype")) {
176
                            this.organizationInfo.dataProviders[counter]['type'] = relation['datasourcetype'].classname;
177
                        }
178
                        this.organizationInfo.dataProviders[counter]['websiteUrl'] = relation.websiteurl;
179
                    }*/
180
/* ----->
181
                }
182
            }
183
        }
184
*/
185
        return this.organizationInfo;
186

    
187
    }
188

    
189
    parseOrganizationNameAndUrl(organization: any): any {
190
      let title: {"name": string, "url": string} = {"name": "", "url": ""};
191

    
192
      if(organization != null) {
193
        if(organization.hasOwnProperty("websiteurl")) {
194
            title = {"name": organization.legalshortname, "url": organization.websiteurl};
195
        } else {
196
            title = {"name": organization.legalshortname, "url": ''};
197
        }
198

    
199
        if(title.name == '') {
200
            title.name = organization.legalname;
201
        }
202
      }
203

    
204
      return title;
205
    }
206

    
207
}
(8-8/23)