aboutsummaryrefslogtreecommitdiffstats
path: root/catalog-ui/src/app/ng2/components/forms/artifacts-form/artifacts.service.ts
blob: f9400e9d1859f1035dcfc6c9353ebd9c0080fdd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import { Injectable } from '@angular/core';
import { Store } from '@ngxs/store';
import { SdcUiCommon, SdcUiComponents, SdcUiServices } from 'onap-ui-angular';
import { ArtifactModel } from '../../../../models';
import { ArtifactGroupType, ArtifactType } from '../../../../utils/constants';
import { TopologyTemplateService } from '../../../services/component-services/topology-template.service';
import { TranslateService } from '../../../shared/translator/translate.service';
import { CreateOrUpdateArtifactAction, DeleteArtifactAction } from '../../../store/actions/artifacts.action';
import { EnvParamsComponent } from '../env-params/env-params.component';
import { ArtifactFormComponent } from './artifact-form.component';

import {
    CreateInstanceArtifactAction,
    DeleteInstanceArtifactAction,
    UpdateInstanceArtifactAction
} from '../../../store/actions/instance-artifacts.actions';

@Injectable()
export class ArtifactsService {

    constructor(private serviceLoader: SdcUiServices.LoaderService,
                private modalService: SdcUiServices.ModalService,
                private topologyTemplateService: TopologyTemplateService,
                private translateService: TranslateService,
                private store: Store) {
    }

    public dispatchArtifactAction = (componentId: string, componentType: string, artifact: ArtifactModel, artifactType: ArtifactGroupType, instanceId: string) => {
        const artifactObj = {
            componentType,
            componentId,
            instanceId,
            artifact
        };

        // Create or update instance artifact
        if (instanceId) {
            if (!artifact.uniqueId) {
                // create instance artifact
                return this.store.dispatch(new CreateInstanceArtifactAction(artifactObj));
            } else {
                // update instance artifact
                return this.store.dispatch(new UpdateInstanceArtifactAction(artifactObj));
            }
        } else {
            // Create or update artifact
            return this.store.dispatch(new CreateOrUpdateArtifactAction(artifactObj));
        }
    }

    public openArtifactModal = (componentId: string, componentType: string, artifact: ArtifactModel, artifactType: ArtifactGroupType, isViewOnly?: boolean, instanceId?: string) => {

        let modalInstance;

        const onOkPressed = () => {
            const updatedArtifact = modalInstance.innerModalContent.instance.artifact;
            this.serviceLoader.activate();
            this.dispatchArtifactAction(componentId, componentType, updatedArtifact, artifactType, instanceId)
                    .subscribe().add(() => this.serviceLoader.deactivate());
        };

        const addOrUpdateArtifactModalConfig = {
            title: (artifact && artifact.uniqueId) ? 'Update Artifact' : 'Create Artifact',
            size: 'md',
            type: SdcUiCommon.ModalType.custom,
            testId: 'upgradeVspModal',
            buttons: [
                {
                    id: 'done',
                    text: 'DONE',
                    disabled: isViewOnly,
                    size: 'Add Another',
                    closeModal: true,
                    callback: onOkPressed
                },
                {text: 'CANCEL', size: 'sm', closeModal: true, type: 'secondary'}
            ] as SdcUiCommon.IModalButtonComponent[]
        } as SdcUiCommon.IModalConfig;

        modalInstance = this.modalService.openCustomModal(addOrUpdateArtifactModalConfig, ArtifactFormComponent, {
            artifact: new ArtifactModel(artifact),
            artifactType,
            instanceId,
            componentType,
            isViewOnly
        });

        if (!isViewOnly) {
            modalInstance.innerModalContent.instance.onValidationChange.subscribe((isValid) => {
                modalInstance.getButtonById('done').disabled = !isValid;
            });
        }
    }

    public openViewEnvParams(componentType: string, componentId: string, artifact: ArtifactModel, instanceId?: string) {
        const envParamsModal = {
            title: artifact.artifactDisplayName,
            size: 'xl',
            type: SdcUiCommon.ModalType.custom,
            testId: 'viewEnvParams',
            isDisabled: false,
        } as SdcUiCommon.IModalConfig;

        this.modalService.openCustomModal(envParamsModal, EnvParamsComponent, {
            isInstanceSelected: !!instanceId,    // equals to instanceId ? true : false
            artifact: new ArtifactModel(artifact),
            isViewOnly: true
        });
    }

    public openUpdateEnvParams(componentType: string, componentId: string, artifact: ArtifactModel, instanceId?: string) {
        let modalInstance;
        const onOkPressed = () => {
            const updatedArtifact = modalInstance.innerModalContent.instance.artifact;
            this.serviceLoader.activate();
            this.dispatchArtifactAction(componentId, componentType, updatedArtifact, ArtifactType.DEPLOYMENT, instanceId)
                    .subscribe().add(() => this.serviceLoader.deactivate());
        };

        const envParamsModal = {
            title: artifact.artifactDisplayName,
            size: 'xl',
            type: SdcUiCommon.ModalType.custom,
            testId: 'envParams',
            isDisabled: false,
            buttons: [
                {
                    id: 'save',
                    text: 'Save',
                    spinner_position: 'left',
                    size: 'sm',
                    callback: onOkPressed,
                    closeModal: true
                },
                {text: 'Cancel', size: 'sm', closeModal: true, type: 'secondary'}
            ] as SdcUiCommon.IModalButtonComponent[]
        } as SdcUiCommon.IModalConfig;

        modalInstance = this.modalService.openCustomModal(envParamsModal, EnvParamsComponent, {
            isInstanceSelected: !!instanceId,    // equals to instanceId ? true : false
            artifact: new ArtifactModel(artifact)
        });

        modalInstance.innerModalContent.instance.onValidationChange.subscribe((isValid) => {
            modalInstance.getButtonById('save').disabled = !isValid;
        });
    }

    public deleteArtifact = (componentType: string, componentId: string, artifact: ArtifactModel, instanceId?: string) => {

        const artifactObject = {
            componentType,
            componentId,
            artifact,
            instanceId
        };

        const onOkPressed: Function = () => {
            this.serviceLoader.activate();
            this.store.dispatch((instanceId) ? new DeleteInstanceArtifactAction(artifactObject) : new DeleteArtifactAction(artifactObject))
                    .subscribe().add(() => this.serviceLoader.deactivate());
        };

        const title = this.translateService.translate('ARTIFACT_VIEW_DELETE_MODAL_TITLE');
        const text = this.translateService.translate('ARTIFACT_VIEW_DELETE_MODAL_TEXT', {name: artifact.artifactDisplayName});
        const okButton = {
            testId: 'OK',
            text: 'OK',
            type: SdcUiCommon.ButtonType.warning,
            callback: onOkPressed,
            closeModal: true
        } as SdcUiComponents.ModalButtonComponent;
        this.modalService.openWarningModal(title, text, 'delete-information-artifact-modal', [okButton]);
    }
}