Project

General

Profile

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