From 54f658724d2f47ed46ad4e179d030e331991a0fe Mon Sep 17 00:00:00 2001 From: Dominik Mizyn Date: Tue, 10 Sep 2019 12:40:45 +0200 Subject: [PATCH] WidgetsController Up WidgetsController putWidgetCatalogSelection Up Issue-ID: PORTAL-710 Change-Id: I76c96d302922e69b113c419f62a4df1bc54a88bc Signed-off-by: Dominik Mizyn --- .../onap/portal/controller/WidgetsController.java | 109 +++++++++++++++++--- .../onap/portal/dao/ep/EpPersUserWidgetSelDao.java | 58 +++++++++++ .../org/onap/portal/dao/fn/EpWidgetCatalogDao.java | 48 +++++++++ .../portal/domain/db/ep/EpPersUserWidgetSel.java | 14 ++- .../onap/portal/service/PersUserWidgetService.java | 111 +++++++++++++++++++++ .../org/onap/portal/service/WidgetService.java | 18 ++++ 6 files changed, 343 insertions(+), 15 deletions(-) create mode 100644 portal-BE/src/main/java/org/onap/portal/dao/ep/EpPersUserWidgetSelDao.java create mode 100644 portal-BE/src/main/java/org/onap/portal/dao/fn/EpWidgetCatalogDao.java create mode 100644 portal-BE/src/main/java/org/onap/portal/service/PersUserWidgetService.java diff --git a/portal-BE/src/main/java/org/onap/portal/controller/WidgetsController.java b/portal-BE/src/main/java/org/onap/portal/controller/WidgetsController.java index e40efe11..63309f32 100644 --- a/portal-BE/src/main/java/org/onap/portal/controller/WidgetsController.java +++ b/portal-BE/src/main/java/org/onap/portal/controller/WidgetsController.java @@ -40,6 +40,7 @@ package org.onap.portal.controller; +import java.io.IOException; import java.security.Principal; import java.util.List; import javax.servlet.http.HttpServletRequest; @@ -47,7 +48,9 @@ import javax.servlet.http.HttpServletResponse; import org.onap.portal.domain.db.fn.FnUser; import org.onap.portal.domain.dto.transport.FieldsValidator; import org.onap.portal.domain.dto.transport.OnboardingWidget; +import org.onap.portal.domain.dto.transport.WidgetCatalogPersonalization; import org.onap.portal.service.AdminRolesService; +import org.onap.portal.service.PersUserWidgetService; import org.onap.portal.service.WidgetService; import org.onap.portal.service.fn.FnUserService; import org.onap.portal.utils.EcompPortalUtils; @@ -66,24 +69,29 @@ import org.springframework.web.bind.annotation.RestController; @Configuration @EnableAspectJAutoProxy public class WidgetsController { + private static final EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(WidgetsController.class); private final FnUserService fnUserService; private final WidgetService widgetService; private final AdminRolesService adminRolesService; private final DataValidator dataValidator; + private final PersUserWidgetService persUserWidgetService; @Autowired - public WidgetsController(FnUserService fnUserService, WidgetService widgetService, - AdminRolesService adminRolesService, DataValidator dataValidator) { + public WidgetsController(final FnUserService fnUserService, final WidgetService widgetService, + final AdminRolesService adminRolesService, final DataValidator dataValidator, + final PersUserWidgetService persUserWidgetService) { this.fnUserService = fnUserService; this.widgetService = widgetService; this.adminRolesService = adminRolesService; this.dataValidator = dataValidator; + this.persUserWidgetService = persUserWidgetService; } - @RequestMapping(value = { "/portalApi/widgets" }, method = RequestMethod.GET, produces = "application/json") - public List getOnboardingWidgets(Principal principal, HttpServletRequest request, HttpServletResponse response) { + @RequestMapping(value = {"/portalApi/widgets"}, method = RequestMethod.GET, produces = "application/json") + public List getOnboardingWidgets(Principal principal, HttpServletRequest request, + HttpServletResponse response) { FnUser user = fnUserService.loadUserByUsername(principal.getName()); List onboardingWidgets = null; @@ -94,24 +102,28 @@ public class WidgetsController { if (!getType.isEmpty() && ("managed".equals(getType) || "all".equals(getType))) { onboardingWidgets = widgetService.getOnboardingWidgets(user, "managed".equals(getType)); } else { - logger.debug(EELFLoggerDelegate.debugLogger, "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'"); + logger.debug(EELFLoggerDelegate.debugLogger, + "WidgetsController.getOnboardingApps - request must contain header 'X-Widgets-Type' with 'all' or 'managed'"); response.setStatus(HttpServletResponse.SC_BAD_REQUEST); } } - EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus()); + EcompPortalUtils + .logAndSerializeObject(logger, "/portalApi/widgets", "GET result =", response.getStatus()); return onboardingWidgets; } - @RequestMapping(value = { "/portalApi/widgets/{widgetId}" }, method = { RequestMethod.PUT }, produces = "application/json") - public FieldsValidator putOnboardingWidget(Principal principal, HttpServletRequest request, @PathVariable("widgetId") Long widgetId, + @RequestMapping(value = {"/portalApi/widgets/{widgetId}"}, method = { + RequestMethod.PUT}, produces = "application/json") + public FieldsValidator putOnboardingWidget(Principal principal, HttpServletRequest request, + @PathVariable("widgetId") Long widgetId, @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) { FnUser user = fnUserService.loadUserByUsername(principal.getName()); FieldsValidator fieldsValidator = null; - if (onboardingWidget!=null){ - if(!dataValidator.isValid(onboardingWidget)){ + if (onboardingWidget != null) { + if (!dataValidator.isValid(onboardingWidget)) { fieldsValidator = new FieldsValidator(); - fieldsValidator.setHttpStatusCode((long)HttpServletResponse.SC_NOT_ACCEPTABLE); + fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE); return fieldsValidator; } } @@ -123,7 +135,8 @@ public class WidgetsController { fieldsValidator = widgetService.setOnboardingWidget(user, onboardingWidget); response.setStatus(fieldsValidator.getHttpStatusCode().intValue()); } - EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =", response.getStatus()); + EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "GET result =", + response.getStatus()); return fieldsValidator; } @@ -135,4 +148,76 @@ public class WidgetsController { } return true; } + + @RequestMapping(value = {"/portalApi/widgets"}, method = {RequestMethod.POST}, produces = "application/json") + public FieldsValidator postOnboardingWidget(Principal principal, HttpServletRequest request, + @RequestBody OnboardingWidget onboardingWidget, HttpServletResponse response) { + FnUser user = fnUserService.loadUserByUsername(principal.getName()); + FieldsValidator fieldsValidator = null; + + if (onboardingWidget != null) { + if (!dataValidator.isValid(onboardingWidget)) { + fieldsValidator = new FieldsValidator(); + fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE); + return fieldsValidator; + } + } + + if (userHasPermissions(user, response, "postOnboardingWidget")) { + onboardingWidget.setId(null); + onboardingWidget.normalize(); + fieldsValidator = widgetService.setOnboardingWidget(user, onboardingWidget); + response.setStatus(fieldsValidator.getHttpStatusCode().intValue()); + } + + EcompPortalUtils + .logAndSerializeObject(logger, "/portalApi/widgets", "POST result =", response.getStatus()); + return fieldsValidator; + } + + @RequestMapping(value = {"/portalApi/widgets/{widgetId}"}, method = { + RequestMethod.DELETE}, produces = "application/json") + public FieldsValidator deleteOnboardingWidget(Principal principal, HttpServletRequest request, + @PathVariable("widgetId") Long widgetId, HttpServletResponse response) { + FnUser user = fnUserService.loadUserByUsername(principal.getName()); + FieldsValidator fieldsValidator = null; + + if (userHasPermissions(user, response, "deleteOnboardingWidget")) { + fieldsValidator = widgetService.deleteOnboardingWidget(user, widgetId); + response.setStatus(fieldsValidator.getHttpStatusCode().intValue()); + } + + EcompPortalUtils.logAndSerializeObject(logger, "/portalApi/widgets/" + widgetId, "DELETE result =", + response.getStatus()); + return fieldsValidator; + } + + @RequestMapping(value = { + "portalApi/widgetCatalogSelection"}, method = RequestMethod.PUT, produces = "application/json") + public FieldsValidator putWidgetCatalogSelection(Principal principal, HttpServletRequest request, + @RequestBody WidgetCatalogPersonalization persRequest, HttpServletResponse response) throws IOException { + FieldsValidator result = new FieldsValidator(); + FnUser user = fnUserService.loadUserByUsername(principal.getName()); + + if (persRequest != null) { + if (!dataValidator.isValid(persRequest)) { + result.setHttpStatusCode((long) HttpServletResponse.SC_NOT_ACCEPTABLE); + return result; + } + } + + try { + if (persRequest.getWidgetId() == null || user == null) { + EcompPortalUtils.setBadPermissions(user, response, "putWidgetCatalogSelection"); + } else { + persUserWidgetService + .setPersUserAppValue(user, persRequest.getWidgetId(), persRequest.getSelect()); + } + } catch (Exception e) { + logger.error(EELFLoggerDelegate.errorLogger, "Failed in putAppCatalogSelection", e); + response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, e.toString()); + } + result.setHttpStatusCode((long) HttpServletResponse.SC_OK); + return result; + } } diff --git a/portal-BE/src/main/java/org/onap/portal/dao/ep/EpPersUserWidgetSelDao.java b/portal-BE/src/main/java/org/onap/portal/dao/ep/EpPersUserWidgetSelDao.java new file mode 100644 index 00000000..899fc1f0 --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/dao/ep/EpPersUserWidgetSelDao.java @@ -0,0 +1,58 @@ +/* + * ============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 java.util.Optional; +import org.onap.portal.domain.db.ep.EpPersUserWidgetSel; +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 EpPersUserWidgetSelDao extends JpaRepository { + + @Query + Optional> getEpPersUserWidgetSelForUserIdAndWidgetId(@Param("USERID") Long userId, @Param("WIDGETID") Long widgetId); +} diff --git a/portal-BE/src/main/java/org/onap/portal/dao/fn/EpWidgetCatalogDao.java b/portal-BE/src/main/java/org/onap/portal/dao/fn/EpWidgetCatalogDao.java new file mode 100644 index 00000000..1e8de2bb --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/dao/fn/EpWidgetCatalogDao.java @@ -0,0 +1,48 @@ +/* + * ============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.fn; + +import org.onap.portal.domain.db.ep.EpWidgetCatalog; +import org.springframework.data.jpa.repository.JpaRepository; + +public interface EpWidgetCatalogDao extends JpaRepository { + +} diff --git a/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpPersUserWidgetSel.java b/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpPersUserWidgetSel.java index a8bc7e9c..9cee72a3 100644 --- a/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpPersUserWidgetSel.java +++ b/portal-BE/src/main/java/org/onap/portal/domain/db/ep/EpPersUserWidgetSel.java @@ -50,6 +50,8 @@ import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.JoinColumn; import javax.persistence.ManyToOne; +import javax.persistence.NamedQueries; +import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.UniqueConstraint; import javax.validation.Valid; @@ -62,6 +64,7 @@ import lombok.NoArgsConstructor; import lombok.Setter; import org.hibernate.validator.constraints.SafeHtml; import org.onap.portal.domain.db.fn.FnUser; +import org.onap.portal.domain.dto.DomainVo; /* CREATE TABLE `ep_pers_user_widget_sel` ( @@ -76,7 +79,11 @@ CREATE TABLE `ep_pers_user_widget_sel` ( CONSTRAINT `fk_2_ep_pers_user_wid_sel_ep_wid` FOREIGN KEY (`widget_id`) REFERENCES `ep_widget_catalog` (`widget_id`) ) */ - +@NamedQueries({ + @NamedQuery( + name = "EpPersUserWidgetSel.getEpPersUserWidgetSelForUserIdAndWidgetId", + query = "FROM EpPersUserWidgetSel WHERE userId = :USERID and widgetId = :WIDGETID") +}) @Table(name = "ep_pers_user_widget_sel", uniqueConstraints = { @UniqueConstraint(columnNames = {"user_id", "widget_id"}) }) @@ -85,13 +92,14 @@ CREATE TABLE `ep_pers_user_widget_sel` ( @Getter @Setter @Entity -public class EpPersUserWidgetSel implements Serializable { +public class EpPersUserWidgetSel extends DomainVo implements Serializable { + @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "id", length = 11, nullable = false) @Digits(integer = 11, fraction = 0) private Long id; - @ManyToOne(fetch = FetchType.LAZY,cascade = CascadeType.ALL) + @ManyToOne(fetch = FetchType.LAZY, cascade = CascadeType.ALL) @JoinColumn(name = "user_id", nullable = false) @NotNull @Valid diff --git a/portal-BE/src/main/java/org/onap/portal/service/PersUserWidgetService.java b/portal-BE/src/main/java/org/onap/portal/service/PersUserWidgetService.java new file mode 100644 index 00000000..364085c4 --- /dev/null +++ b/portal-BE/src/main/java/org/onap/portal/service/PersUserWidgetService.java @@ -0,0 +1,111 @@ +/* + * ============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; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; +import org.onap.portal.dao.ep.EpPersUserWidgetSelDao; +import org.onap.portal.dao.fn.EpWidgetCatalogDao; +import org.onap.portal.domain.db.ep.EpPersUserWidgetSel; +import org.onap.portal.domain.db.fn.FnUser; +import org.onap.portal.domain.dto.ecomp.PersUserWidgetSelection; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +@Service +public class PersUserWidgetService { + + private final EpPersUserWidgetSelDao epPersUserWidgetSelDao; + private final EpWidgetCatalogDao epWidgetCatalogDao; + + @Autowired + public PersUserWidgetService(final EpPersUserWidgetSelDao epPersUserWidgetSelDao, + final EpWidgetCatalogDao epWidgetCatalogDao) { + this.epPersUserWidgetSelDao = epPersUserWidgetSelDao; + this.epWidgetCatalogDao = epWidgetCatalogDao; + } + + public void setPersUserAppValue(FnUser user, Long widgetId, Boolean select) { + if (user == null || widgetId == null) { + throw new IllegalArgumentException("setPersUserAppValue: Null values"); + } + + List persList = getUserWidgetSelction(user, widgetId); + // Key constraint limits to 1 row + PersUserWidgetSelection persRow = null; + if (persList.size() == 1) { + persRow = persList.get(0); + } else { + persRow = new PersUserWidgetSelection(null, user.getId(), widgetId, null); + } + if (select) { + if (persRow.getId() != null) { + epPersUserWidgetSelDao.deleteById(persRow.getId()); + } + persRow.setStatusCode("S"); // show + EpPersUserWidgetSel epPersUserWidgetSel = new EpPersUserWidgetSel(); + epPersUserWidgetSel.setUserId(user); + epPersUserWidgetSel.setWidgetId(epWidgetCatalogDao.findById(widgetId).get()); + epPersUserWidgetSelDao.saveAndFlush(epPersUserWidgetSel); + } else { + if (persRow.getId() != null) { + epPersUserWidgetSelDao.deleteById(persRow.getId()); + } + persRow.setStatusCode("H"); // Hide + EpPersUserWidgetSel epPersUserWidgetSel = new EpPersUserWidgetSel(); + epPersUserWidgetSel.setUserId(user); + epPersUserWidgetSel.setWidgetId(epWidgetCatalogDao.findById(widgetId).get()); + epPersUserWidgetSelDao.saveAndFlush(epPersUserWidgetSel); + } + } + + private List getUserWidgetSelction(FnUser user, Long widgetId) { + return epPersUserWidgetSelDao.getEpPersUserWidgetSelForUserIdAndWidgetId(user.getId(), widgetId) + .orElse(new ArrayList<>()).stream().map( + this::epPersUserWidgetSelToPersUserWidgetSelection).collect(Collectors.toList()); + } + + private PersUserWidgetSelection epPersUserWidgetSelToPersUserWidgetSelection(EpPersUserWidgetSel widgetSel) { + return new PersUserWidgetSelection(widgetSel.getId(), widgetSel.getUserId().getId(), + widgetSel.getWidgetId().getWidgetId(), widgetSel.getStatusCd()); + } +} diff --git a/portal-BE/src/main/java/org/onap/portal/service/WidgetService.java b/portal-BE/src/main/java/org/onap/portal/service/WidgetService.java index 491ce4cc..794f9336 100644 --- a/portal-BE/src/main/java/org/onap/portal/service/WidgetService.java +++ b/portal-BE/src/main/java/org/onap/portal/service/WidgetService.java @@ -244,4 +244,22 @@ public class WidgetService { .getResultList(); } + @Transactional + public FieldsValidator deleteOnboardingWidget(FnUser user, Long onboardingWidgetId) { + FieldsValidator fieldsValidator = new FieldsValidator(); + synchronized (syncRests) { + FnWidget widget = fnWidgetDao.getOne(onboardingWidgetId); + if (widget != null && widget.getAppId() != null) { // widget exists + if (!this.isUserAdminOfAppForWidget(adminRolesService.isSuperAdmin(user), user.getId(), + widget.getAppId())) { + fieldsValidator.setHttpStatusCode((long) HttpServletResponse.SC_FORBIDDEN); + } else { + fnWidgetDao.deleteById(onboardingWidgetId); + fieldsValidator.setHttpStatusCode( + (long) HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } + } + } + return fieldsValidator; + } } -- 2.16.6