2017-12-19 13:53:46 +01:00
import { Injectable } from '@angular/core' ;
import { Http , Response } from '@angular/http' ;
import { Observable } from 'rxjs/Observable' ;
import { OrganizationInfo } from '../utils/entities/organizationInfo' ;
import 'rxjs/add/observable/of' ;
import 'rxjs/add/operator/do' ;
import 'rxjs/add/operator/share' ;
2018-02-05 14:14:59 +01:00
import { EnvProperties } from '../utils/properties/env-properties' ;
2017-12-19 13:53:46 +01:00
@Injectable ( )
export class OrganizationService {
constructor ( private http : Http ) { }
organizationInfo : OrganizationInfo ;
2018-02-05 14:14:59 +01:00
getOrganizationInfo ( id : string , properties :EnvProperties ) : any {
2017-12-19 13:53:46 +01:00
console . info ( "getOrganizationInfo in service" ) ;
2018-05-14 13:42:38 +02:00
let url = properties . searchAPIURLLAst + 'resources?format=json&query=( (oaftype exact organization) and (reldatasourcecompatibilityid=driver or reldatasourcecompatibilityid=driver-openaire2.0 or reldatasourcecompatibilityid=openaire2.0 or reldatasourcecompatibilityid=openaire3.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
//let url = "http://beta.services.openaire.eu:8480/search/rest/v2/api/resources?format=json&query=(%20(oaftype%20exact%20organization)%20and%20(reldatasourcecompatibilityid=driver%20or%20reldatasourcecompatibilityid=driver-openaire2.0%20or%20reldatasourcecompatibilityid=openaire2.0%20or%20reldatasourcecompatibilityid=openaire3.0%20or%20reldatasourcecompatibilityid=openaire2.0_data%20or%20reldatasourcecompatibilityid=hostedBy%20or%20relprojectid=*))%20and%20(%20objIdentifier%20=dedup_wf_001%3A%3Af1e63493def7cedfa5b497cdbea26faa)";
2018-02-05 14:14:59 +01:00
return this . http . get ( ( properties . useCache ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url )
2017-12-19 13:53:46 +01:00
. map ( res = > < any > res . json ( ) )
. map ( res = > res [ 'results' ] [ 0 ] )
//.map(res => res[0]['result']['metadata']['oaf:entity']['oaf:organization'])
//.map(res => [res, res['rels']['rel']])
. map ( res = > this . parseOrganizationInfo ( res ) ) ;
}
2018-02-05 14:14:59 +01:00
2017-12-19 13:53:46 +01:00
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' ) ;
}
parseOrganizationInfo ( data : any ) : any {
console . info ( "parseOrganizationInfo" ) ;
this . organizationInfo = new OrganizationInfo ( ) ;
let organization ;
let relations ;
if ( data != null ) {
organization = data [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] ;
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 ;
if ( this . organizationInfo . title . name == '' ) {
this . organizationInfo . title . name = this . organizationInfo . name ;
}
if ( organization . hasOwnProperty ( "country" ) ) {
this . organizationInfo . country = organization [ 'country' ] . classname ;
}
}
//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 ) ) {
this . organizationInfo . projects . set ( relation [ 'funding' ] [ 'funder' ] . name ,
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 ;
}
}