CardDao.java

/*
 * The coLAB project
 * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
 *
 * Licensed under the MIT License
 */
package ch.colabproject.colab.api.persistence.jpa.card;

import ch.colabproject.colab.api.exceptions.ColabMergeException;
import ch.colabproject.colab.api.model.card.Card;
import jakarta.ejb.LocalBean;
import jakarta.ejb.Stateless;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import jakarta.persistence.TypedQuery;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import ch.colabproject.colab.api.model.common.DeletionStatus;

import java.time.OffsetDateTime;
import java.util.List;

/**
 * Card persistence
 * <p>
 * Note : Most of database operations are handled by managed entities and cascade.
 *
 * @author sandra
 */
@Stateless
@LocalBean
public class CardDao {

    /** logger */
    private static final Logger logger = LoggerFactory.getLogger(CardDao.class);

    /**
     * Access to the persistence unit
     */
    @PersistenceContext(unitName = "COLAB_PU")
    private EntityManager em;

    /**
     * Find a card by id
     *
     * @param id the id of the card to fetch
     *
     * @return the card with the given id or null if such a card does not exist
     */
    public Card findCard(Long id) {
        logger.trace("find card #{}", id);

        return em.find(Card.class, id);
    }

    /**
     * Get the cards matching the given deletion status for the given number of days
     *
     * @param deletionStatus Deletion status
     * @param nbWaitingDays Number of days since the card was deleted
     *
     * @return List of matching cards
     */
    public List<Card> findOldDeletedCards(DeletionStatus deletionStatus,int nbWaitingDays) {
        logger.trace("find cards to delete for {} days", nbWaitingDays);

        TypedQuery<Card> query = em.createNamedQuery("Card.findOldDeleted", Card.class);

        OffsetDateTime deletionTime = OffsetDateTime.now().minusDays(nbWaitingDays);
        query.setParameter("deletionTime", deletionTime);
        query.setParameter("deletionStatus", deletionStatus);

        return query.getResultList();
    }

    /**
     * Update card. Only fields which are editable by users will be impacted.
     *
     * @param card the card as supplied by clients (ie not managed by JPA)
     *
     * @return return updated managed card
     *
     * @throws ColabMergeException if the update failed
     */
    public Card updateCard(Card card) throws ColabMergeException {
        logger.trace("update card {}", card);

        Card managedCard = this.findCard(card.getId());

        managedCard.mergeToUpdate(card);

        return managedCard;
    }

    /**
     * Persist a brand new card to database
     *
     * @param card the new card to persist
     *
     * @return the new persisted and managed card
     */
    public Card persistCard(Card card) {
        logger.trace("persist card {}", card);

        em.persist(card);

        return card;
    }

    /**
     * Delete the card from database. This can't be undone
     *
     * @param card the card to delete
     */
    public void deleteCard(Card card) {
        logger.trace("delete card {}", card);

        em.remove(card);
    }

}