2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.service;
40 import java.util.ArrayList;
41 import java.util.Collection;
42 import java.util.Comparator;
43 import java.util.HashMap;
44 import java.util.LinkedHashMap;
45 import java.util.List;
48 import java.util.SortedSet;
49 import java.util.TreeSet;
51 import org.json.JSONArray;
52 import org.json.JSONObject;
53 import org.onap.portalapp.portal.domain.CentralizedApp;
54 import org.onap.portalapp.portal.domain.EPUser;
55 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
56 import org.onap.portalsdk.core.domain.MenuData;
57 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
58 import org.onap.portalsdk.core.service.DataAccessService;
59 import org.springframework.beans.factory.annotation.Autowired;
60 import org.springframework.context.annotation.EnableAspectJAutoProxy;
61 import org.springframework.stereotype.Service;
62 import org.springframework.transaction.annotation.Transactional;
64 @Service("leftMenuService")
66 @org.springframework.context.annotation.Configuration
67 @EnableAspectJAutoProxy
70 public class EPLeftMenuServiceImpl implements EPLeftMenuService {
72 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLeftMenuServiceImpl.class);
76 private ExternalAccessRolesService externalAccessRolesService;
78 private DataAccessService dataAccessService;
84 * org.onap.portalapp.portal.service.EPLeftMenuService#getLeftMenuItems
88 public String getLeftMenuItems(EPUser user, Set<MenuData> fullMenuSet, Set<String> roleFunctionSet) {
89 final Map<String, JSONObject> defaultNavMap = new LinkedHashMap<String, JSONObject>();
90 resetNavMap(defaultNavMap);
91 loadDefaultNavMap(defaultNavMap);
92 loadNavMapByUserAdminRole(defaultNavMap,user);
93 loadNavMapByRole(defaultNavMap, fullMenuSet , user);
94 return convertToSideBarModel(defaultNavMap);
100 * @param defaultNavMap
102 private void resetNavMap(Map<String, JSONObject> defaultNavMap) {
103 defaultNavMap.clear();
108 * @param defaultNavMap
111 private void loadNavMapByRole(Map<String, JSONObject> defaultNavMap, Set<MenuData> fullMenuSet , EPUser user) {
113 class SortOrderComparator implements Comparator<MenuData> {
115 public int compare(MenuData e1, MenuData e2) {
116 return e1.getSortOrder().compareTo(e2.getSortOrder());
120 SortedSet<MenuData> sortMenuSet = new TreeSet<MenuData>(new SortOrderComparator());
121 for (MenuData mn : fullMenuSet) {
125 // Remove Roles from left menu if user doesnt have admin access on
126 // centralized application
127 List<CentralizedApp> applicationsList = null;
128 applicationsList = externalAccessRolesService.getCentralizedAppsOfUser(user.getOrgUserId());
129 if (applicationsList.size() == 0)
130 sortMenuSet.removeIf(x -> x.getLabel().contains("Roles"));
132 for (MenuData mn : sortMenuSet) {
133 JSONObject navItemsDetails = new JSONObject();
134 navItemsDetails.put("name", mn.getLabel());
135 navItemsDetails.put("state", mn.getAction());
136 navItemsDetails.put("imageSrc", mn.getImageSrc());
137 defaultNavMap.put(mn.getAction(), navItemsDetails);
143 * @param defaultNavMap
146 private String convertToSideBarModel(Map<String, JSONObject> defaultNavMap) {
147 JSONObject sidebarModel = new JSONObject();
148 JSONArray navItems = new JSONArray();
149 Collection<JSONObject> jsonObjs = defaultNavMap.values();
151 for (JSONObject navItemsDetail : jsonObjs)
152 navItems.put(navItemsDetail);
154 sidebarModel.put("label", "ECOMP portal");
155 sidebarModel.put("navItems", navItems);
156 return sidebarModel.toString();
160 * Loads default entries for regular user.
162 * @param defaultNavMap
164 private void loadDefaultNavMap(Map<String, JSONObject> defaultNavMap) {
166 JSONObject navItemsDetails1 = new JSONObject();
167 navItemsDetails1.put("name", "Home");
168 navItemsDetails1.put("state", "root.applicationsHome");
169 navItemsDetails1.put("imageSrc", "icon-building-home");
170 defaultNavMap.put("root.applicationsHome", navItemsDetails1);
172 JSONObject navItemsDetails2 = new JSONObject();
173 navItemsDetails2.put("name", "Application Catalog");
174 navItemsDetails2.put("state", "root.appCatalog");
175 navItemsDetails2.put("imageSrc", "icon-apps-marketplace");
176 defaultNavMap.put("root.appCatalog", navItemsDetails2);
178 JSONObject navItemsDetails3 = new JSONObject();
179 navItemsDetails3.put("name", "Widget Catalog");
180 navItemsDetails3.put("state", "root.widgetCatalog");
181 navItemsDetails3.put("imageSrc", "icon-apps-marketplace");
182 defaultNavMap.put("root.widgetCatalog", navItemsDetails3);
186 @SuppressWarnings("unchecked")
187 private void loadNavMapByUserAdminRole(Map<String, JSONObject> defaultNavMap, EPUser user) {
188 List<String> applicationsList = new ArrayList<>();
189 final Map<String, Long> appParams = new HashMap<>();
190 appParams.put("userId", user.getId());
191 applicationsList = dataAccessService.executeNamedQuery("getAprroverRoleFunctionsOfUser", appParams, null);
192 if (applicationsList.size() > 0) {
193 JSONObject navItemsDetails = new JSONObject();
194 navItemsDetails.put("name", "Users");
195 navItemsDetails.put("state", "root.users");
196 navItemsDetails.put("imageSrc", "icon-user");
197 defaultNavMap.put("root.users", navItemsDetails);