Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {HttpClient} from "@angular/common/http";
4
import {Observable}     from 'rxjs';
5

    
6

    
7

    
8
import {SearchResult}     from '../utils/entities/searchResult';
9
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
10
import{EnvProperties} from '../utils/properties/env-properties';
11
import {StringUtils} from '../utils/string-utils.class';
12
import {map} from "rxjs/operators";
13

    
14
@Injectable()
15
export class SearchOrganizationsService {
16

    
17
    constructor(private http: HttpClient ) {}
18

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

    
22
        let length = Array.isArray(data) ? data.length : 1;
23

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

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

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

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

    
42

    
43
        let link = properties.searchAPIURLLAst+"organizations";
44

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

    
54

    
55
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
56
                    //.map(res => <any> res.json())
57
                    .pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]));
58
    }
59
    advancedSearchOrganizations (params: string, page: number, size: number, properties:EnvProperties ):any {
60
      let url = properties.searchResourcesAPIURL;
61
      var basicQuery = "oaftype exact organization and "
62
      +"(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
63
                "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 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

    
73
      url += "&page="+(page-1)+"&size="+size;
74
      url += "&format=json";
75

    
76
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
77
      //.map(res => <any> res.json())
78

    
79
      .pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
80
    }
81
    parseResults(data: any): SearchResult[] {
82
        let results: SearchResult[] = [];
83

    
84
        let length = Array.isArray(data) ? data.length : 1;
85

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

    
89
            var result: SearchResult = new SearchResult();
90

    
91
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
92

    
93
            result['title'].name = resData.legalshortname;
94
            if(result['title'].name == '') {
95
                result['title'].name = resData.legalname;
96
            }
97

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

    
102
            if(resData['rels'].hasOwnProperty("rel")) {
103
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
104

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

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

    
118
                            let countProjects = result['projects'].length;
119

    
120
                            result['projects'][countProjects] = {
121
                                "id": "", "acronym": "", "title": "",
122
                                "funderShortname": "", "funderName": "",
123
                                "code": ""
124
                            }
125

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

    
139
                            if(relation.hasOwnProperty("funding")) {
140
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
141

    
142
                                for(let z=0; z<fundingLength; z++) {
143
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
144

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

    
152
                        }
153
                    }
154
                }
155
            }
156

    
157
            if(resData.country.hasOwnProperty("classname")) {
158
                result.country = resData.country.classname;
159
            }
160

    
161
            results.push(result);
162
        }
163

    
164
        return results;
165
    }
166

    
167
    numOfOrganizations(url: string, properties:EnvProperties ): any {
168

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

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

    
181
        let url = properties.searchAPIURLLAst+parameters+"?format=json";
182
        return this.numOfOrganizations(url, properties);
183
    }
184

    
185
    numOfSearchOrganizations(params: string, properties:EnvProperties ):any {
186
        let url = properties.searchAPIURLLAst+"organizations/count?format=json";
187
        if(params != "") {
188
          url += "&q=" + StringUtils.URIEncode(params);
189
        }
190
        return this.numOfOrganizations(url, properties);
191
    }
192
}
(18-18/23)