Update license; improve coverage; add docs dir
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / service / EPLeftMenuServiceImpl.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright © 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  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.openecomp.portalapp.portal.service;
39
40 import java.util.Collection;
41 import java.util.Comparator;
42 import java.util.LinkedHashMap;
43 import java.util.Map;
44 import java.util.Set;
45 import java.util.SortedSet;
46 import java.util.TreeSet;
47
48 import org.json.JSONArray;
49 import org.json.JSONObject;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.context.annotation.EnableAspectJAutoProxy;
52 import org.springframework.stereotype.Service;
53 import org.springframework.transaction.annotation.Transactional;
54
55 import org.openecomp.portalsdk.core.domain.MenuData;
56 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
57 import org.openecomp.portalapp.portal.domain.EPUser;
58 import org.openecomp.portalapp.portal.domain.UserRoles;
59 //import org.openecomp.portalapp.portal.domain.Menu;
60 import org.openecomp.portalapp.portal.logging.aop.EPMetricsLog;
61
62 @Service("leftMenuService")
63 @Transactional
64 @org.springframework.context.annotation.Configuration
65 @EnableAspectJAutoProxy
66 @EPMetricsLog
67
68 public class EPLeftMenuServiceImpl implements EPLeftMenuService {
69
70         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPLeftMenuServiceImpl.class);
71
72         @Autowired
73         private EPAppService appService;
74
75         /*
76          * (non-Javadoc)
77          * 
78          * @see
79          * org.openecomp.portalapp.portal.service.EPLeftMenuService#getLeftMenuItems
80          * (java.util.Set)
81          */
82         @Override
83         public String getLeftMenuItems(EPUser user, Set<MenuData> fullMenuSet, Set<String> roleFunctionSet) {
84                 final Map<String, JSONObject> defaultNavMap = new LinkedHashMap<String, JSONObject>();
85
86                 resetNavMap(defaultNavMap);
87
88                 loadDefaultNavMap(defaultNavMap);
89
90                 // Handle Account Administrator in a special way; soon this will
91                 // be revised as Account Administrator may become obsolete
92                 try {
93                         if (user != null) {
94                                 UserRoles uRoles = appService.getUserProfileNormalizedForLeftMenu(user);
95                                 if (uRoles.getRoles().contains("Account Administrator"))
96                                         loadAccAdminNavMap(defaultNavMap);
97                         }
98                 } catch (Exception e) {
99                         logger.error(EELFLoggerDelegate.errorLogger,
100                                         "getLeftMenuItems: failed to get roles for user " + user.getOrgUserId(), e);
101                 }
102
103                 loadNavMapByRole(defaultNavMap, fullMenuSet);
104
105                 return convertToSideBarModel(defaultNavMap);
106         }
107
108         /**
109          * Clears the map
110          * 
111          * @param defaultNavMap
112          */
113         private void resetNavMap(Map<String, JSONObject> defaultNavMap) {
114                 defaultNavMap.clear();
115         }
116
117         /**
118          * 
119          * @param defaultNavMap
120          * @param fullMenuSet
121          */
122         private void loadNavMapByRole(Map<String, JSONObject> defaultNavMap, Set<MenuData> fullMenuSet) {
123
124                 class SortOrderComparator implements Comparator<MenuData> {
125                         @Override
126                         public int compare(MenuData e1, MenuData e2) {
127                                 return e1.getSortOrder().compareTo(e2.getSortOrder());
128                         }
129                 }
130
131                 SortedSet<MenuData> sortMenuSet = new TreeSet<MenuData>(new SortOrderComparator());
132                 for (MenuData mn : fullMenuSet) {
133                         sortMenuSet.add(mn);
134                 }
135
136                 for (MenuData mn : sortMenuSet) {
137                         JSONObject navItemsDetails = new JSONObject();
138                         navItemsDetails.put("name", mn.getLabel());
139                         navItemsDetails.put("state", mn.getAction());
140                         navItemsDetails.put("imageSrc", mn.getImageSrc());
141                         defaultNavMap.put(mn.getAction(), navItemsDetails);
142                 }
143         }
144
145         /**
146          * 
147          * @param defaultNavMap
148          * @return
149          */
150         private String convertToSideBarModel(Map<String, JSONObject> defaultNavMap) {
151                 JSONObject sidebarModel = new JSONObject();
152                 JSONArray navItems = new JSONArray();
153                 Collection<JSONObject> jsonObjs = defaultNavMap.values();
154
155                 for (JSONObject navItemsDetail : jsonObjs)
156                         navItems.put(navItemsDetail);
157
158                 sidebarModel.put("label", "ECOMP portal");
159                 sidebarModel.put("navItems", navItems);
160                 return sidebarModel.toString();
161         }
162
163         /**
164          * Loads default entries for regular user.
165          * 
166          * @param defaultNavMap
167          */
168         private void loadDefaultNavMap(Map<String, JSONObject> defaultNavMap) {
169
170                 JSONObject navItemsDetails1 = new JSONObject();
171                 navItemsDetails1.put("name", "Home");
172                 navItemsDetails1.put("state", "root.applicationsHome");
173                 navItemsDetails1.put("imageSrc", "icon-building-home");
174                 defaultNavMap.put("root.applicationsHome", navItemsDetails1);
175
176                 JSONObject navItemsDetails2 = new JSONObject();
177                 navItemsDetails2.put("name", "Application Catalog");
178                 navItemsDetails2.put("state", "root.appCatalog");
179                 navItemsDetails2.put("imageSrc", "icon-apps-marketplace");
180                 defaultNavMap.put("root.appCatalog", navItemsDetails2);
181
182                 JSONObject navItemsDetails3 = new JSONObject();
183                 navItemsDetails3.put("name", "Widget Catalog");
184                 navItemsDetails3.put("state", "root.widgetCatalog");
185                 navItemsDetails3.put("imageSrc", "icon-apps-marketplace");
186                 defaultNavMap.put("root.widgetCatalog", navItemsDetails3);
187
188         }
189
190         /**
191          * Loads default entries for application administrator.
192          * 
193          * @param defaultNavMap
194          */
195         private void loadAccAdminNavMap(Map<String, JSONObject> defaultNavMap) {
196
197                 JSONObject navItemsDetails1 = new JSONObject();
198                 navItemsDetails1.put("name", "Users");
199                 navItemsDetails1.put("state", "root.users");
200                 navItemsDetails1.put("imageSrc", "icon-user");
201                 defaultNavMap.put("root.users", navItemsDetails1);
202
203                 // No more widget onboarding like this:
204                 //
205                 // JSONObject navItemsDetails2 = new JSONObject();
206                 // navItemsDetails2.put("name", "Widget Onboarding");
207                 // navItemsDetails2.put("state", "root.widgetOnboarding");
208                 // navItemsDetails2.put("imageSrc", "icon-add-widget");
209                 // defaultNavMap.put("root.widgetOnboarding", navItemsDetails2);
210
211         }
212
213 }