Project

General

Profile

1 61381 k.triantaf
import {Injectable} from '@angular/core';
2
import {HttpClient} from "@angular/common/http";
3
4
5
6
import {SearchResult}     from '../utils/entities/searchResult';
7
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
8
import{EnvProperties} from '../utils/properties/env-properties';
9
import {StringUtils} from '../utils/string-utils.class';
10
import {map} from "rxjs/operators";
11
import {ParsingFunctions} from "../landingPages/landing-utils/parsingFunctions.class";
12
13
@Injectable()
14
export class SearchOrganizationsService {
15
16
    constructor(private http: HttpClient ) {}
17
18
    parseResultsForDeposit(data: any): {"name": string, "id": string}[] {
19
        let results: {"name": string, "id": string}[] = [];
20
21
        let length = Array.isArray(data) ? data.length : 1;
22
23
        for(let i=0; i<length; i++) {
24
            let name: string = '';
25
            let id: string = '';
26
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
27
            name = resData.legalname;
28
            if(name == '') {
29
                name = resData.legalshortname;
30
            }
31
32
            id = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
33
34
            results.push({"name": name, "id": id});
35
        }
36
        return results;
37
    }
38
39
    searchOrganizations (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties):any {
40
41
42
        let link = properties.searchAPIURLLAst+"organizations";
43
44
        let url = link+"?";
45
        if(params!= null && params != ''  ) {
46
            url += params;
47
        }
48
        if(refineParams!= null && refineParams != ''  ) {
49
            url += refineParams;
50
        }
51
        url += "&page="+(page-1)+"&size="+size + "&format=json";
52
53
54
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
55
                    //.map(res => <any> res.json())
56
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]));
57
    }
58
    advancedSearchOrganizations (params: string, page: number, size: number, properties:EnvProperties, refineParams:string=null,  refineFields:string[] =null, refineQuery:string = null   ):any {
59
    // &type=organizations
60
      let url = properties.searchAPIURLLAst+"resources2/?format=json";
61
      var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
62
                "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or " +
63
                "reldatasourcecompatibilityid exact openaire-cris_1.1 or " +
64
                "reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)";
65
66
      url += "&query=";
67
      if(params!= null && params != ''  ) {
68
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
69
      }else{
70
        url +=" ( "+basicQuery+ " ) ";
71
      }
72
        if(refineParams!= null && refineParams != ''  ) {
73
            url += refineParams;
74
        }
75
        if(refineQuery) {
76
            url += "&" + refineQuery;
77
        }
78
      url += "&page="+(page-1)+"&size="+size;
79
80
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
81
      //.map(res => <any> res.json())
82
83
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]));
84
    }
85
    parseResults(data: any): SearchResult[] {
86
        let results: SearchResult[] = [];
87
88
        let length = Array.isArray(data) ? data.length : 1;
89
90
        for(let i=0; i<length; i++) {
91
             let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
92
93
            var result: SearchResult = new SearchResult();
94
95
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
96
97
            result['title'].name = resData.legalshortname;
98
            if(!result['title'].name || result['title'].name == '') {
99
                result['title'].name = resData.legalname;
100
            }
101
102
            //result['title'].url = OpenaireProperties.getsearchLinkToOrganization();
103
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
104
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
105
            let canId = ParsingFunctions.parseRelCanonicalId(Array.isArray(data) ? data[i] : data, "organization");
106
            if(canId){
107
                result['id'] = canId;
108
            }
109
            if(resData['rels'].hasOwnProperty("rel")) {
110
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
111
112
                for(let j=0; j<relLength; j++) {
113
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
114
115
                    if(relation.hasOwnProperty("to")) {
116
                        if(relation['to'].class == "isParticipant") {
117
                            if(result['projects'] == undefined) {
118
                                result['projects'] = new Array<
119
                                    { "id": string, "acronym": string, "title": string,
120
                                      "funderShortname": string, "funderName": string,
121
                                      "code": string
122
                                    }>();
123
                            }
124
125
                            let countProjects = result['projects'].length;
126
127
                            result['projects'][countProjects] = {
128
                                "id": "", "acronym": "", "title": "",
129
                                "funderShortname": "", "funderName": "",
130
                                "code": ""
131
                            }
132
133
                            if(relation.title != 'unidentified') {
134
                                result['projects'][countProjects]['id'] =
135
                                    /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
136
                                result['projects'][countProjects]['acronym'] = relation.acronym;
137
                                result['projects'][countProjects]['title'] = relation.title;
138
                                result['projects'][countProjects]['code'] = relation.code;
139
                            } else {
140
                                result['projects'][countProjects]['id'] = "";
141
                                result['projects'][countProjects]['acronym'] = "";
142
                                result['projects'][countProjects]['title'] = "";
143
                                result['projects'][countProjects]['code'] = "";
144
                            }
145
146
                            if(relation.hasOwnProperty("funding")) {
147
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
148
149
                                for(let z=0; z<fundingLength; z++) {
150
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
151
152
                                    if(fundingData.hasOwnProperty("funder")) {
153
                                        result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
154
                                        result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
155
                                    }
156
                                }
157
                            }
158
159
                        }
160
                    }
161
                }
162
            }
163
164
            if(resData.country.hasOwnProperty("classname")) {
165
                result.country = resData.country.classname;
166
            }
167
168
            results.push(result);
169
        }
170
171
        return results;
172
    }
173
174
    numOfOrganizations(url: string, properties:EnvProperties ): any {
175
176
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
177
                  //.map(res => <any> res.json())
178
                  .pipe(map(res => res['total']));
179
    }
180
181
    numOfEntityOrganizations(id: string, entity: string, properties:EnvProperties ):any {
182
        // currently not used - if used fix entity comparison below
183
        var parameters: string = "";
184
        if(entity == "organization") {
185
          parameters = "organizations/"+id+"/organizations/count";
186
        }
187
188
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
189
        return this.numOfOrganizations(url, properties);
190
    }
191
192
    numOfSearchOrganizations(params: string, properties:EnvProperties, refineParams:string=null ):any {
193
        let url = properties.searchAPIURLLAst+"organizations/count?format=json";
194
        if(params != "") {
195
          url += "&q=" + StringUtils.URIEncode(params);
196
        }
197
        if(refineParams!= null && refineParams != ''  ) {
198
            url += refineParams;
199
        }
200
        return this.numOfOrganizations(url, properties);
201
    }
202
203
    numOfSearchOrganizations2(params: string, properties:EnvProperties, refineParams:string=null ):any {
204
        let url = properties.searchAPIURLLAst+"resources2/?format=json&size=0&type=organizations";
205
        var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
206
          "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or " +
207
          "reldatasourcecompatibilityid exact openaire-cris_1.1 or " +
208
          "reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)";
209
210
        url += "&query=";
211
        if(params!= null && params != ''  ) {
212
            url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
213
        }else{
214
            url +=" ( "+basicQuery+ " ) ";
215
        }
216
        if(refineParams!= null && refineParams != ''  ) {
217
            url += refineParams;
218
        }
219
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
220
          .pipe(map(res => res['meta']['total']));
221
    }
222
}