a94c3b46f6872405687d03ad5ffd6aeb4297ece2
[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  * 
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.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.logging.logic.EELFLoggerDelegate;
58 import org.onap.portalsdk.core.onboarding.exception.PortalAPIException;
59 import org.onap.portalsdk.core.service.FnMenuService;
60 import org.onap.portalsdk.core.service.UserProfileService;
61 import org.onap.portalsdk.core.service.UserService;
62 import org.onap.portalsdk.core.util.SystemProperties;
63 import org.onap.portalsdk.core.web.support.JsonMessage;
64 import org.springframework.beans.factory.annotation.Autowired;
65 import org.springframework.stereotype.Controller;
66 import org.springframework.web.bind.annotation.RequestMapping;
67 import org.springframework.web.bind.annotation.RequestMethod;
68 import org.springframework.web.servlet.ModelAndView;
69
70 import com.fasterxml.jackson.databind.ObjectMapper;
71
72 @Controller
73 @RequestMapping("/")
74 public class ProfileSearchController extends RestrictedBaseController {
75
76         private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(ProfileSearchController.class);
77
78         private static final String APPLICATION_JSON = "application/json";
79         
80         @Autowired
81         private UserProfileService service;
82         
83         @Autowired
84         private UserService userService;
85         
86         @Autowired
87         private FnMenuService fnMenuService;
88         
89         @Autowired
90         private LoginStrategy loginStrategy;
91
92         @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
93         public ModelAndView profileSearch(HttpServletRequest request) {
94                 Map<String, Object> model = new HashMap<>();
95                 ObjectMapper mapper = new ObjectMapper();
96                 List<User> profileList = null;
97                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
98                 try {
99                         profileList = service.findAll();
100                         model.putAll(setDashboardData(request));
101                         model.put("profileList", mapper.writeValueAsString(profileList));
102                 } catch (Exception e) {
103                         logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
104                 }
105                 return new ModelAndView(getViewName(), "model", model);
106         }
107
108         @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
109         public void getUser(HttpServletRequest request, HttpServletResponse response) {
110                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
111                 String  userId = "";
112                 try {
113                          userId =  loginStrategy.getUserId(request);
114                 } catch (PortalAPIException e1) {
115                         logger.error(EELFLoggerDelegate.applicationLogger, "No User found in request", e1);
116                 }
117                 
118                 final String requestedUserId = userId;
119                 ObjectMapper mapper = new ObjectMapper();
120                 List<User> profileList = null;
121                 try {
122                         profileList = service.findAll();
123             User user = profileList.stream()
124                                         .filter(x -> x.getOrgUserId().equals(requestedUserId)).findAny().orElse(null);
125             JsonMessage msg = new JsonMessage(mapper.writeValueAsString(user));
126                         JSONObject j = new JSONObject(msg);
127                         response.setContentType(APPLICATION_JSON);
128                         response.getWriter().write(j.toString());
129                 } catch (Exception e) {
130                         logger.error(EELFLoggerDelegate.applicationLogger, "getUser failed", e);
131                 }
132         }
133
134         @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
135         public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
136                 Map<String, Object> model = new HashMap<>();
137                 ObjectMapper mapper = new ObjectMapper();
138                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
139                 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
140                 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
141                 List<User> profileList = null;
142                 try {
143                         profileList = service.findAll();
144                         model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
145                         profileList = profileList.subList(
146                                         viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
147                                         viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
148                         model.put("profileList", mapper.writeValueAsString(profileList));
149                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
150                         JSONObject j = new JSONObject(msg);
151                         response.setContentType(APPLICATION_JSON);
152                         response.getWriter().write(j.toString());
153                 } catch (Exception e) {
154                         logger.error(EELFLoggerDelegate.applicationLogger, "getUserPagination failed", e);
155                 }
156         }
157
158         @SuppressWarnings("unchecked")
159         private Map<String, Object> setDashboardData(HttpServletRequest request)
160       throws com.fasterxml.jackson.core.JsonProcessingException {
161                 ObjectMapper mapper = new ObjectMapper();
162                 Map<String, Object> model = new HashMap<>();
163                 List<List<MenuData>> childItemList = new ArrayList<>();
164                 List<MenuData> parentList = new ArrayList<>();
165                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
166                 HttpSession session = request.getSession();
167                 try {
168                         Set<MenuData> menuResult = (Set<MenuData>) session
169                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
170                         fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
171                 } catch (Exception e) {
172                         logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
173                 }
174                 model.put("childItemList", mapper.writeValueAsString(childItemList));
175                 model.put("parentList", mapper.writeValueAsString(parentList));
176                 return model;
177         }
178
179         @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
180         public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
181                 try {
182                         logger.info(EELFLoggerDelegate.applicationLogger,
183                                         "Initiating toggleProfileActive in ProfileSearchController");
184                         String userId = request.getParameter("profile_id");
185                         User user = userService.getUser(userId);
186                         user.setActive(!user.getActive());
187                         service.saveUser(user);
188                         logger.info(EELFLoggerDelegate.auditLogger,
189                                         "Change active status for user " + user.getId() + " to " + user.getActive());
190                         ObjectMapper mapper = new ObjectMapper();
191                         response.setContentType(APPLICATION_JSON);
192                         PrintWriter out = response.getWriter();
193                         out.write(mapper.writeValueAsString(user.getActive()));
194                 } catch (Exception e) {
195                         logger.error(EELFLoggerDelegate.applicationLogger, "toggleProfileActive failed", e);
196                 }
197         }
198 }