f5d37e2b162fe9d53d7941e9f91bb5fa87c5320d
[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.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         private static final String APPLICATION_JSON = "application/json";
77         
78         @Autowired
79         private UserProfileService service;
80         
81         @Autowired
82         private UserService userService;
83         
84         @Autowired
85         private FnMenuService fnMenuService;
86
87         @RequestMapping(value = { "/profile_search" }, method = RequestMethod.GET)
88         public ModelAndView profileSearch(HttpServletRequest request) {
89                 Map<String, Object> model = new HashMap<>();
90                 ObjectMapper mapper = new ObjectMapper();
91                 List<User> profileList = null;
92                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating ProfileSearch in ProfileSearchController");
93                 try {
94                         profileList = service.findAll();
95                         model.putAll(setDashboardData(request));
96                         model.put("profileList", mapper.writeValueAsString(profileList));
97                 } catch (Exception e) {
98                         logger.error(EELFLoggerDelegate.applicationLogger, "profileSearch failed", e);
99                 }
100                 return new ModelAndView(getViewName(), "model", model);
101         }
102
103         @RequestMapping(value = { "/get_user" }, method = RequestMethod.GET)
104         public void getUser(HttpServletRequest request, HttpServletResponse response) {
105                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user in ProfileSearchController");
106                 ObjectMapper mapper = new ObjectMapper();
107                 List<User> profileList = null;
108                 try {
109                         profileList = service.findAll();
110                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(profileList));
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, "getUser failed", e);
116                 }
117         }
118
119         @RequestMapping(value = { "/get_user_pagination" }, method = RequestMethod.GET)
120         public void getUserPagination(HttpServletRequest request, HttpServletResponse response) {
121                 Map<String, Object> model = new HashMap<>();
122                 ObjectMapper mapper = new ObjectMapper();
123                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating get_user_pagination in ProfileSearchController");
124                 int pageNum = Integer.parseInt(request.getParameter("pageNum"));
125                 int viewPerPage = Integer.parseInt(request.getParameter("viewPerPage"));
126                 List<User> profileList = null;
127                 try {
128                         profileList = service.findAll();
129                         model.put("totalPage", (int) Math.ceil((double) profileList.size() / viewPerPage));
130                         profileList = profileList.subList(
131                                         viewPerPage * (pageNum - 1) < profileList.size() ? viewPerPage * (pageNum - 1) : profileList.size(),
132                                         viewPerPage * pageNum < profileList.size() ? viewPerPage * pageNum : profileList.size());
133                         model.put("profileList", mapper.writeValueAsString(profileList));
134                         JsonMessage msg = new JsonMessage(mapper.writeValueAsString(model));
135                         JSONObject j = new JSONObject(msg);
136                         response.setContentType(APPLICATION_JSON);
137                         response.getWriter().write(j.toString());
138                 } catch (Exception e) {
139                         logger.error(EELFLoggerDelegate.applicationLogger, "getUserPagination failed", e);
140                 }
141         }
142
143         @SuppressWarnings("unchecked")
144         private Map<String, Object> setDashboardData(HttpServletRequest request)
145       throws com.fasterxml.jackson.core.JsonProcessingException {
146                 ObjectMapper mapper = new ObjectMapper();
147                 Map<String, Object> model = new HashMap<>();
148                 List<List<MenuData>> childItemList = new ArrayList<>();
149                 List<MenuData> parentList = new ArrayList<>();
150                 logger.info(EELFLoggerDelegate.applicationLogger, "Initiating setDashboardData in ProfileSearchController");
151                 HttpSession session = request.getSession();
152                 try {
153                         Set<MenuData> menuResult = (Set<MenuData>) session
154                                         .getAttribute(SystemProperties.getProperty(SystemProperties.APPLICATION_MENU_ATTRIBUTE_NAME));
155                         fnMenuService.setMenuDataStructure(childItemList, parentList, menuResult);
156                 } catch (Exception e) {
157                         logger.error(EELFLoggerDelegate.applicationLogger, "setDashboardData failed", e);
158                 }
159                 model.put("childItemList", mapper.writeValueAsString(childItemList));
160                 model.put("parentList", mapper.writeValueAsString(parentList));
161                 return model;
162         }
163
164         @RequestMapping(value = { "/profile/toggleProfileActive" }, method = RequestMethod.GET)
165         public void toggleProfileActive(HttpServletRequest request, HttpServletResponse response) throws IOException {
166                 try {
167                         logger.info(EELFLoggerDelegate.applicationLogger,
168                                         "Initiating toggleProfileActive in ProfileSearchController");
169                         String userId = request.getParameter("profile_id");
170                         User user = userService.getUser(userId);
171                         user.setActive(!user.getActive());
172                         service.saveUser(user);
173                         logger.info(EELFLoggerDelegate.auditLogger,
174                                         "Change active status for user " + user.getId() + " to " + user.getActive());
175                         ObjectMapper mapper = new ObjectMapper();
176                         response.setContentType(APPLICATION_JSON);
177                         PrintWriter out = response.getWriter();
178                         out.write(mapper.writeValueAsString(user.getActive()));
179                 } catch (Exception e) {
180                         logger.error(EELFLoggerDelegate.applicationLogger, "toggleProfileActive failed", e);
181                 }
182         }
183 }