2019-07-23 14:23:12 +02:00
|
|
|
import {throwError as observableThrowError} from 'rxjs';
|
2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
2019-07-23 14:23:12 +02:00
|
|
|
import {Response} from '@angular/http';
|
|
|
|
import {HttpClient} from '@angular/common/http';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {catchError, map} from "rxjs/operators";
|
2017-12-19 13:53:46 +01:00
|
|
|
@Injectable()
|
|
|
|
export class ContextsService {
|
2019-06-03 15:20:36 +02:00
|
|
|
constructor(private http: HttpClient ) {
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2018-02-05 14:14:59 +01:00
|
|
|
public getCommunities( apiUrl:string):any {
|
2018-05-29 16:58:51 +02:00
|
|
|
let url = apiUrl + 's/';
|
|
|
|
return this.http.get(url)
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(map(res => this.parseCommunities(res, true) ))
|
2017-12-19 13:53:46 +01:00
|
|
|
// .do(request => console.info("Get claims: offset = "))
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(catchError(this.handleError));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2019-05-29 15:56:09 +02:00
|
|
|
public getPublicCommunities( apiUrl:string):any {
|
|
|
|
let url = apiUrl + 's/';
|
|
|
|
return this.http.get(url)
|
2020-06-29 13:10:32 +02:00
|
|
|
.pipe(map(res => this.parseCommunities(res, false) ));
|
2019-05-29 15:56:09 +02:00
|
|
|
}
|
|
|
|
parseCommunities(data, getall){
|
2018-05-29 16:58:51 +02:00
|
|
|
var communities = [];
|
|
|
|
|
|
|
|
for(var i = 0; i< data.length; i++){
|
|
|
|
if(data[i].type && (data[i].type == "ri" || data[i].type == "community")){
|
2019-05-29 15:56:09 +02:00
|
|
|
if(getall || data[i].status!='hidden') {
|
|
|
|
communities.push(data[i]);
|
|
|
|
}
|
2018-05-29 16:58:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return communities;
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
public getCategories(communityId :string, apiUrl:string):any {
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.info('ContextsService: request categories for community with id '+communityId);
|
2018-05-29 16:58:51 +02:00
|
|
|
let url= apiUrl + '/' + communityId ;
|
|
|
|
return this.http.get(url)
|
2019-06-03 15:20:36 +02:00
|
|
|
//.map(request => <any> request.json())
|
2017-12-19 13:53:46 +01:00
|
|
|
// .do(request => console.info("Get claims: offset = " ))
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(catchError(this.handleError));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
public getConcepts(categoryId :string, keyword: string, parsing:boolean, apiUrl:string):any {
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.info('ContextsService: request concept for category with id '+categoryId + ' and keyword '+ keyword);
|
2018-05-29 16:58:51 +02:00
|
|
|
let url= apiUrl + '/category/' + categoryId;
|
|
|
|
|
|
|
|
|
|
|
|
return this.http.get(url )
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(catchError(this.handleError))
|
|
|
|
.pipe(map(res => (parsing)?this.parse(res):res));
|
2018-05-29 16:58:51 +02:00
|
|
|
}
|
|
|
|
public getSubConcepts(subConceptID :string, keyword: string, parsing:boolean, apiUrl:string):any {
|
2019-02-14 11:15:44 +01:00
|
|
|
//console.info('ContextsService: request sub concept for concept with id '+subConceptID + ' and keyword '+ keyword);
|
2018-05-29 16:58:51 +02:00
|
|
|
let url= apiUrl + '/category/concept/' + subConceptID;
|
2017-12-19 13:53:46 +01:00
|
|
|
let key = url+"_parsing="+parsing;
|
|
|
|
|
|
|
|
|
2018-05-29 16:58:51 +02:00
|
|
|
return this.http.get(url )
|
2019-06-03 15:20:36 +02:00
|
|
|
.pipe(catchError(this.handleError))
|
|
|
|
.pipe(map(res => (parsing)?this.parseSubConcepts(res):res));
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2018-06-04 13:04:37 +02:00
|
|
|
parse (data: any):any {
|
|
|
|
var array =[]
|
2017-12-19 13:53:46 +01:00
|
|
|
if(!Array.isArray(data) && data.id && data.label){
|
2018-06-04 13:04:37 +02:00
|
|
|
var value ={id:"",label:"",hasSubConcept:""};
|
2017-12-19 13:53:46 +01:00
|
|
|
value.id = data.id;
|
|
|
|
value.label = data.label;
|
2018-06-04 13:04:37 +02:00
|
|
|
value.hasSubConcept = data.hasSubConcept;
|
2017-12-19 13:53:46 +01:00
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
for(var i = 0; i < data.length; i++){
|
2018-06-04 13:04:37 +02:00
|
|
|
var value={id:"",label:"",hasSubConcept:""};
|
2017-12-19 13:53:46 +01:00
|
|
|
value.id = data[i].id;
|
|
|
|
value.label = data[i].label;
|
2018-06-04 13:04:37 +02:00
|
|
|
value.hasSubConcept = data[i].hasSubConcept;
|
2017-12-19 13:53:46 +01:00
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
2018-05-29 16:58:51 +02:00
|
|
|
}
|
2018-06-04 13:04:37 +02:00
|
|
|
parseSubConcepts (data: any):any {
|
|
|
|
var array = []
|
2018-05-29 16:58:51 +02:00
|
|
|
if(data.length >0 && data[0].concepts){
|
|
|
|
var concepts = data[0].concepts;
|
|
|
|
for(var i = 0; i < concepts.length; i++){
|
2018-06-04 13:04:37 +02:00
|
|
|
var value ={id:"",label:"",hasSubConcept:""};
|
2018-05-29 16:58:51 +02:00
|
|
|
value.id = concepts[i].id;
|
|
|
|
value.label = concepts[i].label;
|
2018-06-04 13:04:37 +02:00
|
|
|
value.hasSubConcept = concepts[i].hasSubConcept;
|
2018-05-29 16:58:51 +02:00
|
|
|
if(concepts[i].concepts){
|
|
|
|
var subconcepts = concepts[i].concepts;
|
|
|
|
for(var x = 0; x < subconcepts.length; x++){
|
2018-06-04 13:04:37 +02:00
|
|
|
var value ={id:"",label:"",hasSubConcept:""};
|
2018-05-29 16:58:51 +02:00
|
|
|
value.id = subconcepts[x].id;
|
|
|
|
value.label = subconcepts[x].label;
|
2018-06-04 13:04:37 +02:00
|
|
|
value.hasSubConcept = subconcepts[x].hasSubConcept;
|
2018-05-29 16:58:51 +02:00
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
|
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);
|
2019-06-03 15:20:36 +02:00
|
|
|
return observableThrowError(error || 'Server error');
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
}
|