151 lines
6.3 KiB
TypeScript
151 lines
6.3 KiB
TypeScript
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http';
|
|
import { Injectable } from '@angular/core';
|
|
import { IsActive } from '@app/core/common/enum/is-active.enum';
|
|
import { DmpBlueprint, DmpBlueprintDefinitionSection, DmpBlueprintPersist, NewVersionDmpBlueprintPersist } from '@app/core/model/dmp-blueprint/dmp-blueprint';
|
|
import { DmpBlueprintLookup } from '@app/core/query/dmp-blueprint.lookup';
|
|
import { MultipleAutoCompleteConfiguration } from '@app/library/auto-complete/multiple/multiple-auto-complete-configuration';
|
|
import { SingleAutoCompleteConfiguration } from '@app/library/auto-complete/single/single-auto-complete-configuration';
|
|
import { BaseHttpParams } from '@common/http/base-http-params';
|
|
import { InterceptorType } from '@common/http/interceptors/interceptor-type';
|
|
import { QueryResult } from '@common/model/query-result';
|
|
import { FilterService } from '@common/modules/text-filter/filter-service';
|
|
import { Guid } from '@common/types/guid';
|
|
import { Observable, throwError } from 'rxjs';
|
|
import { catchError, map } from 'rxjs/operators';
|
|
import { nameof } from 'ts-simple-nameof';
|
|
import { ConfigurationService } from '../configuration/configuration.service';
|
|
import { BaseHttpV2Service } from '../http/base-http-v2.service';
|
|
import { DmpBlueprintStatus } from '@app/core/common/enum/dmp-blueprint-status';
|
|
import { error } from 'console';
|
|
|
|
@Injectable()
|
|
export class DmpBlueprintService {
|
|
|
|
private headers = new HttpHeaders();
|
|
|
|
constructor(private http: BaseHttpV2Service, private httpClient: HttpClient, private configurationService: ConfigurationService, private filterService: FilterService) {
|
|
}
|
|
|
|
private get apiBase(): string { return `${this.configurationService.server}dmp-blueprint`; }
|
|
|
|
query(q: DmpBlueprintLookup): Observable<QueryResult<DmpBlueprint>> {
|
|
const url = `${this.apiBase}/query`;
|
|
return this.http.post<QueryResult<DmpBlueprint>>(url, q).pipe(catchError((error: any) => throwError(error)));
|
|
}
|
|
|
|
getSingle(id: Guid, reqFields: string[] = []): Observable<DmpBlueprint> {
|
|
const url = `${this.apiBase}/${id}`;
|
|
const options = { params: { f: reqFields } };
|
|
|
|
return this.http
|
|
.get<DmpBlueprint>(url, options).pipe(
|
|
catchError((error: any) => throwError(error)));
|
|
}
|
|
|
|
persist(item: DmpBlueprintPersist): Observable<DmpBlueprint> {
|
|
const url = `${this.apiBase}/persist`;
|
|
|
|
return this.http
|
|
.post<DmpBlueprint>(url, item).pipe(
|
|
catchError((error: any) => throwError(error)));
|
|
}
|
|
|
|
delete(id: Guid): Observable<DmpBlueprint> {
|
|
const url = `${this.apiBase}/${id}`;
|
|
|
|
return this.http
|
|
.delete<DmpBlueprint>(url).pipe(
|
|
catchError((error: any) => throwError(error)));
|
|
}
|
|
|
|
clone(id: Guid, reqFields: string[] = []): Observable<DmpBlueprint> {
|
|
const url = `${this.apiBase}/clone/${id}`;
|
|
const options = { params: { f: reqFields } };
|
|
|
|
return this.http
|
|
.get<DmpBlueprint>(url, options).pipe(
|
|
catchError((error: any) => throwError(error)));
|
|
}
|
|
|
|
newVersion(item: NewVersionDmpBlueprintPersist, reqFields: string[] = []): Observable<DmpBlueprint> {
|
|
const url = `${this.apiBase}/new-version`;
|
|
const options = { params: { f: reqFields } };
|
|
|
|
return this.http
|
|
.post<DmpBlueprint>(url, item).pipe(
|
|
catchError((error: any) => throwError(error)));
|
|
}
|
|
|
|
downloadXML(id: Guid): Observable<HttpResponse<Blob>> {
|
|
const url = `${this.apiBase}/xml/export/${id}`;
|
|
let headerXml: HttpHeaders = this.headers.set('Content-Type', 'application/xml');
|
|
const params = new BaseHttpParams();
|
|
params.interceptorContext = {
|
|
excludedInterceptors: [InterceptorType.JSONContentType]
|
|
};
|
|
return this.httpClient.get(url, { params: params, responseType: 'blob', observe: 'response', headers: headerXml });
|
|
}
|
|
|
|
uploadFile(file: FileList, labelSent: string, reqFields: string[] = []): Observable<DmpBlueprint> {
|
|
const url = `${this.apiBase}/xml/import`;
|
|
const params = new BaseHttpParams();
|
|
params.interceptorContext = {
|
|
excludedInterceptors: [InterceptorType.JSONContentType]
|
|
};
|
|
const formData = new FormData();
|
|
formData.append('file', file[0], labelSent);
|
|
return this.http.post(url, formData, { params: params });
|
|
}
|
|
|
|
//
|
|
// Autocomplete Commons
|
|
//
|
|
// tslint:disable-next-line: member-ordering
|
|
singleAutocompleteConfiguration: SingleAutoCompleteConfiguration = {
|
|
initialItems: (data?: any) => this.query(this.buildAutocompleteLookup()).pipe(map(x => x.items)),
|
|
filterFn: (searchQuery: string, data?: any) => this.query(this.buildAutocompleteLookup(searchQuery)).pipe(map(x => x.items)),
|
|
getSelectedItem: (selectedItem: any) => this.query(this.buildAutocompleteLookup(null, null, [selectedItem])).pipe(map(x => x.items[0])),
|
|
displayFn: (item: DmpBlueprint) => item.label,
|
|
titleFn: (item: DmpBlueprint) => item.label,
|
|
valueAssign: (item: DmpBlueprint) => item.id,
|
|
};
|
|
|
|
// tslint:disable-next-line: member-ordering
|
|
multipleAutocompleteConfiguration: MultipleAutoCompleteConfiguration = {
|
|
initialItems: (excludedItems: any[], data?: any) => this.query(this.buildAutocompleteLookup(null, excludedItems ? excludedItems : null)).pipe(map(x => x.items)),
|
|
filterFn: (searchQuery: string, excludedItems: any[]) => this.query(this.buildAutocompleteLookup(searchQuery, excludedItems)).pipe(map(x => x.items)),
|
|
getSelectedItems: (selectedItems: any[]) => this.query(this.buildAutocompleteLookup(null, null, selectedItems)).pipe(map(x => x.items)),
|
|
displayFn: (item: DmpBlueprint) => item.label,
|
|
titleFn: (item: DmpBlueprint) => item.label,
|
|
valueAssign: (item: DmpBlueprint) => item.id,
|
|
};
|
|
|
|
public buildAutocompleteLookup(like?: string, excludedIds?: Guid[], ids?: Guid[], statuses?: DmpBlueprintStatus[]): DmpBlueprintLookup {
|
|
const lookup: DmpBlueprintLookup = new DmpBlueprintLookup();
|
|
lookup.page = { size: 100, offset: 0 };
|
|
if (excludedIds && excludedIds.length > 0) { lookup.excludedIds = excludedIds; }
|
|
if (ids && ids.length > 0) { lookup.ids = ids; }
|
|
lookup.isActive = [IsActive.Active];
|
|
lookup.statuses = statuses;
|
|
lookup.project = {
|
|
fields: [
|
|
nameof<DmpBlueprint>(x => x.id),
|
|
nameof<DmpBlueprint>(x => x.label)
|
|
]
|
|
};
|
|
lookup.order = { items: [nameof<DmpBlueprint>(x => x.label)] };
|
|
if (like) { lookup.like = this.filterService.transformLike(like); }
|
|
return lookup;
|
|
}
|
|
|
|
//
|
|
//
|
|
// UI Helpers
|
|
//
|
|
//
|
|
|
|
getSection(dmpBlueprint: DmpBlueprint, sectionId: Guid): DmpBlueprintDefinitionSection {
|
|
return dmpBlueprint?.definition?.sections?.find(x => x.id === sectionId);
|
|
}
|
|
}
|