2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
|
2020-11-11 15:43:13 +01:00
|
|
|
import {throwError} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {AutoCompleteValue} from '../searchPages/searchUtils/searchHelperClasses.class';
|
2019-06-03 15:20:36 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
|
2018-02-05 14:14:59 +01:00
|
|
|
import{EnvProperties} from '../utils/properties/env-properties';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {map} from "rxjs/operators";
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class RefineFieldResultsService {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient ) {}
|
2018-11-19 11:15:13 +01:00
|
|
|
getRefineFieldsResultsByEntityName(fields:string[], entityName:string, properties:EnvProperties, communityQuery=null):any{
|
2018-02-05 14:14:59 +01:00
|
|
|
let url = properties.searchAPIURLLAst + this.getSearchAPIURLForEntity(entityName)+"?format=json&refine=true&page=1&size=0";
|
2017-12-19 13:53:46 +01:00
|
|
|
for(var i=0; i < fields.length; i++){
|
|
|
|
url += "&fields="+fields[i];
|
|
|
|
}
|
2018-11-19 11:15:13 +01:00
|
|
|
if(communityQuery!= null && communityQuery != '' ) {
|
|
|
|
url += communityQuery;
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => <any> res.json())
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],fields, entityName)]));
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
}
|
2023-11-24 10:44:23 +01:00
|
|
|
|
|
|
|
getAllRefineFieldResultsByFieldName(fieldName:string, entityName:string, properties:EnvProperties, refineQuery:string=null):any{
|
|
|
|
// let keys:string[]=["funder", "relfunder", "fundinglevel"];
|
|
|
|
let url = properties.searchAPIURLLAst +this.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName +('&sf='+fieldName)+ "&format=json";
|
|
|
|
if(refineQuery!= null && refineQuery != '' ) {
|
|
|
|
url += refineQuery;
|
|
|
|
}
|
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
|
|
|
//.map(res => <any> res.json())
|
|
|
|
|
|
|
|
.pipe(map(res => [res['meta'].total, RefineResultsUtils.parse(res['refineResults'],[fieldName], entityName)]));
|
|
|
|
}
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
getRefineFieldResultsByFieldName(fieldName:string, entityName:string, properties:EnvProperties):any{
|
2023-11-24 15:57:35 +01:00
|
|
|
let keys:string[]=["funder", "fundinglevel"]; // this covers funder, relfunder, funding stream fields and funding level fields
|
2023-11-24 10:44:23 +01:00
|
|
|
let link = properties.searchAPIURLLAst +this.getSearchAPIURLForEntity(entityName)+"?fields="+fieldName +(this.fieldIncludesAnyOfTheKeywords(fieldName, keys)?('&sf='+fieldName):'')+ "&format=json";
|
|
|
|
return this.getField(link,fieldName, properties);
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2023-11-24 10:44:23 +01:00
|
|
|
fieldIncludesAnyOfTheKeywords(field: string, keywords: string[]) {
|
|
|
|
for(let keyword of keywords) {
|
|
|
|
if(field.toString().indexOf(keyword)!=-1) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
getField (link:string,fieldName:string, properties:EnvProperties):any{
|
2017-12-19 13:53:46 +01:00
|
|
|
let url = link+"&refine=true&page=1&size=0";
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(res => <any> res.json())
|
|
|
|
.pipe(map(res => res['refineResults']))
|
|
|
|
.pipe(map(res => this.parse(res,fieldName)));
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
parse(data: any,fieldName:string):any {
|
|
|
|
var values:AutoCompleteValue[] = [];
|
|
|
|
if(data){
|
|
|
|
let field = data[fieldName];
|
|
|
|
for(let i=0; i<field.length; i++) {
|
|
|
|
var value:AutoCompleteValue = new AutoCompleteValue();
|
|
|
|
value.label = field[i].name;
|
|
|
|
value.label = RefineResultsUtils.inParenthesisThePartAfterCharacters(field[i],"||");
|
|
|
|
value.id = field[i].id;
|
|
|
|
values.push(value);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return values;
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
getSearchAPIURLForEntity(entityType:string):string{
|
|
|
|
var suffix = "";
|
|
|
|
if(entityType == "project"){
|
|
|
|
suffix="projects/";
|
|
|
|
}else if(entityType == "publication"){
|
|
|
|
suffix="publications/";
|
|
|
|
}else if(entityType == "dataset"){
|
|
|
|
suffix="datasets/";
|
|
|
|
} else if(entityType == "software"){
|
|
|
|
suffix="software/";
|
2018-07-25 15:27:22 +02:00
|
|
|
} else if(entityType == "other"){
|
|
|
|
suffix="other/";
|
2018-02-05 14:14:59 +01:00
|
|
|
}else if(entityType == "organization"){
|
|
|
|
suffix="organizations/";
|
|
|
|
}else if(entityType == "dataprovider"){
|
|
|
|
suffix="datasources/";
|
|
|
|
}else if(entityType == "person"){
|
|
|
|
suffix="people/";
|
2020-03-04 13:42:52 +01:00
|
|
|
}else if(entityType == "result"){
|
2022-11-28 18:22:01 +01:00
|
|
|
suffix="results/";
|
2018-02-05 14:14:59 +01:00
|
|
|
}
|
|
|
|
return suffix;
|
|
|
|
}
|
2019-06-03 15:20:36 +02:00
|
|
|
private handleError (error: HttpErrorResponse) {
|
2017-12-19 13:53:46 +01:00
|
|
|
// in a real world app, we may send the error to some remote logging infrastructure
|
|
|
|
// instead of just logging it to the console
|
|
|
|
console.log(error);
|
2019-06-03 15:20:36 +02:00
|
|
|
return throwError(error || 'Server error');
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|