f661163d556134791acfd8987cc12dd2e0f9f048
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / portal / service / EPLeftMenuServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * 
37  */
38 package org.onap.portalapp.portal.service;
39
40 import java.util.Collection;
41 import java.util.Comparator;
42 import java.util.LinkedHashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.Set;
46 import java.util.SortedSet;
47 import java.util.TreeSet;
48
49 import org.json.JSONArray;
50 import org.json.JSONObject;
51 import org.onap.portalapp.portal.domain.CentralizedApp;
52 import org.onap.portalapp.portal.domain.EPUser;
53 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
54 import org.onap.portalsdk.core.domain.MenuData;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.springframework.beans.factory.annotation.Autowired;
57 import org.springframework.context.annotation.EnableAspectJAutoProxy;
58 import org.springframework.stereotype.Service;
59 import org.springframework.transaction.annotation.Transactional;
60
61 @Service("leftMenuService")
62 @Transactional
63 @org.springframework.context.annotation.Configuration
64 @EnableAspectJAutoProxy
65 @EPMetricsLog
66
67 public class EPLeftMenuServiceImpl implements EPLeftMenuService {
68
69         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLeftMenuServiceImpl.class);
70
71         
72         @Autowired
73         private ExternalAccessRolesService externalAccessRolesService;
74         /*
75          * (non-Javadoc)
76          * 
77          * @see
78          * org.onap.portalapp.portal.service.EPLeftMenuService#getLeftMenuItems
79          * (java.util.Set)
80          */
81         @Override
82         public String getLeftMenuItems(EPUser user, Set<MenuData> fullMenuSet, Set<String> roleFunctionSet) {
83                 final Map<String, JSONObject> defaultNavMap = new LinkedHashMap<String, JSONObject>();
84                 resetNavMap(defaultNavMap);
85                 loadDefaultNavMap(defaultNavMap);
86                 loadNavMapByRole(defaultNavMap, fullMenuSet , user);
87                 return convertToSideBarModel(defaultNavMap);
88         }
89
90         /**
91          * Clears the map
92          * 
93          * @param defaultNavMap
94          */
95         private void resetNavMap(Map<String, JSONObject> defaultNavMap) {
96                 defaultNavMap.clear();
97         }
98
99         /**
100          * 
101          * @param defaultNavMap
102          * @param fullMenuSet
103          */
104         private void loadNavMapByRole(Map<String, JSONObject> defaultNavMap, Set<MenuData> fullMenuSet , EPUser user) {
105
106                 class SortOrderComparator implements Comparator<MenuData> {
107                         @Override
108                         public int compare(MenuData e1, MenuData e2) {
109                                 return e1.getSortOrder().compareTo(e2.getSortOrder());
110                         }
111                 }
112
113                 SortedSet<MenuData> sortMenuSet = new TreeSet<MenuData>(new SortOrderComparator());
114                 for (MenuData mn : fullMenuSet) {
115                         sortMenuSet.add(mn);
116                 }
117                 
118                 // Remove Roles from left menu if user doesnt have admin access on
119                 // centralized application
120                 List<CentralizedApp> applicationsList = null;
121                 applicationsList = externalAccessRolesService.getCentralizedAppsOfUser(user.getOrgUserId());
122                 if (applicationsList.size() == 0)
123                         sortMenuSet.removeIf(x -> x.getLabel().contains("Roles"));
124
125                 for (MenuData mn : sortMenuSet) {
126                         JSONObject navItemsDetails = new JSONObject();
127                         navItemsDetails.put("name", mn.getLabel());
128                         navItemsDetails.put("state", mn.getAction());
129                         navItemsDetails.put("imageSrc", mn.getImageSrc());
130                         defaultNavMap.put(mn.getAction(), navItemsDetails);
131                 }
132         }
133
134         /**
135          * 
136          * @param defaultNavMap
137          * @return
138          */
139         private String convertToSideBarModel(Map<String, JSONObject> defaultNavMap) {
140                 JSONObject sidebarModel = new JSONObject();
141                 JSONArray navItems = new JSONArray();
142                 Collection<JSONObject> jsonObjs = defaultNavMap.values();
143
144                 for (JSONObject navItemsDetail : jsonObjs)
145                         navItems.put(navItemsDetail);
146
147                 sidebarModel.put("label", "ECOMP portal");
148                 sidebarModel.put("navItems", navItems);
149                 return sidebarModel.toString();
150         }
151
152         /**
153          * Loads default entries for regular user.
154          * 
155          * @param defaultNavMap
156          */
157         private void loadDefaultNavMap(Map<String, JSONObject> defaultNavMap) {
158
159                 JSONObject navItemsDetails1 = new JSONObject();
160                 navItemsDetails1.put("name", "Home");
161                 navItemsDetails1.put("state", "root.applicationsHome");
162                 navItemsDetails1.put("imageSrc", "icon-building-home");
163                 defaultNavMap.put("root.applicationsHome", navItemsDetails1);
164
165                 JSONObject navItemsDetails2 = new JSONObject();
166                 navItemsDetails2.put("name", "Application Catalog");
167                 navItemsDetails2.put("state", "root.appCatalog");
168                 navItemsDetails2.put("imageSrc", "icon-apps-marketplace");
169                 defaultNavMap.put("root.appCatalog", navItemsDetails2);
170
171                 JSONObject navItemsDetails3 = new JSONObject();
172                 navItemsDetails3.put("name", "Widget Catalog");
173                 navItemsDetails3.put("state", "root.widgetCatalog");
174                 navItemsDetails3.put("imageSrc", "icon-apps-marketplace");
175                 defaultNavMap.put("root.widgetCatalog", navItemsDetails3);
176
177         }
178
179 }