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

163 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 {DataProviderInfo} from '../../utils/entities/dataProviderInfo';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import{EnvProperties} from '../../utils/properties/env-properties';
@Injectable()
export class DataProviderService {
constructor(private http: Http ) {}
dataProviderInfo: DataProviderInfo;
getDataproviderInfo (id: string, properties:EnvProperties):any {
console.info("getDataProviderInfo in service");
let url = properties.searchAPIURLLAst + 'datasources/' +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 => [res['oaf:datasource'],
res['oaf:datasource']['datasourcetype'],
res['oaf:datasource']['openairecompatibility'],
res['oaf:datasource']['accessinfopackage'],
res['oaf:datasource']['rels']['rel']
])
.map(res => this.parseDataProviderInfo(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');
}
parseDataProviderInfo (data: any):any {
this.dataProviderInfo = new DataProviderInfo();
if(data[0] != null) {
this.dataProviderInfo.title = {"name": (data[0].englishname)?data[0].englishname: data[0].officialname, "url": data[0].websiteurl};
this.dataProviderInfo.officialName = data[0].officialname;
var originalId =(data[0].originalId)?data[0].originalId:"";
if(originalId && originalId != ""){
if(originalId.indexOf("opendoar____::") != -1){
this.dataProviderInfo.openDoarURL = "http://www.opendoar.org/find.php?format=full&rID="+originalId.split("opendoar____::")[1];
}else if (originalId.indexOf("re3data_____::") != -1){
this.dataProviderInfo.r3DataURL = "http://service.re3data.org/repository/"+originalId.split("re3data_____::")[1];
}
}
}
if(data[1] != null) {
this.dataProviderInfo.type = data[1].classname;
if(data[1].classid == "entityregistry" || data[1].classid == "entityregistry::projects" || data[1].classid == "entityregistry::repositories") {
this.dataProviderInfo.registry = true;
} else {
this.dataProviderInfo.registry = false;
}
if(this.dataProviderInfo.tabs == undefined) {
this.dataProviderInfo.tabs = new Array<{"name": string, "content": string}>();
}
if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
}
if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Research Data", "content": "datasetsTab"});
}
if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
}
if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Content Providers", "content": "datasourcesTab"});
}
this.dataProviderInfo.tabs.push({"name": "Organizations", "content": "organizationsTab"});
if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Related Content Providers", "content": "relatedDatasourcesTab"});
}
if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
}
this.dataProviderInfo.tabs.push({"name": "Metrics", "content": "metricsTab"});
if(this.dataProviderInfo.resultTypes.collectedFrom.has(data[1].classid)) {
this.dataProviderInfo.resultsBy = "collectedFrom";
} else if(this.dataProviderInfo.resultTypes.hostedBy.has(data[1].classid)) {
this.dataProviderInfo.resultsBy = "hostedBy";
}
}
if(!this.dataProviderInfo.registry) {
if(data[2] != null) {
this.dataProviderInfo.compatibility = data[2].classname;
}
if(data[3] != null) {
let oaiPmhURL: string;
if(Array.isArray(data[3])) {
oaiPmhURL = data[3][0];
}
else {
oaiPmhURL = data[3];
}
if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
}
}
}
if(data[4] != null) {
let mydata;
let counter = 0;
let countriesSet: Set<string>;
let length = data[4].length!=undefined ? data[4].length : 1;
for(let i=0; i<length; i++) {
mydata = data[4].length!=undefined ? data[4][i] : data[4];
if(mydata.hasOwnProperty("to")) {
if(mydata['to'].class == "provides" && mydata['to'].type == "organization") {
//if(this.dataProviderInfo.organizations == undefined) {
if(this.dataProviderInfo.organizations.length == 0) {
//this.dataProviderInfo.organizations = new Array<{"name": string, "url": string}>();
this.dataProviderInfo.countries = new Array<string>();
countriesSet = new Set<string>();
}
this.dataProviderInfo.organizations[counter] = {"name": "", "id": ""};
this.dataProviderInfo.organizations[counter]['name'] = mydata.legalname;
this.dataProviderInfo.organizations[counter]['id'] = mydata['to'].content;
if(mydata.country != '' && mydata['country'].classname != '') {
if(!countriesSet.has(mydata['country'].classname)) {
this.dataProviderInfo.countries.push(mydata['country'].classname);
countriesSet.add(mydata['country'].classname);
}
}
counter++;
}
}
}
}
return this.dataProviderInfo;
}
}