Merge "Fixed sonar issue of EPFusionBaseController.java"
[portal.git] / ecomp-portal-BE-common / src / main / java / org / onap / portalapp / controller / EPFusionBaseController.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  *
7  * Modifications Copyright © 2018 IBM.
8  * ===================================================================
9  *
10  * Unless otherwise specified, all software contained herein is licensed
11  * under the Apache License, Version 2.0 (the "License");
12  * you may not use this software except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *             http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * Unless otherwise specified, all documentation contained herein is licensed
24  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
25  * you may not use this documentation except in compliance with the License.
26  * You may obtain a copy of the License at
27  *
28  *             https://creativecommons.org/licenses/by/4.0/
29  *
30  * Unless required by applicable law or agreed to in writing, documentation
31  * distributed under the License is distributed on an "AS IS" BASIS,
32  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
33  * See the License for the specific language governing permissions and
34  * limitations under the License.
35  *
36  * ============LICENSE_END============================================
37  *
38  * 
39  */
40 package org.onap.portalapp.controller;
41
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Set;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpSession;
50
51 import org.onap.portalapp.portal.domain.EPUser;
52 import org.onap.portalapp.util.EPUserUtils;
53 import org.onap.portalsdk.core.controller.FusionBaseController;
54 import org.onap.portalsdk.core.domain.MenuData;
55 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
56 import org.onap.portalsdk.core.util.SystemProperties;
57 import org.springframework.stereotype.Controller;
58 import org.springframework.web.bind.annotation.ModelAttribute;
59
60 import com.fasterxml.jackson.databind.ObjectMapper;
61
62 @Controller
63 public abstract class EPFusionBaseController extends FusionBaseController {
64         
65         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPFusionBaseController.class);
66                 
67         @Override
68         public boolean isAccessible() {
69                 return true;
70         }
71
72         @Override
73         public boolean isRESTfulCall() {
74                 return true;
75         }
76
77         @ModelAttribute("menu")
78         public Map<String, Object> messages(HttpServletRequest request) {
79                 HttpSession session = null;
80                 Map<String, Object> model = new HashMap<>();
81                 session = request.getSession();
82                 EPUser user = EPUserUtils.getUserSession(request);
83                 if (session != null && user != null) {
84                         @SuppressWarnings("unchecked")
85                         Set<MenuData> menuResult = (Set<MenuData>) session
86                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
87                         try {
88                                 model = setMenu(menuResult);
89                         } catch (Exception e) {
90                                 logger.error(EELFLoggerDelegate.errorLogger, "messages failed", e);
91                         }
92                 }
93                 
94                 return model;
95         }
96
97         @Override
98         public Map<String, Object> setMenu(Set<MenuData> menuResult) throws Exception {
99                 ObjectMapper mapper = new ObjectMapper();
100                 List<List<MenuData>> childItemList = new ArrayList<>();
101                 
102                 List<MenuData> parentList = new ArrayList<>();
103                 
104                 Map<String, Object> model = new HashMap<>();
105                 for (MenuData menu : menuResult) {
106                         MenuData parentData = new MenuData();
107                         parentData.setLabel(menu.getLabel());
108                         parentData.setAction(menu.getAction());
109                         parentData.setImageSrc(menu.getImageSrc());
110                         parentList.add(parentData);
111                         List<MenuData> tempList = new ArrayList<>();
112                         for (Object o : menu.getChildMenus()) {
113                                 MenuData m = (MenuData) o;
114                                 MenuData data = new MenuData();
115                                 data.setLabel(m.getLabel());
116                                 data.setAction(m.getAction());
117                                 data.setImageSrc(m.getImageSrc());
118                                 tempList.add(data);
119                         }
120                         childItemList.add(tempList);
121                 }
122                 model.put("childItemList",  mapper.writeValueAsString(childItemList));
123                 model.put("parentList", mapper.writeValueAsString(parentList));
124                 return model;
125         }
126 }