ResourceDao.java

  1. /*
  2.  * The coLAB project
  3.  * Copyright (C) 2021-2023 AlbaSim, MEI, HEIG-VD, HES-SO
  4.  *
  5.  * Licensed under the MIT License
  6.  */
  7. package ch.colabproject.colab.api.persistence.jpa.document;

  8. import ch.colabproject.colab.api.exceptions.ColabMergeException;
  9. import ch.colabproject.colab.api.model.document.AbstractResource;
  10. import ch.colabproject.colab.api.model.document.Resource;
  11. import ch.colabproject.colab.api.model.document.ResourceRef;
  12. import java.util.List;
  13. import java.util.stream.Collectors;
  14. import javax.ejb.LocalBean;
  15. import javax.ejb.Stateless;
  16. import javax.persistence.EntityManager;
  17. import javax.persistence.PersistenceContext;
  18. import javax.persistence.TypedQuery;
  19. import org.slf4j.Logger;
  20. import org.slf4j.LoggerFactory;

  21. /**
  22.  * Resource and resource reference persistence
  23.  * <p>
  24.  * Note : Most of database operations are handled by managed entities and cascade.
  25.  *
  26.  * @author sandra
  27.  */
  28. @Stateless
  29. @LocalBean
  30. public class ResourceDao {

  31.     /** logger */
  32.     private static final Logger logger = LoggerFactory.getLogger(ResourceDao.class);

  33.     /**
  34.      * Access to the persistence unit
  35.      */
  36.     @PersistenceContext(unitName = "COLAB_PU")
  37.     private EntityManager em;

  38. //    /**
  39. //     * Find a resource by id
  40. //     *
  41. //     * @param id the id of the resource to fetch
  42. //     *
  43. //     * @return the resource with the given id or null if such a resource does not exist
  44. //     */
  45. //    private Resource findResource(Long id) {
  46. //        logger.trace("find resource #{}", id);
  47. //
  48. //        return em.find(Resource.class, id);
  49. //    }

  50. //    /**
  51. //     * Find a resource reference by id
  52. //     *
  53. //     * @param id the id of the resource reference to fetch
  54. //     *
  55. //     * @return the resource reference with the given id or null if such a resource reference does
  56. //     *         not exist
  57. //     */
  58. //    private ResourceRef findResourceRef(Long id) {
  59. //        logger.trace("find resource reference #{}", id);
  60. //
  61. //        return em.find(ResourceRef.class, id);
  62. //    }

  63.     /**
  64.      * @param id the id of the resource / resource reference to fetch
  65.      *
  66.      * @return the resource / resource reference with the given id or null if such a resource /
  67.      *         resource reference does not exist
  68.      */
  69.     public AbstractResource findResourceOrRef(Long id) {
  70.         logger.trace("find abstract resource #{}", id);

  71.         return em.find(AbstractResource.class, id);
  72.     }

  73.     /**
  74.      * Retrieve the resources references that have the given target
  75.      *
  76.      * @param target the target
  77.      *
  78.      * @return the matching references
  79.      */
  80.     public List<ResourceRef> findDirectReferences(AbstractResource target) {
  81.         logger.trace("find the direct references of the target {}", target);

  82.         TypedQuery<ResourceRef> query = em.createNamedQuery("ResourceRef.findDirectReferences",
  83.             ResourceRef.class);

  84.         query.setParameter("targetId", target.getId());

  85.         return query.getResultList();
  86.     }

  87.     /**
  88.      * Retrieve all resources which transitively references the given target
  89.      *
  90.      * @param target the target
  91.      *
  92.      * @return the matching references
  93.      */
  94.     public List<ResourceRef> findAllReferences(AbstractResource target) {
  95.         logger.trace("find all references to the target {}", target);

  96.         // first, fetch direct references
  97.         List<ResourceRef> list = findDirectReferences(target);

  98.         // and recurse
  99.         list.addAll(list.stream().flatMap(res -> {
  100.             return findAllReferences(res).stream();
  101.         }).collect(Collectors.toList()));

  102.         return list;
  103.     }

  104.     /**
  105.      * Update resource. Only fields which are editable by users will be impacted.
  106.      *
  107.      * @param <T>           a sub type of AbstractResource
  108.      * @param resourceOrRef the resource as supplied by clients (ie not managed by JPA)
  109.      *
  110.      * @return return updated managed resource
  111.      *
  112.      * @throws ColabMergeException if the update failed
  113.      */
  114.     public <T extends AbstractResource> T updateResourceOrRef(T resourceOrRef)
  115.         throws ColabMergeException {
  116.         logger.trace("update resource or ref {}", resourceOrRef);

  117.         @SuppressWarnings("unchecked")
  118.         T managedResource = (T) this.findResourceOrRef(resourceOrRef.getId());

  119.         managedResource.mergeToUpdate(resourceOrRef);

  120.         return managedResource;
  121.     }

  122.     /**
  123.      * Persist a brand new resource to database
  124.      *
  125.      * @param resource the new resource to persist
  126.      *
  127.      * @return the new persisted and managed resource
  128.      */
  129.     public Resource persistResource(Resource resource) {
  130.         logger.trace("persist resource {}", resource);

  131.         em.persist(resource);

  132.         return resource;
  133.     }

  134.     // Note : there is no persistResourceRef because they are automatically generated
  135.     // and are persisted by cascade

  136.     /**
  137.      * Delete the resource / resource reference from database. This can't be undone
  138.      *
  139.      * @param resourceOrRef the resource / resource reference to delete
  140.      */
  141.     public void deleteResourceOrRef(AbstractResource resourceOrRef) {
  142.         logger.trace("delete abstract resource {}", resourceOrRef);

  143.         // TODO: move to recycle bin first

  144.         em.remove(resourceOrRef);
  145.     }

  146. }