openaire-library/landingPages/result/resultLanding.service.ts

466 lines
20 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {throwError} from 'rxjs';
import {ResultLandingInfo} from '../../utils/entities/resultLandingInfo';
import {EnvProperties} from '../../utils/properties/env-properties';
import {ParsingFunctions} from '../landing-utils/parsingFunctions.class';
import {map} from "rxjs/operators";
import {HostedByCollectedFrom, Organization} from "../../utils/result-preview/result-preview";
import {Dates, Identifier} from "../../utils/string-utils.class";
import {properties} from "../../../../environments/environment";
@Injectable()
export class ResultLandingService {
constructor(private http: HttpClient ) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
resultLandingInfo: ResultLandingInfo;
private buildResultLandingInfoUrl(id: string, identifier: Identifier, type: string): string {
if (id) {
let url = properties.searchAPIURLLAst;
if (type === 'publication') {
url += 'publications/';
} else if (type === 'dataset') {
url += 'datasets/';
} else if (type === 'software') {
url += 'software/';
} else if (type === 'orp') {
url += 'other/';
} else if (type === 'result') {
url += 'results/';
}
url += id + '?format=json';
return url;
} else if (identifier) {
// pid = "10.3389/fphys.2014.00466";
let url = properties.searchAPIURLLAst + "resources2";
url += "?pid="+identifier.id + "&pidtype=" + identifier.class + "&type=";
if (type === 'publication') {
url += 'publications';
} else if (type === 'dataset') {
url += 'datasets';
} else if (type === 'software') {
url += 'software';
} else if (type === 'orp') {
url += 'other';
} else if (type === 'result') {
url += 'results';
}
url += "&format=json";
return url;
}
}
getResultLandingInfo (id: string, identifier: Identifier, type: string, provenanceActionVocabulary: any, properties: EnvProperties): any {
let url: string = this.buildResultLandingInfoUrl(id, identifier, type);
let finalUrl: string = (properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url;
return this.http.get(finalUrl)
.pipe(map(res => {
if(!id && identifier) {
if(!res['results'] || res['results'].length == 0) {
throw new HttpErrorResponse({
status: 404,
statusText: "Not found",
url: finalUrl,
error: "Http failure response for "+finalUrl+": 404 Not Found"
});
}
return res['results'][0];
} else {
return res;
}
}))
.pipe(map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity'], res]))
.pipe(map(res => [
res[1]['oaf:result'], // 0
res[1]['oaf:result']['title'], // 1
res[1]['oaf:result']['rels']['rel'], // 2
res[1]['oaf:result']['children'], // 3
res[1]['oaf:result']['pid'], // 4
res[1]['oaf:result']['journal'], // 5
res[1]['oaf:result']['language'], // 6
res[1]['oaf:result']['subject'], // 7
res[1]['oaf:result']['context'], // 8
res[1]['oaf:result']['creator'], // 9
res[1]['oaf:result']['country'] , // 10
res[1]['oaf:result']['programmingLanguage'], // 11 - software
//res[1]['oaf:result']['resulttype'],
(res[1]['extraInfo'] !== undefined && res[1]['extraInfo']['citations'] !== undefined)
? res[1]['extraInfo']['citations']['citation'] : null, // 12
res[0], // 13
res[2] // 14
]))
.pipe(map(res => this.parseResultLandingInfo(res, provenanceActionVocabulary, properties)));
}
// getProvenanceActionVocabulary (properties: EnvProperties): any {
// let url = properties.vocabulariesAPI+"dnet:provenanceActions.json";
//
// return this.http.get((properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
// .pipe(map(res => res['terms']))
// .pipe(map(res => this.parseProvenanceActionVocabulary(res, properties)));
// }
//
// parseProvenanceActionVocabulary(terms: any, properties: EnvProperties) {
// var provenanceActionVocabulary: {} = {};
// for(let term of terms) {
// provenanceActionVocabulary[term.code] = term.englishName;
// }
// return provenanceActionVocabulary;
// }
private handleError (error: HttpErrorResponse) {
// in a real world app, we may send the error to some remote logging infrastructure
// instead of just logging it to the console
console.log(error);
return throwError(error || 'Server error');
}
parseResultLandingInfo (data: any, provenanceActionVocabulary: any, properties: EnvProperties): any {
this.resultLandingInfo = new ResultLandingInfo();
// res
this.resultLandingInfo.record = data[14];
this.resultLandingInfo.relcanId = ParsingFunctions.parseRelCanonicalId(this.resultLandingInfo.record, "result");
this.resultLandingInfo.resultType = data[0].resulttype.classid;
// res['result']['metadata']['oaf:entity']['oaf:result']
if (data[0] != null) {
let date: string = (data[0].dateofacceptance ? data[0].dateofacceptance : '') + ''; // transform to string in case it is an integer
this.resultLandingInfo.date = (date && (date).indexOf('-') !== -1) ? date.split('-')[0] : date;
this.resultLandingInfo.dateofacceptance = data[0].dateofacceptance?Dates.getDate(data[0].dateofacceptance):null;
this.resultLandingInfo.publisher = data[0].publisher;
if(!Array.isArray(data[0].description)) {
//this.resultLandingInfo.description = String(data[0].description);
this.resultLandingInfo.description = (data[0] && data[0].description) ? String(data[0].description) : "";
} else {
//this.resultLandingInfo.description = String(data[0].description[0]);
this.resultLandingInfo.description = (data[0] && data[0].description[0]) ? String(data[0].description[0]) : "";
}
this.resultLandingInfo.embargoEndDate = data[0].embargoenddate?Dates.getDate(data[0].embargoenddate):null;
}
if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classname")) {
this.resultLandingInfo.accessMode = data[0]['bestaccessright'].classname;
}
// res['result']['metadata']['oaf:entity']['oaf:result']['title']
if(data[1] != null) {
if(Array.isArray(data[1])) {
this.resultLandingInfo.title = (data[1][0] && data[1][0].content) ? String(data[1][0].content) : "";
if(data[1][1].classid === 'subtitle') {
this.resultLandingInfo.subtitle = (data[1][1] && data[1][1].content) ? String(data[1][1].content) : "";
}
} else {
this.resultLandingInfo.title = (data[1] && data[1].content) ? String(data[1].content) : "";
}
}
// res['result']['metadata']['oaf:entity']['oaf:result']['rels']['rel']
if(data[2] != null) {
let relation;
let length = Array.isArray(data[2]) ? data[2].length : 1;
for(let i=0; i<length; i++) {
relation = Array.isArray(data[2]) ? data[2][i] : data[2];
if(relation.hasOwnProperty("to")) {
if(relation['to'].class == "isProducedBy") {
this.resultLandingInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.resultLandingInfo.fundedByProjects, relation, provenanceActionVocabulary);
} else if(relation['to'].class == "isRelatedTo") {
let provenanceAction: string = "";
if(provenanceActionVocabulary != null && relation.provenanceaction in provenanceActionVocabulary) {
provenanceAction = provenanceActionVocabulary[relation.provenanceaction];
}
this.resultLandingInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.resultLandingInfo.relatedResearchResults, relation, provenanceAction);
} else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
this.resultLandingInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.resultLandingInfo.similarResearchResults, relation);
} else if(relation['to'].class == "hasAuthorInstitution") {
this.resultLandingInfo.organizations = this.parseRelatedOrganizations(this.resultLandingInfo.organizations, relation);
} else if(relation['to'].class == "isSupplementedBy") {
this.resultLandingInfo.supplementaryResearchResults = this.parsingFunctions.parseSupplementaryResearchResults(this.resultLandingInfo.supplementaryResearchResults, relation);
} else if(relation['to'].class == "isSupplementTo") {
this.resultLandingInfo.supplementedByResearchResults = this.parsingFunctions.parseSupplementedByResearchResults(this.resultLandingInfo.supplementedByResearchResults, relation);
}
}
}
}
// res['result']['metadata']['oaf:entity']['oaf:result']['children']
if(data[3] != null) {
if(data[3].hasOwnProperty("result")) {
this.resultLandingInfo.deletedByInferenceIds = [];
let length = Array.isArray(data[3]['result']) ? data[3]['result'].length : 1;
for(let i=0; i<length; i++) {
let result = Array.isArray(data[3]['result']) ? data[3]['result'][i] : data[3]['result'];
this.resultLandingInfo.deletedByInferenceIds.push(result.objidentifier);
}
}
if(data[3].hasOwnProperty("instance")) {
this.resultLandingInfo.hostedBy_collectedFrom = new Array<HostedByCollectedFrom>();
this.resultLandingInfo.types = new Array<string>();
let types = new Set<string>();
let counter = 0;
let instance;
let length = Array.isArray(data[3]['instance']) ? data[3]['instance'].length : 1;
for(let i=0; i<length; i++) {
instance = Array.isArray(data[3]['instance']) ? data[3]['instance'][i] : data[3]['instance'];
this.parsingFunctions.parseTypes(this.resultLandingInfo.types, types, instance);
if(instance.hasOwnProperty("webresource")) {
let url;
if(!Array.isArray(instance['webresource'])) {
url = instance['webresource'].url;
} else{
url = instance['webresource'][0].url;
}
/**********************************************************/
if(instance.hasOwnProperty("hostedby")) {
counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.resultLandingInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.resultLandingInfo.title*/, this.resultLandingInfo.accessMode);
}
/**********************************************************/
}
}
/* Order Download from via openness*/
this.resultLandingInfo.hostedBy_collectedFrom.sort((a, b) => {
if(a.bestAccessMode && a.bestAccessMode.toLowerCase() === 'open access') {
return -1;
} else if(b.bestAccessMode && b.bestAccessMode.toLowerCase() === 'open access') {
return 1;
} else if(!a.bestAccessMode || a.bestAccessMode.toLowerCase() !== 'not available') {
return 1;
} else if(!b.bestAccessMode || b.bestAccessMode.toLowerCase() !== 'not available') {
return -1;
} else {
return 0;
}
});
}
if(data[3].hasOwnProperty("externalreference")) {
let externalResults: Map<string, Map<string, string>> = this.parseBioentitiesAndSoftware(data[3]);
this.resultLandingInfo.bioentities = externalResults;
}
}
// res['result']['metadata']['oaf:entity']['oaf:result']['pid']
if(data[4] != null) {
this.resultLandingInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
}
// res['result']['metadata']['oaf:entity']['oaf:result']['journal']
if(data[5] != null) {
this.resultLandingInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": "", "issue": "", "volume": "", "start_page": "", "end_page": ""}
this.resultLandingInfo.journal['journal'] = data[5].content;
this.resultLandingInfo.journal['issn'] = data[5].issn;
this.resultLandingInfo.journal['lissn'] = data[5].lissn;
this.resultLandingInfo.journal['eissn'] = data[5].eissn;
this.resultLandingInfo.journal['issue'] = data[5].iss;
this.resultLandingInfo.journal['volume'] = data[5].vol;
this.resultLandingInfo.journal['start_page'] = data[5].sp;
this.resultLandingInfo.journal['end_page'] = data[5].ep;
}
// res['result']['metadata']['oaf:entity']['oaf:result']['language']
if(data[6] != null) {
this.resultLandingInfo.languages = this.parsingFunctions.parseLanguages(data[6]);
// let languagesAndCodes: string[][] = this.parsingFunctions.parseLanguages(data[6]);
// this.resultLandingInfo.languages = languagesAndCodes[0];
// this.resultLandingInfo.languageCodes = languagesAndCodes[1];
}
// res['result']['metadata']['oaf:entity']['oaf:result']['country']
if(data[10] != null) {
this.resultLandingInfo.countries = this.parsingFunctions.parseCountries(data[10]);
}
// res['result']['metadata']['oaf:entity']['oaf:result']['subject']
if(data[7] != null) {
let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[7]);
this.resultLandingInfo.subjects = subjectResults[0];
this.resultLandingInfo.otherSubjects = subjectResults[1];
this.resultLandingInfo.classifiedSubjects = subjectResults[2];
}
this.resultLandingInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
this.resultLandingInfo.hostedBy_collectedFrom, this.resultLandingInfo.publisher,
this.resultLandingInfo.journal, this.resultLandingInfo.identifiers);
// res['result']['metadata']['oaf:entity']['oaf:result']['programmingLanguage']
if(data[11] != null) {
this.resultLandingInfo.programmingLanguages = this.parsingFunctions.parseProgrammingLanguages(data[11]);
}
// res['result']['metadata']['oaf:entity']['extraInfo']['citations']['citation']
if(data[12] != null) {
this.resultLandingInfo.references = this.parsingFunctions.parseReferences(data[12]);
}
// res['result']['metadata']['oaf:entity']['oaf:result']['context']
if(data[8] != null) {
this.resultLandingInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
}
// res['result']['header']['dri:status']
if(data[13] != null && data[13] == "under curation") {
this.resultLandingInfo.underCurationMessage = true;
} else {
this.resultLandingInfo.underCurationMessage = false;
}
// res['result']['metadata']['oaf:entity']['oaf:result']['creator']
if(data[9] != null) {
if(this.resultLandingInfo.authors == undefined) {
this.resultLandingInfo.authors = new Array<{"fullName": string, "orcid": string}>();
}
let authors = data[9];
let length = Array.isArray(authors) ? authors.length : 1;
for(let i=0; i<length; i++) {
let author = Array.isArray(authors) ? authors[i] : authors;
if(author) {
this.resultLandingInfo['authors'][author.rank] = {"fullName": author.content, "orcid": author.orcid};
}
}
this.resultLandingInfo.authors = this.resultLandingInfo.authors.filter(function (item) {
return (item != undefined && item.fullName != undefined);
});
}
this.resultLandingInfo.relatedResearchResults = this.parsingFunctions.sortByPercentage(this.resultLandingInfo.relatedResearchResults);
this.resultLandingInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.resultLandingInfo.similarResearchResults);
this.resultLandingInfo.supplementaryResearchResults = this.parsingFunctions.sortByPercentage(this.resultLandingInfo.supplementaryResearchResults);
this.resultLandingInfo.supplementedByResearchResults = this.parsingFunctions.sortByPercentage(this.resultLandingInfo.supplementedByResearchResults);
return this.resultLandingInfo;
}
parseRelatedOrganizations(organizations: Organization[], relation: any): Organization[] {
if(organizations == undefined) {
organizations = []
}
let organization: { "name": string, "shortname": string,
"id": string, "websiteUrl": string,
"country": string, "trust": number
} = {
name: "", shortname: "",
id: "", websiteUrl: "",
country: "", trust: null
};
organization.id = relation['to'].content;
organization.name = relation.legalname;
organization.shortname = relation.legalshortname;
organization.websiteUrl = relation.websiteurl;
if(relation.country) {
organization.country = relation.country.classname;
}
if(relation.trust) {
organization.trust = Math.round(relation.trust*100);
}
organizations.push(organization);
return organizations;
}
parseBioentitiesAndSoftware(children: any) : Map<string, Map<string, string>> {
let bioentities: Map<string, Map<string, string>>;
let length = Array.isArray(children['externalreference']) ? children['externalreference'].length : 1;
let externalreference;
for(let i=0; i<length; i++) {
externalreference = Array.isArray(children['externalreference']) ? children['externalreference'][i] : children['externalreference'];
if(externalreference.hasOwnProperty("qualifier")) {
if(externalreference['qualifier'].classid == "accessionNumber") {
if(bioentities == undefined) {
bioentities = new Map<string, Map<string, string>>();
}
if(!bioentities.has(externalreference.sitename)) {
bioentities.set(externalreference.sitename, new Map<string, string>());
}
bioentities.get(externalreference.sitename).set(externalreference.refidentifier, externalreference.url);
}
}
}
return bioentities;
}
getOpenCitations(id: string, properties:EnvProperties) {
//https://services.openaire.eu/opencitations/getCitations?id=doajarticles::2634200c24772ee8f10232d3e184ec65
let url = properties.openCitationsAPIURL+id;// "https://services.openaire.eu/opencitations/getCitations?id=" + id;
let key = url;
//return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
return this.http.get(url)
.pipe(map(res => [res['total'], this.parseOpenCitations(res['results'])]));
}
parseOpenCitations(openCitations: any): {"url": string, "title": string, "year": string, "doi": string, "authors": string[]}[] {
//title, doi, authors
let citations = new Array<{"url": string, "title": string, "year": string, "doi": string, "authors": string[]}>();
let openCitation;
let length = Array.isArray(openCitations) ? openCitations.length : 1;
for(let i=0; i<length; i++) {
openCitation = Array.isArray(openCitations) ? openCitations[i] : openCitations;
let citation: {"url": string, "title": string, "year": string, "doi": string, "authors": string[]} = {"url": "", "title": "", "year": "", "doi": "", "authors": []};
if(openCitation && openCitation.id) {
citation.url = openCitation.id;
citation.title = openCitation.title;
//citation.type = openCitation.type;
citation.year = (openCitation.pubDate && (openCitation.pubDate).indexOf('-') !== -1)?openCitation.pubDate.split('-')[0]:openCitation.pubDate;
citation.doi = openCitation.doi;
let authorsLength = Array.isArray(openCitation.authors) ? openCitation.authors.length : 1;
for(let i=0; i<authorsLength; i++) {
let author = Array.isArray(openCitation.authors) ? openCitation.authors[i] : openCitation.authors;
if(author) {
let lastFirstName: string = "";
if(author.lastName) {
lastFirstName = author.lastName;
}
if(author.lastName && author.firstName) {
lastFirstName += ", ";
}
if(author.firstName) {
lastFirstName += author.firstName;
}
if(lastFirstName) {
citation.authors.push(lastFirstName);
}
}
}
citations.push(citation);
}
}
return citations;
}
}