File

projects/netgrif-components-core/src/lib/task/services/cancel-task.service.ts

Description

Service that handles the logic of canceling a task.

Extends

TaskHandlingService

Index

Methods

Constructor

constructor(_log: LoggerService, _taskEventService: TaskEventService, _taskResourceService: TaskResourceService, _translate: TranslateService, _snackBar: SnackBarService, _taskState: TaskRequestStateService, _userComparator: UserComparatorService, _taskEvent: TaskEventService, _taskDataService: TaskDataService, _eventQueue: EventQueueService, _eventService: EventService, _changedFieldsService: ChangedFieldsService, _taskOperations: TaskOperations, _selectedCaseService: SelectedCaseService, _taskViewService: TaskViewService, _taskContentService: TaskContentService, permissionService: PermissionService)
Parameters :
Name Type Optional
_log LoggerService No
_taskEventService TaskEventService No
_taskResourceService TaskResourceService No
_translate TranslateService No
_snackBar SnackBarService No
_taskState TaskRequestStateService No
_userComparator UserComparatorService No
_taskEvent TaskEventService No
_taskDataService TaskDataService No
_eventQueue EventQueueService No
_eventService EventService No
_changedFieldsService ChangedFieldsService No
_taskOperations TaskOperations No
_selectedCaseService SelectedCaseService No
_taskViewService TaskViewService No
_taskContentService TaskContentService No
permissionService PermissionService No

Methods

Public cancel
cancel(afterAction: AfterAction)

Performs the 'cancel' operation on the task held by TaskContentService.

Doesn't send any requests if the loading indicator is in it's active state. Otherwise sets the indicator to the active state and disables it once the request response is received.

The argument can be used to chain operations together, or to execute code conditionally based on the success state of the cancel operation.

If the task held within the TaskContentService changes before a response is received, the response will be ignored and the afterAction will not be executed.

Parameters :
Name Type Optional Default value Description
afterAction AfterAction No new AfterAction()

if cancel completes successfully true will be emitted into this Subject, otherwise false will be emitted

Returns : void
Protected cancelRequest
cancelRequest(afterAction: AfterAction, canceledTaskId: string, nextEvent: AfterAction, forceReload: boolean)

Calls the endpoint and processes the possible responses. If set to false a regular reload is performed instead.

Parameters :
Name Type Optional Default value Description
afterAction AfterAction No new AfterAction()

the action that should be performed after the request is processed

canceledTaskId string No

the id of the task that is being canceled

nextEvent AfterAction No new AfterAction()

indicates to the event queue that the next event can be processed

forceReload boolean No

whether a force reload of the task data should be performed after cancel. If set to false a regular reload is performed instead.

Returns : void
Protected completeActions
completeActions(afterAction: AfterAction, nextEvent: AfterAction, bool: boolean, outcome?: TaskEventOutcome)

complete all action streams and send notification with selected boolean

Parameters :
Name Type Optional
afterAction AfterAction No
nextEvent AfterAction No
bool boolean No
outcome TaskEventOutcome Yes
Returns : void
Protected performCancelRequest
performCancelRequest(afterAction: AfterAction, nextEvent: AfterAction, forceReload: boolean)

Performs a cancel request on the task currently stored in the taskContent service If set to false a regular reload is performed instead.

Parameters :
Name Type Optional Description
afterAction AfterAction No

the action that should be performed after the request is processed

nextEvent AfterAction No

indicates to the event queue that the next event can be processed

forceReload boolean No

whether a force reload of the task data should be performed after cancel. If set to false a regular reload is performed instead.

Returns : void
Protected sendNotification
sendNotification(success: boolean, outcome?: TaskEventOutcome)

Publishes a cancel notification to the TaskEventService

Parameters :
Name Type Optional Description
success boolean No

whether the cancel operation was successful or not

outcome TaskEventOutcome Yes
Returns : void
Protected isTaskPresent
isTaskPresent()
Inherited from TaskHandlingService
Returns : boolean

true if a {

Protected isTaskRelevant
isTaskRelevant(requestedTaskId: string)
Inherited from TaskHandlingService

Checks whether the current state of the TaskContentService and optionally if the SelectedCaseService, is still relevant to the task that was requested.

This method is useful if you use UnlimitedTaskContentService, or a similar implementation. It is possible for the currently "selected" task to change in-between a backend request was sent and the response was received. In that case the response is no longer relevant and should be discarded, otherwise an illegal task state could be achieved on frontend.

Parameters :
Name Type Optional Description
requestedTaskId string No

the stringId of the requested task

Returns : boolean

true if the requested task is still relevant to the state of the frontend. Returns false otherwise.

import {Inject, Injectable, Optional} from '@angular/core';
import {LoggerService} from '../../logger/services/logger.service';
import {TaskContentService} from '../../task-content/services/task-content.service';
import {TaskEventService} from '../../task-content/services/task-event.service';
import {TaskResourceService} from '../../resources/engine-endpoint/task-resource.service';
import {TranslateService} from '@ngx-translate/core';
import {SnackBarService} from '../../snack-bar/services/snack-bar.service';
import {TaskRequestStateService} from './task-request-state.service';
import {TaskHandlingService} from './task-handling-service';
import {NAE_TASK_OPERATIONS} from '../models/task-operations-injection-token';
import {TaskOperations} from '../interfaces/task-operations';
import {UserComparatorService} from '../../user/services/user-comparator.service';
import {SelectedCaseService} from './selected-case.service';
import {createTaskEventNotification} from '../../task-content/model/task-event-notification';
import {TaskEvent} from '../../task-content/model/task-event';
import {TaskDataService} from './task-data.service';
import {take} from 'rxjs/operators';
import {TaskViewService} from '../../view/task-view/service/task-view.service';
import {CancelTaskEventOutcome} from '../../event/model/event-outcomes/task-outcomes/cancel-task-event-outcome';
import {EventOutcomeMessageResource} from '../../resources/interface/message-resource';
import {EventQueueService} from '../../event-queue/services/event-queue.service';
import {QueuedEvent} from '../../event-queue/model/queued-event';
import {AfterAction} from '../../utility/call-chain/after-action';
import {PermissionService} from '../../authorization/permission/permission.service';
import {ChangedFieldsService} from '../../changed-fields/services/changed-fields.service';
import { EventService} from '../../event/services/event.service';
import {ChangedFieldsMap} from '../../event/services/interfaces/changed-fields-map';
import {TaskEventOutcome} from '../../event/model/event-outcomes/task-outcomes/task-event-outcome';

/**
 * Service that handles the logic of canceling a task.
 */
@Injectable()
export class CancelTaskService extends TaskHandlingService {

    constructor(protected _log: LoggerService,
                protected _taskEventService: TaskEventService,
                protected _taskResourceService: TaskResourceService,
                protected _translate: TranslateService,
                protected _snackBar: SnackBarService,
                protected _taskState: TaskRequestStateService,
                protected _userComparator: UserComparatorService,
                protected _taskEvent: TaskEventService,
                protected _taskDataService: TaskDataService,
                protected _eventQueue: EventQueueService,
                protected _eventService: EventService,
                protected _changedFieldsService: ChangedFieldsService,
                @Inject(NAE_TASK_OPERATIONS) protected _taskOperations: TaskOperations,
                @Optional() _selectedCaseService: SelectedCaseService,
                @Optional() protected _taskViewService: TaskViewService,
                _taskContentService: TaskContentService,
                protected permissionService: PermissionService) {
        super(_taskContentService, _selectedCaseService);
    }

    /**
     * Performs the 'cancel' operation on the task held by {@link TaskContentService}.
     *
     * Doesn't send any requests if the loading indicator is in it's active state.
     * Otherwise sets the indicator to the active state and disables it once the request response is received.
     *
     * The argument can be used to chain operations together,
     * or to execute code conditionally based on the success state of the cancel operation.
     *
     * If the task held within the {@link TaskContentService} changes before a response is received, the response will be ignored
     * and the `afterAction` will not be executed.
     * @param afterAction if cancel completes successfully `true` will be emitted into this Subject, otherwise `false` will be emitted
     */
    public cancel(afterAction: AfterAction = new AfterAction()) {
        this._eventQueue.scheduleEvent(new QueuedEvent(
            () => this.permissionService.canCancel(this._safeTask),
            nextEvent => {
                this.performCancelRequest(afterAction, nextEvent, this._taskViewService !== null && !this._taskViewService.allowMultiOpen);
            },
            nextEvent => {
                this.completeActions(afterAction, nextEvent, false);
            }
        ));
    }

    /**
     * Performs a `cancel` request on the task currently stored in the `taskContent` service
     * @param afterAction the action that should be performed after the request is processed
     * @param nextEvent indicates to the event queue that the next event can be processed
     * @param forceReload whether a force reload of the task data should be performed after cancel.
     * If set to `false` a regular reload is performed instead.
     */
    protected performCancelRequest(afterAction: AfterAction, nextEvent: AfterAction, forceReload: boolean) {
        const canceledTaskId = this._safeTask.stringId;

        // this is probably no longer necessary because of the event queue
        if (this._taskState.isLoading(canceledTaskId)) {
            nextEvent.resolve(true);
            return;
        }

        this._taskState.startLoading(canceledTaskId);
        this.cancelRequest(afterAction, canceledTaskId, nextEvent, forceReload);
    }

    /**
     * Calls the endpoint and processes the possible responses.
     * @param afterAction the action that should be performed after the request is processed
     * @param canceledTaskId the id of the task that is being canceled
     * @param nextEvent indicates to the event queue that the next event can be processed
     * @param forceReload whether a force reload of the task data should be performed after cancel.
     * If set to `false` a regular reload is performed instead.
     */
    protected cancelRequest(afterAction: AfterAction = new AfterAction(),
                            canceledTaskId: string,
                            nextEvent: AfterAction = new AfterAction(),
                            forceReload: boolean) {
        this._taskResourceService.cancelTask(this._safeTask.stringId).pipe(take(1)).subscribe(
            (outcomeResource: EventOutcomeMessageResource) => {
            this._taskState.stopLoading(canceledTaskId);
            if (!this.isTaskRelevant(canceledTaskId)) {
                this._log.debug('current task changed before the cancel response could be received, discarding...');
                nextEvent.resolve(false);
                return;
            }

            if (outcomeResource.success) {
                this._taskContentService.updateStateData(outcomeResource.outcome as CancelTaskEventOutcome);
                const changedFieldsMap: ChangedFieldsMap = this._eventService
                    .parseChangedFieldsFromOutcomeTree(outcomeResource.outcome);
                if (!!changedFieldsMap) {
                    this._changedFieldsService.emitChangedFields(changedFieldsMap);
                }
                forceReload ? this._taskOperations.forceReload() : this._taskOperations.reload();
                this.completeActions(afterAction, nextEvent, true, outcomeResource.outcome as CancelTaskEventOutcome);
            } else if (outcomeResource.error !== undefined) {
                if (outcomeResource.error !== '') {
                    this._snackBar.openErrorSnackBar(outcomeResource.error);
                }
                if (outcomeResource.outcome !== undefined) {
                    const changedFieldsMap = this._eventService.parseChangedFieldsFromOutcomeTree(outcomeResource.outcome);
                    this._changedFieldsService.emitChangedFields(changedFieldsMap);
                }
                this.completeActions(afterAction, nextEvent, false);
            }
        }, error => {
            this._taskState.stopLoading(canceledTaskId);
            this._log.debug('canceling task failed', error);

            if (!this.isTaskRelevant(canceledTaskId)) {
                this._log.debug('current task changed before the cancel error could be received');
                nextEvent.resolve(false);
                return;
            }

            this._snackBar.openErrorSnackBar(`${this._translate.instant('tasks.snackbar.cancelTask')}
             ${this._task} ${this._translate.instant('tasks.snackbar.failed')}`);
            this.completeActions(afterAction, nextEvent, false);
        });
    }

    /**
     * complete all action streams and send notification with selected boolean
     */
    protected completeActions(afterAction: AfterAction, nextEvent: AfterAction, bool: boolean, outcome?: TaskEventOutcome): void {
        this.sendNotification(bool, outcome);
        afterAction.resolve(bool);
        nextEvent.resolve(bool);
    }

    /**
     * Publishes a cancel notification to the {@link TaskEventService}
     * @param success whether the cancel operation was successful or not
     * @param outcome
     */
    protected sendNotification(success: boolean, outcome?: TaskEventOutcome): void {
        this._taskEvent.publishTaskEvent(createTaskEventNotification(this._safeTask, TaskEvent.CANCEL, success, outcome));
    }
}

result-matching ""

    No results matching ""