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

207 lines
8.1 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {OrpInfo} from '../../utils/entities/orpInfo';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import{EnvProperties} from '../../utils/properties/env-properties';
import { ParsingFunctions } from '../landing-utils/parsingFunctions.class';
@Injectable()
export class OrpService {
constructor(private http: Http ) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
orpInfo: OrpInfo;
getOrpInfo (id: string, properties:EnvProperties):any {
console.info("getOrpInfo in service");
let url = properties.searchAPIURLLAst+'other/'+id+"?format=json";
let key = url;
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.do(res => console.info(res['result']['metadata']['oaf:entity']))
.map(res => [res['result']['header']['dri:status'], res['result']['metadata']['oaf:entity']['oaf:result'],res])
.map(res => [res[1],
res[1]['title'],
res[1]['rels']['rel'],
res[1]['children'],
res[1]['pid'],
res[1]['subject'],
res[1]['bestaccessright'],
res[1]['collectedfrom'],
res[1]['context'],
res[0],
res[1]['creator'],
res[1]['language'],
res[1]['country'],
res[2]
]).map(res => this.parseOrpInfo(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');
}
parseOrpInfo (data: any):any {
this.orpInfo = new OrpInfo();
this.orpInfo.record = data[13];
if(data[0] != null) {
var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
this.orpInfo.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
this.orpInfo.dateofacceptance = data[0].dateofacceptance;
this.orpInfo.publisher = data[0].publisher;
if(!Array.isArray(data[0].description)) {
this.orpInfo.description = data[0].description;
} else {
this.orpInfo.description = data[0].description[0];
}
this.orpInfo.embargoEndDate = data[0].embargoenddate;
}
if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
this.orpInfo.accessMode = data[0]['bestaccessright'].classid;
}
if(data[1] != null) {
if(Array.isArray(data[1])) {
this.orpInfo.title = data[1][0].content;
} else {
this.orpInfo.title = data[1].content;
}
}
if(data[2] != null) {
let relation;
let length = data[2].length!=undefined ? data[2].length : 1;
for(let i=0; i<length; i++) {
relation = data[2].length!=undefined ? data[2][i] : data[2];
if(relation.hasOwnProperty("to")) {
if(relation['to'].class == "isProducedBy") {
this.orpInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.orpInfo.fundedByProjects, relation, this.orpInfo.projectsProvenanceVocabulary);
} else if(relation['to'].class == "isRelatedTo") {
let provenanceAction: string;
if(relation.provenanceaction in this.orpInfo.researchResultsProvenanceVocabulary) {
provenanceAction = this.orpInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
} else {
provenanceAction = "Other"
}
this.orpInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.orpInfo.relatedResearchResults, relation, provenanceAction);
} else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
this.orpInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.orpInfo.similarResearchResults, relation);
}
}
}
}
if(data[3] != null) {
if(data[3].hasOwnProperty("instance")) {
this.orpInfo.hostedBy_collectedFrom = new Array<{ "downloadName": string, "downloadUrl": string[],
"collectedName": string, "collectedId": string,
"accessMode": string[], "bestAccessMode": string,
"type": string, "year":string}>();
this.orpInfo.types = new Array<string>();
let types = new Set<string>();
let counter = 0;
let instance;
let length = data[3]['instance'].length!=undefined ? data[3]['instance'].length : 1;
for(let i=0; i<length; i++) {
instance = data[3]['instance'].length!=undefined ? data[3]['instance'][i] : data[3]['instance'];
this.parsingFunctions.parseTypes(this.orpInfo.types, types, instance);
if(instance.hasOwnProperty("webresource")) {
let url;
if(instance['webresource'].length == undefined) {
url = instance['webresource'].url;
} else{
url = instance['webresource'][0].url;
}
if(instance.hasOwnProperty("hostedby")) {
counter = this.parsingFunctions.parseHostedBy_collectedFrom(this.orpInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.orpInfo.title*/, this.orpInfo.accessMode);
}
}
}
}
}
if(data[4] != null) {
this.orpInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
}
if(data[5] != null) {
let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
this.orpInfo.subjects = subjectResults[0];
this.orpInfo.otherSubjects = subjectResults[1];
this.orpInfo.classifiedSubjects = subjectResults[2];
}
this.orpInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
this.orpInfo.hostedBy_collectedFrom, this.orpInfo.publisher,
null, this.orpInfo.identifiers/*, this.orpInfo.title*/);
if(data[8] != null) {
this.orpInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
}
if(data[9] != null && data[9] == "under curation") {
this.orpInfo.underCurationMessage = true;
} else {
this.orpInfo.underCurationMessage = false;
}
if(data[10] != null) {
if(this.orpInfo.authors == undefined) {
this.orpInfo.authors = new Array<string>();
}
let authors = data[10];
let length = Array.isArray(authors) ? authors.length : 1;
for(let i=0; i<length; i++) {
let author = Array.isArray(authors) ? authors[i] : authors;
this.orpInfo.authors[author.rank-1] = author.content;
}
this.orpInfo.authors = this.orpInfo.authors.filter(function (item) {
return (item != undefined);
});
}
if(data[11] != null) {
this.orpInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
}
if(data[12] != null) {
this.orpInfo.countries = this.parsingFunctions.parseCountries(data[12]);
}
if(this.orpInfo.relatedResearchResults) {
let self = this;
this.orpInfo.relatedResearchResults.forEach(function (value, key, map) {
self.orpInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
});
}
this.orpInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.orpInfo.similarResearchResults);
return this.orpInfo;
}
}