2 * ================================================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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 * ================================================================================
20 package org.openecomp.portalapp.controller.core;
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.ArrayList;
25 import java.util.HashMap;
26 import java.util.List;
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.http.HttpSession;
34 import org.json.JSONObject;
35 import org.openecomp.portalsdk.core.controller.RestrictedBaseController;
36 import org.openecomp.portalsdk.core.domain.MenuData;
37 import org.openecomp.portalsdk.core.domain.User;
38 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
39 import org.openecomp.portalsdk.core.service.FnMenuService;
40 import org.openecomp.portalsdk.core.service.UserProfileService;
41 import org.openecomp.portalsdk.core.util.SystemProperties;
42 import org.openecomp.portalsdk.core.web.support.JsonMessage;
43 import org.springframework.beans.factory.annotation.Autowired;
44 import org.springframework.stereotype.Controller;
45 import org.springframework.web.bind.annotation.RequestMapping;
46 import org.springframework.web.bind.annotation.RequestMethod;
47 import org.springframework.web.servlet.ModelAndView;
49 import com.fasterxml.jackson.databind.ObjectMapper;
53 public class ProfileSearchController extends RestrictedBaseController {
55 private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
58 private UserProfileService service;
61 private FnMenuService fnMenuService;
63 @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
64 public ModelAndView profileSearch(HttpServletRequest request) {
65 Map<String, Object> model = new HashMap<String, Object>();
66 ObjectMapper mapper = new ObjectMapper();
67 List<User> profileList = null;
68 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
70 profileList = service.findAll();
71 model.putAll(setDashboardData(request));
72 model.put("profileList", mapper.writeValueAsString(profileList));
73 } catch (Exception e) {
74 logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
76 return new ModelAndView(getViewName(), "model", model);
79 @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
80 public void getUser(HttpServletRequest request, HttpServletResponse response) {
81 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
82 ObjectMapper mapper = new ObjectMapper();
83 List<User> profileList = null;
85 profileList = service.findAll();
86 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(profileList));
87 JSONObject j = new JSONObject(msg);
88 response.setContentType("application/json");
89 response.getWriter().write(j.toString());
90 } catch (Exception e) {
91 logger.error(EELFLoggerDelegate.applicationLogger, "getUser failed", e);
95 @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
96 public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
97 Map<String, Object> model = new HashMap<String, Object>();
98 ObjectMapper mapper = new ObjectMapper();
99 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
100 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
101 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
102 List<User> profileList = null;
104 profileList = service.findAll();
105 model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
106 profileList = profileList.subList(
107 viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
108 viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
109 model.put("profileList", mapper.writeValueAsString(profileList));
110 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
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, "getUserPagination failed", e);
119 @SuppressWarnings("unchecked")
120 private Map<String, Object> setDashboardData(HttpServletRequest request) throws Exception {
121 ObjectMapper mapper = new ObjectMapper();
122 Map<String, Object> model = new HashMap<String, Object>();
123 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
124 List<MenuData> parentList = new ArrayList<MenuData>();
125 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
126 HttpSession session = request.getSession();
128 Set<MenuData> menuResult = (Set<MenuData>) session
129 .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
130 fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
131 } catch (Exception e) {
132 logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
134 model.put("childItemList", mapper.writeValueAsString(childItemList));
135 model.put("parentList", mapper.writeValueAsString(parentList));
139 @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
140 public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
142 logger.info(EELFLoggerDelegate.applicationLogger,
143 "Initiating toggleProfileActive in ProfileSearchController");
144 String userId = request.getParameter("profile_id");
145 User user = (User) service.getUser(userId);
146 user.setActive(!user.getActive());
147 service.saveUser(user);
148 logger.info(EELFLoggerDelegate.auditLogger,
149 "Change active status for user " + user.getId() + " to " + user.getActive());
150 ObjectMapper mapper = new ObjectMapper();
151 response.setContentType("application/json");
152 PrintWriter out = response.getWriter();
153 out.write(mapper.writeValueAsString(user.getActive()));
154 } catch (Exception e) {
155 logger.error(EELFLoggerDelegate.applicationLogger, "toggleProfileActive failed", e);