import {Injectable} from '@angular/core'; import {Http, Response} from '@angular/http'; import {Observable} from 'rxjs/Observable'; import {AutoCompleteValue} from '../../searchPages/searchUtils/searchHelperClasses.class'; import 'rxjs/add/observable/of'; import 'rxjs/add/operator/do'; import 'rxjs/add/operator/share'; import{EnvProperties} from '../properties/env-properties'; import {StringUtils} from '../string-utils.class'; @Injectable() export class EntitiesSearchService { public ready:boolean = false; constructor(private http: Http ) {} 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; 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; console.info("In searchOrganizationsforDeposit"); 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))+")) " } 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; 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)) .map(request => { this.ready = true; return this.parse(request,oafEntityType,type); }).catch((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 != '' ) { 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; 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