src/lib/service/prepare/prepare.service.ts
Collection of Prepare Services
Properties |
|
Methods |
|
constructor(backend: BackendService, uploadService: UploadService)
|
|||||||||
Parameters :
|
Public addTemplate | |||||||||
addTemplate(preparedItemId: , templateId: )
|
|||||||||
Add a template to a prepared item.
Parameters :
Returns :
Observable<PreparedItem>
Observable |
Public createPreparedItem |
createPreparedItem(parentId: string, childType?: string)
|
Returns :
Observable<any>
|
Public createPreparedItemPostUrl | ||||||||
createPreparedItemPostUrl(params: PreparedPostParam)
|
||||||||
Creates an URL for creating a PreparedItem.
Parameters :
Returns :
string
string A POST URL for creating PreparedItems |
deletePreparedItem | ||||||||
deletePreparedItem(preparedItemId: string)
|
||||||||
Removes a prepared item.
Parameters :
Returns :
Observable<any>
Observable |
Public deletePreparedItemContent | ||||||||||||
deletePreparedItemContent(preparedItemId: string, index?: number)
|
||||||||||||
Deletes a prepared creation object content element by index or the complete content. If you provide an index the content consists only of one element, the content is completely removed from the prepared creation object.
Parameters :
Returns :
Observable<PreparedItem>
Observable |
Public getPrepareChildTypes | ||||||||||||
getPrepareChildTypes(parentId: string, withContentOnly?: boolean)
|
||||||||||||
Fetches a list of allowed child object types for a selected parent.
Parameters :
Returns :
Observable<IPrepareChildTypes>
Observable |
Public getPreparedItems$ | ||||||||
getPreparedItems$(noForms?: boolean)
|
||||||||
Retrieves a list of the users prepared items.
Parameters :
Returns :
Observable<PreparedItem[]>
Observable<PreparedItem[]> A collection of prepared item objects |
Public getPrepareRootTypes |
getPrepareRootTypes()
|
Fetches a list of object types allowed to be placed directly below root.
Returns :
Observable<ObjectType[]>
Observable<ObjectType[]> A list of supported object types |
Public getPreviewUri | ||||||||||||||||
getPreviewUri(id: string, index?: number, rendition?: string)
|
||||||||||||||||
fetch the preview uri for a prepared item
Parameters :
Returns :
string
string Preview uri |
Public getTemplatePreviewUri | ||||||||||||
getTemplatePreviewUri(id: , rendition?: string)
|
||||||||||||
Returns url of a template preview
Parameters :
Returns :
string
string The template preview url. |
Public getTemplates | ||||||
getTemplates(objectTypeName: string)
|
||||||
Fetches a list of available templates for a given object type.
Parameters :
Returns :
Observable<PreparedItemTemplate[]>
|
refreshPreparedItemsCount |
refreshPreparedItemsCount()
|
Public Fetch Prepare items
Returns :
void
|
Public updateChildType | ||||||||||||
updateChildType(objectType: string, preparedItemId: string)
|
||||||||||||
Updates the prepared items target object type.
Parameters :
Returns :
Observable<PreparedItem>
Observable |
Public updatePreparedItemIndexData | ||||||
updatePreparedItemIndexData(preparedItemId: , data: )
|
||||||
Parameters :
Returns :
any
Observable |
uploadContent | |||||||||
uploadContent(preparedItemId: string, file: File)
|
|||||||||
Upload Content
Parameters :
Returns :
any
Observable |
uploadToTarget | ||||||||||||||||
uploadToTarget(uploadTarget: UploadTarget, uploadFiles: UploadFileItem[], useBatchUpload?: boolean)
|
||||||||||||||||
Uploads files to an upload target.
Parameters :
Returns :
Observable<any>
Observable |
i | ||||||
i:
|
||||||
Type : number
|
||||||
Default value : 0
|
||||||
Creates a prepared item on the backed side. |
||||||
Parameters :
|
Public preparedItemCount$ |
preparedItemCount$:
|
Type : Observable<number>
|
Default value : this.preparedItemCountSource.asObservable()
|
Public preparedItems$ |
preparedItems$:
|
Type : Observable<PreparedItem[]>
|
Default value : this.preparedItemsSource.asObservable()
|
import {mergeMap, map, tap} from 'rxjs/operators';
import {Injectable} from '@angular/core';
import {Utils} from '../../util/utils';
import {BackendService} from '../backend/backend.service';
import {Observable, ReplaySubject} from 'rxjs';
import {ObjectType} from '../../model/object-type.model';
import {PreparedItem, PreparedItemTemplate} from '../../model/prepared-item.model';
import {UploadService} from '../upload/upload.service';
import {UploadTarget} from '../../model/upload-target.model';
import {UploadFileItem} from '../../model/upload-file-item.model';
import {IPrepareChildTypes} from './objectTypes.interface';
/**
* @ignore
*/
export interface PreparedPostParam {
parentId: string;
/**
* flag indicating whether or not to create one PreparedItem using multiple files uploaded (false)
* or creating a PreparedItem for each file posted to the URL (true)
*/
createMany?: boolean;
childType?: string;
}
/**
* Collection of Prepare Services
*/
@Injectable({
providedIn: 'root'
})
export class PrepareService {
private preparedItems: PreparedItem[];
private preparedItemsSource = new ReplaySubject<PreparedItem[]>(1);
public preparedItems$: Observable<PreparedItem[]> = this.preparedItemsSource.asObservable();
private preparedItemCount: number;
private preparedItemCountSource = new ReplaySubject<number>(1);
public preparedItemCount$: Observable<number> = this.preparedItemCountSource.asObservable();
constructor(private backend: BackendService,
private uploadService: UploadService) {
}
/**
* Public Fetch Prepare items
*/
refreshPreparedItemsCount() {
this.fetchPreparedItemsCount();
}
/**
* Fetches the number of prepared items for the current user.
*/
private fetchPreparedItemsCount() {
this.backend.getJson('/prepare/state')
.pipe(
map((res) => res.count)
)
.subscribe((res) => {
this.preparedItemCount = res;
this.preparedItemCountSource.next(this.preparedItemCount);
});
}
/**
* Creates a prepared item on the backed side.
*
* @param parentId The ID of the parent dms object to add the prepared item to
* @param childType The type of object to be created
* @returns Observable<any>
*/
i = 0;
public createPreparedItem(parentId: string, childType?: string): Observable<any> {
this.i++;
return this.backend
.post(this.createPreparedItemPostUrl({
parentId: parentId,
childType: childType ? childType : null
}), {})
.pipe(
tap(() => this.getPreparedItems$())
);
}
/**
*
* @param preparedItemId
* @param data
* @returns Observable<any>
*/
public updatePreparedItemIndexData(preparedItemId, data) {
return this.backend
.put('/prepare/' + preparedItemId, data)
.pipe(
tap(() => this.getPreparedItems$())
);
}
/**
* Creates an URL for creating a PreparedItem.
*
* @param params Params for generating appropriate uri.
* @returns string A POST URL for creating PreparedItems
*/
public createPreparedItemPostUrl(params: PreparedPostParam): string {
let url = params.createMany ? '/prepare/createmany' : '/prepare/create';
if (params) {
url = Utils.buildUri(url, params);
}
return url;
}
/**
* Retrieves a list of the users prepared items.
*
* @param noForms If set to true the prepared items will be fetched without the form which is faster
* @returns Observable<PreparedItem[]> A collection of prepared item objects
*/
public getPreparedItems$(noForms?: boolean): Observable<PreparedItem[]> {
this.fetchPreparedItems(noForms).subscribe();
return this.preparedItems$;
}
/**
* Retrieve PrepareItems and form them
*
* @param boolean noForms
* @returns Observable<PreparedItem[]>
*/
private fetchPreparedItems(noForms?: boolean): Observable<PreparedItem[]> {
let uri = (noForms) ? '/prepare' : '/prepare?form=true';
return this.backend
.getJson(uri)
.pipe(
map((res: any) => res.map(i => new PreparedItem(i))),
tap(() => this.fetchPreparedItemsCount()),
tap((preparedItems: PreparedItem[]) => this.preparedItemsSource.next(preparedItems.sort(Utils.sortValues('created', 'desc'))))
);
}
/**
* Removes a prepared item.
*
* @param preparedItemId ID of the item to be removed
* @returns Observable<any> Server response
*/
deletePreparedItem(preparedItemId: string): Observable<any> {
return this.backend
.del(`/prepare/${preparedItemId}`)
.pipe(
tap(() => this.getPreparedItems$())
);
}
/**
* Fetches a list of object types allowed to be placed directly below root.
*
* @returns Observable<ObjectType[]> A list of supported object types
*/
public getPrepareRootTypes(): Observable<ObjectType[]> {
return this.backend
.getViaTempCache('/prepare/types')
.pipe(
map((res) => res.types.map((type) => new ObjectType(type)))
);
}
/**
* Fetches a list of allowed child object types for a selected parent.
*
* @param parentId The id of the parent dms object (context folder)
* @param withContentOnly If set to true only child types, that support content are returned.
* @returns Observable<IPrepareChildTypes> A list of supported object types
*/
public getPrepareChildTypes(parentId: string, withContentOnly?: boolean): Observable<IPrepareChildTypes> {
const params = parentId ? {parentId, withcontent: withContentOnly} : {withcontent: withContentOnly}
const uri = Utils.buildUri('/prepare/types', params);
return this.backend
.getViaTempCache(uri)
.pipe(
map(res => ({parent: res.parent, types: res.types.map((type) => new ObjectType(type))}))
);
}
/**
* fetch the preview uri for a prepared item
*
* @param id The id of the prepared item
* @param index When a prepared item contains more than one contents, the index specifies which content
* @param rendition Target rendition (PDF)
* to retrieve the preview uri for
* @returns string Preview uri
*/
public getPreviewUri(id: string, index?: number, rendition?: string): string {
return Utils.buildUri(`${this.backend.getServiceBase()}/prepare/${id}/content`, {rendition, index});
}
/**
* Updates the prepared items target object type.
*
* @param objectType The target object types name
* @param preparedItemId The id of the prepared item to set the type for
* @returns Observable<PreparedItem> The updated prepared item
*/
public updateChildType(objectType: string, preparedItemId: string): Observable<PreparedItem> {
return this.backend
.put(`/prepare/${preparedItemId}/${objectType}?form=true&contentmeta=true`)
.pipe(
map(res => new PreparedItem(res))
);
}
/**
* Add a template to a prepared item.
*
* @param preparedItemId The id of the prepared item to add the template to
* @param templateId The id of the template to be added
* @returns Observable<PreparedItem> The updated prepared item
*/
public addTemplate(preparedItemId, templateId): Observable<PreparedItem> {
return this.backend
.put(`/prepare/${preparedItemId}/template?templateid=${templateId}`)
.pipe(
map(res => new PreparedItem(res))
);
}
/**
* Fetches a list of available templates for a given object type.
*
* @param Observable<PreparedItemTemplate[]> objectTypeName
*/
public getTemplates(objectTypeName: string): Observable<PreparedItemTemplate[]> {
return this.backend
.getJson('/template/list?type=' + objectTypeName)
.pipe(
map((res: any) => res as PreparedItemTemplate[])
);
}
/**
* Returns url of a template preview
*
* @param id Template ID
* @param rendition Target rendition (PDF)
* @returns string The template preview url.
*/
public getTemplatePreviewUri(id, rendition?: string): string {
return Utils.buildUri(`${this.backend.getServiceBase()}/template/${id}/content`, {rendition});
}
/**
* Commit a prepared item. This will create an actual dms object.
* Updates the prepared creation object target type
*
* @param preparedItemId ID of the prepared item
* @param preparedItemType Name of the prepared items target object type
* @param preparedItemData Object holding data of the prepared item
* @returns The id of the created dms object
*/
public commitPreparedItem(preparedItemId, preparedItemType, preparedItemData): Observable<any> {
return this.backend
.put(`/prepare/${preparedItemId}/${preparedItemType}`)
.pipe(
mergeMap(() => this.backend.post(`/prepare/commit/${preparedItemId}`, preparedItemData)),
tap(() => this.getPreparedItems$()),
map(res => res.id)
);
}
/**
* Upload Content
*
* @param string preparedItemId
* @param File file
* @returns Observable<any>
*/
uploadContent(preparedItemId: string, file: File) {
const uri = `${this.backend.getBaseWithContext(this.backend.getServiceBase())}/prepare/${preparedItemId}/content.json?form=true&contentmeta=true`;
return this.uploadService
.upload(uri, [file])
.pipe(
// tap(() => this.getPreparedItems$())
);
}
/**
* Uploads files to an upload target.
*
* @param uploadTarget The upload target to upload files to
* @param uploadFiles Files to be uploaded
* @param useBatchUpload Flag indicating whether or not to use batch processing on the backend side
* @returns Observable<any>
*/
uploadToTarget(uploadTarget: UploadTarget, uploadFiles: UploadFileItem[], useBatchUpload?: boolean): Observable<any> {
const files = uploadFiles.map(f => f._file);
return this.uploadService
.upload(this.getUploadTarget(uploadTarget, useBatchUpload), files)
.pipe(
tap(() => this.getPreparedItems$())
);
}
/**
* Get the uri to post files to based on the upload target.
*
* @param uploadTarget The target to upload files to
* @param useBatchUpload Flag indicating whether or not to upload all files in a single post
* @returns string the uri
*/
private getUploadTarget(uploadTarget: UploadTarget, useBatchUpload?: boolean): string {
let uri;
if (uploadTarget.type === UploadTarget.ROOT) {
/**
* upload to general filing location
*
* @type string
*/
uri = this.backend.getBaseWithContext(this.backend.getServiceBase()) + this.createPreparedItemPostUrl({
parentId: null,
createMany: !useBatchUpload
}
);
} else if (uploadTarget.type === UploadTarget.OBJECT) {
/**
* upload to dms object, means adding or replacing content file
*/
const {referenceObject} = uploadTarget;
uri = `${this.backend.getBaseWithContext(this.backend.getServiceBase())}/dms/${referenceObject.id}/contents?type=${referenceObject.type.name}`;
} else if (uploadTarget.type === UploadTarget.FAVORITE || uploadTarget.type === UploadTarget.AGENT) {
/**
* upload to dms object, means adding or replacing content file
*/
const referenceObject = uploadTarget;
uri = this.backend.getBaseWithContext(this.backend.getServiceBase()) + this.createPreparedItemPostUrl({
parentId: referenceObject.id,
createMany: !useBatchUpload,
childType: referenceObject?.context ? undefined : null
});
} else if (uploadTarget.type === UploadTarget.CONTEXT || uploadTarget.type === UploadTarget.CONTEXT_TREE) {
/**
* upload to context folder, means adding a new dms object to a specific
* context folder ...
* ... if we got a subFolder (node from the context tree) set up we'll add that too
*
* @type string
*/
uri = this.backend.getBaseWithContext(this.backend.getServiceBase()) + this.createPreparedItemPostUrl({
parentId: uploadTarget.referenceObject.id,
createMany: !useBatchUpload,
childType: uploadTarget.subFolder && uploadTarget.subFolder.data.type ? uploadTarget.subFolder.data.type : null
});
}
return uri;
}
/**
* Deletes a prepared creation object content element by index or the complete content.
* If you provide an index the content consists only of one element,
* the content is completely removed from the prepared creation object.
*
* @param preparedItemId ID of the item with the content to be removed
* @param index Parameter that determines which content element should be deleted.
* @returns Observable<PreparedItem> The updated prepared item
*/
public deletePreparedItemContent(preparedItemId: string, index?: number): Observable<PreparedItem> {
const url = `/prepare/${preparedItemId}/content` + (index !== undefined ? `?index=${index}` : '');
return this.backend
.del(url)
.pipe(
map(res => new PreparedItem(res))
);
}
}