explore-services/portal-2/src/app/services/dataset.service.ts

564 lines
28 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 {OpenaireProperties} from '../utils/properties/openaireProperties';
@Injectable()
export class DatasetService {
constructor(private http: Http) {}
datasetInfo: DatasetInfo;
getDatasetInfo (id: string):any {
console.info("getDatasetInfo in service");
let url = OpenaireProperties.getSearchAPIURL()+'datasets/'+id;
return this.http.get(url)
.map(res => <any> res.json())
.do(res => console.info(res['result']['metadata']['oaf:entity']))
.map(res => res['result']['metadata']['oaf:entity']['oaf:result'])
.map(res => [res,
res['title'],
res['rels']['rel'],
res['children'],
res['pid'],
res['subject'],
res['bestlicense'],
res['collectedfrom'],
res['context'],
res['resulttype']
])
.map(res => this.parseDatasetInfo(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.error(error);
return Observable.throw(error || 'Server error');
}
parseDatasetInfo (data: any):any {
this.datasetInfo = new DatasetInfo();
if(data[0] != null) {
this.datasetInfo.date = data[0].dateofacceptance.substring(0,4);
this.datasetInfo.publisher = data[0].publisher;
if(!Array.isArray(data[0].description)) {
this.datasetInfo.description = data[0].description;
} else {
this.datasetInfo.description = data[0].description[0];
}
this.datasetInfo.embargoEndDate = data[0].embargoenddate;
}
if(data[1] != null) {
this.datasetInfo.title = {"name": "", "url": "", "accessMode": ""};
if(Array.isArray(data[1])) {
this.datasetInfo.title['name'] = data[1][0].content;
} else {
this.datasetInfo.title['name'] = data[1].content;
}
}
if(data[2] != null) {
let mydata;
let counter = 0;
let length = data[2].length!=undefined ? data[2].length : 1;
for(let i=0; i<length; i++) {
mydata = length > 1 ? data[2][i] : data[2];
if(mydata.hasOwnProperty("to")) {
if(mydata['to'].class == "hasAuthor") {
if(this.datasetInfo.authors == undefined) {
this.datasetInfo.authors = new Array<{"name": string, "url": string}>();
}
this.datasetInfo.authors[mydata.ranking-1] = {"name": "", "url": ""};
this.datasetInfo.authors[mydata.ranking-1]['name'] = mydata.fullname;
this.datasetInfo.authors[mydata.ranking-1]['url'] = OpenaireProperties.getsearchLinkToPerson()+mydata['to'].content;
} else if(mydata['to'].class == "isProducedBy") {
if(this.datasetInfo.fundedByProjects == undefined) {
this.datasetInfo.fundedByProjects = new Array<
{ "url": string, "acronym": string, "title": string,
"funderShortname": string, "funderName": string,
"funding": string, "inline": boolean
}>();
}
counter = this.datasetInfo.fundedByProjects.length;
this.datasetInfo.fundedByProjects[counter] = {
"url": "", "acronym": "", "title": "",
"funderShortname": "", "funderName": "",
"funding": "", "inline": false
}
this.datasetInfo.fundedByProjects[counter]['url'] =
OpenaireProperties.getsearchLinkToProject()+mydata['to'].content;
this.datasetInfo.fundedByProjects[counter]['acronym'] = mydata.acronym;
this.datasetInfo.fundedByProjects[counter]['title'] = mydata.title;
if(mydata.hasOwnProperty("funding")) {
let length1 = Array.isArray(mydata['funding']) ? mydata['funding'].length : 1;
for(let j=0; j<length1; j++) {
let funding;
let mydata1 = length1 > 1 ? mydata['funding'][j] : mydata['funding'];
if(mydata1.hasOwnProperty("funder")) {
this.datasetInfo.fundedByProjects[counter]['funderShortname'] = mydata1['funder'].shortname;
this.datasetInfo.fundedByProjects[counter]['funderName'] = mydata1['funder'].name;
}
if(mydata1.hasOwnProperty("funding_level_2")) {
funding = mydata1['funding_level_2'].content;
} else if(mydata1.hasOwnProperty("funding_level_1")) {
funding = mydata1['funding_level_1'].content;
} else if(mydata1.hasOwnProperty("funding_level_0")) {
funding = mydata1['funding_level_0'].content;
}
if(funding != undefined) {
funding = funding.split("::");
if(this.datasetInfo.fundedByProjects[counter]['funding'] != "") {
this.datasetInfo.fundedByProjects[counter]['funding'] += ", "+funding[1];
} else {
this.datasetInfo.fundedByProjects[counter]['funding'] = funding[1];
}
for(let i=2; i<funding.length; i++) {
this.datasetInfo.fundedByProjects[counter]['funding'] += " | " + funding[i];
}
}
}
}
} else if(mydata['to'].class == "hasAmongTopNSimilarDocuments") {
if(this.datasetInfo.similarResearchResults == undefined) {
this.datasetInfo.similarResearchResults = new Array<{
"name": string, "url": string, "date": string,
"trust": string, "class": string}>();
}
counter = this.datasetInfo.similarResearchResults.length;
this.datasetInfo.similarResearchResults[counter] = {"name": "", "url": "", "date": "", "trust": "", "class": ""}
let url;
if(mydata['resulttype'].classname == "publication") {
url = OpenaireProperties.getsearchLinkToPublication()+mydata['to'].content;
this.datasetInfo.similarResearchResults[counter]['class'] = "publication";
} else {
url = OpenaireProperties.getsearchLinkToDataset()+mydata['to'].content;
this.datasetInfo.similarResearchResults[counter]['class'] = "dataset";
}
this.datasetInfo.similarResearchResults[counter]['url'] = url;
this.datasetInfo.similarResearchResults[counter]['name'] = mydata['title'].content;
this.datasetInfo.similarResearchResults[counter]['date'] = mydata.dateofacceptance.substring(0,4);;
this.datasetInfo.similarResearchResults[counter]['trust'] = Math.round(mydata.trust*100)+"%";
} else if(mydata['to'].class == "isRelatedTo") {
if(this.datasetInfo.relatedResearchResults == undefined) {
this.datasetInfo.relatedResearchResults = new Array<{
"name": string, "url": string, "date": string,
"trust": string, "class": string}>();
}
counter = this.datasetInfo.relatedResearchResults.length;
this.datasetInfo.relatedResearchResults[counter] = {"name": "", "url": "", "date": "", "trust": "", "class": ""}
let url;
if(mydata['resulttype'].classname == "publication") {
url = OpenaireProperties.getsearchLinkToPublication()+mydata['to'].content;
this.datasetInfo.relatedResearchResults[counter]['class'] = "publication";
} else {
url = OpenaireProperties.getsearchLinkToDataset()+mydata['to'].content;
this.datasetInfo.relatedResearchResults[counter]['class'] = "dataset";
}
this.datasetInfo.relatedResearchResults[counter]['url'] = url;
this.datasetInfo.relatedResearchResults[counter]['name'] = mydata['title'].content;
this.datasetInfo.relatedResearchResults[counter]['date'] = mydata.dateofacceptance.substring(0,4);;
this.datasetInfo.relatedResearchResults[counter]['trust'] = Math.round(mydata.trust*100)+"%";
}
}
}
this.datasetInfo.authors = this.datasetInfo.authors.filter(function (item) {
return (item != undefined);
});
}
if(data[3] != null) {
if(data[3].hasOwnProperty("instance")) {
this.datasetInfo.downloadFrom = new Map<string, {"url": string[], "accessMode": string[]}>();
this.datasetInfo.publishedIn = new Map<string, {"url": string[], "accessMode": string[]}>()
let length = data[3]['instance'].length!=undefined ? data[3]['instance'].length : 1;
let counter = 0;
let counter1 = 0;
let counter2 = 0;
let mydata;
for(let i=0; i<length; i++) {
mydata = length > 1 ? data[3]['instance'][i] : data[3]['instance'];
if(this.datasetInfo.type == undefined) {
if(mydata.hasOwnProperty("instancetype")) {
if(mydata['instancetype'].classname != null
&& mydata['instancetype'].classname != ""
&& mydata['instancetype'].classname != "Unknown"){
this.datasetInfo.type = mydata['instancetype'].classname;
}
}
}
if(mydata.hasOwnProperty("webresource")) {
let url;
if(mydata['webresource'].length == undefined) {
url = mydata['webresource'].url;
} else{
url = mydata['webresource'][0].url;
}
if(!this.datasetInfo.downloadFrom.has(url) && mydata.hasOwnProperty("hostedby")) {
if(mydata['hostedby'].name != "other resources" && mydata['hostedby'].name != "Unknown Repository") {
if(!this.datasetInfo.downloadFrom.has(mydata['hostedby'].name)) {
this.datasetInfo.downloadFrom.set(mydata['hostedby'].name, {"url": null, "accessMode": null});
}
if(this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['url'] == null) {
this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['url'] = new Array<string>();
}
counter2 = this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['url'].length;
this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['url'][counter2] = url;
if(this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['accessMode'] == null) {
this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['accessMode'] = new Array<string>();
}
if(mydata.hasOwnProperty("licence")) {
this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['accessMode'][counter2] = mydata['licence'].classid;
} else {
this.datasetInfo.downloadFrom.get(mydata['hostedby'].name)['accessMode'][counter2] = "";
}
} else {
if(data[0] != null && data[0].hasOwnProperty("source")) {
let key: string;
if(Array.isArray(data[0].source)) {
if(!this.datasetInfo.publishedIn.has(data[0]['source'][counter1])) {
key = data[0]['source'][counter1];
}
} else {
if(!this.datasetInfo.publishedIn.has(data[0]['source'])) {
key = data[0]['source'];
}
}
this.datasetInfo.publishedIn.set(key, {"url": null, "accessMode": null});
if(this.datasetInfo.publishedIn.get(key)['url'] == null) {
this.datasetInfo.publishedIn.get(key)['url'] = new Array<string>();
}
counter2 = this.datasetInfo.publishedIn.get(key)['url'].length;
this.datasetInfo.publishedIn.get(key)['url'][counter2] = url;
if(this.datasetInfo.publishedIn.get(key)['accessMode'] == null) {
this.datasetInfo.publishedIn.get(key)['accessMode'] = new Array<string>();
}
if(mydata.hasOwnProperty("licence")) {
this.datasetInfo.publishedIn.get(key)['accessMode'][counter2] = mydata['licence'].classid;
} else {
this.datasetInfo.publishedIn.get(key)['accessMode'][counter2] = "";
}
counter1++;
}
}
if(this.datasetInfo.title != undefined) {
if(this.datasetInfo.title['url'] == undefined) {
this.datasetInfo.title['url'] = url;
}
switch (this.datasetInfo.title['licence']) {
case undefined:
this.datasetInfo.title['licence'] = mydata['licence'].classid;
this.datasetInfo.title['url'] = url;
break;
case "CLOSED":
if(mydata['licence'].classid == "OPEN" ||
mydata['licence'].classid == "EMBARGO" ||
mydata['licence'].classid == "RESTRICTED") {
this.datasetInfo.title['licence'] = mydata['licence'].classid;
this.datasetInfo.title['url'] = url;
}
break;
case "RESTRICTED":
if(mydata['licence'].classid == "OPEN" ||
mydata['licence'].classid == "EMBARGO") {
this.datasetInfo.title['licence'] = mydata['licence'].classid;
this.datasetInfo.title['url'] = url;
}
break;
case "EMBARGO":
if(mydata['licence'].classid == "OPEN") {
this.datasetInfo.title['licence'] = mydata['licence'].classid;
this.datasetInfo.title['url'] = url;
}
break;
}
}
}
}
}
}
}
if(data[4] != null) {
let counter = 0;
this.datasetInfo.identifiers = new Map<string, string[]>();
if(data[4].hasOwnProperty("classname") && data[4]['classname'] != "") {
if(data[4].classname == "doi" || data[4].classname == "pmc") {
if(!this.datasetInfo.identifiers.has(data[4].classname)) {
this.datasetInfo.identifiers.set(data[4].classname, new Array<string>());
}
counter = this.datasetInfo.identifiers.get(data[4].classname).length;
this.datasetInfo.identifiers.get(data[4].classname)[counter] = data[4].content;
}
} else {
for(let i=0; i<data[4].length; i++) {
if(data[4][i].classname == "doi" || data[4][i].classname == "pmc") {
if(!this.datasetInfo.identifiers.has(data[4][i].classname)) {
this.datasetInfo.identifiers.set(data[4][i].classname, new Array<string>());
}
counter = this.datasetInfo.identifiers.get(data[4][i].classname).length;
this.datasetInfo.identifiers.get(data[4][i].classname)[counter] = data[4][i].content;
}
}
}
}
if(data[5] != null) {
this.datasetInfo.classifiedSubjects = new Map<string, string[]>();
this.datasetInfo.subjects = new Array<string>();
let mydata;
let length = data[5].length!=undefined ? data[5].length : 1;
for(let i=0; i<length; i++) {
mydata = length > 1 ? data[5][i] : data[5];
if(mydata.classid != "") {
if(mydata.inferred == true) {
if(!this.datasetInfo.classifiedSubjects.has(mydata.classid)) {
this.datasetInfo.classifiedSubjects.set(mydata.classid, new Array<string>());
}
let counter = this.datasetInfo.classifiedSubjects.get(mydata.classid).length;
this.datasetInfo.classifiedSubjects.get(mydata.classid)[counter] = mydata.content;
} else {
let counter = this.datasetInfo.subjects.length;
this.datasetInfo.subjects[counter] = mydata.content;
}
}
}
}
if(data[6] != null) {
this.datasetInfo.bestlicense = data[6].classid;
}
if(data[7] != null) {
this.datasetInfo.collectedFrom = new Array<{"name": string, "url": string}>();
let mydata;
let length = data[7].length!=undefined ? data[7].length : 1;
for(let i=0; i<length; i++) {
mydata = length > 1 ? data[7][i] : data[7];
let link = OpenaireProperties.getsearchLinkToDataProvider();
this.datasetInfo.collectedFrom[i] = {"name": "", "url": ""};
this.datasetInfo.collectedFrom[i]['name'] = mydata.name;
this.datasetInfo.collectedFrom[i]['url'] = link+mydata.id;
}
}
if(this.datasetInfo.publisher != null
&& this.datasetInfo.identifiers != null
&& this.datasetInfo.identifiers.has("doi")) {
if( this.datasetInfo.downloadFrom == null) {
this.datasetInfo.downloadFrom = new Map<string,{"url": string[], "accessMode": string[]}>();
}
this.datasetInfo.downloadFrom.set(this.datasetInfo.publisher, {"url": null, "accessMode": null});
let url = OpenaireProperties.getDoiURL()+this.datasetInfo.identifiers.get("doi");
this.datasetInfo.downloadFrom.get(this.datasetInfo.publisher)['url'] = new Array<string>();
this.datasetInfo.downloadFrom.get(this.datasetInfo.publisher)['accessMode'] = new Array<string>();
this.datasetInfo.downloadFrom.get(this.datasetInfo.publisher)['url'][0] = url;
this.datasetInfo.downloadFrom.get(this.datasetInfo.publisher)['accessMode'][0] = "";
if(this.datasetInfo.title != undefined && this.datasetInfo.title['url'] == "") {
this.datasetInfo.title['url'] = url;
}
}
if(data[8] != null) {
this.datasetInfo.contexts = new Array<
{ "labelContext": string, "labelCategory": string, "labelConcept": string, "inline": boolean}>();
let position = 0;
let labels = "";
let mydata;
let length = data[8].length!=undefined ? data[8].length : 1;
for(let i=0; i<length; i++) {
mydata = length > 1 ? data[8][i] : data[8];
if(mydata.hasOwnProperty("type") && mydata['type'] == "community") {
if(mydata.hasOwnProperty("category")) {
if(mydata['category'].hasOwnProperty("concept")) {
let mydata1;
let length1 = mydata['category']['concept'].length!=undefined ? mydata['category']['concept'].length : 1;
for(let j=0; j<length1; j++) {
mydata1 = length1 > 1 ? mydata['category']['concept'][j] : mydata['category']['concept'];
this.datasetInfo.contexts[position] = {"labelContext": "", "labelCategory": "", "labelConcept": "", inline: false};
this.datasetInfo.contexts[position]['labelContext'] = mydata.label;
this.datasetInfo.contexts[position]['labelCategory'] = mydata['category'].label;;
this.datasetInfo.contexts[position]['labelConcept'] = mydata1.label;
position++;
}
} else {
this.datasetInfo.contexts[position] = {"labelContext": "", "labelCategory": "", "labelConcept": "", inline: false};
this.datasetInfo.contexts[position]['labelContext'] = mydata.label;
this.datasetInfo.contexts[position]['labelCategory'] = mydata['category'].label;;
this.datasetInfo.contexts[position]['labelConcept'] = null;
}
} else {
this.datasetInfo.contexts[position] = {"labelContext": "", "labelCategory": "", "labelConcept": "", inline: false};
this.datasetInfo.contexts[position]['labelContext'] = mydata.label;
this.datasetInfo.contexts[position]['labelCategory'] = null;
this.datasetInfo.contexts[position]['labelConcept'] = null;
}
}
}
}
if(data[9] != null && this.datasetInfo.type == undefined) {
if(data[9].hasOwnProperty('classname')) {
this.datasetInfo.type = data[9].classname;
}
}
//this.printdatasetInfo();
return this.datasetInfo;
}
printDatasetInfo() {
console.info("DATE: "+this.datasetInfo.date);
console.info("PUBLISHER: "+this.datasetInfo.publisher);
console.info("DESCRIPTION: "+this.datasetInfo.description);
console.info("TITLE: "+this.datasetInfo.title);
console.info("AUTHORS: "+this.datasetInfo.authors);
console.info("\nFUNDED BY PROJECTS:");
if(this.datasetInfo.fundedByProjects != undefined) {
this.datasetInfo.fundedByProjects.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
/*
console.info("\nRELATED RESEARCH DATA:");
if(this.datasetInfo.relatedResearchData != undefined) {
this.datasetInfo.relatedResearchData.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
console.info("\nSIMILAR datasetS:");
if(this.datasetInfo.similarPublications != undefined) {
this.datasetInfo.similarPublications.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
*/
console.info("TYPE: "+this.datasetInfo.type);
console.info("\nDOWNLOAD FROM:");
if(this.datasetInfo.downloadFrom != undefined) {
this.datasetInfo.downloadFrom.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
console.info("\nIDENTIFIERS:");
if(this.datasetInfo.identifiers != undefined) {
this.datasetInfo.identifiers.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
console.info("SUBJECTS: "+this.datasetInfo.subjects);
console.info("\nCLASSIFIED OBJECTS:");
if(this.datasetInfo.classifiedSubjects != undefined) {
this.datasetInfo.classifiedSubjects.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
console.info("BEST LICENSE: "+this.datasetInfo.bestlicense);
console.info("\nCOLLECTED FROM:");
if(this.datasetInfo.collectedFrom != undefined) {
this.datasetInfo.collectedFrom.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
console.info("\nDOWNLOAD FROM:");
if(this.datasetInfo.downloadFrom != undefined) {
this.datasetInfo.downloadFrom.forEach(function (value, key, map) {
console.info(key + " = " + value);
});
} else {
console.info("undefined");
}
console.info("\n");
}
}