openaire-library/services/searchPublications.service.ts

461 lines
19 KiB
TypeScript
Raw Normal View History

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 {SearchResult} from '../utils/entities/searchResult';
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
import { ParsingFunctions } from '../landingPages/landing-utils/parsingFunctions.class';
import{EnvProperties} from '../utils/properties/env-properties';
@Injectable()
export class SearchPublicationsService {
private sizeOfDescription: number = 270;
public parsingFunctions: ParsingFunctions = new ParsingFunctions();
constructor(private http: Http ) {}
searchPublications (params: string, refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties):any {
let link = properties.searchAPIURLLAst+"publications";
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())
// .do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
}
searchPublicationById (id: string, properties:EnvProperties ):any {
let url = properties.searchAPIURLLAst+"publications/"+id+"?format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => this.parseResults(res));
}
searchAggregators (id: string, params: string, refineParams:string, page: number, size: number, properties:EnvProperties ):any {
let link = properties.searchAPIURLLAst+"publications";
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)
.map(res => <any> res.json())
.map(res => this.parseRefineResults(id, res['refineResults']));
}
searchPublicationsByDois (DOIs: string[], refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
let link = properties.searchAPIURLLAst+"publications";
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)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "publication")]);
}
advancedSearchPublications (params: string, page: number, size: number, properties:EnvProperties ):any {
let url = properties.searchResourcesAPIURL;
var basicQuery = "(oaftype exact result) and (resulttypeid exact publication) ";
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((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
searchPublicationsForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
let link = properties.searchAPIURLLAst;
let url = link+params+"/publications"+ "?format=json";
url += "&page="+(page-1)+"&size="+size;
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
searchPublicationsForDataproviders(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)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
/*
searchPublicationsCSV (params: string, refineParams:string, page: number, size: number):any {
let link = properties.searchAPIURLLAst+"publications";
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())
//.do(res => console.info(res))
.map(res => this.parseResultsCSV(res['results']));
}
*/
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:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
var result: SearchResult = new SearchResult();
result.entityType = "publication";
if(resData['language'] && resData['language'] != null) {
result.languages = new Array<string>();
result.types = new Array<string>();
let types = new Set<string>();
let counter = 0;
let instance;
let length = Array.isArray(resData['children']['instance']) ? resData['children']['instance'].length : 1;
for(let i=0; i<length; i++) {
instance = Array.isArray(resData['children']['instance']) ? resData['children']['instance'][i] : resData['children']['instance'];
this.parsingFunctions.parseTypes(result.types, types, instance);
}
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<resData['language'].length; i++) {
if(resData['language'][i].classname != "Undetermined" && resData['language'][i].classname) {
result.languages.push(resData['language'][i].classname);
}
}
}
}
result['title'] = {"name": '', "accessMode": '', "sc39": ''};
if(Array.isArray(resData['title'])) {
// resData['title'][0].hasOwnProperty("content") {
result['title'].name = resData['title'][0].content;
// }
} else {
// resData['title'].hasOwnProperty("content") {
result['title'].name = resData['title'].content;
// }
}
//result['title'].url = OpenaireProperties.getsearchLinkToPublication();
//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['bestaccessright'].hasOwnProperty("classid")) {
result['title'].accessMode = resData['bestaccessright'].classid;
}
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 == "hasAuthor") {
if(result['authors'] == undefined) {
result['authors'] = new Array<{"name": string, "id": string}>();
}
result['authors'].push({"name": relation.fullname, "id": relation['to'].content});
} else */if(relation['to'].class == "isProducedBy") {
result['projects'] = this.parseProjects(result['projects'], relation);
}
}
}
}
if(resData.hasOwnProperty("creator") && resData['creator'] != null) {
if(result['authors'] == undefined) {
result['authors'] = new Array<string>();
}
let authors = resData['creator'];
let length = Array.isArray(authors) ? authors.length : 1;
for(let i=0; i<length; i++) {
let author = Array.isArray(authors) ? authors[i] : authors;
result.authors[author.rank-1] = author.content;
}
result.authors = result.authors.filter(function (item) {
return (item != undefined);
});
}
var date:string = (resData.dateofacceptance)+""; // transform to string in case it is an integer
result.year = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
if(!Array.isArray(resData.description)) {
result.description = resData.description;
} else {
result.description = resData.description[0];
}
if(result.description.length > this.sizeOfDescription) {
result.description = result.description.substring(0, this.sizeOfDescription) + "...";
}
result.embargoEndDate = resData.embargoenddate;
results.push(result);
}
return results;
}
parseProjects(projects: { "id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"code": string }[], relation: any ) : {
"id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"code": string }[] {
if(projects == undefined) {
projects = new Array<
{ "id": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"code": string
}>();
}
let countProjects = projects.length;
projects[countProjects] = {
"id": "", "acronym": "", "title": "",
"funderShortname": "", "funderName": "",
"code": ""
}
if(relation.title != 'unidentified') {
projects[countProjects]['id'] =
/*OpenaireProperties.getsearchLinkToProject() + */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<fundingLength; z++) {
let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
if(fundingData.hasOwnProperty("funder")) {
projects[countProjects]['funderShortname'] = fundingData['funder'].shortname;
projects[countProjects]['funderName'] = fundingData['funder'].name;
}
}
}
return projects;
}
/*
parseResultsCSV(data: any): any {
let results: any = [];
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:result'] : data['result']['metadata']['oaf:entity']['oaf:result'];
var result: any = [];
if(Array.isArray(resData['title'])) {
result.push(this.quote(resData['title'][0].content));
} else {
result.push(this.quote(resData['title'].content));
}
var authors: string[] = [];
var projects: string[] = [];
var funder: string = "";
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 == "hasAuthor") {
authors.push(relation.fullname);
} else if(relation['to'].class == "isProducedBy") {
if(relation.code != "") {
projects.push(relation.title+" ("+relation.code+")");
} else {
projects.push(relation.title);
}
if(relation.hasOwnProperty("funding")) {
let fundingLength = Array.isArray(relation['funding']) ? relation['funding'].length : 1;
for(let z=0; z<fundingLength; z++) {
let fundingData = Array.isArray(relation['funding']) ? relation['funding'][z] : relation['funding'];
if(fundingData.hasOwnProperty("funder")) {
funder = fundingData['funder'].shortname;
}
}
}
}
}
}
result.push(this.quote(authors));
var year: string = "";
if(resData.hasOwnProperty("dateofacceptance")) {
year = resData.dateofacceptance;
}
result.push(year);
var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
result.push(this.quote(id));
result.push(funder);
result.push(this.quote(projects));
} else {
result.push(this.quote(''));
var year: string = "";
if(resData.hasOwnProperty("dateofacceptance")) {
year = resData.dateofacceptance;
}
result.push(year);
var id:string = Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
result.push(this.quote(id));
result.push(funder);
result.push("");
}
//result.push(resData.embargoenddate);
if(resData['bestaccessright'].hasOwnProperty("classid")) {
result.push(this.quote(resData['bestaccessright'].classid));
} else {
result.push("");
}
results.push(result);
}
return results;
}
*/
parseRefineResults(id: string, data: any): any {
var results:any = [];
if(data.hasOwnProperty("resulthostingdatasource")) {
let length = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'].length : 1;
for(let i=0; i<length; i++) {
let datasource = Array.isArray(data['resulthostingdatasource']) ? data['resulthostingdatasource'][i] : data['resulthostingdatasource'];
let result: {"name": string, "id": string, "count": number} = {"name": "", "id": "", "count": 0};
result['name'] = datasource.name;
result['id'] = datasource.id.split("||")[0];
//result['url'] = OpenaireProperties.getsearchLinkToDataProvider()+result['id'];
result['count'] = datasource.count;
if(result['id'] != id && result['name'] != "Unknown Repository") {
results.push(result);
}
}
}
return results;
}
numOfPublications(url: string, properties:EnvProperties): any {
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res.total);
}
numOfEntityPublications(id: string, entity: string, properties:EnvProperties):any {
var parameters: string = "";
if(entity == "project") {
parameters = "projects/"+id+"/publications/count";
}
let url = properties.searchAPIURLLAst+parameters+"?format=json";
return this.numOfPublications(url, properties);
}
numOfSearchPublications(params: string, properties:EnvProperties):any {
let url = properties.searchAPIURLLAst+"publications/count?format=json";
if(params != "") {
url += "&q=" + params;
}
return this.numOfPublications(url, properties);
}
/*
private quote(word: any): string {
return '"'+word+'"';
}
*/
}