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.Iterator;
45 import java.util.LinkedHashMap;
46 import java.util.List;
49 import java.util.SortedSet;
50 import java.util.TreeSet;
52 import org.json.JSONArray;
53 import org.json.JSONObject;
54 import org.onap.portalapp.portal.domain.CentralizedApp;
55 import org.onap.portalapp.portal.domain.DisplayText;
56 import org.onap.portalapp.portal.domain.EPUser;
57 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
58 import org.onap.portalapp.portal.utils.EPCommonSystemProperties;
59 import org.onap.portalsdk.core.domain.MenuData;
60 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
61 import org.onap.portalsdk.core.service.DataAccessService;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.context.annotation.EnableAspectJAutoProxy;
64 import org.springframework.stereotype.Service;
65 import org.springframework.transaction.annotation.Transactional;
67 @Service("leftMenuService")
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
73 public class EPLeftMenuServiceImpl implements EPLeftMenuService {
75 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLeftMenuServiceImpl.class);
78 private ExternalAccessRolesService externalAccessRolesService;
80 private DataAccessService dataAccessService;
82 public final String appHome = "applicationsHome";
83 public final String appCatalog = "appCatalog";
84 public final String widCatalog = "widgetCatalog";
85 public final String users = "users";
91 * @see org.onap.portalapp.portal.service.EPLeftMenuService#getLeftMenuItems
95 public String getLeftMenuItems(EPUser user, Set<MenuData> fullMenuSet, Set<String> roleFunctionSet) {
96 final Map<String, JSONObject> defaultNavMap = new LinkedHashMap<String, JSONObject>();
97 resetNavMap(defaultNavMap);
98 loadDefaultNavMap(defaultNavMap);
99 loadNavMapByUserAdminRole(defaultNavMap, user);
100 loadNavMapByRole(defaultNavMap, fullMenuSet, user);
101 return convertToSideBarModel(defaultNavMap);
107 * @param defaultNavMap
109 private void resetNavMap(Map<String, JSONObject> defaultNavMap) {
110 defaultNavMap.clear();
115 * @param defaultNavMap
118 private void loadNavMapByRole(Map<String, JSONObject> defaultNavMap, Set<MenuData> fullMenuSet, EPUser user) {
120 class SortOrderComparator implements Comparator<MenuData> {
122 public int compare(MenuData e1, MenuData e2) {
123 return e1.getSortOrder().compareTo(e2.getSortOrder());
127 // mulitilanguage impl
128 String loginId = user.getLoginId();
129 HashMap loginParams = new HashMap();
130 loginParams.put("login_id", loginId);
131 List<EPUser> epUsers = dataAccessService.executeNamedQuery("getEPUserByLoginId", loginParams, new HashMap());
132 Integer languageId = 1;
133 for (EPUser epUser : epUsers) {
134 languageId = epUser.getLanguageId();
136 Iterator<MenuData> iterator = fullMenuSet.iterator();
137 HashMap params = new HashMap();
138 params.put("language_id", languageId);
139 List<DisplayText> displayTexts = dataAccessService.executeNamedQuery("displayText", params, new HashMap());
140 while (iterator.hasNext()) {
141 MenuData menuData = iterator.next();
142 for (int index = 0; index < displayTexts.size(); index++) {
143 DisplayText displayText = displayTexts.get(index);
144 if (menuData.getId().equals(displayText.getTextId())) {
145 menuData.setLabel(displayText.getLabel());
151 SortedSet<MenuData> sortMenuSet = new TreeSet<MenuData>(new SortOrderComparator());
152 for (MenuData mn : fullMenuSet) {
156 // Remove Roles from left menu if user doesnt have admin access on
157 // centralized application
158 List<CentralizedApp> applicationsList = null;
159 applicationsList = externalAccessRolesService.getCentralizedAppsOfUser(user.getOrgUserId());
160 if (applicationsList.size() == 0)
161 sortMenuSet.removeIf(x -> x.getLabel().contains("Roles"));
163 for (MenuData mn : sortMenuSet) {
164 JSONObject navItemsDetails = new JSONObject();
165 navItemsDetails.put("name", mn.getLabel());
166 navItemsDetails.put("state", mn.getAction());
167 navItemsDetails.put("imageSrc", mn.getImageSrc());
168 defaultNavMap.put(mn.getAction(), navItemsDetails);
174 * @param defaultNavMap
177 private String convertToSideBarModel(Map<String, JSONObject> defaultNavMap) {
178 JSONObject sidebarModel = new JSONObject();
179 JSONArray navItems = new JSONArray();
180 Collection<JSONObject> jsonObjs = defaultNavMap.values();
182 for (JSONObject navItemsDetail : jsonObjs)
183 navItems.put(navItemsDetail);
185 sidebarModel.put("label", "Portal");
186 sidebarModel.put("navItems", navItems);
187 return sidebarModel.toString();
191 * Loads default entries for regular user.
193 * @param defaultNavMap
195 private void loadDefaultNavMap(Map<String, JSONObject> defaultNavMap) {
196 String leftMenuRootValue = getLeftMenuPrefixValue();
198 JSONObject navItemsDetails1 = new JSONObject();
199 navItemsDetails1.put("name", "Home");
200 navItemsDetails1.put("state",leftMenuRootValue+appHome);
201 navItemsDetails1.put("imageSrc", "home");
202 defaultNavMap.put(appHome, navItemsDetails1);
204 JSONObject navItemsDetails2 = new JSONObject();
205 navItemsDetails2.put("name", "Application Catalog");
206 navItemsDetails2.put("state",leftMenuRootValue+appCatalog);
207 navItemsDetails2.put("imageSrc", "apps");
208 defaultNavMap.put(appCatalog, navItemsDetails2);
210 JSONObject navItemsDetails3 = new JSONObject();
211 navItemsDetails3.put("name", "Widget Catalog");
212 navItemsDetails3.put("state",leftMenuRootValue+widCatalog);
213 navItemsDetails3.put("imageSrc", "apps");
214 defaultNavMap.put(widCatalog, navItemsDetails3);
219 @SuppressWarnings("unchecked")
220 private void loadNavMapByUserAdminRole(Map<String, JSONObject> defaultNavMap, EPUser user) {
221 String leftMenuRootValue = getLeftMenuPrefixValue();
222 List<String> applicationsList = new ArrayList<>();
223 final Map<String, Long> appParams = new HashMap<>();
224 appParams.put("userId", user.getId());
225 applicationsList = dataAccessService.executeNamedQuery("getAprroverRoleFunctionsOfUser", appParams, null);
226 if (applicationsList.size() > 0) {
227 JSONObject navItemsDetails = new JSONObject();
228 navItemsDetails.put("name", "Users");
229 navItemsDetails.put("state",leftMenuRootValue+users);
230 navItemsDetails.put("imageSrc", "person");
231 defaultNavMap.put(users, navItemsDetails);
235 protected String getLeftMenuPrefixValue() {
236 String leftMenuRootValue = getLeftMenuValue();
237 if (leftMenuRootValue != null) {
238 leftMenuRootValue += ".";
240 leftMenuRootValue = "";
243 return leftMenuRootValue;
247 String getLeftMenuValue() {
248 return EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.PORTAL_LEFT_MENU) ? EPCommonSystemProperties.getProperty(EPCommonSystemProperties.PORTAL_LEFT_MENU):null;