2459e598ef4cf1513a8d9111325f7ec04610f644
[portal/sdk.git] /
1 /*
2  * ============LICENSE_START==========================================
3  * ONAP Portal SDK
4  * ===================================================================
5  * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
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
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
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.
20  *
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
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
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.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.controller.core;
39
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;
45 import java.util.Map;
46 import java.util.Set;
47
48 import javax.servlet.http.HttpServletRequest;
49 import javax.servlet.http.HttpServletResponse;
50 import javax.servlet.http.HttpSession;
51
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;
67
68 import com.fasterxml.jackson.databind.ObjectMapper;
69
70 @Controller
71 @RequestMapping("/")
72 public class ProfileSearchController extends RestrictedBaseController {
73
74         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
75         
76         @Autowired
77         private UserProfileService service;
78         
79         @Autowired
80         private UserService userService;
81         
82         @Autowired
83         private FnMenuService fnMenuService;
84
85         @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
86         public ModelAndView profileSearch(HttpServletRequest request) {
87                 Map<String, Object> model = new HashMap<>();
88                 ObjectMapper mapper = new ObjectMapper();
89                 List<User> profileList = null;
90                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
91                 try {
92                         profileList = service.findAll();
93                         model.putAll(setDashboardData(request));
94                         model.put("profileList", mapper.writeValueAsString(profileList));
95                 } catch (Exception e) {
96                         logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
97                 }
98                 return new ModelAndView(getViewName(), "model", model);
99         }
100
101         @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
102         public void getUser(HttpServletRequest request, HttpServletResponse response) {
103                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
104                 ObjectMapper mapper = new ObjectMapper();
105                 List<User> profileList = null;
106                 try {
107                         profileList = service.findAll();
108                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(profileList));
109                         JSONObject j = new JSONObject(msg);
110                         response.setContentType("application/json");
111                         response.getWriter().write(j.toString());
112                 } catch (Exception e) {
113                         logger.error(EELFLoggerDelegate.applicationLogger, "getUser failed", e);
114                 }
115         }
116
117         @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
118         public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
119                 Map<String, Object> model = new HashMap<>();
120                 ObjectMapper mapper = new ObjectMapper();
121                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
122                 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
123                 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
124                 List<User> profileList = null;
125                 try {
126                         profileList = service.findAll();
127                         model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
128                         profileList = profileList.subList(
129                                         viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
130                                         viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
131                         model.put("profileList", mapper.writeValueAsString(profileList));
132                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
133                         JSONObject j = new JSONObject(msg);
134                         response.setContentType("application/json");
135                         response.getWriter().write(j.toString());
136                 } catch (Exception e) {
137                         logger.error(EELFLoggerDelegate.applicationLogger, "getUserPagination failed", e);
138                 }
139         }
140
141         @SuppressWarnings("unchecked")
142         private Map<String, Object> setDashboardData(HttpServletRequest request) throws Exception {
143                 ObjectMapper mapper = new ObjectMapper();
144                 Map<String, Object> model = new HashMap<>();
145                 List<List<MenuData>> childItemList = new ArrayList<>();
146                 List<MenuData> parentList = new ArrayList<>();
147                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
148                 HttpSession session = request.getSession();
149                 try {
150                         Set<MenuData> menuResult = (Set<MenuData>) session
151                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
152                         fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
153                 } catch (Exception e) {
154                         logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
155                 }
156                 model.put("childItemList", mapper.writeValueAsString(childItemList));
157                 model.put("parentList", mapper.writeValueAsString(parentList));
158                 return model;
159         }
160
161         @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
162         public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
163                 try {
164                         logger.info(EELFLoggerDelegate.applicationLogger,
165                                         "Initiating toggleProfileActive in ProfileSearchController");
166                         String userId = request.getParameter("profile_id");
167                         User user = userService.getUser(userId);
168                         user.setActive(!user.getActive());
169                         service.saveUser(user);
170                         logger.info(EELFLoggerDelegate.auditLogger,
171                                         "Change active status for user " + user.getId() + " to " + user.getActive());
172                         ObjectMapper mapper = new ObjectMapper();
173                         response.setContentType("application/json");
174                         PrintWriter out = response.getWriter();
175                         out.write(mapper.writeValueAsString(user.getActive()));
176                 } catch (Exception e) {
177                         logger.error(EELFLoggerDelegate.applicationLogger, "toggleProfileActive failed", e);
178                 }
179         }
180 }