openaire-library/services/searchDataproviders.service.ts

462 lines
21 KiB
TypeScript

import {Injectable} from '@angular/core';
import {Http, Response} from '@angular/http';
import {Observable} from 'rxjs/Observable';
import {SearchResult} from '../utils/entities/searchResult';
import {RefineResultsUtils} from './servicesUtils/refineResults.class';
import 'rxjs/add/observable/of';
import 'rxjs/add/operator/do';
import 'rxjs/add/operator/share';
import {StringUtils} from '../utils/string-utils.class';
import{EnvProperties} from '../utils/properties/env-properties';
@Injectable()
export class SearchDataprovidersService {
constructor(private http: Http ) {}
searchDataproviders (params: string, refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
let link = properties.searchAPIURLLAst+"datasources";
let url = link+"?";
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
}
//((oaftype exact datasource) and(collectedfromdatasourceid exact "openaire____::47ce9e9f4fad46e732cff06419ecaabb"))
advancedSearchDataproviders (params: string, page: number, size: number, properties: EnvProperties ):any {
let url = properties.searchResourcesAPIURL;
var basicQuery = "(oaftype exact datasource) " +
'and (datasourcecompatibilityid <> "UNKNOWN")';
url += "?query=";
if(params!= null && params != '' ) {
url +=" ( "+basicQuery+ " ) " +" and (" + params + ")";
}else{
url +=" ( "+basicQuery+ " ) ";
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results'])])
}
searchCompatibleDataprovidersTable ( properties:EnvProperties):any {
let size: number = 0;
let url: string= properties.searchResourcesAPIURL;
url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other) not(datasourcetypeuiid exact "pubsrepository::journal") not(datasourcetypeuiid exact "aggregator::pubsrepository::journals"))';
url += "&page=0&size=0&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).map(res => <any> res.json())
.map(res => res['meta'].total);
}
searchCompatibleDataproviders (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
let url: string = properties.searchResourcesAPIURL;
url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other) not(datasourcetypeuiid exact "pubsrepository::journal") not(datasourcetypeuiid exact "aggregator::pubsrepository::journals"))';
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
}
searchEntityRegistriesTable (properties:EnvProperties):any {
let size: number = 0;
let url: string= properties.searchResourcesAPIURL;
url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
url += "&page=0&size=0&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).map(res => <any> res.json())
.map(res => res['meta'].total);
}
searchEntityRegistries (params: string,refineParams:string, page: number, size: number, refineFields:string[], properties:EnvProperties ):any {
let url = properties.searchResourcesAPIURL;
url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
}
searchJournalsTable ( properties:EnvProperties):any {
let size: number = 0;
let url: string= properties.searchResourcesAPIURL;
url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
url += "&page=0&size=0&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).map(res => <any> res.json())
.map(res => res['meta'].total);
}
searchJournals (params: string,refineParams:string, page: number, size: number, refineFields:string[] , properties:EnvProperties):any {
let url: string = properties.searchResourcesAPIURL;
//url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => [res['meta'].total, this.parseResults(res['results']),RefineResultsUtils.parse(res['refineResults'],refineFields, "datasource")]);
}
searchDataprovidersForDeposit (id: string,type:string, page: number, size: number, properties:EnvProperties):any {
let link = properties.searchResourcesAPIURL;
var compatibilities = "";
if(type == "Research Data"){
//compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
compatibilities = " and (datasourcecompatibilityid = openaire2.0_data)";
}else if(type == "Publications"){
//compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
compatibilities = " and (datasourcecompatibilityid <> openaire2.0_data)";
}
let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+compatibilities+" ":"")+") and (relorganizationid exact "+id+")";
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
searchDataProvidersBySubjects(keyword:string, type:string, page: number, size: number, properties:EnvProperties):any {
let link = properties.searchResourcesAPIURL;
var compatibilities = "";
if(type == "Research Data"){
compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid = openaire2.0_data)"
}else if(type == "Publications"){
compatibilities = " and (datasourcecompatibilityid <> UNKNOWN) and (datasourcecompatibilityid <> openaire2.0_data)"
}
let url = link+"?query=(((deletedbyinference = false) AND (oaftype exact datasource)) "+((compatibilities && compatibilities.length > 0)?" "+
compatibilities+" ":"")+") "+
" and (datasourcesubject all "+'"'+keyword+'"'+") " ;
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
getDataProvidersforEntityRegistry(datasourceId: string, page: number, size: number , properties:EnvProperties):any {
let url = properties.searchResourcesAPIURL;
var basicQuery = "(oaftype exact datasource) "
url += "?query=";
if(datasourceId!= null && datasourceId != '' ) {
url +=" ( "+basicQuery+ " ) " +" and (collectedfromdatasourceid exact \"" + datasourceId + "\")";
}else{
url +=" ( "+basicQuery+ " ) ";
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
searchDataprovidersForEntity (params: string, page: number, size: number, properties:EnvProperties):any {
let link = properties.searchAPIURLLAst;
let url = link+params+"/datasources?format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => [res['meta'].total, this.parseResults(res['results'])]);
}
/*
searchDataprovidersCSV (params: string, refineParams:string, page: number, size: number):any {
let link = OpenaireProperties. getSearchAPIURLLast()+"datasources";
let url = link+"?";
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page-1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => this.parseResultsCSV(res['results']));
}
*/
/*
searchEntityRegistriesCSV (params: string,refineParams:string, page: number, size: number):any {
let url = properties.searchAPIURLLAst;
url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))"
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page - 1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => this.parseResultsCSV(res['results']));
}
*/
/*
searchCompatibleDataprovidersCSV (params: string,refineParams:string, page: number, size: number):any {
let url = properties.searchAPIURLLAst;
url += "?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other))"
if(params!= null && params != '' ) {
url += params;
}
if(refineParams!= null && refineParams != '' ) {
url += refineParams;
}
url += "&page="+(page - 1)+"&size="+size+"&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
//.do(res => console.info(res))
.map(res => this.parseResultsCSV(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:datasource'] : data['result']['metadata']['oaf:entity']['oaf:datasource'];
var result: SearchResult = new SearchResult();
result['title'] = {"name": '', "accessMode": '', "sc39": ''};
result['title'].name = resData.officialname;
result['englishname'] = resData.englishname;
//result['title'].url = OpenaireProperties.getsearchLinkToDataProvider();
//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'];
result['type'] = this.getDataproviderType(resData);
let typeid: string = resData['datasourcetype'].classid;
//console.info(typeid);
if(typeid != "entityregistry" && typeid != "entityregistry::projects" && typeid != "entityregistry::repositories") {
if(resData.hasOwnProperty('accessinfopackage')) {
let OAIPMHURL: string;
if(Array.isArray(resData['accessinfopackage'])) {
OAIPMHURL = resData['accessinfopackage'][0];
} else {
OAIPMHURL = resData['accessinfopackage'];
}
if(OAIPMHURL != '' && OAIPMHURL != 'unknown') {
result['OAIPMHURL'] = OAIPMHURL;
}
}
let compatibility = this.getDataproviderCompatibility(resData);
result['compatibility'] = compatibility.classname;
if(compatibility.classid == "UNKNOWN") {
result['compatibilityUNKNOWN'] = true;
}
} else {
result['compatibility'] = "";
}
result['websiteURL'] = resData.websiteurl;
let res:[string[], {"name":string, "id":string}[]] = this.getDataproviderCountriesOrganizations(resData, true, true);
result['organizations'] = res[1];
result['countries'] = res[0];
result['subjects'] = this.getDataproviderSubjects(resData);
//console.log(result['subjects']);
results.push(result);
}
return results;
}
getDataproviderSubjects(resData: any): string [] {
var subjects:string [] = [];
let length = Array.isArray(resData['subjects']) ? resData['subjects'].length : 1;
for(let i=0; i<length; i++) {
let subject = Array.isArray(resData['subjects']) ? resData['subjects'][i] :resData['subjects'];
subjects.push(subject.content);
}
return subjects;
}
getDataproviderType(resData: any): string {
if(resData['datasourcetype'].hasOwnProperty("classname")) {
return resData['datasourcetype'].classname;
} else {
return '';
}
}
getDataproviderCompatibility(resData: any): {"classid": string, "classname": string} {
if(resData.hasOwnProperty('openairecompatibility')) {
return {"classid": resData['openairecompatibility'].classid, "classname": resData['openairecompatibility'].classname};
} else {
return {"classid": "", "classname": ""};
}
}
getDataproviderCountriesOrganizations(resData: any, getCountries: boolean, getOrganizations: boolean): [string[], {"name": string, "id": string}[]] {
let countries: string[] = [];
let organizations: {"name": string, "id": string}[] = [];
if(resData['rels'].hasOwnProperty("rel")) {
let countriesSet: Set<string> = new Set<string>();
let relLength = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'].length : 1;
for(let i=0; i<relLength; i++) {
let relation = Array.isArray(resData['rels']['rel']) ? resData['rels']['rel'][i] : resData['rels']['rel'];
if(relation.hasOwnProperty("to")) {
if(relation['to'].class == "provides" && relation['to'].type == "organization") {
if(getOrganizations) {
let item: {"name":string, "id":string} = {"name": "", "id": ""};
//item['name'] = relation.legalname;
if(relation.legalshortname) {
item['name'] = relation.legalshortname;
} else {
item['name'] = relation.legalname;
}
item['id'] = /*OpenaireProperties.getsearchLinkToOrganization()+*/relation['to'].content;
organizations.push(item);
}
if(getCountries) {
if(relation.hasOwnProperty('country') &&
relation.country.hasOwnProperty('classname')) {
if(!countriesSet.has(relation.country.classname)) {
countriesSet.add(relation.country.classname);
countries.push(relation.country.classname);
}
}
}
}
}
}
}
return [countries, organizations];
}
/*
parseResultsCSV(data: any): any {
let results: any = [];
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:datasource'] : data['result']['metadata']['oaf:entity']['oaf:datasource'];
var result: any = [];
result.push(this.quote(resData.officialname));
result.push(this.quote(this.getDataproviderType(resData)));
result.push(this.quote(this.getDataproviderCountriesOrganizations(resData, true, false)[0]));
result.push(this.quote(this.getDataproviderCompatibility(resData)));
results.push(result);
}
return results;
}
*/
numOfDataproviders(url: string, properties:EnvProperties):any {
//let url = OpenaireProperties. getSearchAPIURLLast()+params+(params.indexOf("?") == -1 ?"?":"&")+"format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url)
.map(res => <any> res.json())
.map(res => res.total);
}
numOfEntityDataproviders(id: string, entity: string, properties:EnvProperties):any {
var parameters = "";
if(entity == "organization") {
parameters = "organizations/"+id+"/datasources/count";
}
let url = properties.searchAPIURLLAst+parameters+"?format=json";
return this.numOfDataproviders(url, properties);
}
numOfSearchDataproviders(params: string, properties:EnvProperties):any {
let url: string = properties.searchAPIURLLAst+"datasources/count?format=json";
if(params != "") {
url += "&q=" + StringUtils.URIEncode(params);
}
return this.numOfDataproviders(url, properties);
}
/*
private quote(word: any): string {
return '"'+word+'"';
}
*/
getDataprovidersTableResults (queryType:string, properties:EnvProperties):any {
let size: number = 0;
let url: string= properties.searchAPIURLLAst;
if(queryType == "compatible"){
url += '?query=((oaftype exact datasource) not (datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) not(datasourcetypeuiid = other) not(datasourcetypeuiid exact "pubsrepository::journal") not(datasourcetypeuiid exact "aggregator::pubsrepository::journals"))';
}else if(queryType=="registries"){
url += "?query=((oaftype exact datasource) and(datasourcetypeuiid = other))";
}else if(queryType=="journals"){
url += '?query=((oaftype exact datasource) not(datasourcecompatibilityid = UNKNOWN) not(datasourcecompatibilityid = notCompatible) and (datasourcetypeuiid exact "pubsrepository::journal" or datasourcetypeuiid exact "aggregator::pubsrepository::journals" ))';
}
url += "&page=0&size=0&format=json";
return this.http.get((properties.useCache)? (properties.cacheUrl+encodeURIComponent(url)): url).map(res => <any> res.json())
.map(res => res['meta'].total);
}
}