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, refineParams:string=null,  refineFields:string[] =null, refineQuery:string = null   ):any {
60
    // &type=organizations
61
      let url = properties.searchAPIURLLAst+"resources2/?format=json";
62
      var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
63
                "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.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
        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 == '') {
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

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

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

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

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

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

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

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

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

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

    
156
                        }
157
                    }
158
                }
159
            }
160

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

    
165
            results.push(result);
166
        }
167

    
168
        return results;
169
    }
170

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

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

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

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

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