2020-10-20 16:47:54 +02:00
|
|
|
import {BehaviorSubject, from, Observable, Subscription, 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 {HttpClient} from '@angular/common/http';
|
2019-06-03 15:20:36 +02:00
|
|
|
import {catchError, map} from "rxjs/operators";
|
2020-10-20 16:47:54 +02:00
|
|
|
import {properties} from "../../../../../environments/environment";
|
|
|
|
|
|
|
|
@Injectable({ providedIn: 'root' })
|
2017-12-19 13:53:46 +01:00
|
|
|
export class ContextsService {
|
2020-10-20 16:47:54 +02:00
|
|
|
private communitiesSubject: BehaviorSubject<any> = new BehaviorSubject(null);
|
2022-09-23 11:24:15 +02:00
|
|
|
private promise: Promise<void>;
|
2020-10-20 16:47:54 +02:00
|
|
|
private sub: Subscription = null;
|
|
|
|
|
2021-06-15 14:54:40 +02:00
|
|
|
constructor(private http: HttpClient=null ) {
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
|
|
|
|
2020-10-20 16:47:54 +02:00
|
|
|
ngOnDestroy() {
|
|
|
|
if(this.sub) {
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2020-10-20 16:47:54 +02:00
|
|
|
|
|
|
|
public initCommunities() {
|
|
|
|
let url = properties.contextsAPI + 's/';
|
2022-09-23 11:24:15 +02:00
|
|
|
this.promise = new Promise<void>((resolve => {
|
2020-10-20 16:47:54 +02:00
|
|
|
this.sub = this.http.get((properties.useLongCache) ? (properties.cacheUrl + encodeURIComponent(url)) : url)
|
|
|
|
.pipe(map(res => this.parseCommunities(res, true) ))
|
|
|
|
.subscribe(
|
|
|
|
(communties) => {
|
|
|
|
this.communitiesSubject.next(communties);
|
|
|
|
resolve();
|
|
|
|
},
|
|
|
|
error => {
|
|
|
|
this.communitiesSubject.error(error);
|
|
|
|
resolve();
|
|
|
|
});
|
|
|
|
}));
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
async getCommunitiesByStateAsync(getAll: boolean) {
|
|
|
|
if(!this.promise) {
|
|
|
|
this.initCommunities();
|
|
|
|
}
|
|
|
|
|
|
|
|
await this.promise;
|
2020-11-11 15:43:13 +01:00
|
|
|
if(this.sub){
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
2020-10-20 16:47:54 +02:00
|
|
|
if(getAll) {
|
|
|
|
return this.communitiesSubject.getValue();
|
|
|
|
} else {
|
|
|
|
return this.communitiesSubject.getValue().filter(community => community.status != 'hidden');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getPublicCommunitiesByState(): Observable<any> {
|
|
|
|
return from(this.getCommunitiesByStateAsync(false));
|
|
|
|
}
|
|
|
|
|
|
|
|
getCommunitiesByState(): Observable<any> {
|
|
|
|
return from(this.getCommunitiesByStateAsync(true));
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-11-11 15:43:13 +01:00
|
|
|
//console.info('ContextsService: request subscriptions 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
|
|
|
}
|
|
|
|
|
2021-07-14 13:19:57 +02:00
|
|
|
private handleError (error: any) {
|
2017-12-19 13:53:46 +01:00
|
|
|
// 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
|
|
|
}
|
|
|
|
}
|