CardTypeRef.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.card;

  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import javax.json.bind.annotation.JsonbTransient;
  11. import javax.persistence.Entity;
  12. import javax.persistence.FetchType;
  13. import javax.persistence.Index;
  14. import javax.persistence.ManyToOne;
  15. import javax.persistence.NamedQuery;
  16. import javax.persistence.Table;
  17. import javax.persistence.Transient;
  18. import javax.validation.constraints.NotNull;

  19. /**
  20.  * Reference to another existing abstract card type.
  21.  *
  22.  * @author maxence
  23.  * @author sandra
  24.  */
  25. @Entity
  26. @Table(
  27.     indexes = {
  28.         @Index(columnList = "target_id"),
  29.     }
  30. )
  31. @NamedQuery(name = "CardTypeRef.findTargetIds",
  32.     query = "SELECT ctr.target.id FROM CardTypeRef ctr WHERE ctr.id IN :initIds")
  33. @NamedQuery(name = "CardTypeRef.findDirectReferences",
  34.     query = "SELECT ctr FROM CardTypeRef ctr "
  35.         + "WHERE ctr.target IS NOT NULL AND ctr.target.id = :targetId")
  36. @NamedQuery(name = "CardTypeRef.findDirectReferencesIds",
  37.     query = "SELECT ctr.id FROM CardTypeRef ctr "
  38.         + "WHERE ctr.target IS NOT NULL AND ctr.target.id IN :initIds")
  39. public class CardTypeRef extends AbstractCardType {

  40.     private static final long serialVersionUID = 1L;

  41.     // ---------------------------------------------------------------------------------------------
  42.     // fields
  43.     // ---------------------------------------------------------------------------------------------

  44.     /**
  45.      * The abstract card type this reference aims at
  46.      */
  47.     @ManyToOne(fetch = FetchType.LAZY)
  48.     @NotNull
  49.     @JsonbTransient
  50.     private AbstractCardType target;

  51.     /**
  52.      * The id of the abstract card type this reference aims at (serialization sugar)
  53.      */
  54.     @Transient
  55.     private Long targetId;

  56.     // ---------------------------------------------------------------------------------------------
  57.     // getters and setters
  58.     // ---------------------------------------------------------------------------------------------

  59.     /**
  60.      * get the card type (or card type reference) this reference aims at
  61.      *
  62.      * @return the referenced type
  63.      */
  64.     public AbstractCardType getTarget() {
  65.         return this.target;
  66.     }

  67.     /**
  68.      * @param target the card type (or card type reference) this reference aims at
  69.      */
  70.     public void setTarget(AbstractCardType target) {
  71.         this.target = target;
  72.     }

  73.     /**
  74.      * get the id of the card type (or card type reference) this reference aims at. To be sent to
  75.      * client
  76.      *
  77.      * @return id of the abstractCardType or null
  78.      */
  79.     public Long getTargetId() {
  80.         if (this.target != null) {
  81.             return this.target.getId();
  82.         } else {
  83.             return targetId;
  84.         }
  85.     }

  86.     /**
  87.      * set the id of the card type (or card type reference) this reference aims at. For
  88.      * serialization only
  89.      *
  90.      * @param id the id of the abstractCardType
  91.      */
  92.     public void setTargetId(Long id) {
  93.         this.targetId = id;
  94.     }

  95.     // ---------------------------------------------------------------------------------------------
  96.     // concerning the whole class
  97.     // ---------------------------------------------------------------------------------------------

  98.     /**
  99.      * Resolve the reference.
  100.      *
  101.      * @return the abstractCardType
  102.      */
  103.     @Override
  104.     public CardType resolve() {
  105.         if (this.target == null) {
  106.             return null;
  107.         }

  108.         return this.target.resolve();
  109.     }

  110.     @Override
  111.     public List<AbstractCardType> expand() {
  112.         List<AbstractCardType> list = new ArrayList<>();

  113.         list.add(this);
  114.         if (this.target != null) {
  115.             list.addAll(this.target.expand());
  116.         }
  117.         return list;
  118.     }

  119.     @Override
  120.     public String toString() {
  121.         return "CardTypeRef{"
  122.             + "id=" + getId() + ", deletion=" + getDeletionStatus()
  123.             + ", targetId=" + targetId + '}';
  124.     }

  125. }