Project

General

Profile

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

    
7

    
8

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

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

    
25
        return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
26
                    //.map(res => <any> res.json())
27

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

    
30
     }
31
    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

    
35
    }
36

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

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

    
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
    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
      } else if(entityType == "other"){
73
        suffix="other/";
74
      }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
    private handleError (error: HttpErrorResponse) {
84
      // 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
      return throwError(error  || 'Server error');
88
    }
89
}
(12-12/23)