fix for internationalization change for left menu
[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.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;
47 import java.util.Map;
48 import java.util.Set;
49 import java.util.SortedSet;
50 import java.util.TreeSet;
51
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;
66
67 @Service("leftMenuService")
68 @Transactional
69 @org.springframework.context.annotation.Configuration
70 @EnableAspectJAutoProxy
71 @EPMetricsLog
72
73 public class EPLeftMenuServiceImpl implements EPLeftMenuService {
74
75         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLeftMenuServiceImpl.class);
76
77         @Autowired
78         private ExternalAccessRolesService externalAccessRolesService;
79         @Autowired
80         private DataAccessService dataAccessService;
81         
82         public final String appHome = "applicationsHome";
83         public final String appCatalog = "appCatalog";
84         public final String widCatalog = "widgetCatalog";
85         public final String users = "users";
86         
87
88         /*
89          * (non-Javadoc)
90          * 
91          * @see org.onap.portalapp.portal.service.EPLeftMenuService#getLeftMenuItems
92          * (java.util.Set)
93          */
94         @Override
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);
102         }
103
104         /**
105          * Clears the map
106          * 
107          * @param defaultNavMap
108          */
109         private void resetNavMap(Map<String, JSONObject> defaultNavMap) {
110                 defaultNavMap.clear();
111         }
112
113         /**
114          * 
115          * @param defaultNavMap
116          * @param fullMenuSet
117          */
118         private void loadNavMapByRole(Map<String, JSONObject> defaultNavMap, Set<MenuData> fullMenuSet, EPUser user) {
119
120                 class SortOrderComparator implements Comparator<MenuData> {
121                         @Override
122                         public int compare(MenuData e1, MenuData e2) {
123                                 return e1.getSortOrder().compareTo(e2.getSortOrder());
124                         }
125                 }
126
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();
135                 }
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());
146                                         break;
147                                 }
148                         }
149                 }
150
151                 SortedSet<MenuData> sortMenuSet = new TreeSet<MenuData>(new SortOrderComparator());
152                 for (MenuData mn : fullMenuSet) {
153                         sortMenuSet.add(mn);
154                 }
155
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"));
162
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);
169                 }
170         }
171
172         /**
173          * 
174          * @param defaultNavMap
175          * @return
176          */
177         private String convertToSideBarModel(Map<String, JSONObject> defaultNavMap) {
178                 JSONObject sidebarModel = new JSONObject();
179                 JSONArray navItems = new JSONArray();
180                 Collection<JSONObject> jsonObjs = defaultNavMap.values();
181
182                 for (JSONObject navItemsDetail : jsonObjs)
183                         navItems.put(navItemsDetail);
184
185                 sidebarModel.put("label", "Portal");
186                 sidebarModel.put("navItems", navItems);
187                 return sidebarModel.toString();
188         }
189
190         /**
191          * Loads default entries for regular user.
192          * 
193          * @param defaultNavMap
194          */
195         private void loadDefaultNavMap(Map<String, JSONObject> defaultNavMap) {
196                 String leftMenuRootValue = getLeftMenuPrefixValue();
197                 
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);
203
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);
209
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);
215
216         }
217
218
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);
232                 }
233         }
234
235         protected String getLeftMenuPrefixValue() {
236                 String leftMenuRootValue = getLeftMenuValue();
237                 if (leftMenuRootValue != null) {
238                         leftMenuRootValue += ".";
239                 } else {
240                         leftMenuRootValue = "";
241                 }
242                 
243                 return leftMenuRootValue;
244         
245         }
246
247         String getLeftMenuValue() {
248                 return EPCommonSystemProperties.containsProperty(EPCommonSystemProperties.PORTAL_LEFT_MENU) ? EPCommonSystemProperties.getProperty(EPCommonSystemProperties.PORTAL_LEFT_MENU):null;
249         }
250
251 }