Project

General

Profile

1
import {Injectable} from '@angular/core';
2
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
3
import {throwError} from 'rxjs';
4
import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class';
5

    
6

    
7

    
8
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
9
import{EnvProperties} from '../utils/properties/env-properties';
10
import {map} from "rxjs/operators";
11

    
12
@Injectable()
13
export class RefineFieldResultsService {
14
     constructor(private http: HttpClient ) {}
15
     getRefineFieldsResultsByEntityName(fields:string[], entityName:string, properties:EnvProperties, communityQuery=null):any{
16
        let url = properties.searchAPIURLLAst + this.getSearchAPIURLForEntity(entityName)+"?format=json&refine=true&page=1&size=0";
17
        for(var i=0; i < fields.length; i++){
18
            url += "&fields="+fields[i];
19
        }
20
        if(communityQuery!= null && communityQuery != ''  ) {
21
            url += communityQuery;
22
        }
23
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
24
                    //.map(res => <any> res.json())
25

    
26
                    .pipe(map(res =>   [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],fields, entityName)]));
27

    
28
     }
29
    getRefineFieldResultsByFieldName(fieldName:string, entityName:string, properties:EnvProperties):any{
30
       let key:string="fundinglevel";
31
       let link = properties.searchAPIURLLAst +this.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName +(fieldName.toString().indexOf(key)!=-1?('&sf='+fieldName):'')+ "&format=json";
32
        return  this.getField(link,fieldName, properties);
33

    
34
    }
35

    
36
    getField (link:string,fieldName:string, properties:EnvProperties):any{
37
      let url = link+"&refine=true&page=1&size=0";
38

    
39
      return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
40
                  //.map(res => <any> res.json())
41
                  .pipe(map(res => res['refineResults']))
42
                  .pipe(map(res =>  this.parse(res,fieldName)));
43

    
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
    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
      } else if(entityType == "other"){
72
        suffix="other/";
73
      }else if(entityType == "organization"){
74
        suffix="organizations/";
75
      }else if(entityType == "dataprovider"){
76
        suffix="datasources/";
77
      }else if(entityType == "person"){
78
        suffix="people/";
79
      }else if(entityType == "result"){
80
        suffix="publications/";
81
      }
82
      return  suffix;
83
    }
84
    private handleError (error: HttpErrorResponse) {
85
      // 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
      return throwError(error  || 'Server error');
89
    }
90
}
(12-12/23)