openaire-library/landingPages/publication/publication.service.ts

431 lines
18 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {PublicationInfo} from '../../utils/entities/publicationInfo';
import {DeletedByInferenceResult} from '../../utils/entities/deletedByInferenceResult';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import 'rxjs/add/operator/map';
import{EnvProperties} from '../../utils/properties/env-properties';
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
@Injectable()
export class PublicationService {
constructor(private http: Http ) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
publicationInfo: PublicationInfo;
getPublicationInfo (id: string, properties:EnvProperties):any {
let url = properties.searchAPIURLLAst + 'publications/' +id+"?format=json";
let key = url;
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity'], res])
.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']['bestaccessright'], //8
res[1]['oaf:result']['collectedfrom'], //9
(res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null, //10
res[1]['oaf:result']['context'], //11
res[0], //12
res[1]['oaf:result']['creator'], //13
res[2], //14
res[1]['oaf:result']['country'] //15
])
.map(res => this.parsePublicationInfo(res, properties));
}
private handleError (error: Response) {
// 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 Observable.throw(error || 'Server error');
}
parsePublicationInfo (data: any, properties: EnvProperties):any {
this.publicationInfo = new PublicationInfo();
this.publicationInfo.record = data[14];
if(data[0] != null) {
var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
this.publicationInfo.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
this.publicationInfo.dateofacceptance = data[0].dateofacceptance;
this.publicationInfo.publisher = data[0].publisher;
if(!Array.isArray(data[0].description)) {
this.publicationInfo.description = String(data[0].description);
} else {
this.publicationInfo.description = String(data[0].description[0]);
}
this.publicationInfo.embargoEndDate = data[0].embargoenddate;
}
/*
this.publicationInfo.title = {"name": "", "url": "", "accessMode": ""};
if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
this.publicationInfo.title.accessMode = data[0]['bestaccessright'].classid;
}
if(data[1] != null) {
if(Array.isArray(data[1])) {
this.publicationInfo.title['name'] = data[1][0].content;
} else {
this.publicationInfo.title['name'] = data[1].content;
}
}
*/
if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
this.publicationInfo.accessMode = data[0]['bestaccessright'].classid;
}
if(data[1] != null) {
if(Array.isArray(data[1])) {
this.publicationInfo.title = data[1][0].content;
} else {
this.publicationInfo.title = data[1].content;
}
}
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.publicationInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.publicationInfo.fundedByProjects, relation, this.publicationInfo.projectsProvenanceVocabulary);
} else if(relation['to'].class == "isRelatedTo") {
let provenanceAction: string;
if(relation.provenanceaction in this.publicationInfo.researchResultsProvenanceVocabulary) {
provenanceAction = this.publicationInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
} else {
provenanceAction = "Other";
}
this.publicationInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.publicationInfo.relatedResearchResults, relation, provenanceAction);
} else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
this.publicationInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.publicationInfo.similarResearchResults, relation);
} else if(relation['to'].class == "hasAuthorInstitution") {
this.publicationInfo.organizations = this.parseRelatedOrganizations(this.publicationInfo.organizations, relation);
}
}
}
}
if(data[3] != null) {
if(data[3].hasOwnProperty("result")) {
this.publicationInfo.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.publicationInfo.deletedByInferenceIds.push(result.objidentifier);
}
}
if(data[3].hasOwnProperty("instance")) {
//this.publicationInfo.collectedFrom = new Array<{"name": string, "id": string}>();
//this.publicationInfo.downloadFrom = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
//this.publicationInfo.publishedIn = new Map<string, {"url": string[], "accessMode": string[], "bestAccessMode": string}>();
this.publicationInfo.hostedBy_collectedFrom = new Array<{"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}>();
this.publicationInfo.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.publicationInfo.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.publicationInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.publicationInfo.title*/, this.publicationInfo.accessMode);
}
/**********************************************************/
}
}
}
if(data[3].hasOwnProperty("externalreference")) {
let externalResults: [Map<string, Map<string, string>>, { "name": string, "url": string}[]] = this.parseBioentitiesAndSoftware(data[3]);
this.publicationInfo.bioentities = externalResults[0];
this.publicationInfo.software = externalResults[1];
}
}
if(data[4] != null) {
this.publicationInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
}
if(data[5] != null) {
this.publicationInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": "", "issue": "", "volume": "", "start_page": "", "end_page": ""}
this.publicationInfo.journal['journal'] = data[5].content;
this.publicationInfo.journal['issn'] = data[5].issn;
this.publicationInfo.journal['lissn'] = data[5].lissn;
this.publicationInfo.journal['eissn'] = data[5].eissn;
this.publicationInfo.journal['issue'] = data[5].iss;
this.publicationInfo.journal['volume'] = data[5].vol;
this.publicationInfo.journal['start_page'] = data[5].sp;
this.publicationInfo.journal['end_page'] = data[5].ep;
}
if(data[6] != null) {
this.publicationInfo.languages = this.parsingFunctions.parseLanguages(data[6]);
}
if(data[15] != null) {
this.publicationInfo.countries = this.parsingFunctions.parseCountries(data[15]);
}
if(data[7] != null) {
let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[7]);
this.publicationInfo.subjects = subjectResults[0];
this.publicationInfo.otherSubjects = subjectResults[1];
this.publicationInfo.classifiedSubjects = subjectResults[2];
}
// if(data[8] != null) {
// this.publicationInfo.bestaccessright = data[8].classid;
// }
// if(data[9] != null) {
// this.publicationInfo.collectedFrom = this.parsingFunctions.parseCollectedFrom(data[9]);
// }
this.publicationInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
this.publicationInfo.hostedBy_collectedFrom, this.publicationInfo.publisher,
this.publicationInfo.journal, this.publicationInfo.identifiers/*,
this.publicationInfo.title*/);
if(data[10] != null) {
this.publicationInfo.references = this.parsingFunctions.parseReferences(data[10]);
}
if(data[11] != null) {
this.publicationInfo.contexts = this.parsingFunctions.parseContexts(data[11]);
}
if(data[12] != null && data[12] == "under curation") {
this.publicationInfo.underCurationMessage = true;
} else {
this.publicationInfo.underCurationMessage = false;
}
if(data[13] != null) {
if(this.publicationInfo.authors == undefined) {
this.publicationInfo.authors = new Array<{"fullName": string, "orcid": string}>();
}
let authors = data[13];
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) {
if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
author.ORCID = author.ORCID.substr(properties.orcidURL.length);
}
this.publicationInfo['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
}
}
this.publicationInfo.authors = this.publicationInfo.authors.filter(function (item) {
return (item != undefined && item.fullName != undefined);
});
}
if(this.publicationInfo.relatedResearchResults) {
let self = this;
this.publicationInfo.relatedResearchResults.forEach(function (value, key, map) {
self.publicationInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
});
}
this.publicationInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.publicationInfo.similarResearchResults);
return this.publicationInfo;
}
parseRelatedOrganizations(organizations: {"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[], relation: any):
{"name": string, "shortname":string, "id": string, "websiteUrl": string, "country": string, "trust": number}[] {
if(organizations == undefined) {
organizations = new Array<{
"name": string, "shortname": string,
"id": string, "websiteUrl": string,
"country": string, "trust": number}>();
}
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>>, { "name": string, "url": string}[]] {
let bioentities: Map<string, Map<string, string>>;
let software: {"name": string, "url": 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);
} else if(externalreference['qualifier'].classid == "software") {
if(software == undefined) {
software = new Array<{"name": string, "url": string}>();
}
software.push({"name": externalreference.sitename, "url": externalreference.url});
}
}
}
return [bioentities, software];
}
/*
parseReferences(citations: any): {"name": string, "url": string}[] {
let references = new Array<{"name": string, "url": string}>();
let citation;
let length = Array.isArray(citations) ? citations.length : 1;
for(let i=0; i<length; i++) {
citation = Array.isArray(citations) ? citations[i] : citations;
let url;
if(citation.hasOwnProperty("id")) {
let citationId;
let length1 = Array.isArray(citation['id']) ? citation['id'].length : 1;
for(let j=0; j<length1; j++) {
citationId = Array.isArray(citation['id']) ? citation['id'][j] : citation['id'];
if(citationId.type == "pmid") {
url = "http://www.ncbi.nlm.nih.gov/pubmed/"+citationId.value;
}
}
}
references[citation.position-1] = { "name": "", "url": ""};
references[citation.position-1]['name'] = citation.rawText;
references[citation.position-1]['url'] = url;
}
return references;
}
*/
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)
.map(res => <any> res.json())
.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;
}
}