2017-12-19 13:53:46 +01:00
import { Injectable } from '@angular/core' ;
2019-06-03 15:20:36 +02:00
import { HttpClient , HttpErrorResponse } from "@angular/common/http" ;
2020-11-11 15:43:13 +01:00
import { throwError } from 'rxjs' ;
2017-12-19 13:53:46 +01:00
import { OrganizationInfo } from '../utils/entities/organizationInfo' ;
2019-06-03 15:20:36 +02:00
2018-02-05 14:14:59 +01:00
import { EnvProperties } from '../utils/properties/env-properties' ;
2019-06-03 15:20:36 +02:00
import { map } from "rxjs/operators" ;
2020-08-19 13:01:22 +02:00
import { ParsingFunctions } from "../landingPages/landing-utils/parsingFunctions.class" ;
2017-12-19 13:53:46 +01:00
@Injectable ( )
export class OrganizationService {
2019-06-03 15:20:36 +02:00
constructor ( private http : HttpClient ) { }
2017-12-19 13:53:46 +01:00
organizationInfo : OrganizationInfo ;
2018-02-05 14:14:59 +01:00
getOrganizationInfo ( id : string , properties :EnvProperties ) : any {
2020-07-15 13:25:17 +02:00
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+')';
2017-12-19 13:53:46 +01:00
2018-02-05 14:14:59 +01:00
return this . http . get ( ( properties . useCache ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url )
2019-06-03 15:20:36 +02:00
//.map(res => <any> res.json())
2020-07-15 13:25:17 +02:00
//.pipe(map(res => res['results']))
//.pipe(map(res => res['result']['metadata']['oaf:entity']['oaf:organization']))
2017-12-19 13:53:46 +01:00
//.map(res => [res, res['rels']['rel']])
2019-06-03 15:20:36 +02:00
. pipe ( map ( res = > this . parseOrganizationInfo ( res ) ) ) ;
2017-12-19 13:53:46 +01:00
}
2018-02-05 14:14:59 +01:00
2019-02-12 12:15:21 +01:00
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 )
2019-06-03 15:20:36 +02:00
//.map(res => <any> res.json())
. pipe ( map ( res = > res [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] ) )
. pipe ( map ( res = > this . parseOrganizationNameAndUrl ( res ) ) ) ;
2019-02-12 12:15:21 +01:00
}
2019-06-03 15:20:36 +02:00
private handleError ( error : HttpErrorResponse ) {
2017-12-19 13:53:46 +01:00
// 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 ) ;
2019-06-03 15:20:36 +02:00
return throwError ( error || 'Server error' ) ;
2017-12-19 13:53:46 +01:00
}
parseOrganizationInfo ( data : any ) : any {
this . organizationInfo = new OrganizationInfo ( ) ;
2020-08-19 13:01:22 +02:00
this . organizationInfo . relcanId = ParsingFunctions . parseRelCanonicalId ( data , "organization" ) ;
2017-12-19 13:53:46 +01:00
let organization ;
let relations ;
if ( data != null ) {
organization = data [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] ;
2019-11-07 10:51:09 +01:00
this . organizationInfo . objIdentifier = data [ "result" ] [ "header" ] [ "dri:objIdentifier" ] ;
2017-12-19 13:53:46 +01:00
relations = data [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] [ 'rels' ] [ 'rel' ] ;
} else {
return null ;
}
if ( organization != null ) {
if ( organization . hasOwnProperty ( "websiteurl" ) ) {
this . organizationInfo . title = { "name" : organization . legalshortname , "url" : organization . websiteurl } ;
} else {
this . organizationInfo . title = { "name" : organization . legalshortname , "url" : '' } ;
}
this . organizationInfo . name = organization . legalname ;
2020-07-14 11:57:11 +02:00
if ( ! this . organizationInfo . title . name || this . organizationInfo . title . name == '' ) {
2017-12-19 13:53:46 +01:00
this . organizationInfo . title . name = this . organizationInfo . name ;
}
if ( organization . hasOwnProperty ( "country" ) ) {
this . organizationInfo . country = organization [ 'country' ] . classname ;
}
2019-10-02 16:15:08 +02:00
if ( organization . hasOwnProperty ( "children" ) ) {
let children = organization [ 'children' ] ;
if ( children . hasOwnProperty ( "organization" ) ) {
this . organizationInfo . deletedByInferenceIds = [ ] ;
let length = Array . isArray ( children [ 'organization' ] ) ? children [ 'organization' ] . length : 1 ;
for ( let i = 0 ; i < length ; i ++ ) {
let result = Array . isArray ( children [ 'organization' ] ) ? children [ 'organization' ] [ i ] : children [ 'organization' ] ;
this . organizationInfo . deletedByInferenceIds . push ( result . objidentifier ) ;
}
}
}
2017-12-19 13:53:46 +01:00
}
//Comment Parsing Projects info
/ *
if ( data [ 1 ] != null ) {
let counter ;
let length = relations . length != undefined ? relations.length : 1 ;
for ( let i = 0 ; i < length ; i + + ) {
let relation = relations . length != undefined ? relations [ i ] : relations ;
if ( relation . hasOwnProperty ( "to" ) ) {
if ( relation [ 'to' ] . class == "isParticipant" ) {
if ( relation . hasOwnProperty ( "funding" ) ) {
if ( relation [ 'funding' ] . hasOwnProperty ( "funder" ) ) {
if ( this . organizationInfo . projects == undefined ) {
this . organizationInfo . projects = new Map < string ,
{ "name" : string , "id" : string , "code" : string ,
"acronym" : string , "funder" : string , "funderId" : string ,
"fundingStream" : string , "fundingLevel1" : string , "fundingLevel2" : string ,
"sc39" : string , "startDate" : string , "endDate" : string } [ ] > ( ) ;
}
if ( ! this . organizationInfo . projects . has ( relation [ 'funding' ] [ 'funder' ] . name ) ) {
2019-06-12 16:13:56 +02:00
this . organizationInfo . projects . setValues ( relation [ 'funding' ] [ 'funder' ] . name ,
2017-12-19 13:53:46 +01:00
new Array < { "name" : string , "id" : string , "code" : string ,
"acronym" : string , "funder" : string , "funderId" : string ,
"fundingStream" : string , "fundingLevel1" : string , "fundingLevel2" : string ,
"sc39" : string , "startDate" : string , "endDate" : string } > ( ) ) ;
}
counter = this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) . length ;
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] =
{ "name" : "" , "id" : "" , "code" : "" ,
"acronym" : "" , "funder" : "" , "funderId" : "" ,
"fundingStream" : "" , "fundingLevel1" : "" , "fundingLevel2" : "" ,
"sc39" : "" , "startDate" : "" , "endDate" : "" } ;
//let url = "";
if ( relation [ 'to' ] . content != null && relation [ 'to' ] . content != "" ) {
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'id' ] = relation [ 'to' ] . content ;
//url = OpenaireProperties.getsearchLinkToProject()+relation['to'].content;
}
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'name' ] = relation . title ;
//this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['url'] = url;
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'code' ] = relation . code ;
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'acronym' ] = relation . acronym ;
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'funder' ] = relation [ 'funding' ] [ 'funder' ] . shortname ;
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'funderId' ] = relation [ 'funding' ] [ 'funder' ] . id ;
if ( relation [ 'funding' ] . hasOwnProperty ( "funding_level_0" ) ) {
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'fundingStream' ] = relation [ 'funding' ] [ 'funding_level_0' ] . name ;
}
if ( relation [ 'funding' ] . hasOwnProperty ( "funding_level_1" ) ) {
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'fundingLevel1' ] = relation [ 'funding' ] [ 'funding_level_1' ] . name ;
}
if ( relation [ 'funding' ] . hasOwnProperty ( "funding_level_2" ) ) {
this . organizationInfo . projects . get ( relation [ 'funding' ] [ 'funder' ] . name ) [ counter ] [ 'fundingLevel2' ] = relation [ 'funding' ] [ 'funding_level_2' ] . name ;
}
//this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['sc39'] =
//this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['startDate'] =
//this.organizationInfo.projects.get(relation['funding']['funder'].name)[counter]['endDate'] =
}
}
}
} / * else if ( relation [ 'to' ] . class == "isProvidedBy" ) {
if ( this . organizationInfo . dataProviders == undefined ) {
this . organizationInfo . dataProviders = new Array < { "name" : string , "url" : string , "type" : string , "websiteUrl" : string , "organizations" : { "name" : string , "url" : string } [ ] } > ( ) ;
}
counter = this . organizationInfo . dataProviders . length ;
this . organizationInfo . dataProviders [ counter ] = { "name" : "" , "url" : "" , "type" : "" , "websiteUrl" : "" , "organizations" : [ ] }
let url = "" ;
if ( relation [ 'to' ] . content != null && relation [ 'to' ] . content != "" ) {
url = OpenaireProperties . getsearchLinkToDataProvider ( ) + relation [ 'to' ] . content ;
}
this . organizationInfo . dataProviders [ counter ] [ 'name' ] = relation . officialname ;
this . organizationInfo . dataProviders [ counter ] [ 'url' ] = url ;
if ( relation . hasOwnProperty ( "datasourcetype" ) ) {
this . organizationInfo . dataProviders [ counter ] [ 'type' ] = relation [ 'datasourcetype' ] . classname ;
}
this . organizationInfo . dataProviders [ counter ] [ 'websiteUrl' ] = relation . websiteurl ;
} * /
/ * - - - - - >
}
}
}
* /
return this . organizationInfo ;
}
2019-02-12 12:15:21 +01:00
parseOrganizationNameAndUrl ( organization : any ) : any {
let title : { "name" : string , "url" : string } = { "name" : "" , "url" : "" } ;
if ( organization != null ) {
if ( organization . hasOwnProperty ( "websiteurl" ) ) {
title = { "name" : organization . legalshortname , "url" : organization . websiteurl } ;
} else {
title = { "name" : organization . legalshortname , "url" : '' } ;
}
if ( title . name == '' ) {
title . name = organization . legalname ;
}
}
return title ;
}
2017-12-19 13:53:46 +01:00
}