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.portalsdk.core.domain.support.CollaborateList;
 
  60 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
 
  61 import org.springframework.beans.factory.annotation.Autowired;
 
  62 import org.springframework.web.bind.annotation.RequestBody;
 
  63 import org.springframework.web.bind.annotation.RequestMapping;
 
  64 import org.springframework.web.bind.annotation.RequestMethod;
 
  65 import org.springframework.web.bind.annotation.RequestParam;
 
  66 import org.springframework.web.bind.annotation.RestController;
 
  69 @RequestMapping("/portalApi/search")
 
  70 public class DashboardSearchResultController extends EPRestrictedBaseController {
 
  72         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(DashboardSearchResultController.class);
 
  75         private DashboardSearchService searchService;
 
  78          * Gets all widgets by type: NEW or RESOURCE
 
  83          * @return Rest response wrapped around a CommonWidgetMeta object.
 
  85         @RequestMapping(value = "/widgetData", method = RequestMethod.GET, produces = "application/json")
 
  86         public PortalRestResponse<CommonWidgetMeta> getWidgetData(HttpServletRequest request,
 
  87                         @RequestParam String resourceType) {
 
  88                 return new PortalRestResponse<CommonWidgetMeta>(PortalRestStatusEnum.OK, "success",
 
  89                                 searchService.getWidgetData(resourceType));
 
  93          * Saves all: news and resources
 
  95          * @param commonWidgetMeta
 
  96          *            read from POST body.
 
  97          * @return Rest response wrapped around a String; e.g., "success" or "ERROR"
 
  99         @RequestMapping(value = "/widgetDataBulk", method = RequestMethod.POST, produces = "application/json")
 
 100         public PortalRestResponse<String> saveWidgetDataBulk(@RequestBody CommonWidgetMeta commonWidgetMeta) {
 
 101                 logger.debug(EELFLoggerDelegate.debugLogger, "saveWidgetDataBulk: argument is {}", commonWidgetMeta);
 
 102                 if (commonWidgetMeta.getCategory() == null || commonWidgetMeta.getCategory().trim().equals(""))
 
 103                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "ERROR",
 
 104                                         "Category cannot be null or empty");
 
 106                 for (CommonWidget cw : commonWidgetMeta.getItems()) {
 
 107                         String err = validateCommonWidget(cw);
 
 109                                 return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, err, null);
 
 111                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "success",
 
 112                                 searchService.saveWidgetDataBulk(commonWidgetMeta));
 
 116          * Saves one: news or resource
 
 118          * @param commonWidget
 
 119          *            read from POST body
 
 120          * @return Rest response wrapped around a String; e.g., "success" or "ERROR"
 
 122         @RequestMapping(value = "/widgetData", method = RequestMethod.POST, produces = "application/json")
 
 123         public PortalRestResponse<String> saveWidgetData(@RequestBody CommonWidget commonWidget) {
 
 124                 logger.debug(EELFLoggerDelegate.debugLogger, "saveWidgetData: argument is {}", commonWidget);
 
 125                 if (commonWidget.getCategory() == null || commonWidget.getCategory().trim().equals(""))
 
 126                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "ERROR",
 
 127                                         "Cateogry cannot be null or empty");
 
 128                 String err = validateCommonWidget(commonWidget);
 
 130                         return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, err, null);
 
 131                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "success",
 
 132                                 searchService.saveWidgetData(commonWidget));
 
 136          * Used by the validate function
 
 138         private final SimpleDateFormat yearMonthDayFormat = new SimpleDateFormat("yyyy-MM-dd");
 
 141          * Validates the content of a common widget.
 
 144          * @return null on success; an error message if validation fails.
 
 147         private String validateCommonWidget(CommonWidget cw) {
 
 149                         if (cw.getEventDate() != null && cw.getEventDate().trim().length() > 0)
 
 150                                 yearMonthDayFormat.parse(cw.getEventDate());
 
 151                 } catch (ParseException ex) {
 
 152                         return ex.toString();
 
 158          * Deletes one: news or resource
 
 160          * @param commonWidget
 
 161          *            read from POST body
 
 162          * @return Rest response wrapped around a String; e.g., "success" or "ERROR"
 
 164         @RequestMapping(value = "/deleteData", method = RequestMethod.POST, produces = "application/json")
 
 165         public PortalRestResponse<String> deleteWidgetData(@RequestBody CommonWidget commonWidget) {
 
 166                 logger.debug(EELFLoggerDelegate.debugLogger, "deleteWidgetData: argument is {}", commonWidget);
 
 167                 return new PortalRestResponse<String>(PortalRestStatusEnum.OK, "success",
 
 168                                 searchService.deleteWidgetData(commonWidget));
 
 172          * Searches all portal for the input string.
 
 175          * @param searchString
 
 176          * @return Rest response wrapped around a Map of String to List of Search
 
 179         @RequestMapping(value = "/allPortal", method = RequestMethod.GET, produces = "application/json")
 
 180         public PortalRestResponse<Map<String, List<SearchResultItem>>> searchPortal(HttpServletRequest request,
 
 181                         @RequestParam String searchString) {
 
 183                 EPUser user = EPUserUtils.getUserSession(request);
 
 186                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR,
 
 187                                                 "searchPortal: User object is null? - check logs",
 
 188                                                 new HashMap<String, List<SearchResultItem>>());
 
 189                         } else if (searchString == null || searchString.trim().length() == 0) {
 
 190                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "searchPortal: String string is null",
 
 191                                                 new HashMap<String, List<SearchResultItem>>());
 
 193                                 logger.debug(EELFLoggerDelegate.debugLogger, "searchPortal: user {}, search string '{}'",
 
 194                                                 user.getLoginId(), searchString);
 
 195                                 Map<String, List<SearchResultItem>> results = searchService.searchResults(user.getLoginId(),
 
 197                                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success", results);
 
 199                 } catch (Exception e) {
 
 200                         logger.error(EELFLoggerDelegate.errorLogger, "searchPortal failed", e);
 
 201                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage() + " - check logs.",
 
 202                                         new HashMap<String, List<SearchResultItem>>());
 
 207          * Gets all active users.
 
 209          * TODO: should only the superuser be allowed to use this API?
 
 212          * @return Rest response wrapped around a list of String
 
 214         @RequestMapping(value = "/activeUsers", method = RequestMethod.GET, produces = "application/json")
 
 215         public List<String> getActiveUsers(HttpServletRequest request) {
 
 216                 List<String> activeUsers = null;
 
 217                 List<String> onlineUsers = new ArrayList<>();
 
 219                         EPUser user = EPUserUtils.getUserSession(request);
 
 220                         String userId = user.getOrgUserId();
 
 222                         activeUsers = searchService.getRelatedUsers(userId);
 
 223                         HashSet<String> usersSet = (HashSet<String>) CollaborateList.getInstance().getAllUserName();
 
 224                         for (String users : activeUsers) {
 
 225                                 if (usersSet.contains(users)) {
 
 226                                         onlineUsers.add(users);
 
 230                 } catch (Exception e) {
 
 231                         logger.error(EELFLoggerDelegate.errorLogger, "getActiveUsers failed", e);
 
 237          * Gets only those users that are 'related' to the currently logged-in user.
 
 240          * @return Rest response wrapped around a List of String
 
 242         @RequestMapping(value = "/relatedUsers", method = RequestMethod.GET, produces = "application/json")
 
 243         public PortalRestResponse<List<String>> activeUsers(HttpServletRequest request) {
 
 244                 EPUser user = EPUserUtils.getUserSession(request);
 
 247                                 return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, "User object is null? - check logs",
 
 250                                 logger.debug(EELFLoggerDelegate.debugLogger, "activeUsers: searching for user {}", user.getLoginId());
 
 251                                 return new PortalRestResponse<>(PortalRestStatusEnum.OK, "success",
 
 252                                                 searchService.getRelatedUsers(user.getLoginId()));
 
 254                 } catch (Exception e) {
 
 255                         logger.error(EELFLoggerDelegate.errorLogger, "activeUsers failed", e);
 
 256                         return new PortalRestResponse<>(PortalRestStatusEnum.ERROR, e.getMessage() + " - check logs.",