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

    
9
import {OpenaireProperties} from '../utils/properties/openaireProperties';
10
import {SearchResult}     from '../utils/entities/searchResult';
11
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
12

    
13
@Injectable()
14
export class SearchPublicationsService {
15
    private sizeOfDescription: number = 497;
16

    
17
    constructor(private http: Http, public _cache: CacheService) {}
18

    
19
    searchPublications (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
20

    
21
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
22

    
23
        let url = link+"?"+"&format=json&";
24
        if(params!= null && params != ''  ) {
25
            url += params;
26
        }
27
        if(refineParams!= null && refineParams != ''  ) {
28
            url += refineParams;
29
        }
30
        url += "&page="+(page-1)+"&size="+size;
31

    
32
        let key = url;
33
        if (this._cache.has(key)) {
34
          return Observable.of(this._cache.get(key));
35
        }
36

    
37
        return this.http.get(url)
38
                    .map(res => <any> res.json())
39
                    //.do(res => console.info(res))
40
                    .map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields)])
41
                    .do(res => {
42
                      this._cache.set(key, res);
43
                    });
44
    }
45

    
46
    advancedSearchPublications (params: string, page: number, size: number ):any {
47
      let url = OpenaireProperties.getSearchResourcesAPIURL();
48
      var basicQuery = "(oaftype exact result) and (resulttypeid exact publication) ";
49
      url += "?query=";
50
      if(params!= null && params != ''  ) {
51
        url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
52
      }else{
53
        url +=" ( "+basicQuery+ " ) ";
54
      }
55

    
56
      url += "&page="+(page-1)+"&size="+size;
57
      url += "&format=json";
58
      let key = url;
59
      if (this._cache.has(key)) {
60
        return Observable.of(this._cache.get(key));
61
      }
62
      return this.http.get(url)
63
      .map(res => <any> res.json())
64
      //.do(res => console.info(res))
65
      .map(res => [res['meta'].total, this.parseResults(res['results'])])
66
      .do(res => {
67
        this._cache.set(key, res);
68
      });
69
    }
70
    searchPublicationsForEntity (params: string, page: number, size: number):any {
71
        let link = OpenaireProperties.getSearchAPIURLLast();
72
        let url = link+params+"/publications"+ "?format=json";
73

    
74
        let key = url;
75
        if (this._cache.has(key)) {
76
          return Observable.of(this._cache.get(key));
77
        }
78

    
79
        return this.http.get(url)
80
                    .map(res => <any> res.json())
81
                    .map(res => [res['meta'].total, this.parseResults(res['results'])])
82
                    .do(res => {
83
                      this._cache.set(key, res);
84
                    });
85
    }
86

    
87
    searchPublicationsForDataproviders(params: string, page: number, size: number):any {
88
        let link = OpenaireProperties.getSearchAPIURLLast();
89
        let url = link+params+ "&format=json";
90
        let key = url;
91
        if (this._cache.has(key)) {
92
          return Observable.of(this._cache.get(key));
93
        }
94
        return this.http.get(url)
95
                    .map(res => <any> res.json())
96
                    .map(res => [res['meta'].total, this.parseResults(res['results'])])
97
                    .do(res => {
98
                      this._cache.set(key, res);
99
                    });
100
    }
101

    
102
	searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
103

    
104
        let link = OpenaireProperties.getSearchAPIURLLast()+"publications";
105

    
106
        let url = link+"?";
107
        if(params!= null && params != ''  ) {
108
            url += params;
109
        }
110
        if(refineParams!= null && refineParams != ''  ) {
111
            url += refineParams;
112
        }
113
        url += "&page="+(page-1)+"&size="+size+ "&format=json";
114

    
115
        let key = url;
116
        if (this._cache.has(key)) {
117
          return Observable.of(this._cache.get(key));
118
        }
119

    
120
        return this.http.get(url)
121
                    .map(res => <any> res.json())
122
                    //.do(res => console.info(res))
123
                    .map(res => this.parseResultsCSV(res['results']))
124
                    .do(res => {
125
                      this._cache.set(key, res);
126
                    });
127
    }
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:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
137

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

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

    
142
            if(Array.isArray(resData['title'])) {
143
                // resData['title'][0].hasOwnProperty("content") {
144
                    result['title'].name = resData['title'][0].content;
145
                // }
146
            } else {
147
                // resData['title'].hasOwnProperty("content") {
148
                    result['title'].name = resData['title'].content;
149
                // }
150
            }
151

    
152
            result['title'].url = OpenaireProperties.getsearchLinkToPublication();
153
            result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
154
            result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
155
            if(resData['bestlicense'].hasOwnProperty("classid")) {
156
                result['title'].accessMode = resData['bestlicense'].classid;
157
            }
158

    
159
            if(resData['rels'].hasOwnProperty("rel")) {
160
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
161

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

    
165
                    if(relation.hasOwnProperty("to")) {
166
                        if(relation['to'].class == "hasAuthor") {
167
                            if(result['authors'] == undefined) {
168
                                result['authors'] = new Array<{"name": string, "url": string}>();
169
                            }
170

    
171
                            result['authors'].push({"name": relation.fullname, "url": OpenaireProperties.getsearchLinkToPerson()+relation['to'].content});
172
                        } else if(relation['to'].class == "isProducedBy") {
173
                            if(result['projects'] == undefined) {
174
                                result['projects'] = new Array<
175
                                    { "url": string, "acronym": string, "title": string,
176
                                      "funderShortname": string, "funderName": string,
177
                                      "code": string
178
                                    }>();
179
                            }
180

    
181
                            let countProjects = result['projects'].length;
182

    
183
                            result['projects'][countProjects] = {
184
                                "url": "", "acronym": "", "title": "",
185
                                "funderShortname": "", "funderName": "",
186
                                "code": ""
187
                            }
188

    
189
                            result['projects'][countProjects]['url'] =
190
                                OpenaireProperties.getsearchLinkToProject() + relation['to'].content;
191
                            result['projects'][countProjects]['acronym'] = relation.acronym;
192
                            result['projects'][countProjects]['title'] = relation.title;
193
                            result['projects'][countProjects]['code'] = relation.code;
194

    
195
                            if(relation.hasOwnProperty("funding")) {
196
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
197

    
198
                                for(let z=0; z<fundingLength; z++) {
199
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
200

    
201
                                    if(fundingData.hasOwnProperty("funder")) {
202
                                        result['projects'][countProjects]['funderShortname'] = fundingData['funder'].shortname;
203
                                        result['projects'][countProjects]['funderName'] = fundingData['funder'].name;
204
                                    }
205
                                }
206
                            }
207

    
208
                        }
209
                    }
210
                }
211
            }
212

    
213
            result.year = resData.dateofacceptance.split('-')[0];
214

    
215
            if(!Array.isArray(resData.description)) {
216
                result.description = resData.description;
217
            } else {
218
                result.description = resData.description[0];
219
            }
220
            if(result.description.length > this.sizeOfDescription) {
221
                result.description = result.description.substring(0, this.sizeOfDescription)+"...";
222
            }
223

    
224
            result.embargoEndDate = resData.embargoenddate;
225

    
226
            results.push(result);
227
        }
228

    
229
        return results;
230
    }
231

    
232
    parseResultsCSV(data: any): any {
233
        let results: any = [];
234

    
235

    
236
        let length = Array.isArray(data) ? data.length : 1;
237

    
238
        for(let i=0; i<length; i++) {
239
            let resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
240

    
241
            var result: any = [];
242

    
243
            if(Array.isArray(resData['title'])) {
244
                result.push(this.quote(resData['title'][0].content));
245
            } else {
246
                result.push(this.quote(resData['title'].content));
247
            }
248

    
249
            var authors: string[] = [];
250
            var projects: string[] = [];
251
            var funder: string = "";
252
            if(resData['rels'].hasOwnProperty("rel")) {
253
                let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
254

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

    
258
                    if(relation.hasOwnProperty("to")) {
259
                        if(relation['to'].class == "hasAuthor") {
260
                            authors.push(relation.fullname);
261
                        } else if(relation['to'].class == "isProducedBy") {
262
                            if(relation.code != "") {
263
                                projects.push(relation.title+" ("+relation.code+")");
264
                            } else {
265
                                projects.push(relation.title);
266
                            }
267

    
268
                            if(relation.hasOwnProperty("funding")) {
269
                                let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
270

    
271
                                for(let z=0; z<fundingLength; z++) {
272
                                    let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
273

    
274
                                    if(fundingData.hasOwnProperty("funder")) {
275
                                        funder = fundingData['funder'].shortname;
276
                                    }
277
                                }
278
                            }
279

    
280
                        }
281
                    }
282
                }
283
                result.push(this.quote(authors));
284
                var year: string = "";
285
                if(resData.hasOwnProperty("dateofacceptance")) {
286
                    year = resData.dateofacceptance;
287
                }
288
                result.push(year);
289
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
290
                result.push(this.quote(id));
291
                result.push(funder);
292
                result.push(this.quote(projects));
293
            } else {
294
                result.push(this.quote(''));
295
                var year: string = "";
296
                if(resData.hasOwnProperty("dateofacceptance")) {
297
                    year = resData.dateofacceptance;
298
                }
299
                result.push(year);
300
                var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
301
                result.push(this.quote(id));
302
                result.push(funder);
303
                result.push("");
304
            }
305

    
306

    
307
            //result.push(resData.embargoenddate);
308

    
309
            if(resData['bestlicense'].hasOwnProperty("classid")) {
310
                result.push(this.quote(resData['bestlicense'].classid));
311
            } else {
312
                result.push("");
313
            }
314

    
315
            results.push(result);
316
        }
317

    
318
        return results;
319
    }
320

    
321
    numOfEntityPublications(id: string, entity: string):any {
322

    
323
        //OpenaireProperties.getSearchAPIURLLast()
324
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
325
        let url = OpenaireProperties.getSearchAPIURLLast()+entity+id+"/publications/count"+ "?format=json";
326
        let key = url;
327
        if (this._cache.has(key)) {
328
          return Observable.of(this._cache.get(key));
329
        }
330
        return this.http.get(url)
331
                    .map(res => <any> res.json())
332
                    .map(res => res.total).do(res => {
333
                      this._cache.set(key, res);
334
                    });
335
    }
336

    
337
    numOfSearchPublications(params: string):any {
338

    
339
        //OpenaireProperties.getSearchAPIURLLast()
340
        //"http://rudie.di.uoa.gr:8080/dnet-functionality-services-2.0.0-SNAPSHOT/rest/v2/api/"
341
        let url = OpenaireProperties.getSearchAPIURLLast()+"publications/count?q="+params+ "&format=json";
342
        let key = url;
343
        if (this._cache.has(key)) {
344
          return Observable.of(this._cache.get(key));
345
        }
346
        return this.http.get(url)
347
                    .map(res => <any> res.json())
348
                    .map(res => res.total).do(res => {
349
                      this._cache.set(key, res);
350
                    });
351
    }
352

    
353
    private quote(word: any): string {
354
        return '"'+word+'"';
355
    }
356
}
(21-21/22)