2023-01-20 16:57:03 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2023-01-26 10:55:02 +01:00
|
|
|
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
|
2023-02-01 15:45:33 +01:00
|
|
|
import { Page, DsmConf, ResourceType, Protocol, WfHistoryEntry, SimpleResource, Context, ContextNode, Vocabulary, VocabularyTerm, KeyValue, BrowseTerm, Datasource } from './is.model';
|
2023-01-27 08:58:33 +01:00
|
|
|
import { FormGroup } from '@angular/forms';
|
|
|
|
import { MatSnackBar } from '@angular/material/snack-bar';
|
2023-01-20 16:57:03 +01:00
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
|
|
|
export class ISService {
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
constructor(public client: HttpClient, public snackBar: MatSnackBar) { }
|
2023-01-20 16:57:03 +01:00
|
|
|
|
2023-01-27 08:58:33 +01:00
|
|
|
loadResourceTypes(onSuccess: Function): void {
|
|
|
|
this.client.get<ResourceType[]>("/ajax/resourceTypes").subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-20 16:57:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
loadResourceType(id: string, onSuccess: Function): void {
|
2023-01-27 08:58:33 +01:00
|
|
|
this.client.get<ResourceType>("/ajax/resourceTypes/" + encodeURIComponent(id)).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-25 16:08:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 08:58:33 +01:00
|
|
|
loadInfo(onSuccess: Function): void {
|
|
|
|
this.client.get<any[]>("/ajax/info/").subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-20 16:57:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 08:58:33 +01:00
|
|
|
loadProtocols(onSuccess: Function): void {
|
|
|
|
this.client.get<Protocol[]>("/ajax/protocols/").subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-20 16:57:03 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
loadSimpleResources(type: string, onSuccess: Function): void {
|
2023-01-27 08:58:33 +01:00
|
|
|
this.client.get<SimpleResource[]>("/ajax/resources/" + encodeURIComponent(type)).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-25 16:08:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
loadSimpleResourceContent(id: any, onSuccess: Function): void {
|
2023-01-26 10:55:02 +01:00
|
|
|
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
|
2023-01-27 08:58:33 +01:00
|
|
|
this.client.get<string>("/ajax/resources/" + encodeURIComponent(id) + '/content', {
|
2023-01-26 10:55:02 +01:00
|
|
|
headers, responseType: 'text' as 'json'
|
2023-01-27 08:58:33 +01:00
|
|
|
}).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
2023-01-26 10:55:02 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
saveSimpleResourceMedatata(res: SimpleResource, onSuccess: Function, relatedForm?: FormGroup): void {
|
2023-01-27 08:58:33 +01:00
|
|
|
this.client.post<void>('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error, relatedForm)
|
|
|
|
});
|
2023-01-25 16:08:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
saveSimpleResourceContent(id: string, content: string, onSuccess: Function, relatedForm?: FormGroup): void {
|
2023-01-26 10:55:02 +01:00
|
|
|
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
|
|
|
|
let body = new HttpParams().set('content', content);
|
2023-01-27 08:58:33 +01:00
|
|
|
this.client.post<void>('/ajax/resources/' + encodeURIComponent(id) + '/content', body, { headers: headers }).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error, relatedForm)
|
|
|
|
});
|
2023-01-26 10:55:02 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
addSimpleResource(name: string, type: string, description: string, content: string, onSuccess: Function, relatedForm?: FormGroup): void {
|
2023-01-26 10:55:02 +01:00
|
|
|
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);
|
2023-01-27 08:58:33 +01:00
|
|
|
this.client.post<void>('/ajax/resources/', body, { headers: headers }).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
2023-01-27 11:44:30 +01:00
|
|
|
error: error => this.showError(error, relatedForm)
|
2023-01-27 08:58:33 +01:00
|
|
|
});
|
2023-01-26 10:55:02 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
deleteSimpleResource(resourceId: string, onSuccess: Function): void {
|
|
|
|
this.client.delete<void>('/ajax/resources/' + encodeURIComponent(resourceId)).subscribe({
|
2023-01-27 08:58:33 +01:00
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-25 16:08:32 +01:00
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
loadWfHistory(total: number, from: number, to: number, onSuccess: Function): void {
|
2023-01-20 16:57:03 +01:00
|
|
|
let params = new HttpParams();
|
|
|
|
if (total && total > 0) { params = params.append('total', total); }
|
2023-01-27 11:44:30 +01:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:00:41 +01:00
|
|
|
loadContext(ctxId:string, onSuccess: Function): void {
|
|
|
|
this.client.get<Context>('./ajax/contexts/' + encodeURIComponent(ctxId)).subscribe({
|
2023-01-27 11:44:30 +01:00
|
|
|
next: data => onSuccess(data),
|
2023-01-27 15:00:41 +01:00
|
|
|
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)
|
2023-01-27 11:44:30 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 15:00:41 +01:00
|
|
|
loadContextConcepts(level:number, nodeId:string, onSuccess: Function): void {
|
|
|
|
this.client.get<ContextNode[]>('./ajax/contexts/' + encodeURIComponent(level) + '/' + encodeURIComponent(nodeId) + '/concepts').subscribe({
|
2023-01-27 08:58:33 +01:00
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
2023-01-27 11:44:30 +01:00
|
|
|
}
|
2023-01-20 16:57:03 +01:00
|
|
|
|
2023-01-30 10:58:34 +01:00
|
|
|
loadVocabularies(onSuccess: Function): void {
|
|
|
|
this.client.get<Vocabulary[]>('./ajax/vocs/').subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-30 12:37:01 +01:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-30 10:58:34 +01:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
2023-01-30 12:37:01 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-30 10:58:34 +01:00
|
|
|
deleteVocabulary(vocId:string, onSuccess: Function): void {
|
|
|
|
this.client.delete<void>('./ajax/vocs/' + encodeURIComponent(vocId)).subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-30 12:37:01 +01:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-02-01 15:45:33 +01:00
|
|
|
dsmConf(onSuccess: Function) {
|
|
|
|
this.client.get<DsmConf>('./ajax/dsm/conf').subscribe({
|
|
|
|
next: data => onSuccess(data),
|
|
|
|
error: error => this.showError(error)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-31 12:36:51 +01:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-31 15:02:51 +01:00
|
|
|
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)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
private showError(error: any, form?: FormGroup) {
|
2023-01-27 08:58:33 +01:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-27 11:44:30 +01:00
|
|
|
private errorMessage(error: any) {
|
2023-01-27 08:58:33 +01:00
|
|
|
if (error.error && error.error.message) {
|
|
|
|
return error.error.message;
|
|
|
|
} else if (error.message) {
|
|
|
|
return error.message;
|
|
|
|
} else {
|
|
|
|
return 'Generic server side error';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 16:57:03 +01:00
|
|
|
}
|