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 { CacheService  } from '../shared/cache.service';
8
import {OpenaireProperties} from '../utils/properties/openaireProperties';
9
import {SearchResult}     from '../utils/entities/searchResult';
10
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
11

    
12
@Injectable()
13
export class SearchOrganizationsService {
14

    
15
    constructor(private http: Http, public _cache: CacheService) {}
16

    
17
    searchOrganizationsForDeposit (params: string, collectedFrom: string, page: number, size: number):any {
18

    
19
        console.info("In searchOrganizationsforDeposit");
20

    
21
        let link = OpenaireProperties.getSearchResourcesAPIURL();
22

    
23
        let url = link+"?query=";
24
        if(params!= null && params != ''  ) {
25
          url += "((oaftype exact organization and deletedbyinference=false and "+
26
            "(reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=*))"+
27
            " and ((organizationlegalname all "+this.quote(params)+") or (organizationlegalshortname all "+this.quote(params)+")) " +
28
            // "and " + this.quote(params) + " " +
29
            "and (collectedfromdatasourcename exact "+collectedFrom+")) "
30

    
31
        }
32

    
33
        url += "&page="+(page-1)+"&size="+size;
34
        url += "&format=json";
35

    
36
        let key = url;
37

    
38
        if (this._cache.has(key)) {
39
          return Observable.of(this._cache.get(key));
40
        }
41

    
42
        return this.http.get(url)
43
                    .map(res => <any> res.json())
44
                    .do(res => console.info(res))
45
                    .map(res => this.parseResultsForDeposit(res['results']))
46
                    .do(res => {
47
                      this._cache.set(key, res);
48
                    });
49
                    //.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields)]);
50
    }
51

    
52
    quote(params: string):string {
53
        return encodeURIComponent('"'+params+'"');
54
        //return '"'+params+'"';
55
    }
56

    
57
    parseResultsForDeposit(data: any): {"name": string, "id": string}[] {
58
        let results: {"name": string, "id": string}[] = [];
59

    
60
        let length = Array.isArray(data) ? data.length : 1;
61

    
62
        for(let i=0; i<length; i++) {
63
            let name: string = '';
64
            let id: string = '';
65
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:organization'] : data['result']['metadata']['oaf:entity']['oaf:organization'];
66
            name = resData.legalname;
67
            if(name == '') {
68
                name = resData.legalshortname;
69
            }
70

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

    
73
            results.push({"name": name, "id": id});
74
        }
75
        console.info(results);
76
        return results;
77
    }
78

    
79
    searchOrganizations (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
80

    
81
        console.info("In searchOrganizations");
82

    
83
        let link = OpenaireProperties.getSearchAPIURL()+"organizations";
84

    
85
        let url = link+"?";
86
        if(params!= null && params != ''  ) {
87
            url += params;
88
        }
89
        if(refineParams!= null && refineParams != ''  ) {
90
            url += refineParams;
91
        }
92
        url += "&page="+page+"&size="+size;
93

    
94
        let key = url;
95
        if (this._cache.has(key)) {
96
          return Observable.of(this._cache.get(key));
97
        }
98
        return this.http.get(url)
99
                    .map(res => <any> res.json())
100
                    //.do(res => console.info(res))
101
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields)])
102
                    .do(res => {
103
                      this._cache.set(key, res);
104
                    });
105
    }
106
    advancedSearchOrganizations (params: string, page: number, size: number ):any {
107
      let url = OpenaireProperties.getSearchResourcesAPIURL();
108
      var basicQuery = "(oaftype exact organization) "
109
      url += "?query=";
110
      if(params!= null && params != ''  ) {
111
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
112
      }else{
113
        url +=" ( "+basicQuery+ " ) ";
114
      }
115

    
116
      url += "&page="+(page-1)+"&size="+size;
117
      url += "&format=json";
118
      let key = url;
119
      if (this._cache.has(key)) {
120
        return Observable.of(this._cache.get(key));
121
      }
122
      return this.http.get(url)
123
      .map(res => <any> res.json())
124
      //.do(res => console.info(res))
125
      .map(res => [res['meta'].total, this.parseResults(res['results'])])
126
      .do(res => {
127
        this._cache.set(key, res);
128
      });
129
    }
130
    parseResults(data: any): SearchResult[] {
131
        let results: SearchResult[] = [];
132

    
133
        let length = Array.isArray(data) ? data.length : 1;
134

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

    
138
            var result: SearchResult = new SearchResult();
139

    
140
            result['title'] = {"name": '', "url": '', "accessMode": ''};
141

    
142
            result['title'].name = resData.legalshortname;
143
            if(result['title'].name == '') {
144
                result['title'].name = resData.legalname;
145
            }
146

    
147
            result['title'].url = OpenaireProperties.getsearchLinkToOrganization();
148
            result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
149

    
150
            if(resData['rels'].hasOwnProperty("rel")) {
151
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
152

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

    
156
                    if(relation.hasOwnProperty("to")) {
157
                        if(relation['to'].class == "isParticipant") {
158
                            if(result['projects'] == undefined) {
159
                                result['projects'] = new Array<
160
                                    { "url": string, "acronym": string, "title": string,
161
                                      "funderShortname": string, "funderName": string,
162
                                      "code": string
163
                                    }>();
164
                            }
165

    
166
                            let countProjects = result['projects'].length;
167

    
168
                            result['projects'][countProjects] = {
169
                                "url": "", "acronym": "", "title": "",
170
                                "funderShortname": "", "funderName": "",
171
                                "code": ""
172
                            }
173

    
174
                            result['projects'][countProjects]['url'] =
175
                                OpenaireProperties.getsearchLinkToProject() + relation['to'].content;
176
                            result['projects'][countProjects]['acronym'] = relation.acronym;
177
                            result['projects'][countProjects]['title'] = relation.title;
178
                            result['projects'][countProjects]['code'] = relation.code;
179

    
180
                            if(relation.hasOwnProperty("funding")) {
181
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
182

    
183
                                for(let z=0; z<fundingLength; z++) {
184
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
185

    
186
                                    if(fundingData.hasOwnProperty("funder")) {
187
                                        result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
188
                                        result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
189
                                    }
190
                                }
191
                            }
192

    
193
                        }
194
                    }
195
                }
196
            }
197

    
198
            if(resData.country.hasOwnProperty("classname")) {
199
                result.country = resData.country.classname;
200
            }
201

    
202
            results.push(result);
203
        }
204

    
205
        return results;
206
    }
207

    
208
    numOfEntityOrganizations(id: string, entity: string):any {
209

    
210
        //OpenaireProperties.getSearchAPIURL()
211
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
212
        let url = OpenaireProperties.getSearchAPIURL()+entity+id+"/organizations/count"
213
        let key = url;
214
        if (this._cache.has(key)) {
215
          return Observable.of(this._cache.get(key));
216
        }
217
        return this.http.get(url)
218
                    .map(res => <any> res.json())
219
                    .map(res => res.total)
220
                    .do(res => {
221
                      this._cache.set(key, res);
222
                    });
223
    }
224

    
225
    numOfSearchOrganizations(params: string):any {
226

    
227
        //OpenaireProperties.getSearchAPIURL()
228
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
229
        let url = OpenaireProperties.getSearchAPIURL()+"organizations/count?q="+params;
230
        let key = url;
231
        if (this._cache.has(key)) {
232
          return Observable.of(this._cache.get(key));
233
        }
234
        return this.http.get(url)
235
                    .map(res => <any> res.json())
236
                    .map(res => res.total)
237
                    .do(res => {
238
                      this._cache.set(key, res);
239
                    });
240
    }
241
}
(18-18/22)