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' ;
2019-06-03 15:20:36 +02:00
2018-02-05 14:14:59 +01:00
import { EnvProperties } from '../properties/env-properties' ;
2017-12-19 13:53:46 +01:00
2019-06-03 15:20:36 +02:00
import { catchError , map } from "rxjs/operators" ;
2017-12-19 13:53:46 +01:00
@Injectable ( )
export class EntitiesSearchService {
public ready :boolean = false ;
2019-06-03 15:20:36 +02:00
constructor ( private http : HttpClient ) { }
2017-12-19 13:53:46 +01:00
2019-10-16 12:18:32 +02:00
/ * s e a r c h P r o j e c t s B y F u n d e r ( k e y w o r d : s t r i n g , f u n d e r I d : s t r i n g , p r o p e r t i e s : E n v P r o p e r t i e s ) : a n y {
2017-12-19 13:53:46 +01:00
this . ready = false ;
2018-02-05 14:14:59 +01:00
let url = properties . searchResourcesAPIURL ;
2019-02-14 11:15:44 +01:00
//console.log("Funder is " + funderId);
2017-12-19 13:53:46 +01:00
url = url + "?query=(oaftype exact project and deletedbyinference=false) " ;
if ( keyword != null && keyword != '' ) {
url += 'and ((projectcode_nt exact "' + keyword + '" )or(' + keyword + ')) ' ;
url += ( ( funderId && funderId . length > 0 ) ? ( "&fq=funder exact " + '"' + funderId + '"' ) : "" ) ;
}
url += "&page=0&size=10" ;
url += "&format=json" ;
2019-02-14 11:15:44 +01:00
//console.log("URL " + url);
2018-02-05 14:14:59 +01:00
// let url = properties.searchAPIURLLAst+"projects?"+((keyword && keyword.length > 0)?("q=" +keyword):"")+((funderId && funderId.length > 0 )?"&fq=funder exact " + '"'+funderId+ '"':"")+"&size=10&page=0&format=json";
return this . http . get ( ( properties . useCache ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url ) . toPromise ( )
2017-12-19 13:53:46 +01:00
. then ( request = >
{
2019-06-03 15:20:36 +02:00
//request = request.json().results;
request = request [ 'results' ] ;
2017-12-19 13:53:46 +01:00
this . ready = true ;
return this . parse ( request , "oaf:project" , "project" ) ;
} ) . catch ( ( ex ) = > {
console . error ( 'An error occured' , ex ) ;
2021-06-15 15:20:42 +02:00
return [ { id : '-2' , label : 'Error' } ] ;
2017-12-19 13:53:46 +01:00
} ) ;
2019-10-16 12:18:32 +02:00
} * /
2024-02-01 18:46:34 +01:00
// not used
/ * *
* @deprecated
* /
2018-02-05 14:14:59 +01:00
searchByDepositType ( keyword :string , DepositType :string , properties :EnvProperties ) : any {
2017-12-19 13:53:46 +01:00
this . ready = false ;
2018-02-05 14:14:59 +01:00
let link = properties . searchResourcesAPIURL ;
2017-12-19 13:53:46 +01:00
let url = link + "?query=" ;
if ( keyword != null && keyword != '' ) {
2019-02-12 12:15:21 +01:00
/ * u r l + = " ( ( o a f t y p e e x a c t o r g a n i z a t i o n a n d d e l e t e d b y i n f e r e n c e = f a l s e a n d " +
2018-05-14 13:42:38 +02:00
"(reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=* or reldatasourcecompatibilityid = native))" +
2017-12-19 13:53:46 +01:00
" and ((organizationlegalname all " + '"' + keyword + '"' + ") or (organizationlegalshortname all " + '"' + keyword + '"' + ")) " +
// "and " + this.quote(params) + " " +
2019-02-12 12:15:21 +01:00
"and (collectedfrom exact " + StringUtils . quote ( StringUtils . URIEncode ( DepositType ) ) + ")) " * /
2017-12-19 13:53:46 +01:00
2019-02-12 12:15:21 +01:00
// in search there is this filter:
//reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*
//url += "((oaftype exact organization and deletedbyinference=false )"+
url += "((oaftype exact organization and deletedbyinference=false and" +
2020-02-26 12:24:50 +01:00
"(reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire4.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=* or reldatasourcecompatibilityid = native))" +
2019-02-12 12:15:21 +01:00
" and ((organizationlegalname all " + '"' + keyword + '"' + ") or (organizationlegalshortname all " + '"' + keyword + '"' + ")) " +
// "and " + this.quote(params) + " " +
//"and (collectedfrom exact "+StringUtils.quote(StringUtils.URIEncode(DepositType))+")) "
")" ;
2017-12-19 13:53:46 +01:00
}
url += "&page=0&size=10" ;
url += "&format=json" ;
2018-02-05 14:14:59 +01:00
// let url = properties.searchAPIURLLAst+"projects?"+((keyword && keyword.length > 0)?("q=" +keyword):"")+((funderId && funderId.length > 0 )?"&fq=funderid exact " + '"'+funderId+ '"':"")+"&size=10&page=0&format=json";
return this . http . get ( ( properties . useCache ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url ) . toPromise ( )
2017-12-19 13:53:46 +01:00
. then ( request = >
{
2019-06-03 15:20:36 +02:00
//request = request.json().results;
request = request [ 'results' ] ;
2019-02-14 11:15:44 +01:00
//console.log(request);
2017-12-19 13:53:46 +01:00
this . ready = true ;
return this . parse ( request , "oaf:organization" , "organization" ) ;
} ) . catch ( ( ex ) = > {
console . error ( 'An error occured' , ex ) ;
2021-06-15 15:20:42 +02:00
return [ { id : '-2' , label : 'Error' } ] ;
2017-12-19 13:53:46 +01:00
} ) ;
}
2018-02-05 14:14:59 +01:00
searchByType ( keyword :string , type : string , properties :EnvProperties ) {
2017-12-19 13:53:46 +01:00
if ( type == "project" ) {
2018-02-05 14:14:59 +01:00
return this . searchEntity ( keyword , "projects" , "oaf:project" , "project" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "dataset" ) {
2018-02-05 14:14:59 +01:00
return this . searchEntity ( keyword , "datasets" , "oaf:result" , "dataset" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "datasource" || type == "hostedBy" || type == "collectedFrom" ) {
2018-02-05 14:14:59 +01:00
return this . searchEntity ( keyword , "datasources" , "oaf:datasource" , "datasource" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "publication" ) {
2018-02-05 14:14:59 +01:00
return this . searchEntity ( keyword , "publications" , "oaf:result" , "publication" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "organization" ) {
2018-02-05 14:14:59 +01:00
return this . searchEntity ( keyword , "organizations" , "oaf:organization" , "organization" , properties ) ;
2017-12-19 13:53:46 +01:00
}
}
2018-02-05 14:14:59 +01:00
fetchByType ( id :string , type : string , properties :EnvProperties ) {
2017-12-19 13:53:46 +01:00
if ( type == "project" ) {
2018-02-05 14:14:59 +01:00
return this . fetchEntity ( id , "projects" , "oaf:project" , "project" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "dataset" ) {
2018-02-05 14:14:59 +01:00
return this . fetchEntity ( id , "datasets" , "oaf:result" , "dataset" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "datasource" || type == "hostedBy" || type == "collectedFrom" ) {
2018-02-05 14:14:59 +01:00
return this . fetchEntity ( id , "datasources" , "oaf:datasource" , "datasource" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "publication" ) {
2018-02-05 14:14:59 +01:00
return this . fetchEntity ( id , "publications" , "oaf:result" , "publication" , properties ) ;
2017-12-19 13:53:46 +01:00
} else if ( type == "organization" ) {
2018-02-05 14:14:59 +01:00
return this . fetchEntity ( id , "organizations" , "oaf:organization" , "organization" , properties ) ;
2017-12-19 13:53:46 +01:00
}
}
2018-02-05 14:14:59 +01:00
private searchEntity ( keyword : string , APIname :string , oafEntityType :string , type : string , properties :EnvProperties ) : any {
let link = properties . searchAPIURLLAst + APIname ;
return this . search ( link , keyword , oafEntityType , type , properties )
2017-12-19 13:53:46 +01:00
}
2018-02-05 14:14:59 +01:00
private fetchEntity ( id : string , APIname :string , oafEntityType :string , type : string , properties :EnvProperties ) : any {
let link = properties . searchAPIURLLAst + APIname ;
return this . fetch ( link , id , oafEntityType , type , properties )
2017-12-19 13:53:46 +01:00
}
2018-02-05 14:14:59 +01:00
private fetch ( link , id , oafEntityType , type , properties :EnvProperties ) {
2017-12-19 13:53:46 +01:00
this . ready = false ;
let url = link + "/" + id + "?format=json" ;
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(request => <any> request.json())
2017-12-19 13:53:46 +01:00
// .do(res => console.info(res))
2019-06-03 15:20:36 +02:00
. pipe ( map ( request = > {
2017-12-19 13:53:46 +01:00
this . ready = true ;
return this . parse ( request , oafEntityType , type ) ;
2019-06-03 15:20:36 +02:00
} ) )
. pipe ( catchError ( ( ex ) = > {
2017-12-19 13:53:46 +01:00
console . error ( 'An error occured' , ex ) ;
2021-06-15 15:20:42 +02:00
return [ { id : '-2' , label : 'Error' } ] ;
2019-06-03 15:20:36 +02:00
} ) ) ;
2017-12-19 13:53:46 +01:00
}
2018-02-05 14:14:59 +01:00
private search ( link , keyword , oafEntityType , type , properties :EnvProperties ) {
2017-12-19 13:53:46 +01:00
this . ready = false ;
let url = link + "?" ;
if ( keyword != null && keyword != '' ) {
2019-02-11 11:52:24 +01:00
if ( type == "project" ) {
//name, title, acronym, grantid
url += "fq=" + '(projectcode_nt="' + keyword + '" ) or (fundershortname=' + '"' + keyword + '"' + ') or (projectacronym="' + keyword + '" ) or (projecttitle="' + keyword + '")' ;
} else if ( type == "organization" ) {
//name fields
url += "fq=" + '(organizationlegalname="' + keyword + '" ) or (organizationlegalshortname=' + '"' + keyword + '")' ;
} else {
url += "q=" + keyword ;
}
2017-12-19 13:53:46 +01:00
}
url += "&page=0&size=" + 10 + "&format=json" ;
2018-02-05 14:14:59 +01:00
return this . http . get ( ( properties . useCache ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url ) . toPromise ( )
2017-12-19 13:53:46 +01:00
. then ( request = >
{
2019-06-03 15:20:36 +02:00
//request = request.json().results;
request = request [ 'results' ] ;
2017-12-19 13:53:46 +01:00
this . ready = true ;
return this . parse ( request , oafEntityType , type ) ;
} ) . catch ( ( ex ) = > {
console . error ( 'An error occured' , ex ) ;
return [ { id : '-2' , label : 'Error' } ] ;
} ) ;
}
private parse ( data : any , oafEntityType :string , type : string ) {
var array :any = [ ]
let length = Array . isArray ( data ) ? data.length : 1 ;
for ( let i = 0 ; i < length ; i + + ) {
let resData = Array . isArray ( data ) ? data [ i ] [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ oafEntityType ] : data [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ oafEntityType ] ;
var value :any = { } ;
if ( resData [ 'title' ] ) {
if ( Array . isArray ( resData [ 'title' ] ) ) {
value . label = resData [ 'title' ] [ 0 ] ;
} else {
value . label = resData [ 'title' ] ;
}
if ( oafEntityType == "oaf:result" ) {
value . label = value . label . content ;
value . result = resData ;
}
} else if ( resData [ "fullname" ] ) {
if ( Array . isArray ( resData [ "fullname" ] ) ) {
value . label = resData [ "fullname" ] [ 0 ] ;
} else {
value . label = resData [ "fullname" ] ;
}
} else if ( resData [ "legalname" ] ) {
if ( Array . isArray ( resData [ "legalname" ] ) ) {
value . label = resData [ "legalname" ] [ 0 ] ;
} else {
value . label = resData [ "legalname" ] ;
}
2018-12-19 15:07:02 +01:00
if ( resData [ "legalshortname" ] ) {
if ( Array . isArray ( resData [ "legalshortname" ] ) ) {
value . label += " (" + resData [ "legalshortname" ] [ 0 ] + ")" ;
} else {
value . label += " (" + resData [ "legalshortname" ] + ")" ;
}
}
2017-12-19 13:53:46 +01:00
} else if ( resData [ "officialname" ] ) {
if ( Array . isArray ( resData [ "officialname" ] ) ) {
value . label = resData [ "officialname" ] [ 0 ] ;
} else {
value . label = resData [ "officialname" ] ;
}
}
value . id = Array . isArray ( data ) ? data [ i ] [ 'result' ] [ 'header' ] [ 'dri:objIdentifier' ] : data [ 'result' ] [ 'header' ] [ 'dri:objIdentifier' ] ;
if ( type == "project" ) {
value . projectAcronym = resData [ 'acronym' ] ;
value . projectName = value . label ;
value . endDate = null ;
value . startDate = null ;
value . funderId = "" ;
value . funderName = "" ;
value . jurisdiction = "" ;
value . fundingLevel0 = "" ;
value . code = resData [ 'code' ] ;
if ( resData [ 'fundingtree' ] && resData [ 'fundingtree' ] [ 'funder' ] ) {
value . funderId = ( resData [ 'fundingtree' ] [ 'funder' ] [ 'id' ] ) ? resData [ 'fundingtree' ] [ 'funder' ] [ 'id' ] : "" ;
value . funderName = ( resData [ 'fundingtree' ] [ 'funder' ] [ 'shortname' ] ) ? resData [ 'fundingtree' ] [ 'funder' ] [ 'shortname' ] : "" ;
value . jurisdiction = ( resData [ 'fundingtree' ] [ 'funder' ] [ 'jurisdiction' ] ) ? resData [ 'fundingtree' ] [ 'funder' ] [ 'jurisdiction' ] : "" ;
if ( resData [ 'fundingtree' ] [ 'funding_level_2' ] ) {
value . fundingLevel0 = ( resData [ 'fundingtree' ] [ 'funding_level_2' ] && resData [ 'fundingtree' ] [ 'funding_level_2' ] [ 'parent' ] &&
resData [ 'fundingtree' ] [ 'funding_level_2' ] [ 'parent' ] [ 'funding_level_1' ] && resData [ 'fundingtree' ] [ 'funding_level_2' ] [ 'parent' ] [ 'funding_level_1' ] [ 'parent' ]
&& resData [ 'fundingtree' ] [ 'funding_level_2' ] [ 'parent' ] [ 'funding_level_1' ] [ 'parent' ] [ 'funding_level_0' ] ) ?
resData [ 'fundingtree' ] [ 'funding_level_2' ] [ 'parent' ] [ 'funding_level_1' ] [ 'parent' ] [ 'funding_level_0' ] [ 'name' ] : "" ;
} else if ( resData [ 'fundingtree' ] [ 'funding_level_1' ] ) {
value . fundingLevel0 = ( resData [ 'fundingtree' ] [ 'funding_level_1' ] && resData [ 'fundingtree' ] [ 'funding_level_1' ] [ 'parent' ] && resData [ 'fundingtree' ] [ 'funding_level_1' ] [ 'parent' ] [ 'funding_level_0' ] ) ?
resData [ 'fundingtree' ] [ 'funding_level_1' ] [ 'parent' ] [ 'funding_level_0' ] [ 'name' ] : "" ;
} else if ( resData [ 'fundingtree' ] [ 'funding_level_0' ] ) {
value . fundingLevel0 = ( resData [ 'fundingtree' ] [ 'funding_level_0' ] ) ? resData [ 'fundingtree' ] [ 'funding_level_0' ] [ 'name' ] : "" ;
} else {
value . fundingLevel0 = "" ;
}
}
if ( resData . hasOwnProperty ( "startdate" ) ) {
value . startDate = resData . startdate . split ( '-' ) [ 0 ] ;
}
if ( resData . hasOwnProperty ( "enddate" ) ) {
value . endDate = resData . enddate . split ( '-' ) [ 0 ] ;
}
}
// console.info("add:"+value.label+" "+value.id);
array . push ( value ) ;
}
if ( array . length == 0 ) {
array . push ( { id : '-1' , label : 'No results found' } ) ;
}
2019-02-14 11:15:44 +01:00
//console.info("Parsing results.... Size:"+array.length);
2017-12-19 13:53:46 +01:00
return array ;
}
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
}
}