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

340 lines
14 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 '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 {
console.info("getPublicationInfo in service");
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']])
.map(res => [ res[1]['oaf:result'],
res[1]['oaf:result']['title'],
res[1]['oaf:result']['rels']['rel'],
res[1]['oaf:result']['children'],
res[1]['oaf:result']['pid'],
res[1]['oaf:result']['journal'],
res[1]['oaf:result']['language'],
res[1]['oaf:result']['subject'],
res[1]['oaf:result']['bestaccessright'],
res[1]['oaf:result']['collectedfrom'],
(res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null,
res[1]['oaf:result']['context'],
res[0],
res[1]['oaf:result']['creator']
])
.map(res => this.parsePublicationInfo(res));
}
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):any {
this.publicationInfo = new PublicationInfo();
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 = data[0].description;
} else {
this.publicationInfo.description = data[0].description[0];
}
this.publicationInfo.embargoEndDate = data[0].embargoenddate;
}
this.publicationInfo.title = {"name": "", "url": "", "accessMode": ""};
if(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[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("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);
}
/**********************************************************/
}
}
}
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": ""}
this.publicationInfo.journal['journal'] = data[5].content;
this.publicationInfo.journal['issn'] = data[5].issn;
this.publicationInfo.journal['lissn'] = data[5].lissn;
}
if(data[6] != null) {
this.publicationInfo.languages = new Array<string>();
if(!Array.isArray(data[6])) {
if(data[6].classname != "Undetermined" && data[6].classname) {
this.publicationInfo.languages.push(data[6].classname);
}
} else {
for(let i=0; i<data[6].length; i++) {
if(data[6][i].classname != "Undetermined" && data[6][i].classname) {
this.publicationInfo.languages.push(data[6][i].classname);
}
}
}
}
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.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<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;
this.publicationInfo.authors[author.rank-1] = author.content;
}
this.publicationInfo.authors = this.publicationInfo.authors.filter(function (item) {
return (item != undefined);
});
}
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;
}
}