Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3
import {Observable}     from 'rxjs/Observable';
4
import 'rxjs/add/observable/of';
5
import 'rxjs/add/operator/do';
6
import 'rxjs/add/operator/share';
7
import {SearchResult}     from '../utils/entities/searchResult';
8
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
9
import{EnvProperties} from '../utils/properties/env-properties';
10
import {StringUtils} from '../utils/string-utils.class';
11

    
12
@Injectable()
13
export class SearchOrganizationsService {
14

    
15
    constructor(private http: Http ) {}
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
                    .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 ):any {
58
      let url = properties.searchResourcesAPIURL;
59
      var basicQuery = "oaftype exact organization and "
60
      +"(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
61
                "reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or " +
62
                "reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)";
63

    
64
      url += "?query=";
65
      if(params!= null && params != ''  ) {
66
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
67
      }else{
68
        url +=" ( "+basicQuery+ " ) ";
69
      }
70

    
71
      url += "&page="+(page-1)+"&size="+size;
72
      url += "&format=json";
73

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

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

    
82
        let length = Array.isArray(data) ? data.length : 1;
83

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

    
87
            var result: SearchResult = new SearchResult();
88

    
89
            result['title'] = {"name": '', "accessMode": '', "sc39": ''};
90

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

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

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

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

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

    
116
                            let countProjects = result['projects'].length;
117

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

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

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

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

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

    
150
                        }
151
                    }
152
                }
153
            }
154

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

    
159
            results.push(result);
160
        }
161

    
162
        return results;
163
    }
164

    
165
    numOfOrganizations(url: string, properties:EnvProperties ): any {
166

    
167
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
168
                  .map(res => <any> res.json())
169
                  .map(res => res.total);
170
    }
171

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

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

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