2 * Copyright © 2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 import { all, call, put, takeEvery, takeLatest } from 'redux-saga/effects';
17 import { I18n } from 'react-redux-i18n';
19 import { genericNetworkErrorAction } from 'src/appConstants';
21 setWorkflowVersionAction,
22 versionStateChangedAction,
24 } from 'features/version/versionConstants';
25 import { setInputsOutputs } from 'features/version/inputOutput/inputOutputActions';
26 import { SUBMIT_VERSION } from 'features/version/create/createVersionConstants';
30 } from 'features/version/versionController/versionControllerConstants';
31 import versionApi from 'features/version/versionApi';
32 import { notificationActions } from 'shared/notifications/notificationsActions';
33 import { versionState } from 'features/version/versionConstants';
34 import overviewApi from '../workflow/overview/overviewApi';
35 import { versionListFetchAction } from '../workflow/overview/overviewConstansts';
38 deleteCompositionArtifact
39 } from 'features/version/composition/compositionActions';
40 import { getActivitiesList } from 'features/activities/activitiesActions';
43 * Composition validation - converting artifact string to xml
44 * and checking if bpmn diagram has only one child
48 function validateCurrentArtifact(composition) {
49 const parser = new DOMParser();
50 const xml = parser.parseFromString(composition, 'text/xml');
52 xml.getElementsByTagName('bpmndi:BPMNPlane').BPMNPlane_1.children.length
56 function* fetchVersion(action) {
58 yield put(getActivitiesList());
59 const data = yield call(versionApi.fetchVersion, action.payload);
60 const { inputs, outputs, ...rest } = data;
63 if (rest.hasArtifact) {
64 composition = yield call(
65 versionApi.fetchVersionArtifact,
69 //Clearing the store from old artifact using init the default
70 yield put(deleteCompositionArtifact());
73 put(setWorkflowVersionAction(rest)),
74 put(setInputsOutputs({ inputs, outputs })),
75 composition && put(updateComposition(composition))
78 yield put(genericNetworkErrorAction(error));
82 function* watchSubmitVersion(action) {
84 const { workflowId, history } = action.payload;
85 const data = yield call(versionApi.createNewVersion, action.payload);
86 const versions = yield call(overviewApi.getVersions, workflowId);
87 yield put(versionListFetchAction(versions));
89 history.push(`/workflows/workflow/${workflowId}/version/${data.id}`)
92 yield put(genericNetworkErrorAction(error));
96 function* watchUpdateVersion(action) {
101 params: { composition, ...versionData }
103 const isArtifactValid = validateCurrentArtifact(composition);
104 yield call(versionApi.updateVersion, {
109 notificationActions.showSuccess({
110 title: I18n.t('workflow.confirmationMessages.updateTitle'),
111 message: I18n.t('workflow.confirmationMessages.updateMessage')
114 if (isArtifactValid) {
115 yield call(versionApi.updateVersionArtifact, {
118 versionName: versionData.name.split('.').join('_'),
119 versionId: versionData.id,
123 yield call(versionApi.deleteVersionArtifact, {
125 versionId: versionData.id
128 return isArtifactValid;
130 yield put(genericNetworkErrorAction(error));
134 function* watchCertifyVersion(action) {
136 const isArtifactValid = yield call(watchUpdateVersion, action);
137 if (!isArtifactValid)
138 throw new Error('Could not update empty artifact');
139 yield call(versionApi.certifyVersion, {
142 yield put(versionStateChangedAction({ state: versionState.CERTIFIED }));
144 notificationActions.showSuccess({
145 title: I18n.t('workflow.confirmationMessages.certifyTitle'),
146 message: I18n.t('workflow.confirmationMessages.certifyMessage')
151 notificationActions.showError({
152 title: I18n.t('workflow.confirmationMessages.certifyTitle'),
153 message: I18n.t('workflow.composition.certifyArtifact')
156 yield put(genericNetworkErrorAction(error));
160 function* versionSaga() {
161 yield takeLatest(FETCH_REQUESTED, fetchVersion);
162 yield takeEvery(SUBMIT_VERSION, watchSubmitVersion);
163 yield takeEvery(SAVE_ACTION, watchUpdateVersion);
164 yield takeEvery(CERTIFY_ACTION, watchCertifyVersion);
167 export default versionSaga;