diff options
author | Dominik Mizyn <d.mizyn@samsung.com> | 2019-09-10 15:54:28 +0200 |
---|---|---|
committer | Dominik Mizyn <d.mizyn@samsung.com> | 2019-09-10 15:54:32 +0200 |
commit | 25ca3671cea82311d6832963fc189c8bbfab0d23 (patch) | |
tree | 5a02f751ed211e361ee4e23c60ca2c4224d9e20d /portal-BE/src/main/java | |
parent | 9e4e70f058ee09ae2933e4438f75fea5289f147a (diff) |
WidgetsCatalogController up
WidgetsCatalogController up
Issue-ID: PORTAL-710
Change-Id: Ie890b499cd5b2af90755584ce636193cc2536473
Signed-off-by: Dominik Mizyn <d.mizyn@samsung.com>
Diffstat (limited to 'portal-BE/src/main/java')
8 files changed, 756 insertions, 3 deletions
diff --git a/portal-BE/src/main/java/org/onap/portal/controller/WidgetsCatalogController.java b/portal-BE/src/main/java/org/onap/portal/controller/WidgetsCatalogController.java new file mode 100644 index 00000000..decf625d --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/controller/WidgetsCatalogController.java @@ -0,0 +1,430 @@ +/*- + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ +package org.onap.portal.controller; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.OutputStream; +import java.security.Principal; +import java.util.ArrayList; +import java.util.List; +import javax.servlet.ServletContext; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import org.apache.commons.lang.StringUtils; +import org.onap.portal.domain.db.fn.FnUser; +import org.onap.portal.domain.dto.PortalRestResponse; +import org.onap.portal.domain.dto.PortalRestStatusEnum; +import org.onap.portal.domain.dto.ecomp.MicroserviceParameter; +import org.onap.portal.domain.dto.ecomp.WidgetCatalog; +import org.onap.portal.domain.dto.ecomp.WidgetCatalogParameter; +import org.onap.portal.domain.dto.ecomp.WidgetParameterResult; +import org.onap.portal.domain.dto.ecomp.WidgetServiceHeaders; +import org.onap.portal.logging.aop.EPAuditLog; +import org.onap.portal.service.WidgetMService; +import org.onap.portal.service.ep.EpMicroserviceParameterService; +import org.onap.portal.service.ep.EpWidgetCatalogParameterService; +import org.onap.portal.service.fn.FnUserService; +import org.onap.portal.utils.EPCommonSystemProperties; +import org.onap.portal.utils.EcompPortalUtils; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.onap.portalsdk.core.util.SystemProperties; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.EnableAspectJAutoProxy; +import org.springframework.core.io.FileSystemResource; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.util.LinkedMultiValueMap; +import org.springframework.util.MultiValueMap; +import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.client.RestTemplate; +import org.springframework.web.multipart.MultipartFile; +import org.springframework.web.multipart.MultipartHttpServletRequest; + +@RestController +@EnableAspectJAutoProxy +@EPAuditLog +public class WidgetsCatalogController { + + private EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsCatalogController.class); + + private static final String MS_WIDGET_LOCAL_PORT = "microservices.widget.local.port"; + private static final String UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER = "Unauthorized or Forbidden for a disabled user"; + private RestTemplate template = new RestTemplate(); + private String whatService = "widgets-service"; + + private final EpWidgetCatalogParameterService epWidgetCatalogParameterService; + private final EpMicroserviceParameterService epMicroserviceParameterService; + private final WidgetMService widgetMService; + private final FnUserService fnUserService; + + @Autowired + public WidgetsCatalogController(final EpWidgetCatalogParameterService epWidgetCatalogParameterService, + final EpMicroserviceParameterService epMicroserviceParameterService, + final WidgetMService widgetMService, + final FnUserService fnUserService) { + this.epWidgetCatalogParameterService = epWidgetCatalogParameterService; + this.epMicroserviceParameterService = epMicroserviceParameterService; + this.widgetMService = widgetMService; + this.fnUserService = fnUserService; + } + + + @GetMapping(value = { + "/portalApi/microservices/widgetCatalog/{loginName}"}, produces = MediaType.APPLICATION_JSON_VALUE) + public List<WidgetCatalog> getUserWidgetCatalog(@PathVariable("loginName") String loginName) { + List<WidgetCatalog> widgets; + try { + ResponseEntity<List> ans = template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog/" + loginName, + HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class); + widgets = ans.getBody(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "getUserWidgetCatalog failed", e); + return null; + } + return widgets; + } + + @GetMapping(value = {"/portalApi/microservices/widgetCatalog"}, produces = MediaType.APPLICATION_JSON_VALUE) + public List<WidgetCatalog> getWidgetCatalog() { + List<WidgetCatalog> widgets; + try { + ResponseEntity<List> ans = template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog", + HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), List.class); + widgets = ans.getBody(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "getWidgetCatalog failed", e); + return null; + } + return widgets; + } + + @PutMapping(value = { + "/portalApi/microservices/widgetCatalog/{widgetId}"}, produces = MediaType.APPLICATION_JSON_VALUE) + public void updateWidgetCatalog(@RequestBody WidgetCatalog newWidgetCatalog, + @PathVariable("widgetId") long widgetId) throws Exception { + template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog/" + widgetId, + HttpMethod.PUT, new HttpEntity<>(newWidgetCatalog, WidgetServiceHeaders.getInstance()), + String.class); + } + + @DeleteMapping(value = {"/portalApi/microservices/widgetCatalog/{widgetId}"}) + public void deleteOnboardingWidget(@PathVariable("widgetId") long widgetId) throws Exception { + template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog/" + widgetId, + HttpMethod.DELETE, new HttpEntity<>(WidgetServiceHeaders.getInstance()), String.class); + } + + @PostMapping(value = {"/portalApi/microservices/widgetCatalog/{widgetId}"}) + public String updateWidgetCatalogWithFiles(HttpServletRequest request, + @PathVariable("widgetId") long widgetId) { + MultipartHttpServletRequest mRequest; + MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>(); + String fileName; + String tmpFolderName = "/tmp/"; + String respond = null; + FileOutputStream fo = null; + try { + mRequest = (MultipartHttpServletRequest) request; + MultipartFile mFile = mRequest.getFile("file"); + fileName = mFile.getOriginalFilename(); + fo = new FileOutputStream(tmpFolderName + fileName); + fo.write(mFile.getBytes()); + fo.close(); + fo = null; + + HttpHeaders header = new HttpHeaders(); + header.setContentType(MediaType.MULTIPART_FORM_DATA); + multipartRequest.add("file", new FileSystemResource(tmpFolderName + fileName)); + multipartRequest.add("widget", request.getParameter("newWidget")); + respond = template.postForObject( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog/" + widgetId, + new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class); + File f = new File(tmpFolderName + fileName); + f.delete(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed", e); + } finally { + try { + if (fo != null) { + fo.close(); + } + } catch (IOException e) { + logger.error(EELFLoggerDelegate.errorLogger, "updateWidgetCatalogWithFiles failed 2", e); + } + } + return respond; + } + + @PostMapping(value = {"/portalApi/microservices/widgetCatalog"}) + public String createWidgetCatalog(HttpServletRequest request) + throws Exception { + + if (StringUtils.isNotBlank(SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG)) + && SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG) + .equalsIgnoreCase("false")) { + return UNAUTHORIZED_OR_FORBIDDEN_FOR_A_DISABLED_USER; + } + + MultipartHttpServletRequest mRequest; + MultiValueMap<String, Object> multipartRequest = new LinkedMultiValueMap<>(); + String fileName; + String tmpFolderName = "/tmp/"; + String respond = null; + FileOutputStream fo = null; + try { + mRequest = (MultipartHttpServletRequest) request; + MultipartFile mFile = mRequest.getFile("file"); + fileName = mFile.getOriginalFilename(); + fo = new FileOutputStream(tmpFolderName + fileName); + fo.write(mFile.getBytes()); + // silence sonar scan by calling close here + fo.close(); + fo = null; + + HttpHeaders header = new HttpHeaders(); + header.setContentType(MediaType.MULTIPART_FORM_DATA); + multipartRequest.add("file", new FileSystemResource(tmpFolderName + fileName)); + multipartRequest.add("widget", request.getParameter("newWidget")); + + respond = template.postForObject( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog", + new HttpEntity<>(multipartRequest, WidgetServiceHeaders.getInstance()), String.class); + File f = new File(tmpFolderName + fileName); + f.delete(); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed", e); + } finally { + try { + if (fo != null) { + fo.close(); + } + } catch (IOException e) { + logger.error(EELFLoggerDelegate.errorLogger, "createWidgetCatalog failed 2", e); + } + } + return respond; + } + + @GetMapping(value = "/portalApi/microservices/{widgetId}/framework.js") + public String getWidgetFramework(@PathVariable("widgetId") long widgetId) throws Exception { + return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/" + widgetId + "/framework.js", String.class, + WidgetServiceHeaders.getInstance()); + } + + @GetMapping(value = "/portalApi/microservices/{widgetId}/controller.js") + public String getWidgetController(@PathVariable("widgetId") long widgetId) throws Exception { + return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/" + widgetId + "/controller.js", String.class, + WidgetServiceHeaders.getInstance()); + } + + @GetMapping(value = "/portalApi/microservices/{widgetId}/style.css") + public String getWidgetCSS(@PathVariable("widgetId") long widgetId) throws Exception { + return template.getForObject(EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/" + widgetId + "/styles.css", String.class, + WidgetServiceHeaders.getInstance()); + } + + @GetMapping(value = {"/portalApi/microservices/parameters/{widgetId}"}) + public PortalRestResponse<List<WidgetParameterResult>> getWidgetParameterResult(Principal principal, HttpServletRequest request, + @PathVariable("widgetId") long widgetId) throws Exception { + FnUser user = fnUserService.loadUserByUsername(principal.getName()); + + List<WidgetParameterResult> list = new ArrayList<>(); + Long serviceId = template.exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/widgetCatalog/parameters/" + widgetId, + HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), Long.class).getBody(); + if (serviceId == null) { + // return ok/sucess and no service parameter for this widget + return new PortalRestResponse<>(PortalRestStatusEnum.WARN, + "No service parameters for this widget", list); + } else { + List<MicroserviceParameter> defaultParam = epMicroserviceParameterService.getParametersById(serviceId); + for (MicroserviceParameter param : defaultParam) { + WidgetParameterResult userResult = new WidgetParameterResult(); + userResult.setParamId(param.getId()); + userResult.setDefaultValue(param.getPara_value()); + userResult.setParamKey(param.getPara_key()); + WidgetCatalogParameter userValue = epWidgetCatalogParameterService + .getUserParamById(widgetId, user.getId(), + param.getId()); + if (userValue == null) { + userResult.setUserValue(param.getPara_value()); + } else { + userResult.setUserValue(userValue.getUserValue()); + } + list.add(userResult); + } + } + return new PortalRestResponse<List<WidgetParameterResult>>(PortalRestStatusEnum.OK, "SUCCESS", list); + } + + @GetMapping(value = {"/portalApi/microservices/services/{paramId}"}) + public List<WidgetCatalogParameter> getUserParameterById(@PathVariable("paramId") long paramId) { + return epWidgetCatalogParameterService.getUserParameterById(paramId); + } + + @DeleteMapping(value = {"/portalApi/microservices/services/{paramId}"}) + public void deleteUserParameterById(@PathVariable("paramId") long paramId) { + epWidgetCatalogParameterService.deleteUserParameterById(paramId); + } + + @GetMapping(value = {"/portalApi/microservices/download/{widgetId}"}) + public void doDownload(HttpServletRequest request, HttpServletResponse response, + @PathVariable("widgetId") long widgetId) throws Exception { + + ServletContext context = request.getServletContext(); + byte[] byteFile = template + .exchange( + EcompPortalUtils.widgetMsProtocol() + "://" + + widgetMService.getServiceLocation(whatService, + SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_LOCAL_PORT)) + + "/widget/microservices/download/" + widgetId, + HttpMethod.GET, new HttpEntity<>(WidgetServiceHeaders.getInstance()), byte[].class) + .getBody(); + + File downloadFile = File.createTempFile("temp", ".zip"); + try (FileOutputStream stream = new FileOutputStream(downloadFile.getPath())) { + stream.write(byteFile); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e); + throw e; + } + + try (FileInputStream inputStream = new FileInputStream(downloadFile); + OutputStream outStream = response.getOutputStream()) { + String mimeType = context.getMimeType(downloadFile.getPath()); + if (mimeType == null) { + mimeType = "application/octet-stream"; + } + + response.setContentType(mimeType); + response.setContentLength((int) downloadFile.length()); + String headerKey = "Content-Disposition"; + String headerValue = String.format("attachment; filename=\"%s\"", downloadFile.getName()); + downloadFile.delete(); + response.setHeader(headerKey, headerValue); + + byte[] buffer = new byte[32 * 1024]; + int bytesRead; + while ((bytesRead = inputStream.read(buffer)) != -1) { + outStream.write(buffer, 0, bytesRead); + } + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "doDownload failed", e); + throw e; + } + } + + @PostMapping(value = {"/portalApi/microservices/parameters"}) + public PortalRestResponse<String> saveWidgetParameter(Principal principal, HttpServletRequest request, + @RequestBody WidgetCatalogParameter widgetParameters) { + FnUser user = fnUserService.loadUserByUsername(principal.getName()); + widgetParameters.setUserId(user.getId()); + try { + WidgetCatalogParameter oldParam = epWidgetCatalogParameterService + .getUserParamById(widgetParameters.getWidgetId(), + widgetParameters.getUserId(), widgetParameters.getParamId()); + if (oldParam != null) { + widgetParameters.setId(oldParam.getId()); + } + epWidgetCatalogParameterService.saveUserParameter(widgetParameters); + + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "saveWidgetParameter failed", e); + return new PortalRestResponse<String>(PortalRestStatusEnum.ERROR, "FAILURE", e.getMessage()); + } + return new PortalRestResponse<>(PortalRestStatusEnum.OK, "SUCCESS", ""); + } + + @GetMapping(value = {"/portalApi/microservices/uploadFlag"}) + public String getUploadFlag() { + String uplaodFlag = ""; + try { + uplaodFlag = SystemProperties.getProperty(EPCommonSystemProperties.MS_WIDGET_UPLOAD_FLAG); + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "uploadFlag failed", e); + return null; + } + return uplaodFlag; + } + +} diff --git a/portal-BE/src/main/java/org/onap/portal/dao/ep/EpMicroserviceParameterDao.java b/portal-BE/src/main/java/org/onap/portal/dao/ep/EpMicroserviceParameterDao.java new file mode 100644 index 00000000..4bcca7ef --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/dao/ep/EpMicroserviceParameterDao.java @@ -0,0 +1,61 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ + +package org.onap.portal.dao.ep; + +import java.util.List; +import org.onap.portal.domain.db.ep.EpMicroserviceParameter; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +@Transactional +public interface EpMicroserviceParameterDao extends JpaRepository<EpMicroserviceParameter, Long> { + + @Query + void deleteByServiceId(@Param("SERVICEID") Long userId); + @Query + void deleteMicroserviceParameterById(@Param("PARAMID") Long userId); + @Query + List<EpMicroserviceParameter> getParametersById(@Param("SERVICEID") long serviceId); +} diff --git a/portal-BE/src/main/java/org/onap/portal/dao/ep/EpWidgetCatalogParameterDao.java b/portal-BE/src/main/java/org/onap/portal/dao/ep/EpWidgetCatalogParameterDao.java new file mode 100644 index 00000000..04c12324 --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/dao/ep/EpWidgetCatalogParameterDao.java @@ -0,0 +1,25 @@ +package org.onap.portal.dao.ep; + +import java.util.List; +import java.util.Optional; +import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; +import org.springframework.stereotype.Repository; +import org.springframework.transaction.annotation.Transactional; + +@Repository +@Transactional +public interface EpWidgetCatalogParameterDao extends JpaRepository<EpWidgetCatalogParameter, Long> { + + @Query + Optional<List<EpWidgetCatalogParameter>> retrieveByParamId(@Param("PARAMID") Long paramId); + + @Query + void deleteWidgetCatalogParameter(@Param("PARAMID") Long paramId); + + @Query + Optional<List<EpWidgetCatalogParameter>> getUserParamById(@Param("WIDGETID") Long widgetId, + @Param("USERID") Long userId, @Param("PARAMID") Long paramId); +} diff --git a/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpMicroserviceParameter.java b/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpMicroserviceParameter.java index 1c73d962..554dd7b5 100644 --- a/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpMicroserviceParameter.java +++ b/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpMicroserviceParameter.java @@ -79,7 +79,14 @@ CREATE TABLE `ep_microservice_parameter` ( @NamedQueries({ @NamedQuery( name = "EpMicroserviceParameter.deleteByServiceId", - query = "FROM EpMicroserviceParameter WHERE service_id =:serviceId") + query = "DELETE FROM EpMicroserviceParameter WHERE service_id =:SERVICEID"), + @NamedQuery( + name = "EpMicroserviceParameter.getParametersById", + query = "FROM EpMicroserviceParameter WHERE service_id =:SERVICEID"), + @NamedQuery( + name = "EpMicroserviceParameter.deleteMicroserviceParameterById", + query = "DELETE FROM EpMicroserviceParameter WHERE id =:PARAMID" + ) }) @Table(name = "ep_microservice_parameter", indexes = { diff --git a/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpWidgetCatalogParameter.java b/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpWidgetCatalogParameter.java index 3e6e1c6c..2ac07cc3 100644 --- a/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpWidgetCatalogParameter.java +++ b/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpWidgetCatalogParameter.java @@ -51,6 +51,8 @@ import javax.persistence.Id; import javax.persistence.Index; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.validation.Valid; import javax.validation.constraints.Digits; @@ -80,6 +82,19 @@ CREATE TABLE `ep_widget_catalog_parameter` ( ) */ +@NamedQueries({ + @NamedQuery( + name = "EpWidgetCatalogParameter.retrieveByParamId", + query = "FROM EpWidgetCatalogParameter WHERE paramId = :PARAMID"), + @NamedQuery( + name = "EpWidgetCatalogParameter.deleteWidgetCatalogParameter", + query = "DELETE FROM EpWidgetCatalogParameter WHERE paramId = :PARAMID"), + @NamedQuery( + name = "EpWidgetCatalogParameter.getUserParamById", + query = "FROM EpWidgetCatalogParameter WHERE paramId = :PARAMID and userId = :USERID and widgetId = :WIDGETID" + ) +}) + @Table(name = "ep_widget_catalog_parameter", indexes = { @Index(name = "EP_FN_USER_WIDGET_PARAMETER_FK", columnList = "user_id"), @Index(name = "EP_WIDGET_CATALOG_WIDGET_PARAMETER_FK", columnList = "widget_id"), @@ -91,11 +106,12 @@ CREATE TABLE `ep_widget_catalog_parameter` ( @Setter @Entity public class EpWidgetCatalogParameter implements Serializable { + @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", length = 11, nullable = false, columnDefinition = "int(11) AUTO_INCREMENT") @Digits(integer = 11, fraction = 0) - private Integer id; + private Long id; @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "widget_id", nullable = false) @NotNull diff --git a/portal-BE/src/main/java/org/onap/portal/domain/dto/ecomp/WidgetCatalogParameter.java b/portal-BE/src/main/java/org/onap/portal/domain/dto/ecomp/WidgetCatalogParameter.java index cd1b2a7b..e5e7255b 100644 --- a/portal-BE/src/main/java/org/onap/portal/domain/dto/ecomp/WidgetCatalogParameter.java +++ b/portal-BE/src/main/java/org/onap/portal/domain/dto/ecomp/WidgetCatalogParameter.java @@ -58,6 +58,6 @@ public class WidgetCatalogParameter extends DomainVo{ private Long widgetId; private Long userId; private Long paramId; - private String user_value; + private String userValue; } diff --git a/portal-BE/src/main/java/org/onap/portal/service/ep/EpMicroserviceParameterService.java b/portal-BE/src/main/java/org/onap/portal/service/ep/EpMicroserviceParameterService.java new file mode 100644 index 00000000..2544ae6a --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/service/ep/EpMicroserviceParameterService.java @@ -0,0 +1,87 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ + +package org.onap.portal.service.ep; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.hibernate.criterion.Criterion; +import org.hibernate.criterion.Restrictions; +import org.onap.portal.dao.ep.EpMicroserviceParameterDao; +import org.onap.portal.domain.db.ep.EpMicroserviceParameter; +import org.onap.portal.domain.dto.ecomp.MicroserviceParameter; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class EpMicroserviceParameterService { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EpWidgetCatalogParameterService.class); + + private final EpMicroserviceParameterDao epMicroserviceParameterDao; + + @Autowired + public EpMicroserviceParameterService( + final EpMicroserviceParameterDao epMicroserviceParameterDao) { + this.epMicroserviceParameterDao = epMicroserviceParameterDao; + } + + public List<MicroserviceParameter> getParametersById(long serviceId) { + List<Criterion> restrictionsList = new ArrayList<>(); + Criterion contextIdCrit = Restrictions.eq("serviceId", serviceId); + restrictionsList.add(contextIdCrit); + List<MicroserviceParameter> list = mapToMicroserviceParameterList(epMicroserviceParameterDao.getParametersById(serviceId)); + logger.debug(EELFLoggerDelegate.debugLogger, + "getParametersById: microservice parameters list size: " + list.size()); + return list; + } + + private MicroserviceParameter epWidgetCatalogParameterToMicroserviceParameter( + final EpMicroserviceParameter microservice) { + return new MicroserviceParameter(microservice.getId(), microservice.getServiceId().getId(), + microservice.getParaKey(), microservice.getParaValue()); + } + + private List<MicroserviceParameter> mapToMicroserviceParameterList(final List<EpMicroserviceParameter> list){ + return list.stream().map(this::epWidgetCatalogParameterToMicroserviceParameter).collect(Collectors.toList()); + } +} diff --git a/portal-BE/src/main/java/org/onap/portal/service/ep/EpWidgetCatalogParameterService.java b/portal-BE/src/main/java/org/onap/portal/service/ep/EpWidgetCatalogParameterService.java new file mode 100644 index 00000000..8488e5ad --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/service/ep/EpWidgetCatalogParameterService.java @@ -0,0 +1,127 @@ +/* + * ============LICENSE_START========================================== + * ONAP Portal + * =================================================================== + * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved. + * =================================================================== + * Modifications Copyright (c) 2019 Samsung + * =================================================================== + * + * Unless otherwise specified, all software contained herein is licensed + * under the Apache License, Version 2.0 (the "License"); + * you may not use this software except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Unless otherwise specified, all documentation contained herein is licensed + * under the Creative Commons License, Attribution 4.0 Intl. (the "License"); + * you may not use this documentation except in compliance with the License. + * You may obtain a copy of the License at + * + * https://creativecommons.org/licenses/by/4.0/ + * + * Unless required by applicable law or agreed to in writing, documentation + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * ============LICENSE_END============================================ + * + * + */ + +package org.onap.portal.service.ep; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.onap.portal.dao.ep.EpMicroserviceParameterDao; +import org.onap.portal.dao.ep.EpWidgetCatalogParameterDao; +import org.onap.portal.dao.fn.EpWidgetCatalogDao; +import org.onap.portal.dao.fn.FnUserDao; +import org.onap.portal.domain.db.ep.EpWidgetCatalogParameter; +import org.onap.portal.domain.dto.ecomp.WidgetCatalogParameter; +import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class EpWidgetCatalogParameterService { + + EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EpWidgetCatalogParameterService.class); + + private final EpWidgetCatalogParameterDao epWidgetCatalogParameterDao; + private final EpMicroserviceParameterDao epMicroserviceParameterDao; + private final EpWidgetCatalogDao epWidgetCatalogDao; + private final FnUserDao fnUserDao; + + @Autowired + public EpWidgetCatalogParameterService( + final EpWidgetCatalogParameterDao epWidgetCatalogParameterDao, + final EpMicroserviceParameterDao epMicroserviceParameterDao, + EpWidgetCatalogDao epWidgetCatalogDao, FnUserDao fnUserDao) { + this.epWidgetCatalogParameterDao = epWidgetCatalogParameterDao; + this.epMicroserviceParameterDao = epMicroserviceParameterDao; + this.epWidgetCatalogDao = epWidgetCatalogDao; + this.fnUserDao = fnUserDao; + } + + public List<WidgetCatalogParameter> getUserParameterById(Long paramId) { + return mapEpWidgetListToWidgetList( + epWidgetCatalogParameterDao.retrieveByParamId(paramId).orElse(new ArrayList<>())); + } + + public void deleteUserParameterById(Long paramId) { + epWidgetCatalogParameterDao.deleteWidgetCatalogParameter(paramId); + epMicroserviceParameterDao.deleteMicroserviceParameterById(paramId); + } + + public WidgetCatalogParameter getUserParamById(Long widgetId, Long userId, Long paramId) { + WidgetCatalogParameter widgetParam = null; + List<WidgetCatalogParameter> list = mapEpWidgetListToWidgetList( + epWidgetCatalogParameterDao.getUserParamById(widgetId, userId, paramId) + .orElse(new ArrayList<>())); + if (list.size() != 0) { + widgetParam = list.get(0); + } + logger.debug(EELFLoggerDelegate.debugLogger, + "getUserParamById: widget parameters: " + widgetParam); + return widgetParam; + } + + public void saveUserParameter(WidgetCatalogParameter newParameter) { + epWidgetCatalogParameterDao.saveAndFlush(mapToEpWidgetCatalogParameter(newParameter)); + } + + private EpWidgetCatalogParameter mapToEpWidgetCatalogParameter(WidgetCatalogParameter wcp){ + + return new EpWidgetCatalogParameter(wcp.getId(), epWidgetCatalogDao.findById(wcp.getWidgetId()).get(), + fnUserDao.findById(wcp.getUserId()).get(), + epMicroserviceParameterDao.findById(wcp.getParamId()).get(), + wcp.getUserValue()); + } + + private List<EpWidgetCatalogParameter> mapToList(List<WidgetCatalogParameter> list){ + return list.stream().map(this::mapToEpWidgetCatalogParameter).collect(Collectors.toList()); + } + + private WidgetCatalogParameter mapEpWidgetCatalogParametertoWidgetCatalogParameter( + EpWidgetCatalogParameter ewcp) { + return new WidgetCatalogParameter(ewcp.getId(), ewcp.getWidgetId().getWidgetId(), + ewcp.getUserId().getId(), ewcp.getParamId().getId(), ewcp.getUserValue()); + } + + private List<WidgetCatalogParameter> mapEpWidgetListToWidgetList( + List<EpWidgetCatalogParameter> epWidgetCatalogParameters) { + return epWidgetCatalogParameters.stream().map(this::mapEpWidgetCatalogParametertoWidgetCatalogParameter) + .collect(Collectors.toList()); + } +} |