openaire-library/services/searchProjects.service.ts

243 lines
11 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import { } from '../shared/cache.service';
import {OpenaireProperties} from '../utils/properties/openaireProperties';
import {SearchResult} from '../utils/entities/searchResult';
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
@Injectable()
export class SearchProjectsService {
private sizeOfDescription: number = 270;
constructor(private http: Http ) {}
searchProjects (params: string, refineParams:string, page: number, size: number, refineFields:string[] ):any {
console.info("In searchProjects");
let link = OpenaireProperties.getSearchAPIURLLast()+"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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]);
}
getProjectsforDataProvider (datasourceId: string, page: number, size: number ):any {
let url = OpenaireProperties.getSearchResourcesAPIURL();
var basicQuery = "(oaftype exact project) "
url += "?query=";
if(datasourceId!= null && datasourceId != '' ) {
url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
}else{
url +=" ( "+basicQuery+ " ) ";
}
url += "&page="+(page-1)+"&size="+size;
url += "&format=json";
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
advancedSearchProjects (params: string, page: number, size: number ):any {
let url = OpenaireProperties.getSearchResourcesAPIURL();
var basicQuery = "(oaftype exact project) "
url += "?query=";
if(params!= null && params != '' ) {
url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
}else{
url +=" ( "+basicQuery+ " ) ";
}
url += "&page="+(page-1)+"&size="+size;
url += "&format=json";
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
getProjectsForOrganizations (organizationId: string, filterquery: string, page: number, size: number, refineFields:string[] ):any {
let url = OpenaireProperties.getSearchResourcesAPIURL();
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(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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "project")]);
}
getFunders():any {
let url = OpenaireProperties.getSearchAPIURLLast()+"projects?refine=true&fields=funder&size=0"+ "&format=json";;
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, res['refineResults']['funder']]);
}
searchForProjectsObs(keyword:string, funderId:string):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((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url).toPromise()
.then(request =>{
return (request.json().response.results)?request.json().response.results.result:request.json().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 resData = Array.isArray(data) ? data[i]['result']['metadata']['oaf:entity']['oaf:project'] : data['result']['metadata']['oaf:entity']['oaf:project'];
var result: SearchResult = new SearchResult();
result.openAccessMandate = (resData['oamandatepublications'])?true:false;
result['title'] = {"name": '', "accessMode": '', "sc39": ''};
if(resData['acronym'] != undefined && resData['acronym'] != "") {
result['title'].name = resData['acronym'] + " - ";
}
if(Array.isArray(resData['title'])) {
// resData['title'][0].hasOwnProperty("content") {
result['title'].name += resData['title'][0];
// }
} else {
// resData['title'].hasOwnProperty("content") {
result['title'].name += resData['title'];
// }
}
if(result['title'].name != '') {
result['title'].name += " ("+resData['code']+")"
}
if(resData['ecsc39'] == true) {
result['title'].sc39 = "sc39";
}
// if(resData['title'].hasOwnProperty("content")) {
// result['title'].name = resData['title'].content;
// }
//result['title'].url = OpenaireProperties.getsearchLinkToProject();
//result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
result['id'] = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
if(resData['rels'].hasOwnProperty("rel")) {
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
for(let j=0; j<relLength; j++) {
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][j] : resData['rels']['rel'];
if(relation.hasOwnProperty("to")) {
if(relation['to'].class == "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['to'].content;
if(relation.legalshortname) {
result['organizations'][countOrganizations]['name'] = relation.legalshortname;
} else {
result['organizations'][countOrganizations]['name'] = relation.legalname;
}
}
}
}
}
if(resData.hasOwnProperty("fundingtree")) {
if(result['funders'] == undefined) {
result['funders'] = new Array<
{"funderShortname": string, "funderName": string}>();
}
let fundingLength = Array.isArray(resData['fundingtree']) ? resData['fundingtree'].length : 1;
for(let z=0; z<fundingLength; z++) {
let fundingData = Array.isArray(resData['fundingtree']) ? resData['fundingtree'][z] : resData['fundingtree'];
let countFunders = result['funders'].length;
if(fundingData.hasOwnProperty("funder")) {
result['funders'][countFunders] = {"funderShortname": "", "funderName": ""};
result['funders'][countFunders]['funderShortname'] = fundingData['funder'].shortname;
result['funders'][countFunders]['funderName'] = fundingData['funder'].name;
}
}
}
if(resData.hasOwnProperty("startdate")) {
result.startYear = resData.startdate.split('-')[0];
}
if(resData.hasOwnProperty("enddate")) {
result.endYear = resData.enddate.split('-')[0];
}
results.push(result);
}
return results;
}
numOfProjects(url: string):any {
return this.http.get((OpenaireProperties.isCacheEnabled())? (OpenaireProperties.getCacheUrl()+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res.total);
}
numOfEntityProjects(id: string, entity: string):any {
//let url = OpenaireProperties.getSearchAPIURLLast()+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
var parameters: string = "";
if(entity == "organization") {
parameters = "organizations/"+id+"/projects/count";
}
let url = OpenaireProperties.getSearchAPIURLLast()+parameters+ "?format=json";
return this.numOfProjects(url);
}
numOfSearchProjects(params: string):any {
let url = OpenaireProperties.getSearchAPIURLLast()+"projects/count?format=json";
if(params != "") {
url += "&q=" + params;
}
return this.numOfProjects(url);
}
}