a425812f8a30ab3c262be0b71497bbe87b5efde6
[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  *
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.onap.portalapp.controller;
39
40 import java.util.ArrayList;
41 import java.util.HashMap;
42 import java.util.List;
43 import java.util.Map;
44 import java.util.Set;
45
46 import javax.servlet.http.HttpServletRequest;
47 import javax.servlet.http.HttpSession;
48
49 import org.onap.portalapp.portal.domain.EPUser;
50 import org.onap.portalapp.util.EPUserUtils;
51 import org.onap.portalsdk.core.controller.FusionBaseController;
52 import org.onap.portalsdk.core.domain.MenuData;
53 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
54 import org.onap.portalsdk.core.util.SystemProperties;
55 import org.springframework.stereotype.Controller;
56 import org.springframework.web.bind.annotation.ModelAttribute;
57
58 import com.fasterxml.jackson.databind.ObjectMapper;
59
60 @Controller
61 public abstract class EPFusionBaseController extends FusionBaseController {
62         
63         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPFusionBaseController.class);
64                 
65         @Override
66         public boolean isAccessible() {
67                 return true;
68         }
69
70         public boolean isRESTfulCall() {
71                 return true;
72         }
73
74         @ModelAttribute("menu")
75         public Map<String, Object> messages(HttpServletRequest request) {
76                 HttpSession session = null;
77                 Map<String, Object> model = new HashMap<String, Object>();
78                 session = request.getSession();
79                 EPUser user = EPUserUtils.getUserSession(request);
80                 if (session != null && user != null) {
81                         @SuppressWarnings("unchecked")
82                         Set<MenuData> menuResult = (Set<MenuData>) session
83                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
84                         try {
85                                 model = setMenu(menuResult);
86                         } catch (Exception e) {
87                                 logger.error(EELFLoggerDelegate.errorLogger, "messages failed", e);
88                         }
89                 }
90                 
91                 return model;
92         }
93
94         public Map<String, Object> setMenu(Set<MenuData> menuResult) throws Exception {
95                 ObjectMapper mapper = new ObjectMapper();
96                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
97                 
98                 List<MenuData> parentList = new ArrayList<MenuData>();
99                 
100                 Map<String, Object> model = new HashMap<String, Object>();
101                 for (MenuData menu : menuResult) {
102                         MenuData parentData = new MenuData();
103                         parentData.setLabel(menu.getLabel());
104                         parentData.setAction(menu.getAction());
105                         parentData.setImageSrc(menu.getImageSrc());
106                         parentList.add(parentData);
107                         List<MenuData> tempList = new ArrayList<MenuData>();
108                         for (Object o : menu.getChildMenus()) {
109                                 MenuData m = (MenuData) o;
110                                 MenuData data = new MenuData();
111                                 data.setLabel(m.getLabel());
112                                 data.setAction(m.getAction());
113                                 data.setImageSrc(m.getImageSrc());
114                                 tempList.add(data);
115                         }
116                         childItemList.add(tempList);
117                 }
118                 model.put("childItemList",  mapper.writeValueAsString(childItemList));
119                 model.put("parentList", mapper.writeValueAsString(parentList));
120                 return model;
121         }
122 }