2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.controller.core;
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;
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import javax.servlet.http.HttpSession;
52 import org.json.JSONObject;
53 import org.onap.portalsdk.core.controller.RestrictedBaseController;
54 import org.onap.portalsdk.core.domain.MenuData;
55 import org.onap.portalsdk.core.domain.User;
56 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
57 import org.onap.portalsdk.core.service.FnMenuService;
58 import org.onap.portalsdk.core.service.UserProfileService;
59 import org.onap.portalsdk.core.service.UserService;
60 import org.onap.portalsdk.core.util.SystemProperties;
61 import org.onap.portalsdk.core.web.support.JsonMessage;
62 import org.springframework.beans.factory.annotation.Autowired;
63 import org.springframework.stereotype.Controller;
64 import org.springframework.web.bind.annotation.RequestMapping;
65 import org.springframework.web.bind.annotation.RequestMethod;
66 import org.springframework.web.servlet.ModelAndView;
68 import com.fasterxml.jackson.databind.ObjectMapper;
72 public class ProfileSearchController extends RestrictedBaseController {
74 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
76 private static final String APPLICATION_JSON = "application/json";
79 private UserProfileService service;
82 private UserService userService;
85 private FnMenuService fnMenuService;
87 @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
88 public ModelAndView profileSearch(HttpServletRequest request) {
89 Map<String, Object> model = new HashMap<>();
90 ObjectMapper mapper = new ObjectMapper();
91 List<User> profileList = null;
92 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
94 profileList = service.findAll();
95 model.putAll(setDashboardData(request));
96 model.put("profileList", mapper.writeValueAsString(profileList));
97 } catch (Exception e) {
98 logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
100 return new ModelAndView(getViewName(), "model", model);
103 @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
104 public void getUser(HttpServletRequest request, HttpServletResponse response) {
105 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
106 ObjectMapper mapper = new ObjectMapper();
107 List<User> profileList = null;
109 profileList = service.findAll();
110 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(profileList));
111 JSONObject j = new JSONObject(msg);
112 response.setContentType(APPLICATION_JSON);
113 response.getWriter().write(j.toString());
114 } catch (Exception e) {
115 logger.error(EELFLoggerDelegate.applicationLogger, "getUser failed", e);
119 @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
120 public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
121 Map<String, Object> model = new HashMap<>();
122 ObjectMapper mapper = new ObjectMapper();
123 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
124 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
125 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
126 List<User> profileList = null;
128 profileList = service.findAll();
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(profileList));
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);
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();
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);
159 model.put("childItemList", mapper.writeValueAsString(childItemList));
160 model.put("parentList", mapper.writeValueAsString(parentList));
164 @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
165 public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
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);