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

259 lines
11 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {DatasetInfo} from '../../utils/entities/datasetInfo';
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 DatasetService {
constructor(private http: Http ) {
this.parsingFunctions = new ParsingFunctions();
}
public parsingFunctions: ParsingFunctions;
datasetInfo: DatasetInfo;
getDatasetInfo (id: string, properties:EnvProperties):any {
let url = properties.searchAPIURLLAst+'datasets/'+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']['oaf:result'],res])
.map(res => [res[1], //0
res[1]['title'], //1
res[1]['rels']['rel'], //2
res[1]['children'], //3
res[1]['pid'], //4
res[1]['subject'], //5
res[1]['bestaccessright'], //6
res[1]['collectedfrom'], //7
res[1]['context'], //8
//res[1]['resulttype'],
res[0], //9
res[1]['creator'], //10
res[1]['language'], //11
res[1]['country'], //12
res[2], //13
(res[1]['extraInfo']!= undefined && res[1]['extraInfo']['citations']!= undefined)? res[1]['extraInfo']['citations']['citation']:null, //14
res[1]['journal'] //15
]).map(res => this.parseDatasetInfo(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');
}
parseDatasetInfo (data: any, properties: EnvProperties):any {
this.datasetInfo = new DatasetInfo();
this.datasetInfo.record = data[13];
// ['result']['metadata']['oaf:entity']['oaf:result']
if(data[0] != null) {
var date:string = (data[0].dateofacceptance)+""; // transform to string in case it is an integer
this.datasetInfo.date = (date && (date).indexOf('-') !== -1)?date.split('-')[0]:date;
this.datasetInfo.dateofacceptance = data[0].dateofacceptance;
this.datasetInfo.publisher = data[0].publisher;
if(!Array.isArray(data[0].description)) {
this.datasetInfo.description = String(data[0].description);
} else {
this.datasetInfo.description = String(data[0].description[0]);
}
this.datasetInfo.embargoEndDate = data[0].embargoenddate;
}
/*this.datasetInfo.title = {"name": "", "url": "", "accessMode": ""};
if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
this.datasetInfo.title.accessMode = data[0]['bestaccessright'].classid;
}
if(data[1] != null) {
if(Array.isArray(data[1])) {
this.datasetInfo.title['name'] = data[1][0].content;
} else {
this.datasetInfo.title['name'] = data[1].content;
}
}*/
if(data[0]['bestaccessright'] && data[0]['bestaccessright'].hasOwnProperty("classid")) {
this.datasetInfo.accessMode = data[0]['bestaccessright'].classid;
}
// ['result']['metadata']['oaf:entity']['oaf:result']['title']
if(data[1] != null) {
if(Array.isArray(data[1])) {
this.datasetInfo.title = data[1][0].content;
if(data[1][1].classid == "subtitle") {
this.datasetInfo.subtitle = data[1][1].content;
}
} else {
this.datasetInfo.title = data[1].content;
}
}
// ['result']['metadata']['oaf:entity']['oaf:result']['rels']['rel']
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.datasetInfo.fundedByProjects = this.parsingFunctions.parseFundingByProjects(this.datasetInfo.fundedByProjects, relation, this.datasetInfo.projectsProvenanceVocabulary);
} else if(relation['to'].class == "isRelatedTo") {
let provenanceAction: string;
if(relation.provenanceaction in this.datasetInfo.researchResultsProvenanceVocabulary) {
provenanceAction = this.datasetInfo.researchResultsProvenanceVocabulary[relation.provenanceaction];
} else {
provenanceAction = "Other"
}
this.datasetInfo.relatedResearchResults = this.parsingFunctions.parseRelatedResearchResults(this.datasetInfo.relatedResearchResults, relation, provenanceAction);
} else if(relation['to'].class == "hasAmongTopNSimilarDocuments") {
this.datasetInfo.similarResearchResults = this.parsingFunctions.parseSimilarResearchResults(this.datasetInfo.similarResearchResults, relation);
}
}
}
}
// ['result']['metadata']['oaf:entity']['oaf:result']['children']
if(data[3] != null) {
if(data[3].hasOwnProperty("instance")) {
this.datasetInfo.hostedBy_collectedFrom = new Array<{ "downloadName": string, "downloadUrl": string[],
"collectedName": string, "collectedId": string,
"accessMode": string[], "bestAccessMode": string,
"type": string, "year":string}>();
this.datasetInfo.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.datasetInfo.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.datasetInfo.hostedBy_collectedFrom, instance, data[0], url, counter/*, this.datasetInfo.title*/, this.datasetInfo.accessMode);
}
}
}
}
}
// ['result']['metadata']['oaf:entity']['oaf:result']['pid']
if(data[4] != null) {
this.datasetInfo.identifiers = this.parsingFunctions.parseIdentifiers(data[4]);
}
// ['result']['metadata']['oaf:entity']['oaf:result']['subject']
if(data[5] != null) {
let subjectResults: [string[], Map<string, string[]>, Map<string, string[]>] = this.parsingFunctions.parseAllSubjects(data[5]);
this.datasetInfo.subjects = subjectResults[0];
this.datasetInfo.otherSubjects = subjectResults[1];
this.datasetInfo.classifiedSubjects = subjectResults[2];
}
this.datasetInfo.hostedBy_collectedFrom = this.parsingFunctions.addPublisherToHostedBy_collectedFrom(
this.datasetInfo.hostedBy_collectedFrom, this.datasetInfo.publisher,
null, this.datasetInfo.identifiers/*, this.datasetInfo.title*/);
// ['result']['metadata']['oaf:entity']['oaf:result']['context']
if(data[8] != null) {
this.datasetInfo.contexts = this.parsingFunctions.parseContexts(data[8]);
}
// if(data[9] != null && this.datasetInfo.type == undefined) {
// if(data[9].hasOwnProperty('classname')) {
// this.datasetInfo.type = data[9].classname;
// }
// }
// ['result']['header']['dri:status']
if(data[9] != null && data[9] == "under curation") {
this.datasetInfo.underCurationMessage = true;
} else {
this.datasetInfo.underCurationMessage = false;
}
// ['result']['metadata']['oaf:entity']['oaf:result']['creator']
if(data[10] != null) {
if(this.datasetInfo.authors == undefined) {
this.datasetInfo.authors = new Array<{"fullName": string, "orcid": 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;
if(author) {
if (author.ORCID && author.ORCID.indexOf(properties.orcidURL) != -1) {
author.ORCID = author.ORCID.substr(properties.orcidURL.length);
}
this.datasetInfo['authors'][author.rank] = {"fullName": author.content, "orcid": author.ORCID};
}
}
this.datasetInfo.authors = this.datasetInfo.authors.filter(function (item) {
return (item != undefined && item.fullName != undefined);
});
}
// ['result']['metadata']['oaf:entity']['oaf:result']['language']
if(data[11] != null) {
this.datasetInfo.languages = this.parsingFunctions.parseLanguages(data[11]);
}
// ['result']['metadata']['oaf:entity']['oaf:result']['country']
if(data[12] != null) {
this.datasetInfo.countries = this.parsingFunctions.parseCountries(data[12]);
}
// ['result']['metadata']['oaf:entity']['oaf:result']['extraInfo']['citations']['citation']
if(data[14] != null) {
this.datasetInfo.references = this.parsingFunctions.parseReferences(data[14]);
}
if(this.datasetInfo.relatedResearchResults) {
let self = this;
this.datasetInfo.relatedResearchResults.forEach(function (value, key, map) {
self.datasetInfo.relatedResearchResults.set(key, self.parsingFunctions.sortByPercentage(value));
});
}
this.datasetInfo.similarResearchResults = this.parsingFunctions.sortByPercentage(this.datasetInfo.similarResearchResults);
if(data[15] != null) {
this.datasetInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": "", "issue": "", "volume": "", "start_page": "", "end_page": ""}
this.datasetInfo.journal['journal'] = data[15].content;
this.datasetInfo.journal['issn'] = data[15].issn;
this.datasetInfo.journal['lissn'] = data[15].lissn;
this.datasetInfo.journal['eissn'] = data[15].eissn;
this.datasetInfo.journal['issue'] = data[15].iss;
this.datasetInfo.journal['volume'] = data[15].vol;
this.datasetInfo.journal['start_page'] = data[15].sp;
this.datasetInfo.journal['end_page'] = data[15].ep;
}
return this.datasetInfo;
}
}