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

220 lines
10 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response,Headers, RequestOptions} 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 {
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']['collectedfrom'],
res['oaf:datasource']['accessinfopackage'],
res['oaf:datasource']['rels']['rel']
])
.map(res => this.parseDataProviderInfo(res));
}
getDataproviderAggregationStatus(original_id: string, properties:EnvProperties):any {
let headers = new Headers({'Content-Type': 'application/json', 'accept': 'application/json'});
let options = new RequestOptions({headers: headers});
let page: number = 0;
let size: number = 1;
return this.http.post(properties.datasourcesAPI+page+"/"+size+"?requestSortBy=id&order=ASCENDING", JSON.stringify({ "id": original_id }), options)
.map(res => <any> res.json())
.map(res => res['datasourceInfo'])
.map(res => this.parseDataproviderAggregationStatus(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');
}
parseDataproviderAggregationStatus(data: any): any {
var aggregationStatus: {"fundedContent": string, "indexRecords": string, "fulltexts": string} = null;
if(data != null && data[0] != null) {
aggregationStatus = {"fundedContent": "-1", "indexRecords": "-1", "fulltexts": "-1"};
aggregationStatus.fundedContent = data[0].fundedContent;
aggregationStatus.indexRecords = data[0].indexRecords;
aggregationStatus.fulltexts = data[0].fulltexts;
}
return aggregationStatus;
}
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 pattern = /.{12}::.+/;
var originalIds =(data[0].originalId)?data[0].originalId:"";
let length = Array.isArray(originalIds) ? originalIds.length : 1;
for(let i=0; i<length; i++) {
var originalId = Array.isArray(originalIds) ? originalIds[i] : originalIds;
var matched = originalId.match(pattern);
if(matched && originalId && originalId != ""){
if(originalId.indexOf("opendoar____::") != -1){
this.dataProviderInfo.openDoarId = originalId.split("opendoar____::")[1];
}else if (originalId.indexOf("re3data_____::") != -1){
this.dataProviderInfo.r3DataId = originalId.split("re3data_____::")[1];
}
this.dataProviderInfo.originalId = originalId;
}
}
}
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}>();
}
this.dataProviderInfo.tabs = [];
if(this.dataProviderInfo.tabsInTypes.publicationsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Publications", "content": "publicationsTab"});
this.dataProviderInfo.tabs2.push("Publications");
}
if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Research Data", "content": "datasetsTab"});
this.dataProviderInfo.tabs2.push("Research Data");
}
if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Projects", "content": "projectsTab"});
this.dataProviderInfo.tabs2.push("Projects");
}
if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Content Providers", "content": "datasourcesTab"});
this.dataProviderInfo.tabs2.push("Content Providers");
}
this.dataProviderInfo.tabs.push({"name": "Organizations", "content": "organizationsTab"});
this.dataProviderInfo.tabs2.push("Organizations");
if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Related Content Providers", "content": "relatedDatasourcesTab"});
this.dataProviderInfo.tabs2.push("Related Content Providers");
}
if(this.dataProviderInfo.tabsInTypes.statisticsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Statistics", "content": "statisticsTab"});
this.dataProviderInfo.tabs2.push("Statistics");
}
if(this.dataProviderInfo.tabsInTypes.softwareTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Software", "content": "softwareTab"});
this.dataProviderInfo.tabs2.push("Software");
}
if(this.dataProviderInfo.tabsInTypes.orpsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Other Research Products", "content": "orpsTab"});
this.dataProviderInfo.tabs2.push("Other Research Products");
}
if(this.dataProviderInfo.tabsInTypes.metricsTab.has(data[1].classid)) {
this.dataProviderInfo.tabs.push({"name": "Metrics", "content": "metricsTab"});
this.dataProviderInfo.tabs2.push("Metrics");
}
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 = {"info": "", "name": "", "id": ""};
this.dataProviderInfo.compatibility.info = data[2].classname;
//this.dataProviderInfo.compatibility = data[2].classname;
}
if(data[2] != null && data[2].classid == "hostedBy" && data[3] != null) {
this.dataProviderInfo.compatibility.name = data[3].name;
this.dataProviderInfo.compatibility.id = data[3].id;
if(this.dataProviderInfo.compatibility.name) {
this.dataProviderInfo.compatibility.info = "Collected from ";
}
}
if(data[4] != null) {
let oaiPmhURL:string;
oaiPmhURL = Array.isArray(data[4]) ? data[4][0]:data[4];
if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
}
}
}
if(data[5] != null) {
let mydata;
let counter = 0;
let countriesSet: Set<string>;
let length = data[5].length!=undefined ? data[5].length : 1;
for(let i=0; i<length; i++) {
mydata = data[5].length!=undefined ? data[5][i] : data[5];
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;
}
}