import {Injectable} from '@angular/core'; import {HttpClient} from "@angular/common/http"; import {SearchResult} from '../utils/entities/searchResult'; import {RefineResultsUtils} from './servicesUtils/refineResults.class'; import {DOI, StringUtils} from '../utils/string-utils.class'; import {ParsingFunctions} from '../landingPages/landing-utils/parsingFunctions.class'; import {EnvProperties} from '../utils/properties/env-properties'; import {map} from "rxjs/operators"; @Injectable() export class SearchResearchResultsService { private sizeOfDescription: number = 270; public parsingFunctions: ParsingFunctions = new ParsingFunctions(); constructor(private http: HttpClient ) {} search (resultType:string, params: string, refineParams:string, page: number, size: number, sortBy: string, refineFields:string[] , properties:EnvProperties):any { let link = properties.searchAPIURLLAst+this.getEntityName(resultType,true); let url = link+"?"; if(params!= null && params != '' ) { url += params; } if(refineParams!= null && refineParams != '' ) { url += refineParams; } if(sortBy) { url += "&sortBy=" + sortBy; } url += "&page="+(page-1)+"&size="+size+"&format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => [res['meta'].total, this.parseResults('ss', res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")])); } searchById (resultType:string, id: string, properties:EnvProperties ):any { let url = properties.searchAPIURLLAst+this.getEntityName(resultType,true) +"/"+id+"?format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => this.parseResults(resultType, res, properties))); } searchAggregators (resultType:string, id: string, params: string, refineParams:string, page: number, size: number, properties:EnvProperties ):any { let link = properties.searchAPIURLLAst+this.getEntityName(resultType,true); let url = link+"?"+"&format=json"; if(params!= null && params != '' ) { url += params; } if(refineParams!= null && refineParams != '' ) { url += refineParams; } url += "&page="+(page-1)+"&size="+size; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => this.parseRefineResults(id, res['refineResults']))); } searchByListOfDOI (resultType:string, DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any { let link = properties.searchAPIURLLAst+ this.getEntityName(resultType,true); let url = link+"?"+"&format=json&"; var doisParams = ""; for(var i =0 ;i < DOIs.length; i++){ doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"'; } if(doisParams.length > 0){ url +="&"+doisParams; } if(refineParams!= null && refineParams != '' ) { url += refineParams; } url += "&page="+(page-1)+"&size="+size; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")])); } advancedSearch (resultType:string, params: string, page: number, size: number, sortBy: string, properties:EnvProperties, refineParams:string=null, refineFields:string[] =null, refineQuery:string = null ):any { let url = properties.searchResourcesAPIURL; var basicQuery = "(oaftype exact result) and (resulttypeid exact "+this.getEntityName(resultType,false) + ") "; url += "?query="; if(params!= null && params != '' ) { url +=" ( "+basicQuery+ " ) " +" and (" + params + ")"; }else{ url +=" ( "+basicQuery+ " ) "; } if(refineParams!= null && refineParams != '' ) { url += refineParams; } if(sortBy) { let sortOptions = sortBy.split(","); url += "sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+" "; } if(refineQuery) { url += "&" + refineQuery; } url += "&page="+(page-1)+"&size="+size; url += "&format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")])); } advancedSearchResults (resultType:string, params: string, page: number, size: number, sortBy: string, properties:EnvProperties, refineParams:string=null, refineFields:string[] =null, refineQuery:string = null ):any { let url = properties.searchAPIURLLAst+"resources2/?format=json"; if(params!= null && params != '' ) { url +="&query=(" + params + ")"; } if(sortBy) { let sortOptions = sortBy.split(","); url += (params ? " " : "&query=(*) ")+"sortBy "+sortOptions[0]+"/sort."+sortOptions[1]+(params ? " " : " "); } if(refineParams!= null && refineParams != '' ) { url += refineParams; } if(refineQuery) { url += "&" + refineQuery; } url += "&page="+(page-1)+"&size="+size; // url += "&format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")])); } searchResultForEntity (resultType:string, params: string, page: number, size: number, properties:EnvProperties):any { let link = properties.searchAPIURLLAst; let url = link+params+"/"+this.getEntityName(resultType,true)+ "?format=json"; url += "&page="+(page-1)+"&size="+size; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties)])); } //???? why different from above? searchForDataproviders(resultType:string, params: string, page: number, size: number, properties:EnvProperties):any { let link = properties.searchAPIURLLAst; let url = link+params+ "&page="+(page-1)+"&size="+size + "&format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => [res['meta'].total, this.parseResults(resultType, res['results'], properties)])); } parseResults(resultType:string, data: any, properties: EnvProperties): SearchResult[] { let results: SearchResult[] = []; let length = Array.isArray(data) ? data.length : 1; for(let i=0; i(); let types = new Set(); let instance; let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1; for(let i=0; i(); if(!Array.isArray(resData['programmingLanguage'])) { if(resData['programmingLanguage'].classname != "Undetermined" && resData['programmingLanguage'].classname) { result.programmingLanguages.push(resData['programmingLanguage'].classname); } } else { for(let i=0; i(); if(!Array.isArray(resData['language'])) { if(resData['language'].classname != "Undetermined" && resData['language'].classname) { result.languages.push(resData['language'].classname); } } else { for(let i=0; i(); if(!Array.isArray(resData['country'])) { if(resData['country'].classname != "Undetermined" && resData['country'].classname) { result.countriesForResults.push(resData['country'].classname); } } else { for(let i=0; i(); } let authors = resData['creator']; let length = Array.isArray(authors) ? authors.length : 1; for(let i=0; i this.sizeOfDescription) { result.description = result.description.substring(0, this.sizeOfDescription) + "..."; } if(resData.embargoenddate && resData.embargoenddate != '') { result.embargoEndDate = new Date(resData.embargoenddate); } if(!Array.isArray(resData.publisher)) { result.publisher = resData.publisher; } else { for(let i=0; i(); } let countProjects = projects.length; projects[countProjects] = { "id": "", "acronym": "", "title": "", "funderShortname": "", "funderName": "", "code": "" }; if(relation.title != 'unidentified') { projects[countProjects]['id'] = relation['to'].content; projects[countProjects]['acronym'] = relation.acronym; projects[countProjects]['title'] = relation.title; projects[countProjects]['code'] = relation.code; } else { projects[countProjects]['id'] = ""; projects[countProjects]['acronym'] = ""; projects[countProjects]['title'] = ""; projects[countProjects]['code'] = ""; } if(relation.hasOwnProperty("funding")) { let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1; for(let z=0; z res['total'])); } numOfEntityResults(resultType:string, id: string, entity: string, properties:EnvProperties):any { var parameters: string = ""; parameters = this.getEntityName(entity, true) + "/"+id+"/"+this.getEntityName(resultType, true)+"/count"; let url = properties.searchAPIURLLAst+parameters+"?format=json"; return this.numOfResults(url , properties); } numOfResearchOutcomes( params: string, properties:EnvProperties, refineParams:string=null):any { let url = properties.searchAPIURLLAst+"resources2/?format=json&size=0&type=results"; if(params.length > 0){ // var DOIs:string[] = DOI.getDOIsFromString(params); // var doisParams = ""; // // for(var i =0 ;i < DOIs.length; i++){ // doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"'; // } // if(doisParams.length > 0){ // url += "&"+doisParams; // }else{ // url += "&query=" + StringUtils.URIEncode(params); // } url += "&query=" + params; } if(refineParams!= null && refineParams != '' ) { url += refineParams; } return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => res['meta']['total'])); } numOfSearchResults(resultType:string, params: string, properties:EnvProperties, refineParams:string=null):any { let url = properties.searchAPIURLLAst+this.getEntityName(resultType, true)+"/count?format=json"; if(params.length > 0){ var DOIs:string[] = DOI.getDOIsFromString(params); var doisParams = ""; for(var i =0 ;i < DOIs.length; i++){ doisParams+=(doisParams.length > 0?"&":"")+'doi="'+ DOIs[i]+'"'; } if(doisParams.length > 0){ url += "&"+doisParams; }else{ url += "&q=" + StringUtils.URIEncode(params); } } if(refineParams!= null && refineParams != '' ) { url += refineParams; } return this.numOfResults(url, properties); } numOfSearchResultsLinkedToPub(resultType:string, properties:EnvProperties):any { let url = properties.searchAPIURLLAst+"resources?query="+encodeURIComponent("( (oaftype exact result) and (resulttypeid exact "+resultType+") and (relresulttype=publication) )")+"&page=0&size=0&format=json"; return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url) .pipe(map(res => res['meta']['total'])); } countTotalResults(resultType:string, properties:EnvProperties, refineParams:string=null):any { let url = properties.searchAPIURLLAst+this.getEntityName(resultType, true)+"/count?format=json"+refineParams; return this.numOfResults(url, properties); } /* private quote(word: any): string { return '"'+word+'"'; } */ private getEntityName (entityType:string, plural:boolean){ if(entityType == "publication" ||entityType == "dataset" || entityType == "organization" || entityType == "datasource" || entityType == "project" ){ if(plural){ return entityType+ "s"; }else{ return entityType; } }else{ return entityType; } } }