2017-12-19 13:53:46 +01:00
import { Injectable } from '@angular/core' ;
2019-06-03 15:20:36 +02:00
import { HttpClient } from "@angular/common/http" ;
2017-12-19 13:53:46 +01:00
import { SearchResult } from '../utils/entities/searchResult' ;
import { RefineResultsUtils } from './servicesUtils/refineResults.class' ;
2018-02-05 14:14:59 +01:00
import { EnvProperties } from '../utils/properties/env-properties' ;
2018-10-30 12:37:52 +01:00
import { StringUtils } from '../utils/string-utils.class' ;
2019-06-03 15:20:36 +02:00
import { map } from "rxjs/operators" ;
2021-02-11 11:20:10 +01:00
import { ParsingFunctions } from "../landingPages/landing-utils/parsingFunctions.class" ;
2024-03-04 12:14:09 +01:00
import { properties } from "../../../environments/environment" ;
2017-12-19 13:53:46 +01:00
@Injectable ( )
export class SearchOrganizationsService {
2024-03-04 12:14:09 +01:00
public parsingFunctions : ParsingFunctions = new ParsingFunctions ( ) ;
2017-12-19 13:53:46 +01:00
2019-06-03 15:20:36 +02:00
constructor ( private http : HttpClient ) { }
2017-12-19 13:53:46 +01:00
parseResultsForDeposit ( data : any ) : { "name" : string , "id" : string } [ ] {
let results : { "name" : string , "id" : string } [ ] = [ ] ;
let length = Array . isArray ( data ) ? data.length : 1 ;
for ( let i = 0 ; i < length ; i + + ) {
let name : string = '' ;
let id : string = '' ;
let resData = Array . isArray ( data ) ? data [ i ] [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] : data [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] ;
name = resData . legalname ;
if ( name == '' ) {
name = resData . legalshortname ;
}
id = Array . isArray ( data ) ? data [ i ] [ 'result' ] [ 'header' ] [ 'dri:objIdentifier' ] : data [ 'result' ] [ 'header' ] [ 'dri:objIdentifier' ] ;
results . push ( { "name" : name , "id" : id } ) ;
}
return results ;
}
2018-02-05 14:14:59 +01:00
searchOrganizations ( params : string , refineParams :string , page : number , size : number , refineFields :string [ ] , properties :EnvProperties ) : any {
2017-12-19 13:53:46 +01:00
2018-02-05 14:14:59 +01:00
let link = properties . searchAPIURLLAst + "organizations" ;
2017-12-19 13:53:46 +01:00
let url = link + "?" ;
if ( params != null && params != '' ) {
url += params ;
}
if ( refineParams != null && refineParams != '' ) {
url += refineParams ;
}
url += "&page=" + ( page - 1 ) + "&size=" + size + "&format=json" ;
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())
. pipe ( map ( res = > [ res [ 'meta' ] . total , this . parseResults ( res [ 'results' ] ) , RefineResultsUtils . parse ( res [ 'refineResults' ] , refineFields , "organization" ) ] ) ) ;
2017-12-19 13:53:46 +01:00
}
2023-12-14 17:14:57 +01:00
advancedSearchOrganizations ( params : string , page : number , size : number , properties :EnvProperties , refineParams :string = null , refineFields :string [ ] = null , refineQuery :string = null , minRef : boolean = false ) : any {
2020-02-24 14:45:22 +01:00
// &type=organizations
let url = properties . searchAPIURLLAst + "resources2/?format=json" ;
2020-02-13 15:44:51 +01:00
var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
2020-02-26 12:24:50 +01:00
"reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or " +
2020-07-15 13:25:17 +02:00
"reldatasourcecompatibilityid exact openaire-cris_1.1 or " +
2018-12-19 15:07:02 +01:00
"reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)" ;
2024-02-01 18:46:34 +01:00
// url += "&query=";
2017-12-19 13:53:46 +01:00
if ( params != null && params != '' ) {
2024-02-01 18:46:34 +01:00
// url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
url += "&query=(" + params + ")" ;
// }else{
// url +=" ( "+basicQuery+ " ) ";
2017-12-19 13:53:46 +01:00
}
2020-02-11 15:56:20 +01:00
if ( refineParams != null && refineParams != '' ) {
url += refineParams ;
}
if ( refineQuery ) {
url += "&" + refineQuery ;
}
2024-02-01 18:46:34 +01:00
url += "&fq=" + basicQuery ;
2017-12-19 13:53:46 +01:00
url += "&page=" + ( page - 1 ) + "&size=" + size ;
2023-12-14 17:14:57 +01:00
url += minRef ? "&minRef=true" : "" ;
2017-12-19 13:53:46 +01:00
2024-02-08 17:05:27 +01:00
// return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
return this . http . get ( ( properties . useLongCache && size == 0 && ! params && ( ! refineQuery || ! refineQuery . includes ( "fq=" ) ) ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url )
//.map(res => <any> res.json())
2017-12-19 13:53:46 +01:00
2020-02-11 15:56:20 +01:00
. pipe ( map ( res = > [ res [ 'meta' ] . total , this . parseResults ( res [ 'results' ] ) , RefineResultsUtils . parse ( res [ 'refineResults' ] , refineFields , "organization" ) ] ) ) ;
2017-12-19 13:53:46 +01:00
}
parseResults ( data : any ) : SearchResult [ ] {
let results : SearchResult [ ] = [ ] ;
let length = Array . isArray ( data ) ? data.length : 1 ;
for ( let i = 0 ; i < length ; i + + ) {
let resData = Array . isArray ( data ) ? data [ i ] [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] : data [ 'result' ] [ 'metadata' ] [ 'oaf:entity' ] [ 'oaf:organization' ] ;
var result : SearchResult = new SearchResult ( ) ;
2022-06-15 12:54:49 +02:00
result [ 'title' ] = { "name" : '' , "accessMode" : '' } ;
2017-12-19 13:53:46 +01:00
2022-11-28 18:22:01 +01:00
if ( resData . legalshortname ) {
result [ 'title' ] . name = StringUtils . HTMLToString ( String ( resData . legalshortname ) ) ;
}
2020-07-14 11:57:11 +02:00
if ( ! result [ 'title' ] . name || result [ 'title' ] . name == '' ) {
2022-11-28 18:22:01 +01:00
result [ 'title' ] . name = StringUtils . HTMLToString ( String ( resData . legalname ) ) ;
2017-12-19 13:53:46 +01:00
}
//result['title'].url = OpenaireProperties.getsearchLinkToOrganization();
//result['title'].url += Array.isArray(data) ? data[i]['result']['header']['dri:objIdentifier'] : data['result']['header']['dri:objIdentifier'];
result [ 'id' ] = Array . isArray ( data ) ? data [ i ] [ 'result' ] [ 'header' ] [ 'dri:objIdentifier' ] : data [ 'result' ] [ 'header' ] [ 'dri:objIdentifier' ] ;
2022-07-12 12:20:28 +02:00
if ( ! result [ 'id' ] . startsWith ( "openorgs____::" ) ) {
let canId = ParsingFunctions . parseRelCanonicalId ( Array . isArray ( data ) ? data [ i ] : data , "organization" ) ;
if ( canId ) {
2021-02-11 11:20:10 +01:00
result [ 'id' ] = canId ;
2022-07-12 12:20:28 +02:00
}
2021-02-11 11:20:10 +01:00
}
2017-12-19 13:53:46 +01:00
if ( resData [ 'rels' ] . hasOwnProperty ( "rel" ) ) {
let relLength = Array . isArray ( resData [ 'rels' ] [ 'rel' ] ) ? resData [ 'rels' ] [ 'rel' ] . length : 1 ;
for ( let j = 0 ; j < relLength ; j + + ) {
let relation = Array . isArray ( resData [ 'rels' ] [ 'rel' ] ) ? resData [ 'rels' ] [ 'rel' ] [ j ] : resData [ 'rels' ] [ 'rel' ] ;
if ( relation . hasOwnProperty ( "to" ) ) {
2021-09-27 11:19:24 +02:00
if ( relation [ 'to' ] . class && relation [ 'to' ] . class . toLowerCase ( ) == "isparticipant" ) {
2017-12-19 13:53:46 +01:00
if ( result [ 'projects' ] == undefined ) {
result [ 'projects' ] = new Array <
{ "id" : string , "acronym" : string , "title" : string ,
"funderShortname" : string , "funderName" : string ,
"code" : string
} > ( ) ;
}
let countProjects = result [ 'projects' ] . length ;
result [ 'projects' ] [ countProjects ] = {
"id" : "" , "acronym" : "" , "title" : "" ,
"funderShortname" : "" , "funderName" : "" ,
"code" : ""
}
if ( relation . title != 'unidentified' ) {
result [ 'projects' ] [ countProjects ] [ 'id' ] =
/*OpenaireProperties.getsearchLinkToProject() + */ relation [ 'to' ] . content ;
result [ 'projects' ] [ countProjects ] [ 'acronym' ] = relation . acronym ;
result [ 'projects' ] [ countProjects ] [ 'title' ] = relation . title ;
result [ 'projects' ] [ countProjects ] [ 'code' ] = relation . code ;
} else {
result [ 'projects' ] [ countProjects ] [ 'id' ] = "" ;
result [ 'projects' ] [ countProjects ] [ 'acronym' ] = "" ;
result [ 'projects' ] [ countProjects ] [ 'title' ] = "" ;
result [ 'projects' ] [ countProjects ] [ 'code' ] = "" ;
}
if ( relation . hasOwnProperty ( "funding" ) ) {
let fundingLength = Array . isArray ( relation [ 'funding' ] ) ? relation [ 'funding' ] . length : 1 ;
for ( let z = 0 ; z < fundingLength ; z + + ) {
let fundingData = Array . isArray ( relation [ 'funding' ] ) ? relation [ 'funding' ] [ z ] : relation [ 'funding' ] ;
if ( fundingData . hasOwnProperty ( "funder" ) ) {
result [ 'projects' ] [ countProjects ] [ 'funderShortname' ] = fundingData [ 'funder' ] . shortname ;
result [ 'projects' ] [ countProjects ] [ 'funderName' ] = fundingData [ 'funder' ] . name ;
}
}
}
}
}
}
}
if ( resData . country . hasOwnProperty ( "classname" ) ) {
result . country = resData . country . classname ;
}
2024-03-04 12:14:09 +01:00
if ( resData [ 'pid' ] && properties . environment != "production" ) {
result . identifiers = this . parsingFunctions . parseIdentifiers ( resData [ 'pid' ] ) ;
}
results . push ( result ) ;
2017-12-19 13:53:46 +01:00
}
return results ;
}
2018-02-05 14:14:59 +01:00
numOfOrganizations ( url : string , properties :EnvProperties ) : any {
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())
. pipe ( map ( res = > res [ 'total' ] ) ) ;
2017-12-19 13:53:46 +01:00
}
2018-02-05 14:14:59 +01:00
numOfEntityOrganizations ( id : string , entity : string , properties :EnvProperties ) : any {
2017-12-19 13:53:46 +01:00
// currently not used - if used fix entity comparison below
var parameters : string = "" ;
if ( entity == "organization" ) {
parameters = "organizations/" + id + "/organizations/count" ;
}
2018-02-05 14:14:59 +01:00
let url = properties . searchAPIURLLAst + parameters + "?format=json" ;
return this . numOfOrganizations ( url , properties ) ;
2017-12-19 13:53:46 +01:00
}
2019-06-12 16:13:56 +02:00
numOfSearchOrganizations ( params : string , properties :EnvProperties , refineParams :string = null ) : any {
2018-02-05 14:14:59 +01:00
let url = properties . searchAPIURLLAst + "organizations/count?format=json" ;
2017-12-19 13:53:46 +01:00
if ( params != "" ) {
2018-10-30 12:37:52 +01:00
url += "&q=" + StringUtils . URIEncode ( params ) ;
2017-12-19 13:53:46 +01:00
}
2019-06-12 16:13:56 +02:00
if ( refineParams != null && refineParams != '' ) {
url += refineParams ;
}
2018-02-05 14:14:59 +01:00
return this . numOfOrganizations ( url , properties ) ;
2017-12-19 13:53:46 +01:00
}
2020-04-07 13:11:31 +02:00
numOfSearchOrganizations2 ( params : string , properties :EnvProperties , refineParams :string = null ) : any {
let url = properties . searchAPIURLLAst + "resources2/?format=json&size=0&type=organizations" ;
2020-07-29 16:04:18 +02:00
var basicQuery = "(reldatasourcecompatibilityid exact driver or reldatasourcecompatibilityid exact driver-openaire2.0 or " +
"reldatasourcecompatibilityid exact openaire2.0 or reldatasourcecompatibilityid exact openaire3.0 or reldatasourcecompatibilityid exact openaire4.0 or " +
"reldatasourcecompatibilityid exact openaire-cris_1.1 or " +
"reldatasourcecompatibilityid exact openaire2.0_data or reldatasourcecompatibilityid exact hostedBy or relproject=*)" ;
2024-02-01 18:46:34 +01:00
// url += "&query=";
2020-07-29 16:04:18 +02:00
if ( params != null && params != '' ) {
2024-02-01 18:46:34 +01:00
// url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
url += "&query=( " + params + " ) " ;
// }else{
// url +=" ( "+basicQuery+ " ) ";
2020-04-07 13:11:31 +02:00
}
if ( refineParams != null && refineParams != '' ) {
url += refineParams ;
}
2024-02-01 18:46:34 +01:00
url += "&fq=" + basicQuery ;
2020-04-07 13:11:31 +02:00
return this . http . get ( ( properties . useCache ) ? ( properties . cacheUrl + encodeURIComponent ( url ) ) : url )
. pipe ( map ( res = > res [ 'meta' ] [ 'total' ] ) ) ;
}
2017-12-19 13:53:46 +01:00
}