2 * ============LICENSE_START==========================================
4 * ===================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ===================================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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.
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
26 * https://creativecommons.org/licenses/by/4.0/
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.
34 * ============LICENSE_END============================================
38 package org.onap.portalapp.portal.controller;
40 import java.text.ParseException;
41 import java.text.SimpleDateFormat;
42 import java.util.ArrayList;
43 import java.util.HashMap;
44 import java.util.HashSet;
45 import java.util.List;
48 import javax.servlet.http.HttpServletRequest;
50 import org.onap.portalapp.controller.EPRestrictedBaseController;
51 import org.onap.portalapp.portal.domain.EPUser;
52 import org.onap.portalapp.portal.ecomp.model.PortalRestResponse;
53 import org.onap.portalapp.portal.ecomp.model.PortalRestStatusEnum;
54 import org.onap.portalapp.portal.ecomp.model.SearchResultItem;
55 import org.onap.portalapp.portal.service.DashboardSearchService;
56 import org.onap.portalapp.portal.transport.CommonWidget;
57 import org.onap.portalapp.portal.transport.CommonWidgetMeta;
58 import org.onap.portalapp.util.EPUserUtils;
59 import org.onap.portalapp.validation.DataValidator;
60 import org.onap.portalapp.validation.SecureString;
61 import org.onap.portalsdk.core.domain.support.CollaborateList;
62 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
63 import org.springframework.beans.factory.annotation.Autowired;
64 import org.springframework.web.bind.annotation.RequestBody;
65 import org.springframework.web.bind.annotation.RequestMapping;
66 import org.springframework.web.bind.annotation.RequestMethod;
67 import org.springframework.web.bind.annotation.RequestParam;
68 import org.springframework.web.bind.annotation.RestController;
71 @RequestMapping("/portalApi/search")
72 public class DashboardSearchResultController extends EPRestrictedBaseController {
74 private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardSearchResultController.class);
75 private DataValidator dataValidator = new DataValidator();
78 private DashboardSearchService searchService;
81 * Gets all widgets by type: NEW or RESOURCE
86 * @return Rest response wrapped around a CommonWidgetMeta object.
88 @RequestMapping(value = "/widgetData", method = RequestMethod.GET, produces = "application/json")
89 public PortalRestResponse<CommonWidgetMeta> getWidgetData(HttpServletRequest request,
90 @RequestParam String resourceType) {
91 if (resourceType !=null){
92 SecureString secureString = new SecureString(resourceType);
93 if (!dataValidator.isValid(secureString))
94 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "Provided data is invalid", null);
96 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
97 searchService.getWidgetData(resourceType));
101 * Saves all: news and resources
103 * @param commonWidgetMeta
104 * read from POST body.
105 * @return Rest response wrapped around a String; e.g., "success" or "ERROR"
107 @RequestMapping(value = "/widgetDataBulk", method = RequestMethod.POST, produces = "application/json")
108 public PortalRestResponse<String> saveWidgetDataBulk(@RequestBody CommonWidgetMeta commonWidgetMeta) {
109 logger.debug(EELFLoggerDelegate.debugLogger, "saveWidgetDataBulk: argument is {}", commonWidgetMeta);
110 if (commonWidgetMeta.getCategory() == null || commonWidgetMeta.getCategory().trim().equals("")){
111 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "ERROR",
112 "Category cannot be null or empty");
114 if(!dataValidator.isValid(commonWidgetMeta))
115 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "ERROR",
116 "Category is not valid");
119 for (CommonWidget cw : commonWidgetMeta.getItems()) {
120 String err = validateCommonWidget(cw);
122 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, err, null);
124 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "success",
125 searchService.saveWidgetDataBulk(commonWidgetMeta));
129 * Saves one: news or resource
131 * @param commonWidget
132 * read from POST body
133 * @return Rest response wrapped around a String; e.g., "success" or "ERROR"
135 @RequestMapping(value = "/widgetData", method = RequestMethod.POST, produces = "application/json")
136 public PortalRestResponse<String> saveWidgetData(@RequestBody CommonWidget commonWidget) {
137 logger.debug(EELFLoggerDelegate.debugLogger, "saveWidgetData: argument is {}", commonWidget);
138 if (commonWidget.getCategory() == null || commonWidget.getCategory().trim().equals("")){
139 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "ERROR",
140 "Cateogry cannot be null or empty");
142 if(!dataValidator.isValid(commonWidget))
143 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "ERROR",
144 "Category is not valid");
146 String err = validateCommonWidget(commonWidget);
148 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, err, null);
149 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
150 searchService.saveWidgetData(commonWidget));
154 * Used by the validate function
156 private final SimpleDateFormat yearMonthDayFormat = new SimpleDateFormat("yyyy-MM-dd");
159 * Validates the content of a common widget.
162 * @return null on success; an error message if validation fails.
165 private String validateCommonWidget(CommonWidget cw) {
167 if (cw.getEventDate() != null && cw.getEventDate().trim().length() > 0)
168 yearMonthDayFormat.parse(cw.getEventDate());
169 } catch (ParseException ex) {
170 return ex.toString();
176 * Deletes one: news or resource
178 * @param commonWidget
179 * read from POST body
180 * @return Rest response wrapped around a String; e.g., "success" or "ERROR"
182 @RequestMapping(value = "/deleteData", method = RequestMethod.POST, produces = "application/json")
183 public PortalRestResponse<String> deleteWidgetData(@RequestBody CommonWidget commonWidget) {
184 logger.debug(EELFLoggerDelegate.debugLogger, "deleteWidgetData: argument is {}", commonWidget);
185 if(!dataValidator.isValid(commonWidget))
186 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "ERROR",
187 "Data is not valid");
188 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
189 searchService.deleteWidgetData(commonWidget));
193 * Searches all portal for the input string.
196 * @param searchString
197 * @return Rest response wrapped around a Map of String to List of Search
200 @RequestMapping(value = "/allPortal", method = RequestMethod.GET, produces = "application/json")
201 public PortalRestResponse<Map<String, List<SearchResultItem>>> searchPortal(HttpServletRequest request,
202 @RequestParam String searchString) {
203 if(searchString!=null){
204 SecureString secureString = new SecureString(searchString);
205 if(!dataValidator.isValid(secureString)){
206 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
207 "searchPortal: User object is invalid",
212 EPUser user = EPUserUtils.getUserSession(request);
215 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
216 "searchPortal: User object is null? - check logs",
218 } else if (searchString == null || searchString.trim().length() == 0) {
219 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "searchPortal: String string is null",
222 logger.debug(EELFLoggerDelegate.debugLogger, "searchPortal: user {}, search string '{}'",
223 user.getLoginId(), searchString);
224 Map<String, List<SearchResultItem>> results = searchService.searchResults(user.getLoginId(),
226 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", results);
228 } catch (Exception e) {
229 logger.error(EELFLoggerDelegate.errorLogger, "searchPortal failed", e);
230 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage() + " - check logs.",
236 * Gets all active users.
238 * TODO: should only the superuser be allowed to use this API?
241 * @return Rest response wrapped around a list of String
243 @RequestMapping(value = "/activeUsers", method = RequestMethod.GET, produces = "application/json")
244 public List<String> getActiveUsers(HttpServletRequest request) {
245 List<String> activeUsers = null;
246 List<String> onlineUsers = new ArrayList<>();
248 EPUser user = EPUserUtils.getUserSession(request);
249 String userId = user.getOrgUserId();
251 activeUsers = searchService.getRelatedUsers(userId);
252 HashSet<String> usersSet = (HashSet<String>) CollaborateList.getInstance().getAllUserName();
253 for (String users : activeUsers) {
254 if (usersSet.contains(users)) {
255 onlineUsers.add(users);
259 } catch (Exception e) {
260 logger.error(EELFLoggerDelegate.errorLogger, "getActiveUsers failed", e);
266 * Gets only those users that are 'related' to the currently logged-in user.
269 * @return Rest response wrapped around a List of String
271 @RequestMapping(value = "/relatedUsers", method = RequestMethod.GET, produces = "application/json")
272 public PortalRestResponse<List<String>> activeUsers(HttpServletRequest request) {
273 EPUser user = EPUserUtils.getUserSession(request);
276 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "User object is null? - check logs",
279 logger.debug(EELFLoggerDelegate.debugLogger, "activeUsers: searching for user {}", user.getLoginId());
280 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
281 searchService.getRelatedUsers(user.getLoginId()));
283 } catch (Exception e) {
284 logger.error(EELFLoggerDelegate.errorLogger, "activeUsers failed", e);
285 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage() + " - check logs.",