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 'rxjs/add/observable/of';
|
|
|
|
import 'rxjs/add/operator/do';
|
|
|
|
import 'rxjs/add/operator/share';
|
|
|
|
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';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
export class SearchOrganizationsService {
|
|
|
|
|
|
|
|
constructor(private http: Http ) {}
|
|
|
|
|
|
|
|
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});
|
|
|
|
}
|
|
|
|
console.info(results);
|
|
|
|
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
|
|
|
|
|
|
|
console.info("In searchOrganizations");
|
|
|
|
|
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)
|
2017-12-19 13:53:46 +01:00
|
|
|
.map(res => <any> res.json())
|
|
|
|
//.do(res => console.info(res))
|
|
|
|
|
|
|
|
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "organization")]);
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
advancedSearchOrganizations (params: string, page: number, size: number, properties:EnvProperties ):any {
|
|
|
|
let url = properties.searchResourcesAPIURL;
|
2017-12-19 13:53:46 +01:00
|
|
|
var basicQuery = "(oaftype exact organization) "
|
|
|
|
url += "?query=";
|
|
|
|
if(params!= null && params != '' ) {
|
|
|
|
url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
|
|
|
|
}else{
|
|
|
|
url +=" ( "+basicQuery+ " ) ";
|
|
|
|
}
|
|
|
|
|
|
|
|
url += "&page="+(page-1)+"&size="+size;
|
|
|
|
url += "&format=json";
|
|
|
|
|
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())
|
|
|
|
//.do(res => console.info(res))
|
|
|
|
|
|
|
|
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
|
|
|
|
}
|
|
|
|
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();
|
|
|
|
|
|
|
|
result['title'] = {"name": '', "accessMode": '', "sc39": ''};
|
|
|
|
|
|
|
|
result['title'].name = resData.legalshortname;
|
|
|
|
if(result['title'].name == '') {
|
|
|
|
result['title'].name = resData.legalname;
|
|
|
|
}
|
|
|
|
|
|
|
|
//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'];
|
|
|
|
|
|
|
|
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")) {
|
|
|
|
if(relation['to'].class == "isParticipant") {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
results.push(result);
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2017-12-19 13:53:46 +01:00
|
|
|
.map(res => <any> res.json())
|
|
|
|
.map(res => res.total);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
numOfSearchOrganizations(params: string, properties:EnvProperties ):any {
|
|
|
|
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
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
return this.numOfOrganizations(url, properties);
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|