CADI AAF Integration and merging the code
[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.LinkedHashMap;
45 import java.util.List;
46 import java.util.Map;
47 import java.util.Set;
48 import java.util.SortedSet;
49 import java.util.TreeSet;
50
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;
63
64 @Service("leftMenuService")
65 @Transactional
66 @org.springframework.context.annotation.Configuration
67 @EnableAspectJAutoProxy
68 @EPMetricsLog
69
70 public class EPLeftMenuServiceImpl implements EPLeftMenuService {
71
72         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLeftMenuServiceImpl.class);
73
74         
75         @Autowired
76         private ExternalAccessRolesService externalAccessRolesService;
77         @Autowired
78         private DataAccessService dataAccessService;
79         
80         /*
81          * (non-Javadoc)
82          * 
83          * @see
84          * org.onap.portalapp.portal.service.EPLeftMenuService#getLeftMenuItems
85          * (java.util.Set)
86          */
87         @Override
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);
95         }
96
97         /**
98          * Clears the map
99          * 
100          * @param defaultNavMap
101          */
102         private void resetNavMap(Map<String, JSONObject> defaultNavMap) {
103                 defaultNavMap.clear();
104         }
105
106         /**
107          * 
108          * @param defaultNavMap
109          * @param fullMenuSet
110          */
111         private void loadNavMapByRole(Map<String, JSONObject> defaultNavMap, Set<MenuData> fullMenuSet , EPUser user) {
112
113                 class SortOrderComparator implements Comparator<MenuData> {
114                         @Override
115                         public int compare(MenuData e1, MenuData e2) {
116                                 return e1.getSortOrder().compareTo(e2.getSortOrder());
117                         }
118                 }
119
120                 SortedSet<MenuData> sortMenuSet = new TreeSet<MenuData>(new SortOrderComparator());
121                 for (MenuData mn : fullMenuSet) {
122                         sortMenuSet.add(mn);
123                 }
124                 
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"));
131
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);
138                 }
139         }
140
141         /**
142          * 
143          * @param defaultNavMap
144          * @return
145          */
146         private String convertToSideBarModel(Map<String, JSONObject> defaultNavMap) {
147                 JSONObject sidebarModel = new JSONObject();
148                 JSONArray navItems = new JSONArray();
149                 Collection<JSONObject> jsonObjs = defaultNavMap.values();
150
151                 for (JSONObject navItemsDetail : jsonObjs)
152                         navItems.put(navItemsDetail);
153
154                 sidebarModel.put("label", "ECOMP portal");
155                 sidebarModel.put("navItems", navItems);
156                 return sidebarModel.toString();
157         }
158
159         /**
160          * Loads default entries for regular user.
161          * 
162          * @param defaultNavMap
163          */
164         private void loadDefaultNavMap(Map<String, JSONObject> defaultNavMap) {
165
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);
171
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);
177
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);
183
184         }
185         
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);
198         }
199         }
200
201 }