Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / LicenseModelActionHelper.js
1 /*!
2  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
13  * or implied. See the License for the specific language governing
14  * permissions and limitations under the License.
15  */
16 import RestAPIUtil from 'nfvo-utils/RestAPIUtil.js';
17 import Configuration from 'sdc-app/config/Configuration.js';
18 import {actionTypes} from './LicenseModelConstants.js';
19 import {actionTypes as modalActionTypes} from 'nfvo-components/modal/GlobalModalConstants.js';
20 import {actionsEnum as vcActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
21 import i18n from 'nfvo-utils/i18n/i18n.js';
22 import LicenseAgreementActionHelper from './licenseAgreement/LicenseAgreementActionHelper.js';
23 import FeatureGroupsActionHelper from './featureGroups/FeatureGroupsActionHelper.js';
24 import EntitlementPoolsActionHelper from './entitlementPools/EntitlementPoolsActionHelper.js';
25 import LicenseKeyGroupsActionHelper from './licenseKeyGroups/LicenseKeyGroupsActionHelper.js';
26 import ItemsHelper from '../../common/helpers/ItemsHelper.js';
27 import MergeEditorActionHelper from 'sdc-app/common/merge/MergeEditorActionHelper.js';
28 import {modalContentMapper} from 'sdc-app/common/modal/ModalContentMapper.js';
29 import {CommitModalType} from 'nfvo-components/panel/versionController/components/CommitCommentModal.jsx';
30 import versionPageActionHelper from 'sdc-app/onboarding/versionsPage/VersionsPageActionHelper.js';
31 import {itemTypes} from 'sdc-app/onboarding/versionsPage/VersionsPageConstants.js';
32 import {catalogItemStatuses} from 'sdc-app/onboarding/onboard/onboardingCatalog/OnboardingCatalogConstants.js';
33 import {actionsEnum as VersionControllerActionsEnum} from 'nfvo-components/panel/versionController/VersionControllerConstants.js';
34
35 function baseUrl() {
36         const restPrefix = Configuration.get('restPrefix');
37         return `${restPrefix}/v1.0/vendor-license-models/`;
38 }
39
40 function fetchLicenseModels() {
41         return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Draft`);
42 }
43
44 function fetchFinalizedLicenseModels() {
45         return RestAPIUtil.fetch(`${baseUrl()}?versionFilter=Certified`);
46 }
47
48 function fetchLicenseModelById(licenseModelId, version) {
49         const {id: versionId} = version;
50         return RestAPIUtil.fetch(`${baseUrl()}${licenseModelId}/versions/${versionId}`);
51 }
52
53 function putLicenseModel(licenseModel) {
54         let {id, vendorName, description, iconRef, version: {id: versionId}} = licenseModel;
55         return RestAPIUtil.put(`${baseUrl()}${id}/versions/${versionId}`, {
56                 vendorName,
57                 description,
58                 iconRef
59         });
60 }
61
62 function putLicenseModelAction({itemId, action, version}) {
63         const {id: versionId} = version;
64         return RestAPIUtil.put(`${baseUrl()}${itemId}/versions/${versionId}/actions`, {action: action});
65 }
66
67 const LicenseModelActionHelper = {
68
69         fetchLicenseModels(dispatch) {
70                 return fetchLicenseModels().then(response => {
71                         dispatch({
72                                 type: actionTypes.LICENSE_MODELS_LIST_LOADED,
73                                 response
74                         });
75                 });
76         },
77
78         fetchFinalizedLicenseModels(dispatch) {
79                 return fetchFinalizedLicenseModels().then(response => dispatch({
80                         type: actionTypes.FINALIZED_LICENSE_MODELS_LIST_LOADED,
81                         response
82                 }));
83
84         },
85
86         fetchLicenseModelById(dispatch, {licenseModelId, version}) {
87
88                 return fetchLicenseModelById(licenseModelId, version).then(response => {
89                         dispatch({
90                                 type: actionTypes.LICENSE_MODEL_LOADED,
91                                 response: {...response, version}
92                         });
93                 });
94         },
95
96         fetchLicenseModelItems(dispatch, {licenseModelId, version}) {
97                 return Promise.all([
98                         LicenseAgreementActionHelper.fetchLicenseAgreementList(dispatch, {licenseModelId, version}),
99                         FeatureGroupsActionHelper.fetchFeatureGroupsList(dispatch, {licenseModelId, version}),
100                         EntitlementPoolsActionHelper.fetchEntitlementPoolsList(dispatch, {licenseModelId, version}),
101                         LicenseKeyGroupsActionHelper.fetchLicenseKeyGroupsList(dispatch, {licenseModelId, version})
102                 ]);
103         },
104
105         manageSubmitAction(dispatch, {licenseModelId, version, isDirty}) {
106                 if(isDirty) {
107                         const onCommit = comment => {
108                                 return this.performVCAction(dispatch, {licenseModelId, action: vcActionsEnum.COMMIT, version, comment}).then(() => {
109                                         return this.performSubmitAction(dispatch, {licenseModelId, version});
110                                 });
111                         };
112                         dispatch({
113                                 type: modalActionTypes.GLOBAL_MODAL_SHOW,
114                                 data: {
115                                         modalComponentName: modalContentMapper.COMMIT_COMMENT,
116                                         modalComponentProps: {
117                                                 onCommit,
118                                                 type: CommitModalType.COMMIT_SUBMIT
119                                         },
120                                         title: i18n('Commit & Submit')
121                                 }
122                         });
123                         return Promise.reject();
124                 }
125                 return this.performSubmitAction(dispatch, {licenseModelId, version});
126         },
127
128         performSubmitAction(dispatch, {licenseModelId, version}) {
129                 return putLicenseModelAction({itemId: licenseModelId, action: vcActionsEnum.SUBMIT, version}).then(() => {
130                         return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id}).then(updatedVersion => {
131                                 dispatch({
132                                         type: modalActionTypes.GLOBAL_MODAL_SUCCESS,
133                                         data: {
134                                                 title: i18n('Submit Succeeded'),
135                                                 msg: i18n('This license model successfully submitted'),
136                                                 cancelButtonText: i18n('OK'),
137                                                 timeout: 2000
138                                         }
139                                 });
140                                 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
141                                 return Promise.resolve(updatedVersion);
142                         });
143                 });
144         },
145
146         performVCAction(dispatch, {licenseModelId, action, version, comment}) {
147                 return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({inMerge, isDirty, updatedVersion}) => {
148                         if (updatedVersion.status === catalogItemStatuses.CERTIFIED &&
149                                 (action === VersionControllerActionsEnum.COMMIT || action === VersionControllerActionsEnum.SYNC)) {
150                                 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
151                                 dispatch({
152                                         type: modalActionTypes.GLOBAL_MODAL_WARNING,
153                                         data: {
154                                                 title: i18n('Commit error'),
155                                                 msg: i18n('Item version already Certified'),
156                                                 cancelButtonText: i18n('Cancel')
157                                         }
158                                 });
159                                 return Promise.resolve(updatedVersion);
160                         }
161                         if (!inMerge) {
162                                 if(action === vcActionsEnum.SUBMIT) {
163                                         return this.manageSubmitAction(dispatch, {licenseModelId, version, isDirty});
164                                 }
165                                 else {
166                                         return ItemsHelper.performVCAction({itemId: licenseModelId, action, version, comment}).then(() => {
167                                                 versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModelId});
168                                                 if (action === vcActionsEnum.SYNC) {
169                                                         return MergeEditorActionHelper.analyzeSyncResult(dispatch, {itemId: licenseModelId, version}).then(({updatedVersion}) => {
170                                                                 return Promise.resolve(updatedVersion);
171                                                         });
172                                                 } else {
173                                                         return ItemsHelper.checkItemStatus(dispatch, {itemId: licenseModelId, versionId: version.id});
174                                                 }
175                                         });
176                                 }
177                         }
178                 });
179         },
180
181         saveLicenseModel(dispatch, {licenseModel}) {
182                 return putLicenseModel(licenseModel).then(() => {
183                         dispatch({
184                                 type: actionTypes.LICENSE_MODEL_LOADED,
185                                 response: licenseModel
186                         });
187                         const {id, version: {id: versionId}} = licenseModel;
188                         return ItemsHelper.checkItemStatus(dispatch, {itemId: id, versionId}).then(updatedVersion => {
189                                 if (updatedVersion.status !== licenseModel.version.status) {
190                                         versionPageActionHelper.fetchVersions(dispatch, {itemType: itemTypes.LICENSE_MODEL, itemId: licenseModel.id});
191                                 }
192                         });
193                 });
194         }
195
196 };
197
198 export default LicenseModelActionHelper;