DocumentRestEndpoint.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.rest.document;

  8. import ch.colabproject.colab.api.controller.document.DocumentManager;
  9. import ch.colabproject.colab.api.exceptions.ColabMergeException;
  10. import ch.colabproject.colab.api.model.document.Document;
  11. import ch.colabproject.colab.api.model.link.StickyNoteLink;
  12. import ch.colabproject.colab.api.persistence.jpa.document.DocumentDao;
  13. import ch.colabproject.colab.generator.model.annotations.AuthenticationRequired;
  14. import java.util.List;
  15. import javax.inject.Inject;
  16. import javax.ws.rs.Consumes;
  17. import javax.ws.rs.GET;
  18. import javax.ws.rs.PUT;
  19. import javax.ws.rs.Path;
  20. import javax.ws.rs.PathParam;
  21. import javax.ws.rs.Produces;
  22. import javax.ws.rs.core.MediaType;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. /**
  26.  * REST document controller
  27.  *
  28.  * @author sandra
  29.  */
  30. @Path("docs")
  31. @Consumes(MediaType.APPLICATION_JSON)
  32. @Produces(MediaType.APPLICATION_JSON)
  33. @AuthenticationRequired
  34. public class DocumentRestEndpoint {

  35.     /** logger */
  36.     private static final Logger logger = LoggerFactory.getLogger(DocumentRestEndpoint.class);

  37.     // *********************************************************************************************
  38.     // injections
  39.     // *********************************************************************************************

  40.     /**
  41.      * The document persistence manager
  42.      */
  43.     @Inject
  44.     private DocumentDao documentDao;

  45.     /**
  46.      * The document specific logic
  47.      */
  48.     @Inject
  49.     private DocumentManager documentManager;

  50.     // *********************************************************************************************
  51.     // CRUD
  52.     // *********************************************************************************************

  53.     /**
  54.      * Get the document identified by the given id
  55.      *
  56.      * @param id id of the document to fetch
  57.      *
  58.      * @return the document or null
  59.      */
  60.     @GET
  61.     @Path("{id: [0-9]+}")
  62.     public Document getDocument(@PathParam("id") Long id) {
  63.         logger.debug("get document #{}", id);
  64.         return documentDao.findDocument(id);
  65.     }

  66.     /**
  67.      * Save changes to database. Only fields which are editable by users will be
  68.      * impacted.
  69.      *
  70.      * @param document document to update
  71.      *
  72.      * @throws ColabMergeException if the merge is impossible
  73.      */
  74.     @PUT
  75.     public void updateDocument(Document document) throws ColabMergeException {
  76.         logger.debug("update document {}", document);
  77.         documentDao.updateDocument(document);
  78.     }

  79.     // *********************************************************************************************
  80.     // change index of a document
  81.     // *********************************************************************************************

  82.     /**
  83.      * Move the given document one floor up
  84.      *
  85.      * @param docId the id of the document to move
  86.      */
  87.     @PUT
  88.     @Path("{docId: [0-9]+}/MoveUp")
  89.     public void moveDocumentUp(@PathParam("docId") Long docId) {
  90.         logger.debug("move document #{} up", docId);

  91.         documentManager.moveDocumentUp(docId);
  92.     }

  93.     /**
  94.      * Move the given document one floor down
  95.      *
  96.      * @param docId the id of the document to move
  97.      */
  98.     @PUT
  99.     @Path("{docId: [0-9]+}/MoveDown")
  100.     public void moveDocumentDown(@PathParam("docId") Long docId) {
  101.         logger.debug("move document #{} down", docId);

  102.         documentManager.moveDocumentDown(docId);
  103.     }

  104.     /**
  105.      * Move the given document above its futureNextDocId
  106.      *
  107.      * @param docId     the id of the document to move
  108.      * @param baseDocId the id of the document which will be below docId
  109.      */
  110.     @PUT
  111.     @Path("{docId: [0-9]+}/MoveAbove/{baseDocId: [0-9]+}")
  112.     public void moveDocumentAbove(@PathParam("docId") Long docId,
  113.             @PathParam("baseDocId") Long baseDocId) {
  114.         logger.debug("move document #{} above #{}", docId, baseDocId);

  115.         documentManager.moveDocumentAbove(docId, baseDocId);
  116.     }

  117.     /**
  118.      * Move the document one floor up
  119.      *
  120.      * @param docId     the id of the document to move
  121.      * @param baseDocId the id of the document which will be above docId
  122.      */
  123.     @PUT
  124.     @Path("{docId: [0-9]+}/MoveBelow/{baseDocId: [0-9]+}")
  125.     public void moveDocumentBelow(@PathParam("docId") Long docId,
  126.             @PathParam("baseDocId") Long baseDocId) {
  127.         logger.debug("move document #{} below #{}", docId, baseDocId);

  128.         documentManager.moveDocumentBelow(docId, baseDocId);
  129.     }

  130.     /**
  131.      * Move the document at the top
  132.      *
  133.      * @param docId the id of the document to move
  134.      */
  135.     @PUT
  136.     @Path("{docId: [0-9]+}/MoveToTop")
  137.     public void moveDocumentToTop(@PathParam("docId") Long docId) {
  138.         logger.debug("move document #{} to the top", docId);

  139.         documentManager.moveDocumentToTop(docId);
  140.     }

  141.     /**
  142.      * Move the document at the bottom
  143.      *
  144.      * @param docId the id of the document to move
  145.      */
  146.     @PUT
  147.     @Path("{docId: [0-9]+}/MoveToBottom")
  148.     public void moveDocumentToBottom(@PathParam("docId") Long docId) {
  149.         logger.debug("move document #{} to the bottom", docId);

  150.         documentManager.moveDocumentToBottom(docId);
  151.     }

  152.     // *********************************************************************************************
  153.     //
  154.     // *********************************************************************************************

  155.     /**
  156.      * Get all sticky note links where the document is the source
  157.      *
  158.      * @param documentId the id of the document
  159.      *
  160.      * @return list of links
  161.      */
  162.     @GET
  163.     @Path("{id: [0-9]+}/StickyNoteLinks")
  164.     public List<StickyNoteLink> getStickyNoteLinksAsSrc(@PathParam("id") Long documentId) {
  165.         logger.debug("Get sticky note links where document #{} is the source", documentId);
  166.         return documentManager.getStickyNoteLinkAsSrc(documentId);
  167.     }

  168. }