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