|
@@ -0,0 +1,201 @@
|
|
|
|
|
+package it.pcdev.dokskan.central.service.impl.strategy.docupload;
|
|
|
|
|
+
|
|
|
|
|
+import it.pcdev.dokskan.central.dto.DocumentDto;
|
|
|
|
|
+import it.pcdev.dokskan.central.dto.DocumentShareDTO;
|
|
|
|
|
+import it.pcdev.dokskan.central.dto.OwnershipDTO;
|
|
|
|
|
+import it.pcdev.dokskan.central.exception.SharePermissionException;
|
|
|
|
|
+import it.pcdev.dokskan.central.mapper.DocumentMapper;
|
|
|
|
|
+import it.pcdev.dokskan.central.model.UserDocumentCrossModel;
|
|
|
|
|
+import it.pcdev.dokskan.central.model.UserModel;
|
|
|
|
|
+import it.pcdev.dokskan.central.repository.*;
|
|
|
|
|
+import it.pcdev.dokskan.central.service.IConfDocumentCategoryService;
|
|
|
|
|
+import jakarta.enterprise.context.ApplicationScoped;
|
|
|
|
|
+import jakarta.inject.Inject;
|
|
|
|
|
+import jakarta.persistence.PersistenceException;
|
|
|
|
|
+import jakarta.transaction.Transactional;
|
|
|
|
|
+import org.slf4j.Logger;
|
|
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.ArrayList;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.Map;
|
|
|
|
|
+import java.util.Optional;
|
|
|
|
|
+import java.util.Set;
|
|
|
|
|
+import java.util.function.Function;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+@ApplicationScoped
|
|
|
|
|
+public class DocumentUploadServiceCommon {
|
|
|
|
|
+
|
|
|
|
|
+ private static final Logger log = LoggerFactory.getLogger(DocumentUploadServiceCommon.class);
|
|
|
|
|
+ protected DocumentMapper documentMapper = DocumentMapper.INSTANCE;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ protected DocumentCategoryCrossRepository documentCategoryCrossRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ protected ConfDocumentCategoryRepository confDocumentCategoryRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ protected DocumentRepository documentRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ protected UserRepository userRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ protected UserDocumentCrossRepository userDocumentCrossRepository;
|
|
|
|
|
+
|
|
|
|
|
+ @Inject
|
|
|
|
|
+ protected IConfDocumentCategoryService IConfDocumentCategoryService;
|
|
|
|
|
+
|
|
|
|
|
+ public List<DocumentDto> getDocumentsByUser(String username) {
|
|
|
|
|
+ log.info("Getting documents by user {}", username);
|
|
|
|
|
+ return userDocumentCrossRepository.getUserAssociationByUser(username)
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .map(el -> {
|
|
|
|
|
+ return documentMapper.toDto(el.getDocument(), documentCategoryCrossRepository);
|
|
|
|
|
+ })
|
|
|
|
|
+ .toList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void deleteDocument(Integer documentId) {
|
|
|
|
|
+ log.info("Deleting document: {}", documentId);
|
|
|
|
|
+ documentRepository.deleteById(documentId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void disassociateUserOrDeleteIfOwner(Integer documentId, String username) {
|
|
|
|
|
+
|
|
|
|
|
+ log.info("Disassociate user {} from document {}", username, documentId);
|
|
|
|
|
+
|
|
|
|
|
+ log.info("Checking if {} is the owner of document {}", username, documentId);
|
|
|
|
|
+
|
|
|
|
|
+ final Boolean isOwner = this.isOwner(documentId, username).isOwner();
|
|
|
|
|
+
|
|
|
|
|
+ if (isOwner == null)
|
|
|
|
|
+ throw new PersistenceException();
|
|
|
|
|
+
|
|
|
|
|
+ log.info("{} {} the owner of document {}", username, isOwner ? "is" : "is not", documentId);
|
|
|
|
|
+
|
|
|
|
|
+ if (isOwner)
|
|
|
|
|
+ this.deleteDocumentAndAllCrosses(documentId);
|
|
|
|
|
+ else
|
|
|
|
|
+ userDocumentCrossRepository.findByUsernameAndDocumentId(documentId, username)
|
|
|
|
|
+ .ifPresent(document -> userDocumentCrossRepository.delete(document));
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void deleteDocumentAndAllCrosses(Integer documentId) {
|
|
|
|
|
+
|
|
|
|
|
+ documentCategoryCrossRepository.findByDocumentId(documentId)
|
|
|
|
|
+ .forEach(documentCategoryCrossRepository::delete);
|
|
|
|
|
+
|
|
|
|
|
+ userDocumentCrossRepository.findByDocumentId(documentId)
|
|
|
|
|
+ .forEach(userDocumentCrossRepository::delete);
|
|
|
|
|
+
|
|
|
|
|
+ documentCategoryCrossRepository.flush();
|
|
|
|
|
+ userDocumentCrossRepository.flush();
|
|
|
|
|
+
|
|
|
|
|
+ this.deleteDocument(documentId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void deleteDocumentDataByUser(String username) {
|
|
|
|
|
+ log.info("Deleting document data by username {}", username);
|
|
|
|
|
+
|
|
|
|
|
+ List<UserDocumentCrossModel> documentCross = userDocumentCrossRepository.getUserAssociationByUser(username);
|
|
|
|
|
+
|
|
|
|
|
+ List<Integer> documentIds = new ArrayList<>();
|
|
|
|
|
+
|
|
|
|
|
+ documentCross
|
|
|
|
|
+ .stream()
|
|
|
|
|
+ .map(UserDocumentCrossModel::getDocument)
|
|
|
|
|
+ .forEach(document -> {
|
|
|
|
|
+ documentCategoryCrossRepository.findByDocumentId(document.getId())
|
|
|
|
|
+ .forEach(documentCategoryCrossRepository::delete);
|
|
|
|
|
+ documentIds.add(document.getId());
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ documentCross.forEach(userDocumentCrossRepository::delete);
|
|
|
|
|
+ documentIds.forEach(this::deleteDocument);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public OwnershipDTO isOwner(Integer documentId, String username) {
|
|
|
|
|
+
|
|
|
|
|
+ return OwnershipDTO
|
|
|
|
|
+ .builder()
|
|
|
|
|
+ .withIsOwner(userDocumentCrossRepository.findByUsernameAndDocumentId(documentId,
|
|
|
|
|
+ username)
|
|
|
|
|
+ .map(UserDocumentCrossModel::getOwnerAssociation)
|
|
|
|
|
+ .orElse(null))
|
|
|
|
|
+ .withOwnerName(userDocumentCrossRepository.findOwnerTupleByDocumentId(documentId)
|
|
|
|
|
+ .map(el -> el.getUser().getUsername())
|
|
|
|
|
+ .orElse(null))
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ public List<DocumentShareDTO> getDocumentShareDetails(Integer documentId, String actualUsername) {
|
|
|
|
|
+ List<UserDocumentCrossModel> udcm = userDocumentCrossRepository.findUsersAccessReadOnly(documentId);
|
|
|
|
|
+
|
|
|
|
|
+ Set<UserModel> users = userRepository.findAllExcludeExceptInput(actualUsername);
|
|
|
|
|
+
|
|
|
|
|
+ Map<UserModel, UserDocumentCrossModel> crossModelMap = udcm.stream()
|
|
|
|
|
+ .collect(Collectors.toMap(
|
|
|
|
|
+ UserDocumentCrossModel::getUser,
|
|
|
|
|
+ Function.identity()));
|
|
|
|
|
+
|
|
|
|
|
+ return users.stream()
|
|
|
|
|
+ .map(user -> {
|
|
|
|
|
+ UserDocumentCrossModel existingCross = crossModelMap.get(user);
|
|
|
|
|
+ boolean active = existingCross != null;
|
|
|
|
|
+
|
|
|
|
|
+ UserDocumentCrossModel crossModel = active
|
|
|
|
|
+ ? existingCross
|
|
|
|
|
+ : new UserDocumentCrossModel.builder()
|
|
|
|
|
+ .user(user)
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ return DocumentShareDTO.mapToDocumentShareDTO(crossModel, active);
|
|
|
|
|
+ })
|
|
|
|
|
+ .toList();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Transactional
|
|
|
|
|
+ public void saveOrUpdateShareInfo(List<DocumentShareDTO> dataInfo, Integer documentId, String requestUser) {
|
|
|
|
|
+
|
|
|
|
|
+ if (!this.isOwner(documentId, requestUser).isOwner())
|
|
|
|
|
+ throw new SharePermissionException();
|
|
|
|
|
+
|
|
|
|
|
+ List<DocumentShareDTO> actualDatabaseInformation = this.getDocumentShareDetails(documentId, requestUser);
|
|
|
|
|
+ dataInfo.stream()
|
|
|
|
|
+ .filter(data -> actualDatabaseInformation.contains(data) && !data.isEnabled())
|
|
|
|
|
+ .forEach(data -> {
|
|
|
|
|
+
|
|
|
|
|
+ Optional<UserDocumentCrossModel> udcm = userDocumentCrossRepository
|
|
|
|
|
+ .findByUsernameAndDocumentId(data.getDocumentId(), data.getUsername());
|
|
|
|
|
+
|
|
|
|
|
+ if (udcm.isPresent()) {
|
|
|
|
|
+ userDocumentCrossRepository.delete(udcm.get());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ dataInfo.stream()
|
|
|
|
|
+ .filter(data -> !actualDatabaseInformation.contains(data) && data.isEnabled())
|
|
|
|
|
+ .forEach(data -> {
|
|
|
|
|
+
|
|
|
|
|
+ UserDocumentCrossModel toSave = new UserDocumentCrossModel.builder()
|
|
|
|
|
+ .document(documentRepository.findById(documentId))
|
|
|
|
|
+ .user(userRepository.findById(data.getUsername()))
|
|
|
|
|
+ .build();
|
|
|
|
|
+
|
|
|
|
|
+ userDocumentCrossRepository.persist(toSave);
|
|
|
|
|
+
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|