openaire-library/services/organization.service.ts

138 lines
5.3 KiB
TypeScript

import {Injectable} from '@angular/core';
import {HttpClient, HttpErrorResponse} from "@angular/common/http";
import {throwError} from 'rxjs';
import {OrganizationInfo} from '../utils/entities/organizationInfo';
import{EnvProperties} from '../utils/properties/env-properties';
import {map} from "rxjs/operators";
import {ParsingFunctions} from "../landingPages/landing-utils/parsingFunctions.class";
import {properties} from "../../../environments/environment";
@Injectable()
export class OrganizationService {
public parsingFunctions: ParsingFunctions = new ParsingFunctions();
constructor(private http: HttpClient ) {}
organizationInfo: OrganizationInfo;
getOrganizationInfo (id: string, properties:EnvProperties):any {
let url = properties.searchAPIURLLAst+'organizations/'+id+'?format=json';
//'&query=( (oaftype exact organization) and (reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.0 or reldatasourcecompatibilityid=openaire4.0 or reldatasourcecompatibilityid=openaire2.0_data or reldatasourcecompatibilityid=hostedBy or relprojectid=* or reldatasourcecompatibilityid = native)) and ( objIdentifier ='+id+')';
return this.http.get((properties.useCache)? (properties.cacheUrl +encodeURIComponent(url)): url)
//.map(res => <any> res.json())
//.pipe(map(res => res['results']))
//.pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:organization']))
//.map(res => [res, res['rels']['rel']])
.pipe(map(res => this.parseOrganizationInfo(res)));
}
getOrganizationNameAndUrlById(id: string, properties: EnvProperties): any {
let url = properties.searchAPIURLLAst+"organizations/"+id+"?format=json";
return this.http.get((properties.useCache) ? (properties.cacheUrl+encodeURIComponent(url)): url)
//.map(res => <any> res.json())
// .pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:organization']))
.pipe(map(res => this.parseOrganizationNameAndUrl(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');
}
parseOrganizationInfo (data: any):any {
if(data == null) {
return null;
}
this.organizationInfo = new OrganizationInfo();
let organization;
if(data["header"]) {
this.organizationInfo.relcanId = ParsingFunctions.parseRelCanonicalId(data,"organization");
this.organizationInfo.objIdentifier = data["header"]["id"];
if(this.organizationInfo.objIdentifier.startsWith("openorgs____::")) {
this.organizationInfo.relcanId = this.organizationInfo.objIdentifier;
}
}
if(data["organization"]) {
organization = data["organization"];
if (organization["websiteurl"]) {
this.organizationInfo.title = {"name": organization.legalshortname, "url": organization.websiteurl};
} else {
this.organizationInfo.title = {"name": organization.legalshortname, "url": ''};
}
this.organizationInfo.name = organization.legalname;
if (!this.organizationInfo.title.name || this.organizationInfo.title.name == '') {
this.organizationInfo.title.name = this.organizationInfo.name;
}
if (organization["country"]) {
this.organizationInfo.country = organization['country'].label;
}
}
if(data["pid"]) {
this.organizationInfo.identifiers = this.parsingFunctions.parseIdentifiers(data['pid']);
}
if(data["links"]) {
let links = data['links'];
let relLength = Array.isArray(links) ? links.length : 1;
for(let i=0; i<relLength; i++) {
let relation = Array.isArray(links) ? links[i] : links;
if (relation["header"]) {
// TODO: Check if merges or isMergedIn
if (relation['header'].relationClass && relation['header'].relationClass.toLowerCase() == "merges" && relation['header'].relatedRecordType == "organization") {
if(!this.organizationInfo.deletedByInferenceIds) {
this.organizationInfo.deletedByInferenceIds = [];
}
this.organizationInfo.deletedByInferenceIds.push(relation['header'].relatedIdentifier);
if(!this.organizationInfo.children) {
this.organizationInfo.children = [];
}
this.organizationInfo.children.push(relation);
}
}
}
}
return this.organizationInfo;
}
parseOrganizationNameAndUrl(res: any): any {
if(!res || res.organization) {
return null;
}
let organization = res.organization;
let title: {"name": string, "url": string} = {"name": "", "url": ""};
if(organization != null) {
if(organization["websiteurl"]) {
title = {"name": organization.legalshortname, "url": organization.websiteurl};
} else {
title = {"name": organization.legalshortname, "url": ''};
}
if(title.name == '') {
title.name = organization.legalname;
}
}
return title;
}
}