dnet-applications/frontends/dnet-is-application/src/app/is.service.ts

241 lines
8.6 KiB
TypeScript

import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
import { Page, ResourceType, Protocol, WfHistoryEntry, SimpleResource, Context, ContextNode, Vocabulary, VocabularyTerm, KeyValue, BrowseTerm, Datasource } from './model/controller.model';
import { Observable, Observer } from 'rxjs';
import { FormGroup } from '@angular/forms';
import { MatSnackBar } from '@angular/material/snack-bar';
@Injectable({
providedIn: 'root'
})
export class ISService {
constructor(public client: HttpClient, public snackBar: MatSnackBar) { }
loadResourceTypes(onSuccess: Function): void {
this.client.get<ResourceType[]>("/ajax/resourceTypes").subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadResourceType(id: string, onSuccess: Function): void {
this.client.get<ResourceType>("/ajax/resourceTypes/" + encodeURIComponent(id)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadInfo(onSuccess: Function): void {
this.client.get<any[]>("/ajax/info/").subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadProtocols(onSuccess: Function): void {
this.client.get<Protocol[]>("/ajax/protocols/").subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadSimpleResources(type: string, onSuccess: Function): void {
this.client.get<SimpleResource[]>("/ajax/resources/" + encodeURIComponent(type)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadSimpleResourceContent(id: any, onSuccess: Function): void {
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
this.client.get<string>("/ajax/resources/" + encodeURIComponent(id) + '/content', {
headers, responseType: 'text' as 'json'
}).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
saveSimpleResourceMedatata(res: SimpleResource, onSuccess: Function, relatedForm?: FormGroup): void {
this.client.post<void>('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
saveSimpleResourceContent(id: string, content: string, onSuccess: Function, relatedForm?: FormGroup): void {
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
let body = new HttpParams().set('content', content);
this.client.post<void>('/ajax/resources/' + encodeURIComponent(id) + '/content', body, { headers: headers }).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
addSimpleResource(name: string, type: string, description: string, content: string, onSuccess: Function, relatedForm?: FormGroup): void {
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
let body = new HttpParams()
.set('name', name)
.set('type', type)
.set('description', description)
.set('content', content);
this.client.post<void>('/ajax/resources/', body, { headers: headers }).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
deleteSimpleResource(resourceId: string, onSuccess: Function): void {
this.client.delete<void>('/ajax/resources/' + encodeURIComponent(resourceId)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadWfHistory(total: number, from: number, to: number, onSuccess: Function): void {
let params = new HttpParams();
if (total && total > 0) { params = params.append('total', total); }
if (from && from > 0) { params = params.append('from', from); }
if (to && to > 0) { params = params.append('to', to); }
this.client.get<WfHistoryEntry[]>('/ajax/wfs/', { params: params }).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadContexts(onSuccess: Function): void {
this.client.get<Context[]>('./ajax/contexts/').subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadContext(ctxId:string, onSuccess: Function): void {
this.client.get<Context>('./ajax/contexts/' + encodeURIComponent(ctxId)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadContextCategories(ctxId:string, onSuccess: Function): void {
this.client.get<ContextNode[]>('./ajax/contexts/' + encodeURIComponent(ctxId) + '/categories').subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadContextConcepts(level:number, nodeId:string, onSuccess: Function): void {
this.client.get<ContextNode[]>('./ajax/contexts/' + encodeURIComponent(level) + '/' + encodeURIComponent(nodeId) + '/concepts').subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadVocabularies(onSuccess: Function): void {
this.client.get<Vocabulary[]>('./ajax/vocs/').subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadVocabulary(vocId:string, onSuccess: Function): void {
this.client.get<Vocabulary>('./ajax/vocs/' + encodeURIComponent(vocId)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
loadVocabularyTerms(vocId:string, onSuccess: Function): void {
this.client.get<VocabularyTerm[]>('./ajax/vocs/' + encodeURIComponent(vocId) + '/terms').subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
saveVocabulary(voc:Vocabulary, onSuccess: Function, relatedForm?: FormGroup): void {
this.client.post<void>('./ajax/vocs/', voc).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
saveVocabularyTerm(vocId:string, term:VocabularyTerm, onSuccess: Function, relatedForm?: FormGroup): void {
this.client.post<void>('./ajax/vocs/' + encodeURIComponent(vocId) + '/terms', term).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
deleteVocabulary(vocId:string, onSuccess: Function): void {
this.client.delete<void>('./ajax/vocs/' + encodeURIComponent(vocId)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
deleteVocabularyTerm(vocId:string, termCode:string, onSuccess: Function): void {
this.client.delete<void>('./ajax/vocs/'
+ encodeURIComponent(vocId)
+ '/terms/'
+ encodeURIComponent(termCode)
).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
dsmBrowsableFields(onSuccess: Function) {
this.client.get<KeyValue[]>('./ajax/dsm/browsableFields').subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
dsmBrowse(field:string, onSuccess: Function) {
this.client.get<BrowseTerm[]>('./ajax/dsm/browse/' + encodeURIComponent(field)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
dsmSearchByField(field:string, value:string, page:number, pageSize:number, onSuccess: Function) {
this.client.get<Page<Datasource>>('./ajax/dsm/searchByField/' + encodeURIComponent(field) + '/' + page + '/' + pageSize + '?value=' + encodeURIComponent(value)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
dsmSearch(value:string, page:number, pageSize:number, onSuccess: Function) {
this.client.get<Page<Datasource>>('./ajax/dsm/search/' + page + '/' + pageSize + '?value=' + encodeURIComponent(value)).subscribe({
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
private showError(error: any, form?: FormGroup) {
const msg = this.errorMessage(error);
if (form) {
form.setErrors({ serverError: msg })
} else if (this.snackBar) {
this.snackBar.open(msg, 'ERROR', {
duration: 5000,
});
} else {
alert(msg);
}
}
private errorMessage(error: any) {
if (error.error && error.error.message) {
return error.error.message;
} else if (error.message) {
return error.message;
} else {
return 'Generic server side error';
}
}
}