openaire-library/landingPages/landing-utils/deletedByInference/deletedByInference.service.ts

171 lines
7.1 KiB
TypeScript
Raw Normal View History

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
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 '../parsingFunctions.class';
@Injectable()
export class DeletedByInferenceService {
private sizeOfDescription: number = 270;
constructor(private http: Http ) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
getDeletedByInferencePublications (id: string, properties:EnvProperties):any {
let url = properties.searchAPIURLLAst + 'deletedByInferencePublications/' +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']['metadata']['oaf:entity'])
.map(res => this.parseDeletedByInferencePublications(res, properties));
}
parseDeletedByInferencePublications (result: any, properties: EnvProperties): DeletedByInferenceResult {
/*title, authors, abstract, List of projects, PIDs,
collectedfrom (link pointing to the download url), access rights*/
//let publications: DeletedByInferenceResult[] = [];
//if(results) {
let publication : DeletedByInferenceResult;
//let length = Array.isArray(results) ? results.length : 1;
//for(let i=0; i<length; i++) {
//var result = Array.isArray(results) ? results[i] : results;
publication = new DeletedByInferenceResult();
if(result) {
if(result['oaf:result']) {
let data = result['oaf:result'];
var date:string = (data.dateofacceptance)+""; // transform to string in case it is an integer
publication.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
publication.dateofacceptance = data.dateofacceptance;
publication.embargoEndDate = data.embargoenddate;
if(!Array.isArray(data.description)) {
publication.description = data.description;
} else {
publication.description = data.description[0];
}
if(publication.description && publication.description.length > this.sizeOfDescription) {
publication.description = publication.description.substring(0, this.sizeOfDescription) + "...";
}
if(data['bestaccessright'] && data['bestaccessright'].hasOwnProperty("classid")) {
publication.accessMode = data['bestaccessright'].classid;
}
}
if(result['oaf:result'] && result['oaf:result']['title']) {
let title = result['oaf:result']['title'];
if(Array.isArray(title)) {
publication.title = title[0].content;
} else {
publication.title = title.content;
}
}
if(result['oaf:result'] && result['oaf:result']['language']) {
publication.languages = this.parsingFunctions.parseLanguages(result['oaf:result']['language']);
}
if(result['oaf:result'] && result['oaf:result']['country']) {
publication.countries = this.parsingFunctions.parseCountries(result['oaf:result']['country']);
}
if(result['oaf:result'] && result['oaf:result']['rels'] && result['oaf:result']['rels']['rel']) {
let relation;
let length = Array.isArray(result['oaf:result']['rels']['rel']) ? result['oaf:result']['rels']['rel'].length : 1;
for(let i=0; i<length; i++) {
relation = Array.isArray(result['oaf:result']['rels']['rel']) ? result['oaf:result']['rels']['rel'][i] : result['oaf:result']['rels']['rel'];
if(relation.hasOwnProperty("to")) {
if(relation['to'].class == "isProducedBy") {
publication.fundedByProjects = this.parsingFunctions.parseFundingByProjects(publication.fundedByProjects, relation, publication.projectsProvenanceVocabulary);
}
}
}
}
if(result['oaf:result'] && result['oaf:result']['children']) {
let children = result['oaf:result']['children'];
if(children.hasOwnProperty("instance")) {
publication.types = new Array<string>();
let types = new Set<string>();
publication.hostedBy_collectedFrom = new Array<{"downloadName": string, "downloadUrl": string[], "collectedName": string, "collectedId": string, "accessMode": string[], "bestAccessMode": string, "type": string, "year":string}>();
let counter = 0;
let instance;
let length = Array.isArray(children['instance']) ? children['instance'].length : 1;
for(let i=0; i<length; i++) {
instance = Array.isArray(children['instance']) ? children['instance'][i] : children['instance'];
this.parsingFunctions.parseTypes(publication.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(publication.hostedBy_collectedFrom, instance, result['oaf:result'], url, counter, publication.accessMode);
}
}
}
}
}
if(result['oaf:result'] && result['oaf:result']['pid']) {
publication.identifiers = this.parsingFunctions.parseIdentifiers(result['oaf:result']['pid']);
}
if(result['oaf:result'] && result['oaf:result']['creator']) {
if(publication.authors == undefined) {
publication.authors = new Array<{"fullName": string, "orcid": string}>();
}
let authors = result['oaf:result']['creator'];
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);
}*/
publication['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
}
}
publication.authors = publication.authors.filter(function (item) {
return (item != undefined && item.fullName != undefined);
});
}
//}
//publications.push(publication);
//}
}
return publication;
}
}