ff80d413c781b6b4c6d243c4c32d1796f1ed180c
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
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  * 
37  */
38 package org.onap.portalapp.controller.core;
39
40 import java.io.IOException;
41 import java.io.PrintWriter;
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.HttpServletResponse;
50 import javax.servlet.http.HttpSession;
51
52 import org.json.JSONObject;
53 import org.onap.portalsdk.core.auth.LoginStrategy;
54 import org.onap.portalsdk.core.controller.RestrictedBaseController;
55 import org.onap.portalsdk.core.domain.MenuData;
56 import org.onap.portalsdk.core.domain.User;
57 import org.onap.portalsdk.core.domain.UserApp;
58 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
59 import org.onap.portalsdk.core.service.FnMenuService;
60 import org.onap.portalsdk.core.service.UserProfileService;
61 import org.onap.portalsdk.core.service.UserService;
62 import org.onap.portalsdk.core.util.SystemProperties;
63 import org.onap.portalsdk.core.web.support.JsonMessage;
64 import org.onap.portalsdk.core.web.support.UserUtils;
65 import org.springframework.beans.factory.annotation.Autowired;
66 import org.springframework.stereotype.Controller;
67 import org.springframework.web.bind.annotation.RequestMapping;
68 import org.springframework.web.bind.annotation.RequestMethod;
69 import org.springframework.web.servlet.ModelAndView;
70
71 import com.fasterxml.jackson.databind.ObjectMapper;
72
73 @Controller
74 @RequestMapping("/")
75 public class ProfileSearchController extends RestrictedBaseController {
76
77         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
78
79         private static final String APPLICATION_JSON = "application/json";
80         
81         @Autowired
82         private UserProfileService service;
83         
84         @Autowired
85         private UserService userService;
86         
87         @Autowired
88         private FnMenuService fnMenuService;
89         
90         @Autowired
91         private LoginStrategy loginStrategy;
92
93         @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
94         public ModelAndView profileSearch(HttpServletRequest request) {
95                 Map<String, Object> model = new HashMap<>();
96                 ObjectMapper mapper = new ObjectMapper();
97                 List<User> profileList = null;
98                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
99                 try {
100                         profileList = service.findAll();
101                         model.putAll(setDashboardData(request));
102                         model.put("profileList", mapper.writeValueAsString(profileList));
103                 } catch (Exception e) {
104                         logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
105                 }
106                 return new ModelAndView(getViewName(), "model", model);
107         }
108
109
110         @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
111         public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
112                 Map<String, Object> model = new HashMap<>();
113                 ObjectMapper mapper = new ObjectMapper();
114                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
115                 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
116                 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
117                 List<User> profileList = null;
118                 List<User> profileFinalList = new ArrayList<>();
119                 try {
120                         profileList = service.findAll();
121                         for(User user: profileList)
122                         {
123                                 Set<UserApp> userapps = user.getUserApps();
124                                 Set<UserApp> userapplications = UserUtils.getUserApps(userapps);
125                                 user.setUserApps(userapplications);
126                                 profileFinalList.add(user);
127                         }
128                         
129                         model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
130                         profileList = profileList.subList(
131                                         viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
132                                         viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
133                         model.put("profileList", mapper.writeValueAsString(profileFinalList));
134                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
135                         JSONObject j = new JSONObject(msg);
136                         response.setContentType(APPLICATION_JSON);
137                         response.getWriter().write(j.toString());
138                 } catch (Exception e) {
139                         logger.error(EELFLoggerDelegate.applicationLogger, "getUserPagination failed", e);
140                 }
141         }
142
143         @SuppressWarnings("unchecked")
144         private Map<String, Object> setDashboardData(HttpServletRequest request)
145       throws com.fasterxml.jackson.core.JsonProcessingException {
146                 ObjectMapper mapper = new ObjectMapper();
147                 Map<String, Object> model = new HashMap<>();
148                 List<List<MenuData>> childItemList = new ArrayList<>();
149                 List<MenuData> parentList = new ArrayList<>();
150                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
151                 HttpSession session = request.getSession();
152                 try {
153                         Set<MenuData> menuResult = (Set<MenuData>) session
154                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
155                         fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
156                 } catch (Exception e) {
157                         logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
158                 }
159                 model.put("childItemList", mapper.writeValueAsString(childItemList));
160                 model.put("parentList", mapper.writeValueAsString(parentList));
161                 return model;
162         }
163
164         @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
165         public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
166                 try {
167                         logger.info(EELFLoggerDelegate.applicationLogger,
168                                         "Initiating toggleProfileActive in ProfileSearchController");
169                         String userId = request.getParameter("profile_id");
170                         User user = userService.getUser(userId);
171                         user.setActive(!user.getActive());
172                         service.saveUser(user);
173                         logger.info(EELFLoggerDelegate.auditLogger,
174                                         "Change active status for user " + user.getId() + " to " + user.getActive());
175                         ObjectMapper mapper = new ObjectMapper();
176                         response.setContentType(APPLICATION_JSON);
177                         PrintWriter out = response.getWriter();
178                         out.write(mapper.writeValueAsString(user.getActive()));
179                 } catch (Exception e) {
180                         logger.error(EELFLoggerDelegate.applicationLogger, "toggleProfileActive failed", e);
181                 }
182         }
183 }