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 UserProfileService service;
61 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,
75 "error while profile_search process in ProfileSearchController" + e.getMessage());
77 return new ModelAndView(getViewName(), "model", model);
80 @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
81 public void GetUser(HttpServletRequest request, HttpServletResponse response) {
82 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
83 ObjectMapper mapper = new ObjectMapper();
84 List<User> profileList = null;
86 profileList = service.findAll();
87 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(profileList));
88 JSONObject j = new JSONObject(msg);
89 response.getWriter().write(j.toString());
91 } catch (Exception e) {
92 logger.error(EELFLoggerDelegate.applicationLogger,
93 "error while get_user process in ProfileSearchController" + e.getMessage());
97 @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
98 public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
99 Map<String, Object> model = new HashMap<String, Object>();
100 ObjectMapper mapper = new ObjectMapper();
101 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
102 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
103 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
104 List<User> profileList = null;
106 profileList = service.findAll();
107 model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
108 profileList = profileList.subList(
109 viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
110 viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
111 model.put("profileList", mapper.writeValueAsString(profileList));
112 JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
113 JSONObject j = new JSONObject(msg);
114 response.getWriter().write(j.toString());
116 } catch (Exception e) {
117 logger.error(EELFLoggerDelegate.applicationLogger,
118 "error while get_user_pagination process in ProfileSearchController" + e.getMessage());
122 @SuppressWarnings("unchecked")
123 public Map<String, Object> setDashboardData(HttpServletRequest request) throws Exception {
124 ObjectMapper mapper = new ObjectMapper();
125 Map<String, Object> model = new HashMap<String, Object>();
126 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
127 List<MenuData> parentList = new ArrayList<MenuData>();
128 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
129 HttpSession session = request.getSession();
131 Set<MenuData> menuResult = (Set<MenuData>) session
132 .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
133 fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
134 } catch (Exception e) {
135 logger.error(EELFLoggerDelegate.applicationLogger,
136 "error while setDashboardData process in ProfileSearchController" + e.getMessage());
138 model.put("childItemList", mapper.writeValueAsString(childItemList));
139 model.put("parentList", mapper.writeValueAsString(parentList));
143 @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
144 public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
146 logger.info(EELFLoggerDelegate.applicationLogger,
147 "Initiating toggleProfileActive in ProfileSearchController");
148 String userId = request.getParameter("profile_id");
149 User user = (User) service.getUser(userId);
150 user.setActive(!user.getActive());
151 service.saveUser(user);
152 logger.info(EELFLoggerDelegate.auditLogger,
153 "Change active status for user " + user.getId() + " to " + user.getActive());
154 ObjectMapper mapper = new ObjectMapper();
155 response.setContentType("application/json");
156 PrintWriter out = response.getWriter();
157 out.write(mapper.writeValueAsString(user.getActive()));
158 } catch (Exception e) {
159 logger.error(EELFLoggerDelegate.applicationLogger,
160 "error while toggleProfileActive process in ProfileSearchController" + e.getMessage());