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