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

366 lines
16 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse, HttpHeaders} from "@angular/common/http";
import {throwError} from 'rxjs';
import {DataProviderInfo, DataproviderProvenance} from '../../utils/entities/dataProviderInfo';
import{EnvProperties} from '../../utils/properties/env-properties';
import {map} from "rxjs/operators";
import {ParsingFunctions} from "../landing-utils/parsingFunctions.class";
import {OpenaireEntities} from "../../utils/properties/searchFields";
import {Identifier, StringUtils} from "../../utils/string-utils.class";
import {properties} from "../../../../environments/environment";
@Injectable()
export class DataProviderService {
constructor(private http: HttpClient ) {
this.parsingFunctions = new ParsingFunctions();
}
dataProviderInfo: DataProviderInfo;
public parsingFunctions: ParsingFunctions;
private buildDatasourceLandingInfoUrl(id: string, identifier: Identifier, typePathParam: string): string {
if (id) {
return properties.searchAPIURLLAst + typePathParam + "/" + id + '?format=json';
} else if (identifier) {
return properties.searchAPIURLLAst + "resources2?query=(pid exact \""+encodeURIComponent(identifier.id) + "\")&type="+typePathParam+"&format=json";
}
}
getDataproviderInfo (id: string, identifier: Identifier, properties: EnvProperties, typePathParam: string): any {
let url: string = this.buildDatasourceLandingInfoUrl(id, identifier, typePathParam);
let finalUrl: string = (properties.useCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url;
return this.http.get(finalUrl)
.pipe(map(res => {
if(!id && identifier) {
if(!res['results'] || res['results'].length == 0) {
throw new HttpErrorResponse({
status: 404,
statusText: "Not found",
url: finalUrl,
error: "Http failure response for "+finalUrl+": 404 Not Found"
});
}
return res['results'][0];
} else {
return res;
}
}))
.pipe(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});
// const options = {
// headers: new HttpHeaders({
// 'Content-Type': 'application/json',
// 'accept': 'application/json'})
// };
//
// 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())
// .pipe(map(res => res['datasourceInfo']))
// .pipe(map(res => this.parseDataproviderAggregationStatus(res)));
return this.http.get(properties.datasourcesAPI+original_id)
.pipe(map(res => res['api']))
.pipe(map(res => this.parseDataproviderAggregationStatus(res)));
}
getCollectedFulltexts(datasourceId: string) {
return this.http.get(properties.pdfStatisticsAPIURL+"/stats/getNumberOfPayloadsForDatasource?datasourceId="+datasourceId)
.pipe(map(res => res))
}
private handleError (error: HttpErrorResponse) {
// 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 throwError(error || 'Server error');
}
parseDataproviderAggregationStatus(apis: any): any {
// var aggregationStatus: {"fundedContent": string, "indexRecords": string, "fulltexts": string, "lastUpdateDate": string} = null;
// if(data != null && data[0] != null) {
// aggregationStatus = {"fundedContent": "-1", "indexRecords": "-1", "fulltexts": "-1", "lastUpdateDate": null};
// aggregationStatus.fundedContent = data[0].fundedContent;
// aggregationStatus.indexRecords = data[0].indexRecords;
// aggregationStatus.fulltexts = data[0].fulltexts;
//
// if(data[0].hasOwnProperty("aggregationHistory")) {
// let length = Array.isArray(data[0]["aggregationHistory"]) ? data[0]["aggregationHistory"].length : 1;
//
// for(let i=0; i<length; i++) {
// var aggregationHistory = Array.isArray(data[0]["aggregationHistory"]) ? data[0]["aggregationHistory"][i] : data[0]["aggregationHistory"];
// if(aggregationHistory && aggregationHistory.indexedVersion == true) {
// aggregationStatus.lastUpdateDate = aggregationHistory.date;
// break;
// }
// }
// }
// }
var aggregationStatus: {"fulltexts": string} = null;
if(apis != null) {
aggregationStatus = {"fulltexts": "-1"};
let mostRecentDate = null;
let length = Array.isArray(apis) ? apis.length : 1;
for (let i = 0; i < length; i++) {
let api = Array.isArray(apis) ? apis[i] : apis;
if(api.compatibility == "files" && (mostRecentDate == null || mostRecentDate < api.lastDownloadDate)) {
aggregationStatus.fulltexts = api.lastDownloadTotal;
mostRecentDate = api.lastDownloadDate;
}
}
}
return aggregationStatus;
}
parseDataProviderInfo (data: any):any {
if(data == null) {
return null;
}
this.dataProviderInfo = new DataProviderInfo();
this.dataProviderInfo.record = data;
if(data["header"]) {
this.dataProviderInfo.objIdentifier = data["header"]["id"];
// TODO: type for canonnical should be datasource or service???
this.dataProviderInfo.relcanId = ParsingFunctions.parseRelCanonicalId(this.dataProviderInfo.record, "datasource");
}
if(data["datasource"]) {
let datasource = data["datasource"];
this.dataProviderInfo.title = {"name": "", "url": datasource.websiteurl};
if(datasource.officialname) {
this.dataProviderInfo.title.name = StringUtils.HTMLToString(String(datasource.officialname));
this.dataProviderInfo.officialName = StringUtils.HTMLToString(String(datasource.officialname));
}
if(datasource.englishname) {
this.dataProviderInfo.title.name = StringUtils.HTMLToString(String(datasource.englishname));
}
var pattern = /.{12}::.+/;
var originalIds =(datasource.originalId)?datasource.originalId:"";
if(originalIds) {
let provenances = new DataproviderProvenance().provenance;
this.dataProviderInfo.provenance = new Map<string, { "url": string[], "name" }>();
const idRegex = RegExp('[^'+'::'+']*$');
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 != "") {
let prefix = originalId.substr(0, 14);
if(provenances.has(prefix)) {
let provenance = provenances.get(prefix);
if(this.dataProviderInfo.provenance.has(provenance.name)) {
this.dataProviderInfo.provenance.get(provenance.name).url.push(provenance.urlPrefix + idRegex.exec(originalId)[0]);
} else {
this.dataProviderInfo.provenance.set(provenance.name, {"url": [provenance.urlPrefix + idRegex.exec(originalId)[0]]})
}
}
this.dataProviderInfo.originalId = originalId;
}
}
}
this.dataProviderInfo.subjects = [];
if(datasource.subjects) {
let length = Array.isArray(datasource['subjects']) ? datasource['subjects'].length : 1;
for (let i = 0; i < length; i++) {
let subject = Array.isArray(datasource['subjects']) ? datasource['subjects'][i] : datasource['subjects'];
if (subject && subject.content) {
this.dataProviderInfo.subjects.push(subject.content);
}
}
}
this.dataProviderInfo.description = this.parsingFunctions.parseDescription(datasource && datasource.description?datasource.description:[]);
this.dataProviderInfo.thematic = datasource.thematic;
if(datasource.jurisdiction) {
this.dataProviderInfo.jurisdiction = datasource.jurisdiction.label;
}
if(datasource.contentpolicies) {
this.dataProviderInfo.contentpolicies = [];
datasource.contentpolicies.forEach(contentpolicy => {
if(contentpolicy.label) {
this.dataProviderInfo.contentpolicies.push(contentpolicy.label);
}
})
}
if(datasource['datasourcetype']) {
let datasourcetype = datasource['datasourcetype'];
this.dataProviderInfo.type = datasourcetype.label;
if(datasourcetype.code == "entityregistry" || datasourcetype.code == "entityregistry::projects" || datasourcetype.code == "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": OpenaireEntities.PUBLICATIONS, "content": "publicationsTab"});
// this.dataProviderInfo.tabs2.push(OpenaireEntities.PUBLICATIONS);
// }
// if(this.dataProviderInfo.tabsInTypes.datasetsTab.has(data[1].classid)) {
// this.dataProviderInfo.tabs.push({"name": OpenaireEntities.DATASETS, "content": "datasetsTab"});
// this.dataProviderInfo.tabs2.push(OpenaireEntities.DATASETS);
// }
//
// if(this.dataProviderInfo.tabsInTypes.projectsTab.has(data[1].classid)) {
// this.dataProviderInfo.tabs.push({"name": OpenaireEntities.PROJECTS, "content": "projectsTab"});
// this.dataProviderInfo.tabs2.push(OpenaireEntities.PROJECTS);
// }
// if(this.dataProviderInfo.tabsInTypes.datasourcesTab.has(data[1].classid)) {
// this.dataProviderInfo.tabs.push({"name": OpenaireEntities.DATASOURCES, "content": "datasourcesTab"});
// this.dataProviderInfo.tabs2.push(OpenaireEntities.DATASOURCES);
// }
//
// if(this.dataProviderInfo.tabsInTypes.relatedDatasourcesTab.has(data[1].classid)) {
// this.dataProviderInfo.tabs.push({"name": "Related "+OpenaireEntities.DATASOURCES, "content": "relatedDatasourcesTab"});
// this.dataProviderInfo.tabs2.push("Related "+OpenaireEntities.DATASOURCES);
// }
//
// 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": OpenaireEntities.SOFTWARE, "content": "softwareTab"});
// this.dataProviderInfo.tabs2.push(OpenaireEntities.SOFTWARE);
// }
//
// if(this.dataProviderInfo.tabsInTypes.orpsTab.has(data[1].classid)) {
// this.dataProviderInfo.tabs.push({"name": OpenaireEntities.OTHER, "content": "orpsTab"});
// this.dataProviderInfo.tabs2.push(OpenaireEntities.OTHER);
// }
//
// 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(datasourcetype.code)) {
this.dataProviderInfo.resultsBy = "collectedFrom";
} else if(this.dataProviderInfo.resultTypes.hostedBy.has(datasourcetype.code)) {
this.dataProviderInfo.resultsBy = "hostedBy";
}
}
if(!this.dataProviderInfo.registry) {
let compatibility = datasource['openairecompatibility'];
if(compatibility) {
this.dataProviderInfo.compatibility = {"info": "", "name": "", "id": ""};
this.dataProviderInfo.compatibility.info = compatibility.label;
}
if(compatibility != null && compatibility.code == "hostedBy" && datasource['collectedfrom'] != null) {
this.dataProviderInfo.compatibility.name = datasource['collectedfrom'].dsName;
this.dataProviderInfo.compatibility.id = datasource['collectedfrom'].dsId;
if(this.dataProviderInfo.compatibility.name) {
this.dataProviderInfo.compatibility.info = "Collected from ";
}
}
if(datasource['accessinfopackage']) {
let oaiPmhURL:string;
oaiPmhURL = Array.isArray(datasource['accessinfopackage']) ? datasource['accessinfopackage'][0]:datasource['accessinfopackage'];
if(oaiPmhURL != '' && oaiPmhURL != 'unknown') {
this.dataProviderInfo.oaiPmhURL = oaiPmhURL;
}
}
}
if(datasource['journal']) {
let journal = datasource['journal'];
this.dataProviderInfo.journal = {"journal": "", "issn": "", "lissn": "", "eissn": ""};
this.dataProviderInfo.journal['journal'] = journal.name;
this.dataProviderInfo.journal['issn'] = journal['issnPrinted'];
this.dataProviderInfo.journal['lissn'] = journal['issnLinking'];
this.dataProviderInfo.journal['eissn'] = journal['issnOnline'];
}else {
this.dataProviderInfo.journal = null;
}
}
if(data['pid']) {
this.dataProviderInfo.identifiers = this.parsingFunctions.parseIdentifiers(data.pid);
}
if(data['links']) {
let links = data['links'];
let relLength = Array.isArray(links) ? links.length : 1;
let counter: number = 0;
let countriesSet: Set<string>;
for (let i = 0; i < relLength; i++) {
let relation = Array.isArray(links) ? links[i] : links;
if (relation["header"] && relation["header"].relationClass && relation['header'].relationClass.toLowerCase() == "isprovidedby" && relation['header'].relatedRecordType == "organization") {
if (this.dataProviderInfo.organizations.length == 0) {
this.dataProviderInfo.countries = new Array<string>();
countriesSet = new Set<string>();
}
this.dataProviderInfo.organizations[counter] = {"acronym": "", "name": "", "id": ""};
this.dataProviderInfo.organizations[counter]['id'] = relation['header'].relatedIdentifier;
if (relation["legalshortname"]) {
this.dataProviderInfo.organizations[counter]['acronym'] = relation.legalshortname;
}
if (relation["legalname"]) {
this.dataProviderInfo.organizations[counter]['name'] = relation.legalname;
}
if (!this.dataProviderInfo.organizations[counter]['acronym'] && !this.dataProviderInfo.organizations[counter]['name']) {
// acronym is displayed with link and name only in tooltip
this.dataProviderInfo.organizations[counter]['acronym'] = "[no title available]";
}
if (relation['header'].country && relation['header']['country'].label) {
if (!countriesSet.has(relation['header']['country'].label)) {
this.dataProviderInfo.countries.push(relation['header']['country'].label);
countriesSet.add(relation['header']['country'].label);
}
}
counter++;
}
}
}
if (data?.measure) {
this.dataProviderInfo.measure = this.parsingFunctions.parseMeasures(data.measure);
}
return this.dataProviderInfo;
}
}