import {Injectable} from '@angular/core'; import {Http, Response} from '@angular/http'; import {HttpClient, HttpErrorResponse} from "@angular/common/http"; import {Observable, throwError} from 'rxjs'; import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class'; import{EnvProperties} from '../properties/env-properties'; import {StringUtils} from '../string-utils.class'; import {catchError, map} from "rxjs/operators"; @Injectable() export class EntitiesSearchService { public ready:boolean = false; constructor(private http: HttpClient ) {} searchProjectsByFunder(keyword:string, funderId:string, properties:EnvProperties ):any { this.ready = false; let url = properties.searchResourcesAPIURL; //console.log("Funder is " + funderId); 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"; //console.log("URL " + url); // 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() .then(request => { //request = request.json().results; request = request['results']; this.ready = true; return this.parse(request,"oaf:project","project"); }).catch((ex) => { console.error('An error occured', ex); return [{id:'-2',label:'Error'}];; }); } searchByDepositType(keyword:string, DepositType:string, properties:EnvProperties ):any { this.ready = false; let link = properties.searchResourcesAPIURL; let url = link+"?query="; if(keyword!= null && keyword != '' ) { /*url += "((oaftype exact organization and deletedbyinference=false and "+ "(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))"+ " and ((organizationlegalname all "+'"'+keyword+'"'+") or (organizationlegalshortname all "+'"'+keyword+'"'+")) " + // "and " + this.quote(params) + " " + "and (collectedfrom exact "+StringUtils.quote(StringUtils.URIEncode(DepositType))+")) "*/ // 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"+ "(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))"+ " and ((organizationlegalname all "+'"'+keyword+'"'+") or (organizationlegalshortname all "+'"'+keyword+'"'+")) " + // "and " + this.quote(params) + " " + //"and (collectedfrom exact "+StringUtils.quote(StringUtils.URIEncode(DepositType))+")) " ")"; } url += "&page=0&size=10"; url += "&format=json"; // 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() .then(request => { //request = request.json().results; request = request['results']; //console.log(request); this.ready = true; return this.parse(request,"oaf:organization","organization"); }).catch((ex) => { console.error('An error occured', ex); return [{id:'-2',label:'Error'}];; }); } searchByType(keyword:string,type:string, properties:EnvProperties ){ if (type == "project"){ return this.searchEntity(keyword,"projects","oaf:project","project", properties); }else if (type == "dataset"){ return this.searchEntity(keyword,"datasets","oaf:result","dataset", properties); }else if (type == "datasource" || type == "hostedBy" || type== "collectedFrom"){ return this.searchEntity(keyword,"datasources","oaf:datasource","datasource", properties); }else if (type == "publication"){ return this.searchEntity(keyword,"publications","oaf:result","publication", properties); }else if (type == "organization"){ return this.searchEntity(keyword,"organizations","oaf:organization","organization", properties); } } fetchByType(id:string,type:string, properties:EnvProperties ){ if (type == "project"){ return this.fetchEntity(id,"projects","oaf:project","project", properties); }else if (type == "dataset"){ return this.fetchEntity(id,"datasets","oaf:result","dataset", properties); }else if (type == "datasource" || type == "hostedBy" || type== "collectedFrom"){ return this.fetchEntity(id,"datasources","oaf:datasource","datasource", properties); }else if (type == "publication"){ return this.fetchEntity(id,"publications","oaf:result","publication", properties); }else if (type == "organization"){ return this.fetchEntity(id,"organizations","oaf:organization","organization", properties); } } 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) } 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) } private fetch (link,id,oafEntityType,type, properties:EnvProperties ){ this.ready = false; let url = link+"/"+id+"?format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) //.map(request => request.json()) // .do(res => console.info(res)) .pipe(map(request => { this.ready = true; return this.parse(request,oafEntityType,type); })) .pipe(catchError((ex) => { console.error('An error occured', ex); return [{id:'-2',label:'Error'}];; })); } private search (link,keyword,oafEntityType,type, properties:EnvProperties ){ this.ready = false; let url = link+"?"; if(keyword!= null && keyword != '' ) { 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; } } url += "&page=0&size="+10+"&format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).toPromise() .then(request => { //request = request.json().results; request = request['results']; 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