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