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.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.onboarding.exception.PortalAPIException;
60 import org.onap.portalsdk.core.service.FnMenuService;
61 import org.onap.portalsdk.core.service.UserProfileService;
62 import org.onap.portalsdk.core.service.UserService;
63 import org.onap.portalsdk.core.util.SystemProperties;
64 import org.onap.portalsdk.core.web.support.JsonMessage;
65 import org.onap.portalsdk.core.web.support.UserUtils;
66 import org.springframework.beans.factory.annotation.Autowired;
67 import org.springframework.stereotype.Controller;
68 import org.springframework.web.bind.annotation.RequestMapping;
69 import org.springframework.web.bind.annotation.RequestMethod;
70 import org.springframework.web.servlet.ModelAndView;
72 import com.fasterxml.jackson.databind.ObjectMapper;
76 public class ProfileSearchController extends RestrictedBaseController {
78 private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
80 private static final String APPLICATION_JSON = "application/json";
83 private UserProfileService service;
86 private UserService userService;
89 private FnMenuService fnMenuService;
92 private LoginStrategy loginStrategy;
94 @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
95 public ModelAndView profileSearch(HttpServletRequest request) {
96 Map<String, Object> model = new HashMap<>();
97 ObjectMapper mapper = new ObjectMapper();
98 List<User> profileList = null;
99 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
101 profileList = service.findAll();
102 model.putAll(setDashboardData(request));
103 model.put("profileList", mapper.writeValueAsString(profileList));
104 } catch (Exception e) {
105 logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
107 return new ModelAndView(getViewName(), "model", model);
111 @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
112 public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
113 Map<String, Object> model = new HashMap<>();
114 ObjectMapper mapper = new ObjectMapper();
115 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
116 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
117 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
118 List<User> profileList = null;
119 List<User> profileFinalList = new ArrayList<>();
121 profileList = service.findAll();
122 for(User user: profileList)
124 Set<UserApp> userapps = user.getUserApps();
125 Set<UserApp> userapplications = UserUtils.getUserApps(userapps);
126 user.setUserApps(userapplications);
127 profileFinalList.add(user);
130 model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
131 profileList = profileList.subList(
132 viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
133 viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
134 model.put("profileList", mapper.writeValueAsString(profileFinalList));
135 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
136 JSONObject j = new JSONObject(msg);
137 response.setContentType("application/json");
138 response.getWriter().write(j.toString());
139 } catch (Exception e) {
140 logger.error(EELFLoggerDelegate.applicationLogger, "getUserPagination failed", e);
144 @SuppressWarnings("unchecked")
145 private Map<String, Object> setDashboardData(HttpServletRequest request)
146 throws com.fasterxml.jackson.core.JsonProcessingException {
147 ObjectMapper mapper = new ObjectMapper();
148 Map<String, Object> model = new HashMap<>();
149 List<List<MenuData>> childItemList = new ArrayList<>();
150 List<MenuData> parentList = new ArrayList<>();
151 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
152 HttpSession session = request.getSession();
154 Set<MenuData> menuResult = (Set<MenuData>) session
155 .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
156 fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
157 } catch (Exception e) {
158 logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
160 model.put("childItemList", mapper.writeValueAsString(childItemList));
161 model.put("parentList", mapper.writeValueAsString(parentList));
165 @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
166 public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
168 logger.info(EELFLoggerDelegate.applicationLogger,
169 "Initiating toggleProfileActive in ProfileSearchController");
170 String userId = request.getParameter("profile_id");
171 User user = userService.getUser(userId);
172 user.setActive(!user.getActive());
173 service.saveUser(user);
174 logger.info(EELFLoggerDelegate.auditLogger,
175 "Change active status for user " + user.getId() + " to " + user.getActive());
176 ObjectMapper mapper = new ObjectMapper();
177 response.setContentType(APPLICATION_JSON);
178 PrintWriter out = response.getWriter();
179 out.write(mapper.writeValueAsString(user.getActive()));
180 } catch (Exception e) {
181 logger.error(EELFLoggerDelegate.applicationLogger, "toggleProfileActive failed", e);