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

329 lines
12 KiB
TypeScript
Raw Normal View History

2023-03-23 12:24:56 +01:00
import { Injectable, OnInit } from '@angular/core';
2023-01-26 10:55:02 +01:00
import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
2023-03-23 12:24:56 +01:00
import { Page, DsmConf, ResourceType, Protocol, WfHistoryEntry, SimpleResource, Context, ContextNode, Vocabulary, VocabularyTerm, KeyValue, BrowseTerm, Datasource, MDStore, MDStoreVersion, MDStoreRecord, EmailTemplate, WfConf, WfSubscription, WfProcessStatus, WfSection } from './is.model';
2023-01-27 08:58:33 +01:00
import { FormGroup } from '@angular/forms';
import { MatSnackBar } from '@angular/material/snack-bar';
2023-03-23 12:24:56 +01:00
import { firstValueFrom, Observable } from 'rxjs';
2023-01-20 16:57:03 +01:00
@Injectable({
2023-02-08 15:12:39 +01:00
providedIn: 'root'
2023-01-20 16:57:03 +01:00
})
export class ISService {
2023-02-08 15:12:39 +01:00
2023-03-23 12:24:56 +01:00
constructor(public client: HttpClient, public snackBar: MatSnackBar) {
2023-02-08 15:12:39 +01:00
}
2023-03-23 12:24:56 +01:00
loadResourceTypes(onSuccess: Function) {
this.httpGet<ResourceType[]>("/ajax/resourceTypes", onSuccess)
}
loadResourceType(id: string, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<ResourceType>("/ajax/resourceTypes/" + encodeURIComponent(id), onSuccess);
2023-02-08 15:12:39 +01:00
}
loadInfo(onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<any[]>("/ajax/info/", onSuccess);
2023-02-08 15:12:39 +01:00
}
loadProtocols(onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<Protocol[]>("/ajax/protocols/", onSuccess);
2023-02-08 15:12:39 +01:00
}
loadSimpleResources(type: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<SimpleResource[]>("/ajax/resources/" + encodeURIComponent(type), onSuccess);
2023-02-08 15:12:39 +01:00
}
loadSimpleResourceContent(id: any, onSuccess: Function): void {
const headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
2023-03-23 08:42:47 +01:00
this.httpGetWithOptions<string>("/ajax/resources/" + encodeURIComponent(id) + '/content', {
2023-02-08 15:12:39 +01:00
headers, responseType: 'text' as 'json'
2023-03-23 08:42:47 +01:00
}, onSuccess);
2023-02-08 15:12:39 +01:00
}
saveSimpleResourceMedatata(res: SimpleResource, onSuccess: Function, relatedForm?: FormGroup): void {
2023-03-23 08:42:47 +01:00
this.httpPost('/ajax/resources/' + encodeURIComponent(res.id) + '/metadata', res, onSuccess, relatedForm);
2023-02-08 15:12:39 +01:00
}
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);
2023-03-23 08:42:47 +01:00
this.httpPostWithOptions('/ajax/resources/' + encodeURIComponent(id) + '/content', body, { headers: headers }, onSuccess, relatedForm);
2023-02-08 15:12:39 +01:00
}
2023-03-03 13:53:21 +01:00
addSimpleResource(name: string, type: string, subtype: string, description: string, content: string, onSuccess: Function, relatedForm?: FormGroup): void {
2023-02-08 15:12:39 +01:00
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
let body = new HttpParams()
.set('name', name)
.set('type', type)
2023-03-03 13:53:21 +01:00
.set('subtype', subtype)
2023-02-08 15:12:39 +01:00
.set('description', description)
.set('content', content);
2023-03-23 08:42:47 +01:00
this.httpPostWithOptions('/ajax/resources/', body, { headers: headers }, onSuccess, relatedForm);
2023-02-08 15:12:39 +01:00
}
deleteSimpleResource(resourceId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpDelete('/ajax/resources/' + encodeURIComponent(resourceId), onSuccess);
2023-02-08 15:12:39 +01:00
}
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); }
2023-03-23 08:42:47 +01:00
this.httpGetWithOptions<WfHistoryEntry[]>('/ajax/wf_history/', { params: params }, onSuccess);
2023-02-08 15:12:39 +01:00
}
loadContexts(onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<Context[]>('./ajax/contexts/', onSuccess);
2023-02-08 15:12:39 +01:00
}
loadContext(ctxId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<Context>('./ajax/contexts/' + encodeURIComponent(ctxId), onSuccess);
2023-02-08 15:12:39 +01:00
}
loadContextCategories(ctxId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<ContextNode[]>('./ajax/contexts/' + encodeURIComponent(ctxId) + '/categories', onSuccess);
2023-02-08 15:12:39 +01:00
}
loadContextConcepts(level: number, nodeId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<ContextNode[]>('./ajax/contexts/' + encodeURIComponent(level) + '/' + encodeURIComponent(nodeId) + '/concepts', onSuccess);
2023-02-08 15:12:39 +01:00
}
loadVocabularies(onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<Vocabulary[]>('./ajax/vocs/', onSuccess);
2023-02-08 15:12:39 +01:00
}
loadVocabulary(vocId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<Vocabulary>('./ajax/vocs/' + encodeURIComponent(vocId), onSuccess);
2023-02-08 15:12:39 +01:00
}
loadVocabularyTerms(vocId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<VocabularyTerm[]>('./ajax/vocs/' + encodeURIComponent(vocId) + '/terms', onSuccess);
2023-02-08 15:12:39 +01:00
}
saveVocabulary(voc: Vocabulary, onSuccess: Function, relatedForm?: FormGroup): void {
2023-03-23 08:42:47 +01:00
this.httpPost('./ajax/vocs/', voc, onSuccess, relatedForm);
2023-02-08 15:12:39 +01:00
}
saveVocabularyTerm(vocId: string, term: VocabularyTerm, onSuccess: Function, relatedForm?: FormGroup): void {
2023-03-23 08:42:47 +01:00
this.httpPost('./ajax/vocs/' + encodeURIComponent(vocId) + '/terms', term, onSuccess, relatedForm);
2023-02-08 15:12:39 +01:00
}
deleteVocabulary(vocId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpDelete('./ajax/vocs/' + encodeURIComponent(vocId), onSuccess);
2023-02-08 15:12:39 +01:00
}
deleteVocabularyTerm(vocId: string, termCode: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpDelete('./ajax/vocs/'
2023-02-08 15:12:39 +01:00
+ encodeURIComponent(vocId)
+ '/terms/'
+ encodeURIComponent(termCode)
2023-03-23 08:42:47 +01:00
, onSuccess);
2023-02-08 15:12:39 +01:00
}
dsmConf(onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<DsmConf>('./ajax/dsm/conf', onSuccess);
2023-02-08 15:12:39 +01:00
}
dsmBrowsableFields(onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<KeyValue[]>('./ajax/dsm/browsableFields', onSuccess);
2023-02-08 15:12:39 +01:00
}
dsmBrowse(field: string, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<BrowseTerm[]>('./ajax/dsm/browse/' + encodeURIComponent(field), onSuccess);
2023-02-08 15:12:39 +01:00
}
dsmSearchByField(field: string, value: string, page: number, pageSize: number, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<Page<Datasource>>('./ajax/dsm/searchByField/' + encodeURIComponent(field) + '/' + page + '/' + pageSize + '?value=' + encodeURIComponent(value), onSuccess);
2023-02-08 15:12:39 +01:00
}
dsmSearch(value: string, page: number, pageSize: number, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<Page<Datasource>>('./ajax/dsm/search/' + page + '/' + pageSize + '?value=' + encodeURIComponent(value), onSuccess);
2023-02-08 15:12:39 +01:00
}
loadMDStores(onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<MDStore[]>("/ajax/mdstores/", onSuccess);
2023-02-08 15:12:39 +01:00
}
loadMDStore(mdId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<MDStore>('./ajax/mdstores/mdstore/' + encodeURIComponent(mdId), onSuccess);
2023-02-08 15:12:39 +01:00
}
2023-02-10 12:18:06 +01:00
addMDStore(format: string, layout: string, interpretation: string, type: string, dsName: string, dsId: string, apiId: string, onSuccess: Function, relatedForm?: FormGroup) {
2023-02-08 15:12:39 +01:00
const headers = new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
let body = new HttpParams()
.set('dsName', dsName)
.set('dsId', dsId)
.set('apiId', apiId);
2023-03-23 08:42:47 +01:00
this.httpPostWithOptions('/ajax/mdstores/new/'
2023-02-08 15:12:39 +01:00
+ encodeURIComponent(format)
+ '/'
+ encodeURIComponent(layout)
+ '/'
2023-02-10 12:18:06 +01:00
+ encodeURIComponent(interpretation)
+ '/'
+ encodeURIComponent(type),
2023-03-23 08:42:47 +01:00
body, { headers: headers }, onSuccess, relatedForm);
2023-02-08 15:12:39 +01:00
}
deleteMDStore(mdId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpDelete('./ajax/mdstores/mdstore/' + encodeURIComponent(mdId), onSuccess);
2023-02-08 15:12:39 +01:00
}
prepareNewMDStoreVersion(mdId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<MDStoreVersion>('./ajax/mdstores/mdstore/' + encodeURIComponent(mdId) + '/newVersion', onSuccess);
2023-02-08 15:12:39 +01:00
}
commitMDStoreVersion(versionId: string, size: number, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<any>('./ajax/mdstores/version/' + encodeURIComponent(versionId) + '/commit/' + size, onSuccess);
2023-02-08 15:12:39 +01:00
}
abortMDStoreVersion(versionId: string, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<any>('./ajax/mdstores/version/' + encodeURIComponent(versionId) + '/abort', onSuccess);
2023-02-08 15:12:39 +01:00
}
deleteMDStoreVersion(versionId: string, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpDelete('./ajax/mdstores/version/' + encodeURIComponent(versionId), onSuccess);
2023-02-08 15:12:39 +01:00
}
resetReadingMDStoreVersion(versionId: string, onSuccess: Function) {
2023-03-23 08:42:47 +01:00
this.httpGet<any>('./ajax/mdstores/version/' + encodeURIComponent(versionId) + '/resetReading', onSuccess);
2023-02-08 15:12:39 +01:00
}
loadMDStoreVersions(mdId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<MDStoreVersion[]>('./ajax/mdstores/mdstore/' + encodeURIComponent(mdId) + '/versions', onSuccess);
2023-02-08 15:12:39 +01:00
}
loadMDStoreVersion(versionId: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<MDStoreVersion>('./ajax/mdstores/version/' + encodeURIComponent(versionId), onSuccess);
2023-02-08 15:12:39 +01:00
}
loadMDStoreVersionRecords(versionId: string, limit: number, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<MDStoreRecord[]>('./ajax/mdstores/version/' + encodeURIComponent(versionId) + '/content/' + limit, onSuccess);
2023-02-08 15:12:39 +01:00
}
2023-02-14 16:55:42 +01:00
testCleaning(rule: string, xml: string, onSuccess: Function, relatedForm?: FormGroup): void {
var headers = new HttpHeaders().set('Content-Type', 'text/plain; charset=utf-8');
2023-03-23 08:42:47 +01:00
this.httpPostWithOptions('./ajax/mapping/clean?rule=' + encodeURIComponent(rule), xml, { headers, responseType: 'text' as 'json' }, onSuccess, relatedForm);
2023-02-14 16:55:42 +01:00
}
2023-03-03 11:13:24 +01:00
loadEmailTemplates(onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpGet<EmailTemplate[]>('./ajax/templates/email/', onSuccess);
2023-03-03 11:13:24 +01:00
}
saveEmailTemplate(email: EmailTemplate, onSuccess: Function, relatedForm?: FormGroup): void {
2023-03-23 08:42:47 +01:00
this.httpPost('./ajax/templates/email/', email, onSuccess, relatedForm);
2023-03-03 11:13:24 +01:00
}
deleteEmailTemplate(id: string, onSuccess: Function): void {
2023-03-23 08:42:47 +01:00
this.httpDelete('./ajax/templates/email/' + encodeURIComponent(id), onSuccess);
2023-03-03 11:13:24 +01:00
}
2023-03-23 15:05:39 +01:00
loadWfSections(onSuccess: Function) {
this.httpGet<WfSection[]>("/ajax/wfs/sections", onSuccess)
}
2023-02-14 16:55:42 +01:00
2023-03-23 12:24:56 +01:00
loadWfConfigurations(sectionId: string, onSuccess: Function): void {
2023-03-23 15:05:39 +01:00
this.httpGet<KeyValue[]>('./ajax/wfs/sections/' + encodeURIComponent(sectionId), onSuccess);
2023-03-21 12:13:23 +01:00
}
2023-03-23 08:42:47 +01:00
loadWfConfiguration(id: string, onSuccess: Function): void {
this.httpGet<WfConf>('./ajax/wfs/conf/' + encodeURIComponent(id), onSuccess);
2023-03-21 12:13:23 +01:00
}
2023-03-23 08:42:47 +01:00
saveWfConfiguration(conf: WfConf, onSuccess: Function, relatedForm?: FormGroup): void {
this.httpPost('./ajax/wfs/conf', conf, onSuccess, relatedForm);
2023-03-21 15:28:18 +01:00
}
2023-03-23 08:42:47 +01:00
deleteWfConfiguration(id: string, onSuccess: Function): void {
this.httpDelete('./ajax/wfs/conf/' + encodeURIComponent(id), onSuccess);
2023-03-21 15:28:18 +01:00
}
2023-03-23 08:42:47 +01:00
startWfConfiguration(id: string, onSuccess: Function): void {
this.httpGet<WfProcessStatus>('./ajax/wfs/conf/' + encodeURIComponent(id) + '/start', onSuccess);
}
findProcess(id: string, onSuccess: Function): void {
this.httpGet<WfProcessStatus>('./ajax/wfs/process/' + encodeURIComponent(id), onSuccess);
}
killProcess(id: string, onSuccess: Function): void {
this.httpDelete('./ajax/wfs/process/' + encodeURIComponent(id), onSuccess);
}
findWfSubscriptions(id: string, onSuccess: Function): void {
this.httpGet<WfSubscription[]>('./ajax/wfs/conf/' + encodeURIComponent(id) + '/subscriptions', onSuccess);
}
saveWfSubscriptions(id: string, subscriptions: WfSubscription[], onSuccess: Function, relatedForm?: FormGroup): void {
this.httpPost('./ajax/wfs/conf/' + encodeURIComponent(id) + '/subscriptions', subscriptions, onSuccess, relatedForm);
}
private httpGet<T>(url: string, onSuccess: Function) {
this.client.get<T>(url).subscribe({
2023-03-21 15:28:18 +01:00
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
2023-03-23 08:42:47 +01:00
private httpGetWithOptions<T>(url: string, options: any, onSuccess: Function) {
this.client.get<T>(url, options).subscribe({
2023-03-21 15:28:18 +01:00
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
2023-03-23 08:42:47 +01:00
private httpDelete(url: string, onSuccess: Function) {
this.client.delete<void>(url).subscribe({
2023-03-21 15:28:18 +01:00
next: data => onSuccess(data),
error: error => this.showError(error)
});
}
2023-03-23 08:42:47 +01:00
private httpPost(url: string, body: any, onSuccess: Function, relatedForm?: FormGroup) {
this.client.post<void>(url, body).subscribe({
2023-03-21 15:28:18 +01:00
next: data => onSuccess(data),
2023-03-23 08:42:47 +01:00
error: error => this.showError(error, relatedForm)
2023-03-21 15:28:18 +01:00
});
}
2023-03-23 08:42:47 +01:00
private httpPostWithOptions(url: string, body: any, options: any, onSuccess: Function, relatedForm?: FormGroup) {
this.client.post<void>(url, body, options).subscribe({
2023-03-21 15:28:18 +01:00
next: data => onSuccess(data),
error: error => this.showError(error, relatedForm)
});
}
2023-02-08 15:12:39 +01:00
private showError(error: any, form?: FormGroup) {
2023-02-14 16:55:42 +01:00
console.log(error);
2023-02-08 15:12:39 +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);
}
}
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';
}
}
2023-01-27 08:58:33 +01:00
2023-01-20 16:57:03 +01:00
}