Project

General

Profile

1
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

    
12
@Injectable()
13
export class SearchOrganizationsService {
14

    
15
    constructor(private http: HttpClient ) {}
16

    
17
    parseResultsForDeposit(data: any): {"name": string, "id": string}[] {
18
        let results: {"name": string, "id": string}[] = [];
19

    
20
        let length = Array.isArray(data) ? data.length : 1;
21

    
22
        for(let i=0; i<length; i++) {
23
            let name: string = '';
24
            let id: string = '';
25
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
26
            name = resData.legalname;
27
            if(name == '') {
28
                name = resData.legalshortname;
29
            }
30

    
31
            id = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
32

    
33
            results.push({"name": name, "id": id});
34
        }
35
        return results;
36
    }
37

    
38
    searchOrganizations (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties):any {
39

    
40

    
41
        let link = properties.searchAPIURLLAst+"organizations";
42

    
43
        let url = link+"?";
44
        if(params!= null && params != ''  ) {
45
            url += params;
46
        }
47
        if(refineParams!= null && refineParams != ''  ) {
48
            url += refineParams;
49
        }
50
        url += "&page="+(page-1)+"&size="+size + "&format=json";
51

    
52

    
53
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
54
                    //.map(res => <any> res.json())
55
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]));
56
    }
57
    advancedSearchOrganizations (params: string, page: number, size: number, properties:EnvProperties, refineParams:string=null,  refineFields:string[] =null, refineQuery:string = null   ):any {
58
    // &type=organizations
59
      let url = properties.searchAPIURLLAst+"resources2/?format=json";
60
      var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
61
                "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or " +
62
                "reldatasourcecompatibilityid exact openaire-cris_1.1 or " +
63
                "reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)";
64

    
65
      url += "&query=";
66
      if(params!= null && params != ''  ) {
67
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
68
      }else{
69
        url +=" ( "+basicQuery+ " ) ";
70
      }
71
        if(refineParams!= null && refineParams != ''  ) {
72
            url += refineParams;
73
        }
74
        if(refineQuery) {
75
            url += "&" + refineQuery;
76
        }
77
      url += "&page="+(page-1)+"&size="+size;
78

    
79
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
80
      //.map(res => <any> res.json())
81

    
82
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]));
83
    }
84
    parseResults(data: any): SearchResult[] {
85
        let results: SearchResult[] = [];
86

    
87
        let length = Array.isArray(data) ? data.length : 1;
88

    
89
        for(let i=0; i<length; i++) {
90
             let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
91

    
92
            var result: SearchResult = new SearchResult();
93

    
94
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
95

    
96
            result['title'].name = resData.legalshortname;
97
            if(!result['title'].name || result['title'].name == '') {
98
                result['title'].name = resData.legalname;
99
            }
100

    
101
            //result['title'].url = OpenaireProperties.getsearchLinkToOrganization();
102
            //result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
103
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
104

    
105
            if(resData['rels'].hasOwnProperty("rel")) {
106
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
107

    
108
                for(let j=0; j<relLength; j++) {
109
                    let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
110

    
111
                    if(relation.hasOwnProperty("to")) {
112
                        if(relation['to'].class == "isParticipant") {
113
                            if(result['projects'] == undefined) {
114
                                result['projects'] = new Array<
115
                                    { "id": string, "acronym": string, "title": string,
116
                                      "funderShortname": string, "funderName": string,
117
                                      "code": string
118
                                    }>();
119
                            }
120

    
121
                            let countProjects = result['projects'].length;
122

    
123
                            result['projects'][countProjects] = {
124
                                "id": "", "acronym": "", "title": "",
125
                                "funderShortname": "", "funderName": "",
126
                                "code": ""
127
                            }
128

    
129
                            if(relation.title != 'unidentified') {
130
                                result['projects'][countProjects]['id'] =
131
                                    /*OpenaireProperties.getsearchLinkToProject() + */relation['to'].content;
132
                                result['projects'][countProjects]['acronym'] = relation.acronym;
133
                                result['projects'][countProjects]['title'] = relation.title;
134
                                result['projects'][countProjects]['code'] = relation.code;
135
                            } else {
136
                                result['projects'][countProjects]['id'] = "";
137
                                result['projects'][countProjects]['acronym'] = "";
138
                                result['projects'][countProjects]['title'] = "";
139
                                result['projects'][countProjects]['code'] = "";
140
                            }
141

    
142
                            if(relation.hasOwnProperty("funding")) {
143
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
144

    
145
                                for(let z=0; z<fundingLength; z++) {
146
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
147

    
148
                                    if(fundingData.hasOwnProperty("funder")) {
149
                                        result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
150
                                        result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
151
                                    }
152
                                }
153
                            }
154

    
155
                        }
156
                    }
157
                }
158
            }
159

    
160
            if(resData.country.hasOwnProperty("classname")) {
161
                result.country = resData.country.classname;
162
            }
163

    
164
            results.push(result);
165
        }
166

    
167
        return results;
168
    }
169

    
170
    numOfOrganizations(url: string, properties:EnvProperties ): any {
171

    
172
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
173
                  //.map(res => <any> res.json())
174
                  .pipe(map(res => res['total']));
175
    }
176

    
177
    numOfEntityOrganizations(id: string, entity: string, properties:EnvProperties ):any {
178
        // currently not used - if used fix entity comparison below
179
        var parameters: string = "";
180
        if(entity == "organization") {
181
          parameters = "organizations/"+id+"/organizations/count";
182
        }
183

    
184
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
185
        return this.numOfOrganizations(url, properties);
186
    }
187

    
188
    numOfSearchOrganizations(params: string, properties:EnvProperties, refineParams:string=null ):any {
189
        let url = properties.searchAPIURLLAst+"organizations/count?format=json";
190
        if(params != "") {
191
          url += "&q=" + StringUtils.URIEncode(params);
192
        }
193
        if(refineParams!= null && refineParams != ''  ) {
194
            url += refineParams;
195
        }
196
        return this.numOfOrganizations(url, properties);
197
    }
198

    
199
    numOfSearchOrganizations2(params: string, properties:EnvProperties, refineParams:string=null ):any {
200
        let url = properties.searchAPIURLLAst+"resources2/?format=json&size=0&type=organizations";
201
        var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
202
          "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or " +
203
          "reldatasourcecompatibilityid exact openaire-cris_1.1 or " +
204
          "reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)";
205

    
206
        url += "&query=";
207
        if(params!= null && params != ''  ) {
208
            url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
209
        }else{
210
            url +=" ( "+basicQuery+ " ) ";
211
        }
212
        if(refineParams!= null && refineParams != ''  ) {
213
            url += refineParams;
214
        }
215
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
216
          .pipe(map(res => res['meta']['total']));
217
    }
218
}
(17-17/23)