|
| 1 | + |
| 2 | +/* Copyright (c) 2018-2025, RTE (http://www.rte-france.com) |
| 3 | + * See AUTHORS.txt |
| 4 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 5 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 6 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 7 | + * SPDX-License-Identifier: MPL-2.0 |
| 8 | + * This file is part of the OperatorFabric project. |
| 9 | + */ |
| 10 | + |
| 11 | +package org.opfab.cards.publication.services; |
| 12 | + |
| 13 | +import org.opfab.cards.publication.model.*; |
| 14 | +import org.opfab.cards.publication.repositories.CardRepository; |
| 15 | +import org.opfab.cards.publication.repositories.UserBasedOperationResult; |
| 16 | +import org.opfab.springtools.error.model.ApiError; |
| 17 | +import org.opfab.springtools.error.model.ApiErrorException; |
| 18 | +import org.opfab.users.model.CurrentUserWithPerimeters; |
| 19 | +import org.springframework.http.HttpStatus; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +public class CardReadAndAckService { |
| 23 | + |
| 24 | + private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(CardReadAndAckService.class); |
| 25 | + |
| 26 | + private CardNotificationService cardNotificationService; |
| 27 | + private CardRepository cardRepository; |
| 28 | + private CardPermissionControlService cardPermissionControlService; |
| 29 | + |
| 30 | + public CardReadAndAckService( |
| 31 | + CardNotificationService cardNotificationService, |
| 32 | + CardRepository cardRepository) { |
| 33 | + this.cardNotificationService = cardNotificationService; |
| 34 | + this.cardRepository = cardRepository; |
| 35 | + this.cardPermissionControlService = new CardPermissionControlService(); |
| 36 | + |
| 37 | + } |
| 38 | + |
| 39 | + public UserBasedOperationResult processUserAcknowledgement(String cardUid, CurrentUserWithPerimeters user, |
| 40 | + List<String> entitiesAcks) { |
| 41 | + if (cardPermissionControlService.isCurrentUserReadOnly(user) && entitiesAcks != null && !entitiesAcks.isEmpty()) |
| 42 | + throw new ApiErrorException( |
| 43 | + new ApiError(HttpStatus.FORBIDDEN, "Acknowledgement impossible : User has READONLY opfab role")); |
| 44 | + |
| 45 | + if (!user.getUserData().getEntities().containsAll(entitiesAcks)) |
| 46 | + throw new ApiErrorException( |
| 47 | + new ApiError(HttpStatus.FORBIDDEN, |
| 48 | + "Acknowledgement impossible : User is not member of all the entities given in the request")); |
| 49 | + |
| 50 | + cardRepository.findByUid(cardUid).ifPresent(selectedCard -> cardNotificationService |
| 51 | + .pushAckOfCardInEventBus(cardUid, selectedCard.id, entitiesAcks, CardOperationTypeEnum.ACK)); |
| 52 | + |
| 53 | + log.info("Set ack on card with uid {} for user {} and entities {}", cardUid, user.getUserData().getLogin(), |
| 54 | + entitiesAcks); |
| 55 | + return cardRepository.addUserAck(user.getUserData(), cardUid, entitiesAcks); |
| 56 | + } |
| 57 | + |
| 58 | + public UserBasedOperationResult processUserRead(String cardUid, String userName) { |
| 59 | + log.info("Set read on card with uid {} for user {} ", cardUid, userName); |
| 60 | + return cardRepository.addUserRead(userName, cardUid); |
| 61 | + } |
| 62 | + |
| 63 | + public UserBasedOperationResult deleteUserRead(String cardUid, String userName) { |
| 64 | + log.info("Delete read on card with uid {} for user {} ", cardUid, userName); |
| 65 | + return cardRepository.deleteUserRead(userName, cardUid); |
| 66 | + } |
| 67 | + |
| 68 | + public UserBasedOperationResult deleteUserAcknowledgement(String cardUid, CurrentUserWithPerimeters user, |
| 69 | + List<String> entitiesAcks) { |
| 70 | + log.info("Delete ack on card with uid {} for user {} and entities {} ", cardUid, user.getUserData().getLogin(), |
| 71 | + entitiesAcks); |
| 72 | + |
| 73 | + if (!user.getUserData().getEntities().containsAll(entitiesAcks)) |
| 74 | + throw new ApiErrorException( |
| 75 | + |
| 76 | + new ApiError(HttpStatus.FORBIDDEN, |
| 77 | + "Cancel acknowledgement impossible : User is not member of all the entities given in the request")); |
| 78 | + |
| 79 | + cardRepository.findByUid(cardUid).ifPresent(selectedCard -> cardNotificationService |
| 80 | + .pushAckOfCardInEventBus(cardUid, selectedCard.id, entitiesAcks, CardOperationTypeEnum.UNACK)); |
| 81 | + |
| 82 | + return cardRepository.deleteUserAck(user.getUserData().getLogin(), cardUid, entitiesAcks); |
| 83 | + |
| 84 | + } |
| 85 | + |
| 86 | + public UserBasedOperationResult resetReadAndAcks(String cardUid) { |
| 87 | + log.info("Delete ack and reads on card with uid {} ", cardUid); |
| 88 | + UserBasedOperationResult acksResult = cardRepository.deleteAcksAndReads(cardUid); |
| 89 | + if (acksResult.isCardFound()) { |
| 90 | + cardRepository.findByUid(cardUid) |
| 91 | + .ifPresent(card -> cardNotificationService.notifyOneCard(card, CardOperationTypeEnum.UPDATE)); |
| 92 | + } |
| 93 | + |
| 94 | + return acksResult; |
| 95 | + } |
| 96 | + |
| 97 | +} |
0 commit comments