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

329 lines
12 KiB
TypeScript

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