c7820115deb45d4293f4eeb58adc6e9639483f15
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * eCOMP Portal SDK
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.portalsdk.core.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.portalsdk.core.domain.MenuData;
32 import org.openecomp.portalsdk.core.domain.User;
33 import org.openecomp.portalsdk.core.interfaces.SecurityInterface;
34 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
35 import org.openecomp.portalsdk.core.menu.MenuBuilder;
36 import org.openecomp.portalsdk.core.service.AppService;
37 import org.openecomp.portalsdk.core.service.DataAccessService;
38 import org.openecomp.portalsdk.core.service.FnMenuService;
39 import org.openecomp.portalsdk.core.util.SystemProperties;
40 import org.openecomp.portalsdk.core.web.support.UserUtils;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.stereotype.Controller;
43 import org.springframework.web.bind.annotation.ModelAttribute;
44
45 import com.fasterxml.jackson.databind.ObjectMapper;
46
47 @Controller
48 public abstract class FusionBaseController implements SecurityInterface{
49         
50         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(FusionBaseController.class);
51         
52         @Override
53         public boolean isAccessible() {
54                 return true;
55         }
56         
57         public boolean isRESTfulCall(){
58                 return true;
59         }
60         @Autowired
61         private FnMenuService fnMenuService;
62         
63         @Autowired
64         private MenuBuilder  menuBuilder;
65            
66         @Autowired
67         private DataAccessService  dataAccessService;
68         
69         @Autowired
70         AppService appService;
71         
72         @SuppressWarnings({ "unchecked", "rawtypes" })
73         @ModelAttribute("menu")
74         public Map<String, Object> getMenu(HttpServletRequest request) {
75                 HttpSession session = null;
76                 Map<String, Object> model = new HashMap<String, Object>();       
77                 try {
78                         try {
79                                 String appName  = appService.getDefaultAppName();
80                                 if (appName==null || appName=="") {
81                                         appName         = SystemProperties.SDK_NAME;
82                                 }
83                         logger.setRequestBasedDefaultsIntoGlobalLoggingContext(request, appName);
84             } catch (Exception e) {
85             }
86                         
87                         session = request.getSession();
88                         User user = UserUtils.getUserSession(request);
89                         if(session!=null && user!=null){
90                                 Set<MenuData> menuResult = (Set<MenuData>) session.getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
91                                 if(menuResult==null){
92                                          Set appMenu = getMenuBuilder().getMenu(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_SET_NAME),dataAccessService);
93                                          session.setAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME),    MenuBuilder.filterMenu(appMenu, request));
94                                          menuResult = (Set<MenuData>) session.getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
95                                 }
96                                 model = setMenu(menuResult);                            
97                         }
98                 } catch (Exception e) {
99                         logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
100                 }
101                 return model;
102         }
103         
104         public Map<String, Object> setMenu(Set<MenuData> menuResult) throws Exception{
105                 ObjectMapper mapper = new ObjectMapper();
106                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();;
107                 List<MenuData> parentList = new ArrayList<MenuData>();;
108                 Map<String, Object> model = new HashMap<String, Object>();
109                 try{
110                         fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);              
111                 }catch(Exception e){
112                         logger.error(EELFLoggerDelegate.errorLogger, e.getMessage());
113                 }               
114                 model.put("childItemList",childItemList!=null?mapper.writeValueAsString(childItemList):"");
115                 model.put("parentList",parentList!=null?mapper.writeValueAsString(parentList):"");
116                 return model;
117         }
118         
119         public MenuBuilder getMenuBuilder() {
120                 return menuBuilder;
121         }
122
123         public void setMenuBuilder(MenuBuilder menuBuilder) {
124                 this.menuBuilder = menuBuilder;
125         }
126
127         public DataAccessService getDataAccessService() {
128                 return dataAccessService;
129         }
130
131         public void setDataAccessService(DataAccessService dataAccessService) {
132                 this.dataAccessService = dataAccessService;
133         }
134         
135 }