export function detailController()

in ui-modules/app-inspector/app/views/main/inspect/management/detail/detail.controller.js [46:151]


export function detailController($scope, $state, $stateParams, entityApi, brSnackbar) {
    $scope.$emit(HIDE_INTERSTITIAL_SPINNER_EVENT);

    const {
        applicationId,
        entityId,
        adjunctId
    } = $stateParams;

    let vm = this;
    let observers = [];

    vm.errors = {};
    vm.applicationId = applicationId;
    vm.entityId = entityId;
    vm.adjunctId = adjunctId;

    entityApi.entity(applicationId, entityId).then((response)=> {
        vm.entity = response.data;
        observers.push(response.subscribe((response)=> {
            vm.entity = response.data;
        }, (response)=> {
            vm.entityNotFound = true;
        }));
    }).catch((error)=> {
        vm.entityNotFound = true;
    });

    // copied from adjuncts-list.js
    function sortHighlights(highlights) {
        if (!highlights) return highlights;
        var result1 = [];
        for (var p in highlights) {
            var result1i = angular.copy(highlights[p]);
            result1i.id = p;
            result1i.display = "ITEM "+p;
            result1.push(result1i);
        }
        result1.sort(function(h1,h2) { return h2.time - h1.time; });
        var result2 = {};
        result1.forEach((x) => result2[x.id] = x);
        return result2;
    }

    entityApi.entityAdjunct(applicationId, entityId, adjunctId).then((response)=> {
        vm.adjunct = response.data;
        vm.highlights = sortHighlights(vm.adjunct.highlights);
        vm.errors.adjunct = undefined;
        observers.push(response.subscribe((response)=> {
            vm.adjunct = response.data;
            vm.highlights = sortHighlights(vm.adjunct.highlights);
            vm.errors.adjunct = undefined;
        }));
    }).catch((error)=> {
        vm.errors.adjunct = 'Cannot load adjunct with ID: ' + adjunctId;
    });

    entityApi.entityAdjunctActivities(applicationId, entityId, adjunctId).then((response)=> {
        vm.activities = response.data;
        vm.errors.activities = undefined;
        observers.push(response.subscribe((response)=> {
            vm.activities = response.data;
            vm.errors.activities = undefined;
        }));
    }).catch((error)=> {
        vm.errors.activities = 'Cannot load activities for adjunct with ID: ' + adjunctId;
    });

    vm.isStartStoppable = () => {
        var type = vm.adjunct.adjunctType || 'policy';
        return angular.isString(type) && START_STOPPABLE_TYPES.includes(type.toLowerCase());
    };

    vm.startAdjunct = function () {
        entityApi.startEntityAdjunct(applicationId, entityId, adjunctId).then((response)=> {
            brSnackbar.create(vm.adjunct.name + ' started successfully');
            vm.adjunct.state = 'RUNNING';
        }).catch((error)=> {
            brSnackbar.create('Cannot start ' + vm.adjunct.name + ': ' + error.data.message);
        });
    };

    vm.stopAdjunct = function () {
        entityApi.stopEntityAdjunct(applicationId, entityId, adjunctId).then((response)=> {
            brSnackbar.create(vm.adjunct.name + ' stopped successfully');
            vm.adjunct.state = 'STOPPED';
        }).catch((error)=> {
            brSnackbar.create('Cannot stop ' + vm.adjunct.name + ': ' + error.data.message);
        });
    };

    vm.destroyAdjunct = function () {
        entityApi.destroyEntityAdjunct(applicationId, entityId, adjunctId).then((response)=> {
            brSnackbar.create(vm.adjunct.name + ' destroyed successfully');
            $state.go(managementState, {applicationId: applicationId, entityId: entityId});
        }).catch((error)=> {
            brSnackbar.create('Cannot destroy ' + vm.adjunct.name + ': ' + error.data.message);
        });
    };

    $scope.$on('$destroy', ()=> {
        observers.forEach((observer)=> {
            observer.unsubscribe();
        });
    });
}