openaire-library/services/searchProjects.service.ts

307 lines
14 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient} from "@angular/common/http";
import {SearchResult} from '../utils/entities/searchResult';
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
import{EnvProperties} from '../utils/properties/env-properties';
import {StringUtils} from '../utils/string-utils.class';
import {map} from "rxjs/operators";
import {ParsingFunctions} from "../landingPages/landing-utils/parsingFunctions.class";
@Injectable()
export class SearchProjectsService {
private sizeOfDescription: number = 270;
public parsingFunctions: ParsingFunctions = new ParsingFunctions();
constructor(private http: HttpClient ) {}
searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
let link = properties.searchAPIURLLAst+"projects";
let url = link+"?";
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page-1)+"&size="+size + "&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
//.map(res => <any> res.json())
.pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
}
getProjectsforDataProvider (datasourceId: string, page: number, size: number, properties:EnvProperties ):any {
let url = properties.searchResourcesAPIURL;
var basicQuery = "(oaftype exact project) ";
var filterQuery = "&fq=(projectcode<>\"unidentified\")";
url += "?query=";
if(datasourceId!= null && datasourceId != '' ) {
url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\" or resulthostingdatasourceid exact \""+ datasourceId + "\")";
}else{
url +=" ( "+basicQuery+ " ) ";
}
url += filterQuery;
url += "&page="+(page-1)+"&size="+size;
url += "&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
//.map(res => <any> res.json())
.pipe(map(res => [res['meta'].total, this.parseResults(res['results'])]));
}
advancedSearchProjects (params: string, page: number, size: number, properties:EnvProperties, refineParams:string=null, refineFields:string[] =null, refineQuery:string = null, minRef: boolean = false, excludeUnidentfied: boolean = true):any {
// &type=projects
let url = properties.searchAPIURLLAst+"resources2/?format=json";
// var basicQuery = "(oaftype exact project) "
var basicQuery = excludeUnidentfied ? "&fq=(projectcode<>\"unidentified\")" : "";
// url += "?query=";
if(params!= null && params != '' ) {
url +="&query=(" + params + ")";
}
// else{
// url +=" ( "+basicQuery+ " ) ";
// }
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
if(refineQuery) {
url += "&" + refineQuery;
}
// url += "&fq="+basicQuery;
url += basicQuery;
url += "&page="+(page-1)+"&size="+size;
url += minRef ? "&minRef=true" : "";
// url += "&format=json";
// return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
return this.http.get((properties.useLongCache && size == 0 && !params && (!refineQuery || !refineQuery.includes("fq="))) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
//.map(res => <any> res.json())
.pipe(map(res => [res['meta'].total, this.parseResults(res['results']), RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
}
getProjectsForOrganizations (organizationId: string, filterquery: string, page: number, size: number, refineFields:string[] , properties:EnvProperties ):any {
let url = properties.searchResourcesAPIURL;
var basicQuery = "(oaftype exact project) "
url += "?query=";
// if(filterquery!= null && filterquery != '' ) {
// url +="( ( "+basicQuery+ " ) and (relorganizationid exact \"" + organizationId + "\")"+" " + filterquery + ")";
// }else{
url +=" (( "+basicQuery+ " ) " +" and (relorganizationid exact \"" + organizationId + "\"))";
// }
if(filterquery) {
url += "&fq=" + filterquery;
}
url += "&fq=(projectcode<>\"unidentified\")";
if(refineFields!= null && refineFields.length > 0 ) {
url +="&refine=true";
for(let i=0; i< refineFields.length ; i++ ){
url +="&fields="+refineFields[i];
}
}
url += "&page="+(page-1)+"&size="+size;
url += "&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
//.map(res => <any> res.json())
.pipe(map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]));
}
getFunders(params: string, properties:EnvProperties ):any {
let url = properties.searchAPIURLLAst+"projects?refine=true&fields=funder"+(params ? "&"+params : "")+"&size=0"+ "&format=json";
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'], ['funder'], 'project')]));
}
searchForProjectsObs(keyword:string, funderId:string, properties:EnvProperties ):any {
let url = 'search?action=search&sTransformer=projects_openaire&query='+
'%28oaftype+exact+project%29+and+%28%28projecttitle+%3D+%22'+keyword+'%22%29+or+%28projectacronym+%3D+%22'+keyword+'%22%29+or+%28projectcode+%3D+%22'+keyword+'%22%29%29+and+%28funder+exact+'+funderId+'%29&size=10&locale=en_GB&format=json';
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).toPromise()
.then(request =>{
//return (request.json().response.results)?request.json().response.results.result:request.json().response.result;
return (request['response'].results)?request['response'].results.result:request['response'].result;
}) ;
}
parseResults(data: any): SearchResult[] {
let results: SearchResult[] = [];
let length = Array.isArray(data) ? data.length : 1;
for (let i = 0; i < length; i++) {
let resBody = Array.isArray(data) ? data[i] : data;
var result: SearchResult = new SearchResult();
if (resBody["header"]) {
result.id = resBody['header']['id'];
}
if (resBody["project"]) {
let resData = resBody['project'];
result.openAccessMandatePublications = resData['oamandatepublications'];
if (resData["oamandatedata"]) {
result.openAccessMandateDatasets = resData['oamandatedata'];
} else {
result.openAccessMandateDatasets = resData['ecarticle29_3'];
}
result['title'] = {"name": '', "accessMode": ''};
// if(resData['acronym'] != undefined && resData['acronym'] != "") {
// result['title'].name = resData['acronym'] + " - ";
// }
if (resData['title']) {
if (Array.isArray(resData['title'])) {
result['title'].name += StringUtils.HTMLToString(String(resData['title'][0]));
} else {
result['title'].name += StringUtils.HTMLToString(String(resData['title']));
}
} else {
result['title'].name = "";
}
// if(result['title'].name != '') {
// result['title'].name += " ("+resData['code']+")"
// }
// if(resData['ecsc39'] == true) {
// result['title'].sc39 = "sc39";
// }
result.acronym = resData['acronym'];
result.code = resData['code'];
result.budget = resData.totalcost;//"10000";
result.contribution = resData.fundedamount;//"200100";
result.currency = resData.currency;//"EUR";
let abstracts = this.parsingFunctions.parseDescription(resData.summary, true);
result.description = abstracts;
// if (result.description && result.description.length > this.sizeOfDescription) {
// result.description = result.description.substring(0, this.sizeOfDescription) + "...";
// }
if (resData["funding"]) {
let fundingLength = Array.isArray(resData['funding']) ? resData['funding'].length : 1;
for (let z = 0; z < fundingLength; z++) {
let fundingData = Array.isArray(resData['funding']) ? resData['funding'][z] : resData['funding'];
if (fundingData["funder"]) {
result['funderShortname'] = fundingData['funder'].shortname;
result['funderName'] = fundingData['funder'].name;
result['funderId'] = fundingData['funder'].id;
result['jurisdiction'] = fundingData['funder']['jurisdiction']?.code;
if (fundingData['level2']) {
result['fundingLevel0'] = (fundingData['level2'] && fundingData['level2']['parent'] &&
fundingData['level2']['parent']['level1'] && fundingData['level2']['parent']['level1']['parent']
&& fundingData['level2']['parent']['level1']['parent']['level0']) ?
fundingData['level2']['parent']['level1']['parent']['level0']['name'] : "";
} else if (fundingData['level1']) {
result['level0'] = (fundingData['level1'] && fundingData['level1']['parent'] && fundingData['level1']['parent']['level0']) ?
fundingData['level1']['parent']['level0']['name'] : "";
} else if (fundingData['level0']) {
result['fundingLevel0'] = (fundingData['level0']) ? fundingData['level0']['name'] : "";
} else {
result['fundingLevel0'] = "";
}
break;
}
}
}
if (resData["startdate"]) {
result.startYear = resData.startdate.split('-')[0];
}
if (resData['enddate']) {
result.endYear = resData.enddate.split('-')[0];
}
// Measure
result.measure = this.parsingFunctions.parseMeasures(resData['measures']);
}
if (resBody["links"]) {
let relLength = Array.isArray(resBody['links']) ? resBody['links'].length : 1;
for (let j = 0; j < relLength; j++) {
let relation = Array.isArray(resBody['links']) ? resBody['links'][j] : resBody['links'];
if (relation["header"]) {
if (relation['header'].relationClass && relation['header'].relationClass.toLowerCase() == "hasparticipant") {
if (result['organizations'] == undefined) {
result['organizations'] = new Array<
{ "name": string, "id": string }>();
}
let countOrganizations = result['organizations'].length;
result['organizations'][countOrganizations] = {"name": "", "id": ""}
result['organizations'][countOrganizations]['id'] =
/*OpenaireProperties.getsearchLinkToOrganization() + */relation['header'].relatedIdentifier;
if (relation.legalshortname) {
result['organizations'][countOrganizations]['name'] = relation.legalshortname;
} else {
result['organizations'][countOrganizations]['name'] = relation.legalname;
}
if (!result['organizations'][countOrganizations]['name']) {
result['organizations'][countOrganizations]['name'] = "[no title available]";
}
}
}
}
}
results.push(result);
}
return results;
}
numOfProjects(url: string, properties:EnvProperties ):any {
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
//.map(res => <any> res.json())
.pipe(map(res => res['total']));
}
numOfEntityProjects(id: string, entity: string, properties:EnvProperties ):any {
//let url = properties.searchAPIURLLAst+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
var parameters: string = "";
if(entity == "organization") {
parameters = "organizations/"+id+"/projects/count";
}
let url = properties.searchAPIURLLAst+parameters+ "?format=json";
return this.numOfProjects(url, properties);
}
numOfSearchProjects(params: string, properties:EnvProperties, refineParams:string=null ):any {
let url = properties.searchAPIURLLAst+"projects/count?format=json";
if(params != "") {
url += "&q=" + StringUtils.URIEncode(params);
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
return this.numOfProjects(url, properties);
}
numOfSearchProjects2(params: string, properties:EnvProperties, refineParams:string=null ):any {
let url = properties.searchAPIURLLAst+"resources2/?format=json&size=0&type=projects";
if(params != "") {
url += "&query=" + params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&fq=(projectcode<>\"unidentified\")";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.pipe(map(res => res['meta']['total']));
}
}