2017-12-19 13:53:46 +01:00
|
|
|
import {Injectable} from '@angular/core';
|
|
|
|
import {Jsonp, URLSearchParams, RequestOptions, Headers} from '@angular/http';
|
|
|
|
import {Http, Response} from '@angular/http';
|
|
|
|
import {Observable} from 'rxjs/Observable';
|
|
|
|
import {Claim} from '../claim';
|
|
|
|
import {AutoCompleteValue} from '../../../searchPages/searchUtils/searchHelperClasses.class';
|
|
|
|
import 'rxjs/add/observable/of';
|
|
|
|
import 'rxjs/add/operator/do';
|
|
|
|
import 'rxjs/add/operator/share';
|
|
|
|
import { COOKIE } from '../../../login/utils/helper.class';
|
|
|
|
@Injectable()
|
|
|
|
export class ContextsService {
|
|
|
|
constructor(private http: Http ) {
|
|
|
|
}
|
|
|
|
|
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/';
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2018-05-29 16:58:51 +02:00
|
|
|
let key = url;
|
2017-12-19 13:53:46 +01:00
|
|
|
console.info('ContextsService: request communities '+url);
|
2018-05-29 16:58:51 +02:00
|
|
|
return this.http.get(url)
|
|
|
|
.map(res => <any> res.json()).map(res => this.parseCommunities(res) )
|
2017-12-19 13:53:46 +01:00
|
|
|
// .do(request => console.info("Get claims: offset = "))
|
|
|
|
.catch(this.handleError);
|
|
|
|
}
|
2018-05-29 16:58:51 +02:00
|
|
|
parseCommunities(data){
|
|
|
|
var communities = [];
|
|
|
|
|
|
|
|
for(var i = 0; i< data.length; i++){
|
|
|
|
if(data[i].type && (data[i].type == "ri" || data[i].type == "community")){
|
|
|
|
communities.push(data[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return communities;
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
public getCategories(communityId :string, apiUrl:string):any {
|
2017-12-19 13:53:46 +01:00
|
|
|
console.info('ContextsService: request categories for community with id '+communityId);
|
2018-05-29 16:58:51 +02:00
|
|
|
let url= apiUrl + '/' + communityId ;
|
2017-12-19 13:53:46 +01:00
|
|
|
let key = url;
|
|
|
|
|
|
|
|
|
2018-05-29 16:58:51 +02:00
|
|
|
return this.http.get(url)
|
|
|
|
.map(request => <any> request.json())
|
2017-12-19 13:53:46 +01:00
|
|
|
// .do(request => console.info("Get claims: offset = " ))
|
|
|
|
.catch(this.handleError);;
|
|
|
|
}
|
2018-02-05 14:14:59 +01:00
|
|
|
public getConcepts(categoryId :string, keyword: string, parsing:boolean, apiUrl:string):any {
|
2017-12-19 13:53:46 +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;
|
|
|
|
let key = url+"_parsing="+parsing;
|
|
|
|
|
|
|
|
|
|
|
|
return this.http.get(url )
|
|
|
|
.map(request => <any> request.json())
|
|
|
|
.catch(this.handleError)
|
|
|
|
.map(res => (parsing)?this.parse(res):res);
|
|
|
|
// .do(res => console.info("Result is "+ res.length ));
|
|
|
|
}
|
|
|
|
public getSubConcepts(subConceptID :string, keyword: string, parsing:boolean, apiUrl:string):any {
|
|
|
|
console.info('ContextsService: request sub concept for concept with id '+subConceptID + ' and keyword '+ keyword);
|
|
|
|
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 )
|
|
|
|
.map(request => <any> request.json())
|
2017-12-19 13:53:46 +01:00
|
|
|
.catch(this.handleError)
|
2018-05-29 16:58:51 +02:00
|
|
|
.map(res => (parsing)?this.parseSubConcepts(res):res);
|
2017-12-19 13:53:46 +01:00
|
|
|
// .do(res => console.info("Result is "+ res.length ));
|
|
|
|
}
|
|
|
|
parse (data: any):AutoCompleteValue[] {
|
|
|
|
var array:AutoCompleteValue[] =[]
|
|
|
|
if(!Array.isArray(data) && data.id && data.label){
|
|
|
|
var value:AutoCompleteValue = new AutoCompleteValue();
|
|
|
|
value.id = data.id;
|
|
|
|
value.label = data.label;
|
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
for(var i = 0; i < data.length; i++){
|
|
|
|
var value:AutoCompleteValue = new AutoCompleteValue();
|
|
|
|
value.id = data[i].id;
|
|
|
|
value.label = data[i].label;
|
|
|
|
array.push(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
return array;
|
|
|
|
|
2018-05-29 16:58:51 +02:00
|
|
|
}
|
|
|
|
parseSubConcepts (data: any):AutoCompleteValue[] {
|
|
|
|
var array:AutoCompleteValue[] =[]
|
|
|
|
if(data.length >0 && data[0].concepts){
|
|
|
|
var concepts = data[0].concepts;
|
|
|
|
for(var i = 0; i < concepts.length; i++){
|
|
|
|
var value:AutoCompleteValue = new AutoCompleteValue();
|
|
|
|
value.id = concepts[i].id;
|
|
|
|
value.label = concepts[i].label;
|
|
|
|
if(concepts[i].concepts){
|
|
|
|
var subconcepts = concepts[i].concepts;
|
|
|
|
for(var x = 0; x < subconcepts.length; x++){
|
|
|
|
var value:AutoCompleteValue = new AutoCompleteValue();
|
|
|
|
value.id = subconcepts[x].id;
|
|
|
|
value.label = subconcepts[x].label;
|
|
|
|
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);
|
|
|
|
return Observable.throw(error || 'Server error');
|
|
|
|
}
|
|
|
|
private getAuthOptions():RequestOptions{
|
|
|
|
let headers = new Headers();
|
|
|
|
headers.append('X-XSRF-TOKEN', COOKIE.getCookie(COOKIE.cookieName_id));
|
|
|
|
let options = new RequestOptions({ headers: headers, withCredentials:true });
|
|
|
|
return options;
|
|
|
|
}
|
|
|
|
}
|