ActivityFlowLink.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.model.link;

  8. import ch.colabproject.colab.api.exceptions.ColabMergeException;
  9. import ch.colabproject.colab.api.model.ColabEntity;
  10. import ch.colabproject.colab.api.model.WithWebsocketChannels;
  11. import ch.colabproject.colab.api.model.card.Card;
  12. import ch.colabproject.colab.api.model.common.DeletionStatus;
  13. import ch.colabproject.colab.api.model.common.Tracking;
  14. import ch.colabproject.colab.api.model.tools.EntityHelper;
  15. import ch.colabproject.colab.api.security.permissions.Conditions;
  16. import ch.colabproject.colab.api.ws.channel.tool.ChannelsBuilders.ChannelsBuilder;
  17. import ch.colabproject.colab.api.ws.channel.tool.ChannelsBuilders.EmptyChannelBuilder;
  18. import javax.json.bind.annotation.JsonbTransient;
  19. import javax.persistence.Embedded;
  20. import javax.persistence.Entity;
  21. import javax.persistence.EnumType;
  22. import javax.persistence.Enumerated;
  23. import javax.persistence.FetchType;
  24. import javax.persistence.GeneratedValue;
  25. import javax.persistence.GenerationType;
  26. import javax.persistence.Id;
  27. import javax.persistence.Index;
  28. import javax.persistence.ManyToOne;
  29. import javax.persistence.SequenceGenerator;
  30. import javax.persistence.Table;
  31. import javax.persistence.Transient;
  32. import javax.validation.constraints.NotNull;

  33. /**
  34.  * Link to show that a card must be handled before another one.
  35.  *
  36.  * @author sandra
  37.  */
  38. @Entity
  39. @Table(
  40.     indexes = {
  41.         @Index(columnList = "nextcard_id"),
  42.         @Index(columnList = "previouscard_id"), }
  43. )
  44. public class ActivityFlowLink implements ColabEntity, WithWebsocketChannels {

  45.     private static final long serialVersionUID = 1L;

  46.     /** Link sequence name */
  47.     public static final String LINK_SEQUENCE_NAME = "link_seq";

  48.     // ---------------------------------------------------------------------------------------------
  49.     // fields
  50.     // ---------------------------------------------------------------------------------------------
  51.     /**
  52.      * Link ID
  53.      */
  54.     @Id
  55.     @SequenceGenerator(name = LINK_SEQUENCE_NAME, allocationSize = 20)
  56.     @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = LINK_SEQUENCE_NAME)
  57.     private Long id;

  58.     /**
  59.      * creation + modification + erasure tracking data
  60.      */
  61.     @Embedded
  62.     private Tracking trackingData;

  63.     /**
  64.      * Is it in a bin or ready to be definitely deleted. Null means active.
  65.      */
  66.     @Enumerated(EnumType.STRING)
  67.     private DeletionStatus deletionStatus;

  68.     /**
  69.      * The card to handle before
  70.      */
  71.     @ManyToOne(fetch = FetchType.LAZY)
  72.     @NotNull
  73.     @JsonbTransient
  74.     private Card previousCard;

  75.     /**
  76.      * The ID of the card to handle before (serialization sugar)
  77.      */
  78.     @Transient
  79.     private Long previousCardId;

  80.     /**
  81.      * The card to handle after
  82.      */
  83.     @ManyToOne(fetch = FetchType.LAZY)
  84.     @NotNull
  85.     @JsonbTransient
  86.     private Card nextCard;

  87.     /**
  88.      * The ID of the card to handle after (serialization sugar)
  89.      */
  90.     @Transient
  91.     private Long nextCardId;

  92.     // ---------------------------------------------------------------------------------------------
  93.     // getters and setters
  94.     // ---------------------------------------------------------------------------------------------
  95.     /**
  96.      * @return the link id
  97.      */
  98.     @Override
  99.     public Long getId() {
  100.         return id;
  101.     }

  102.     /**
  103.      * @param id the link id
  104.      */
  105.     public void setId(Long id) {
  106.         this.id = id;
  107.     }

  108.     /**
  109.      * Get the tracking data
  110.      *
  111.      * @return tracking data
  112.      */
  113.     @Override
  114.     public Tracking getTrackingData() {
  115.         return trackingData;
  116.     }

  117.     /**
  118.      * Set tracking data
  119.      *
  120.      * @param trackingData new tracking data
  121.      */
  122.     @Override
  123.     public void setTrackingData(Tracking trackingData) {
  124.         this.trackingData = trackingData;
  125.     }

  126.     @Override
  127.     public DeletionStatus getDeletionStatus() {
  128.         return deletionStatus;
  129.     }

  130.     @Override
  131.     public void setDeletionStatus(DeletionStatus status) {
  132.         this.deletionStatus = status;
  133.     }

  134.     /**
  135.      * @return the card to handle before
  136.      */
  137.     public Card getPreviousCard() {
  138.         return previousCard;
  139.     }

  140.     /**
  141.      * @param previousCard the card to handle before
  142.      */
  143.     public void setPreviousCard(Card previousCard) {
  144.         this.previousCard = previousCard;
  145.     }

  146.     /**
  147.      * get the id of the card to handle before. To be sent to client
  148.      *
  149.      * @return the id of the card to handle before
  150.      */
  151.     public Long getPreviousCardId() {
  152.         if (this.previousCard != null) {
  153.             return this.previousCard.getId();
  154.         } else {
  155.             return previousCardId;
  156.         }
  157.     }

  158.     /**
  159.      * set the id of the card to handle before. For serialization only
  160.      *
  161.      * @param previousCardId the id of the card to handle before
  162.      */
  163.     public void setPreviousCardId(Long previousCardId) {
  164.         this.previousCardId = previousCardId;
  165.     }

  166.     /**
  167.      * @return the card to handle after
  168.      */
  169.     public Card getNextCard() {
  170.         return nextCard;
  171.     }

  172.     /**
  173.      * @param nextCard the card to handle after
  174.      */
  175.     public void setNextCard(Card nextCard) {
  176.         this.nextCard = nextCard;
  177.     }

  178.     /**
  179.      * get the id of the card to handle after. To be sent to client
  180.      *
  181.      * @return the id of the card to handle after
  182.      */
  183.     public Long getNextCardId() {
  184.         if (this.nextCard != null) {
  185.             return this.nextCard.getId();
  186.         } else {
  187.             return nextCardId;
  188.         }
  189.     }

  190.     /**
  191.      * set the id of the card to handle after. For serialization only
  192.      *
  193.      * @param nextCardId the id of the card to handle after
  194.      */
  195.     public void setNextCardId(Long nextCardId) {
  196.         this.nextCardId = nextCardId;
  197.     }

  198.     // ---------------------------------------------------------------------------------------------
  199.     // concerning the whole class
  200.     // ---------------------------------------------------------------------------------------------

  201.     @Override
  202.     public void mergeToUpdate(ColabEntity other) throws ColabMergeException {
  203.         if (other instanceof ActivityFlowLink) {
  204.             ActivityFlowLink o = (ActivityFlowLink) other;
  205.             this.setDeletionStatus(o.getDeletionStatus());
  206.         } else {
  207.             throw new ColabMergeException(this, other);
  208.         }
  209.     }

  210.     @Override
  211.     public ChannelsBuilder getChannelsBuilder() {
  212.         if (this.nextCard != null) {
  213.             return this.nextCard.getChannelsBuilder();
  214.         } else {
  215.             // such an orphan shouldn't exist...
  216.             return new EmptyChannelBuilder();
  217.         }
  218.     }

  219.     @Override
  220.     @JsonbTransient
  221.     public Conditions.Condition getReadCondition() {
  222.         return new Conditions.Or(
  223.             new Conditions.HasCardReadRight(this.previousCard),
  224.             new Conditions.HasCardReadRight(this.nextCard)
  225.         );
  226.     }

  227.     // TODO what is best : And / Or ?
  228.     @Override
  229.     @JsonbTransient
  230.     public Conditions.Condition getUpdateCondition() {
  231.         return new Conditions.Or(
  232.             this.previousCard.getUpdateCondition(),
  233.             this.nextCard.getUpdateCondition()
  234.         );
  235.     }

  236.     @Override
  237.     public int hashCode() {
  238.         return EntityHelper.hashCode(this);
  239.     }

  240.     @Override
  241.     @SuppressWarnings("EqualsWhichDoesntCheckParameterClass")
  242.     public boolean equals(Object obj) {
  243.         return EntityHelper.equals(this, obj);
  244.     }

  245.     @Override
  246.     public String toString() {
  247.         return "ActivityFlowLink{" + "id=" + id + ", deletion=" + getDeletionStatus()
  248.             + ", previousCardId=" + previousCardId + ", nextCardId=" + nextCardId + "}";
  249.     }

  250. }