[PORTAL-7] Rebase
[portal.git] / ecomp-portal-BE-common / src / main / java / org / openecomp / portalapp / portal / controller / CommonWidgetController.java
1 /*
2  * ================================================================================
3  * ECOMP Portal
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ================================================================================
19  */
20 package org.openecomp.portalapp.portal.controller;
21
22 import javax.servlet.http.HttpServletRequest;
23 import org.openecomp.portalapp.portal.controller.DashboardController.WidgetCategory;
24 import org.openecomp.portalapp.portal.ecomp.model.PortalRestResponse;
25 import org.openecomp.portalapp.portal.ecomp.model.PortalRestStatusEnum;
26 import org.openecomp.portalapp.portal.service.DashboardSearchService;
27 import org.openecomp.portalapp.portal.transport.CommonWidgetMeta;
28 import org.openecomp.portalsdk.core.logging.logic.EELFLoggerDelegate;
29 import org.springframework.beans.factory.annotation.Autowired;
30 import org.springframework.web.bind.annotation.RequestMapping;
31 import org.springframework.web.bind.annotation.RequestMethod;
32 import org.springframework.web.bind.annotation.RequestParam;
33 import org.springframework.web.bind.annotation.RestController;
34
35 @RestController
36 public class CommonWidgetController implements BasicAuthenticationController{
37         
38         
39         private static EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(CommonWidgetController.class);
40         @Autowired
41         private DashboardSearchService searchService;
42         
43         /**
44          * Validates the resource type parameter.
45          * 
46          * @param resourceType
47          * @return True if known in the enum WidgetCategory, else false.
48          */
49         private boolean isValidResourceType(String resourceType) {
50                 if (resourceType == null)
51                         return false;
52                 for (WidgetCategory wc : WidgetCategory.values())
53                         if (wc.name().equals(resourceType))
54                                 return true;
55                 return false;
56         }
57         
58         /**
59          * Gets all widgets of the specified resource type.
60          * 
61          * @param request
62          * @param resourceType
63          *            Request parameter.
64          * @return Rest response wrapped around a CommonWidgetMeta object.
65          */
66         @RequestMapping(value = "/widgetData", method = RequestMethod.GET, produces = "application/json")
67         public PortalRestResponse<CommonWidgetMeta> getWidgetData(HttpServletRequest request,
68                         @RequestParam String resourceType) {
69                 if (!isValidResourceType(resourceType)){
70                         logger.debug(EELFLoggerDelegate.debugLogger, "Unexpected resource type {}", resourceType);
71                         return new PortalRestResponse<CommonWidgetMeta>(PortalRestStatusEnum.ERROR,
72                                         "Unexpected resource type " + resourceType, null);
73                 }
74                 return new PortalRestResponse<CommonWidgetMeta>(PortalRestStatusEnum.OK, "success",
75                                 searchService.getWidgetData(resourceType));
76         }
77                 
78
79 }