Add collaboration feature
[sdc.git] / openecomp-ui / src / sdc-app / onboarding / licenseModel / overview / LicenseModelOverview.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 {connect} from 'react-redux';
17 import LicenseModelActionHelper from 'sdc-app/onboarding/licenseModel/LicenseModelActionHelper.js';
18 import LicenseModelOverviewView from './LicenseModelOverviewView.jsx';
19 import {overviewEditorHeaders, selectedButton} from './LicenseModelOverviewConstants.js';
20 import licenseModelOverviewActionHelper from './licenseModelOverviewActionHelper.js';
21
22 export const mapStateToProps = ({
23         licenseModel: {
24                 licenseModelEditor,
25                 entitlementPool,
26                 licenseAgreement,
27                 featureGroup,
28                 licenseKeyGroup,
29                 licenseModelOverview
30         }
31 }) => {
32
33         let modalHeader, licensingDataList;
34         let isDisplayModal = false;
35
36         const reduceLicenseKeyGroups = (accum, licenseKeyGroupId) => {
37                 let curLicenseKeyGroup = licenseKeyGroup.licenseKeyGroupsList.find(item => {return item.id === licenseKeyGroupId;});
38                 if (curLicenseKeyGroup) {
39                         accum.push({
40                                 ...curLicenseKeyGroup,
41                                 itemType: overviewEditorHeaders.LICENSE_KEY_GROUP
42                         });
43                 }
44                 return accum;
45         };
46
47         const reduceEntitlementPools = (accum, entitlementPoolId) => {
48                 let curEntitlementPool = entitlementPool.entitlementPoolsList.find(item => {return item.id === entitlementPoolId;});
49                 if (curEntitlementPool) {
50                         accum.push ({
51                                 ...curEntitlementPool,
52                                 itemType: overviewEditorHeaders.ENTITLEMENT_POOL
53                         });
54                 }
55                 return accum;
56         };
57
58         const reduceFeatureGroups = (accum, featureGroupId) => {
59                 let curFeatureGroup = featureGroup.featureGroupsList.find(item => {return item.id === featureGroupId;});
60                 if (curFeatureGroup) {
61                         let {entitlementPoolsIds = [], licenseKeyGroupsIds = []} = curFeatureGroup;
62                         accum.push({
63                                 ...curFeatureGroup,
64                                 itemType: overviewEditorHeaders.FEATURE_GROUP,
65                                 children: [
66                                         ...entitlementPoolsIds.length ? entitlementPoolsIds.reduce(reduceEntitlementPools, []) : [],
67                                         ...licenseKeyGroupsIds.length ? licenseKeyGroupsIds.reduce(reduceLicenseKeyGroups, []) : []
68                                 ]
69                         });
70                 }
71                 return accum;
72         };
73
74
75         const checkEP  = (accum, elem) => {
76                 if (!elem.referencingFeatureGroups || !elem.referencingFeatureGroups.length) {
77                         accum.push({
78                                 ...elem,
79                                 itemType: overviewEditorHeaders.ENTITLEMENT_POOL
80                         });
81                 }
82                 return accum;
83         };
84
85         const checkLG = (accum, elem) => {
86                 if (!elem.referencingFeatureGroups || !elem.referencingFeatureGroups.length) {
87                         accum.push({
88                                 ...elem,
89                                 itemType: overviewEditorHeaders.LICENSE_KEY_GROUP
90                         });
91                 }
92                 return accum;
93         };
94
95         const checkFG = (accum, elem) => {
96                 if (!elem.referencingLicenseAgreements || !elem.referencingLicenseAgreements.length) {
97                         let {entitlementPoolsIds = [], licenseKeyGroupsIds = []} = elem;
98                         accum.push({
99                                 ...elem,
100                                 itemType: overviewEditorHeaders.FEATURE_GROUP,
101
102                                 children: [
103                                         ...entitlementPoolsIds.length ? entitlementPoolsIds.reduce(reduceEntitlementPools, []) : [],
104                                         ...licenseKeyGroupsIds.length ? licenseKeyGroupsIds.reduce(reduceLicenseKeyGroups, []) : []
105                                 ]
106
107                         });
108                 }
109                 return accum;
110         };
111
112
113
114         const mapLicenseAgreementData = licenseAgreement => {
115                 let {featureGroupsIds = []} = licenseAgreement;
116                 return {
117                         ...licenseAgreement,
118                         itemType: overviewEditorHeaders.LICENSE_AGREEMENT,
119                         children: featureGroupsIds.length ? featureGroupsIds.reduce(reduceFeatureGroups, []) : []
120                 };
121         };
122
123         if (entitlementPool.entitlementPoolEditor && entitlementPool.entitlementPoolEditor.data) {
124                 modalHeader = overviewEditorHeaders.ENTITLEMENT_POOL;
125                 isDisplayModal = true;
126         }else
127         if (licenseAgreement.licenseAgreementEditor && licenseAgreement.licenseAgreementEditor.data) {
128                 modalHeader = overviewEditorHeaders.LICENSE_AGREEMENT;
129                 isDisplayModal = true;
130         }else
131         if (featureGroup.featureGroupEditor && featureGroup.featureGroupEditor.data) {
132                 modalHeader = overviewEditorHeaders.FEATURE_GROUP;
133                 isDisplayModal = true;
134         }else
135         if (licenseKeyGroup.licenseKeyGroupsEditor && licenseKeyGroup.licenseKeyGroupsEditor.data) {
136                 modalHeader = overviewEditorHeaders.LICENSE_KEY_GROUP;
137                 isDisplayModal = true;
138         }
139
140         let orphanDataList = [
141                 ...featureGroup.featureGroupsList.reduce(checkFG, []),
142                 ...entitlementPool.entitlementPoolsList.reduce(checkEP, []),
143                 ...licenseKeyGroup.licenseKeyGroupsList.reduce(checkLG, [])
144         ];
145
146         licensingDataList = licenseAgreement.licenseAgreementList && licenseAgreement.licenseAgreementList.length ? licenseAgreement.licenseAgreementList.map(mapLicenseAgreementData) : [];
147         let selectedTab = licenseModelOverview.selectedTab;
148         // on first entry, we will decide what tab to open depending on data. if there are no connections, we will open the orphans
149         if (selectedTab === null) {
150                 selectedTab = (licensingDataList.length) ? selectedButton.VLM_LIST_VIEW : selectedButton.NOT_IN_USE;
151         }
152
153         return {
154                 isDisplayModal,
155                 modalHeader,
156                 licenseModelId: licenseModelEditor.data.id,
157                 version: licenseModelEditor.data.version,
158                 licensingDataList,
159                 orphanDataList,
160                 selectedTab
161         };
162 };
163
164 const mapActionsToProps = (dispatch, {licenseModelId}) => {
165         return {
166                 onCallVCAction: action => {
167                         LicenseModelActionHelper.performVCAction(dispatch, {licenseModelId, action});
168                 },
169                 onTabSelect: (buttonTab) => licenseModelOverviewActionHelper.selectVLMListView(dispatch,{buttonTab})
170         };
171 };
172
173 export default connect(mapStateToProps, mapActionsToProps)(LicenseModelOverviewView);