1e600a305acfe0c3e9dc0bf83bfd5f10334aa760
[portal/sdk.git] /
1 /*-
2  * ================================================================================
3  * ECOMP Portal SDK
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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  * ================================================================================
19  */
20 package org.openecomp.portalapp.controller.core;
21
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;
27 import java.util.Map;
28 import java.util.Set;
29
30 import javax.servlet.http.HttpServletRequest;
31 import javax.servlet.http.HttpServletResponse;
32 import javax.servlet.http.HttpSession;
33
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.service.UserService;
42 import org.openecomp.portalsdk.core.util.SystemProperties;
43 import org.openecomp.portalsdk.core.web.support.JsonMessage;
44 import org.springframework.beans.factory.annotation.Autowired;
45 import org.springframework.stereotype.Controller;
46 import org.springframework.web.bind.annotation.RequestMapping;
47 import org.springframework.web.bind.annotation.RequestMethod;
48 import org.springframework.web.servlet.ModelAndView;
49
50 import com.fasterxml.jackson.databind.ObjectMapper;
51
52 @Controller
53 @RequestMapping("/")
54 public class ProfileSearchController extends RestrictedBaseController {
55
56         private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
57         
58         @Autowired
59         private UserProfileService service;
60         
61         @Autowired
62         UserService userService;
63         
64         @Autowired
65         private FnMenuService fnMenuService;
66
67         @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
68         public ModelAndView profileSearch(HttpServletRequest request) {
69                 Map<String, Object> model = new HashMap<String, Object>();
70                 ObjectMapper mapper = new ObjectMapper();
71                 List<User> profileList = null;
72                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
73                 try {
74                         profileList = service.findAll();
75                         model.putAll(setDashboardData(request));
76                         model.put("profileList", mapper.writeValueAsString(profileList));
77                 } catch (Exception e) {
78                         logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
79                 }
80                 return new ModelAndView(getViewName(), "model", model);
81         }
82
83         @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
84         public void getUser(HttpServletRequest request, HttpServletResponse response) {
85                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
86                 ObjectMapper mapper = new ObjectMapper();
87                 List<User> profileList = null;
88                 try {
89                         profileList = service.findAll();
90                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(profileList));
91                         JSONObject j = new JSONObject(msg);
92                         response.setContentType("application/json");
93                         response.getWriter().write(j.toString());
94                 } catch (Exception e) {
95                         logger.error(EELFLoggerDelegate.applicationLogger, "getUser failed", e);
96                 }
97         }
98
99         @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
100         public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
101                 Map<String, Object> model = new HashMap<String, Object>();
102                 ObjectMapper mapper = new ObjectMapper();
103                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
104                 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
105                 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
106                 List<User> profileList = null;
107                 try {
108                         profileList = service.findAll();
109                         model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
110                         profileList = profileList.subList(
111                                         viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
112                                         viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
113                         model.put("profileList", mapper.writeValueAsString(profileList));
114                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
115                         JSONObject j = new JSONObject(msg);
116                         response.setContentType("application/json");
117                         response.getWriter().write(j.toString());
118                 } catch (Exception e) {
119                         logger.error(EELFLoggerDelegate.applicationLogger, "getUserPagination failed", e);
120                 }
121         }
122
123         @SuppressWarnings("unchecked")
124         private Map<String, Object> setDashboardData(HttpServletRequest request) throws Exception {
125                 ObjectMapper mapper = new ObjectMapper();
126                 Map<String, Object> model = new HashMap<String, Object>();
127                 List<List<MenuData>> childItemList = new ArrayList<List<MenuData>>();
128                 List<MenuData> parentList = new ArrayList<MenuData>();
129                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
130                 HttpSession session = request.getSession();
131                 try {
132                         Set<MenuData> menuResult = (Set<MenuData>) session
133                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
134                         fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
135                 } catch (Exception e) {
136                         logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
137                 }
138                 model.put("childItemList", mapper.writeValueAsString(childItemList));
139                 model.put("parentList", mapper.writeValueAsString(parentList));
140                 return model;
141         }
142
143         @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
144         public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
145                 try {
146                         logger.info(EELFLoggerDelegate.applicationLogger,
147                                         "Initiating toggleProfileActive in ProfileSearchController");
148                         String userId = request.getParameter("profile_id");
149                         User user = (User) userService.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, "toggleProfileActive failed", e);
160                 }
161         }
162 }