Project

General

Profile

1 50169 argiro.kok
import {Injectable} from '@angular/core';
2
import {Http, Response} from '@angular/http';
3 55964 argiro.kok
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
4
import {Observable, throwError} from 'rxjs';
5 50169 argiro.kok
import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class';
6 55964 argiro.kok
7
8
9 50169 argiro.kok
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
10 50586 argiro.kok
import{EnvProperties} from '../utils/properties/env-properties';
11 55964 argiro.kok
import {map} from "rxjs/operators";
12 50169 argiro.kok
13
@Injectable()
14
export class RefineFieldResultsService {
15 55964 argiro.kok
     constructor(private http: HttpClient ) {}
16 53860 argiro.kok
     getRefineFieldsResultsByEntityName(fields:string[], entityName:string, properties:EnvProperties, communityQuery=null):any{
17 50586 argiro.kok
        let url = properties.searchAPIURLLAst + this.getSearchAPIURLForEntity(entityName)+"?format=json&refine=true&page=1&size=0";
18 50169 argiro.kok
        for(var i=0; i < fields.length; i++){
19
            url += "&fields="+fields[i];
20
        }
21 53860 argiro.kok
        if(communityQuery!= null && communityQuery != ''  ) {
22
            url += communityQuery;
23
        }
24 50169 argiro.kok
25 50586 argiro.kok
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
26 55964 argiro.kok
                    //.map(res => <any> res.json())
27 50169 argiro.kok
28 55964 argiro.kok
                    .pipe(map(res =>   [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],fields, entityName)]));
29 50169 argiro.kok
30
     }
31 50586 argiro.kok
    getRefineFieldResultsByFieldName(fieldName:string, entityName:string, properties:EnvProperties):any{
32
       let link = properties.searchAPIURLLAst +this.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName + "&format=json";
33
        return  this.getField(link,fieldName, properties);
34 50169 argiro.kok
35
    }
36
37 50586 argiro.kok
    getField (link:string,fieldName:string, properties:EnvProperties):any{
38 50169 argiro.kok
      let url = link+"&refine=true&page=1&size=0";
39
40 50586 argiro.kok
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
41 55964 argiro.kok
                  //.map(res => <any> res.json())
42
                  .pipe(map(res => res['refineResults']))
43
                  .pipe(map(res =>  this.parse(res,fieldName)));
44 50169 argiro.kok
45
    }
46
    parse(data: any,fieldName:string):any {
47
      var values:AutoCompleteValue[] = [];
48
      if(data){
49
          let field = data[fieldName];
50
          for(let i=0; i<field.length; i++) {
51
            var value:AutoCompleteValue = new AutoCompleteValue();
52
            value.label = field[i].name;
53
            value.label = RefineResultsUtils.inParenthesisThePartAfterCharacters(field[i],"||");
54
            value.id = field[i].id;
55
            values.push(value);
56
57
        }
58
    }
59
60
    return values;
61
    }
62 50586 argiro.kok
    getSearchAPIURLForEntity(entityType:string):string{
63
      var suffix = "";
64
      if(entityType == "project"){
65
        suffix="projects/";
66
      }else if(entityType == "publication"){
67
        suffix="publications/";
68
      }else if(entityType == "dataset"){
69
        suffix="datasets/";
70
      } else if(entityType == "software"){
71
        suffix="software/";
72 52816 konstantin
      } else if(entityType == "other"){
73
        suffix="other/";
74 50586 argiro.kok
      }else if(entityType == "organization"){
75
        suffix="organizations/";
76
      }else if(entityType == "dataprovider"){
77
        suffix="datasources/";
78
      }else if(entityType == "person"){
79
        suffix="people/";
80
      }
81
      return  suffix;
82
    }
83 55964 argiro.kok
    private handleError (error: HttpErrorResponse) {
84 50169 argiro.kok
      // in a real world app, we may send the error to some remote logging infrastructure
85
      // instead of just logging it to the console
86
      console.log(error);
87 55964 argiro.kok
      return throwError(error  || 'Server error');
88 50169 argiro.kok
    }
89
}