View Javadoc
1   /*
2    * The coLAB project
3    * Copyright (C) 2021-2024 AlbaSim, MEI, HEIG-VD, HES-SO
4    *
5    * Licensed under the MIT License
6    */
7   package ch.colabproject.colab.api.persistence.jpa.token;
8   
9   import ch.colabproject.colab.api.model.card.Card;
10  import ch.colabproject.colab.api.model.project.InstanceMaker;
11  import ch.colabproject.colab.api.model.project.Project;
12  import ch.colabproject.colab.api.model.team.TeamMember;
13  import ch.colabproject.colab.api.model.token.*;
14  import ch.colabproject.colab.api.model.user.LocalAccount;
15  import org.slf4j.Logger;
16  import org.slf4j.LoggerFactory;
17  
18  import javax.ejb.LocalBean;
19  import javax.ejb.Stateless;
20  import javax.persistence.EntityManager;
21  import javax.persistence.NoResultException;
22  import javax.persistence.PersistenceContext;
23  import java.util.ArrayList;
24  import java.util.List;
25  
26  /**
27   * Token persistence
28   * <p>
29   * Note : Most database operations are handled by managed entities and cascade.
30   *
31   * @author maxence
32   */
33  @Stateless
34  @LocalBean
35  public class TokenDao {
36  
37      /**
38       * logger
39       */
40      private static final Logger logger = LoggerFactory.getLogger(TokenDao.class);
41  
42      /**
43       * Access to the persistence unit
44       */
45      @PersistenceContext(unitName = "COLAB_PU")
46      private EntityManager em;
47  
48      /**
49       * Find a token by id
50       *
51       * @param id the id of the token to fetch
52       * @return the token with the given id or null if such a token does not exist
53       */
54      public Token findToken(Long id) {
55          logger.trace("find token #{}", id);
56  
57          return em.find(Token.class, id);
58      }
59  
60      /**
61       * Find any VerifyLocalAccountToken linked to the given account.
62       *
63       * @param account owner of the token
64       * @return the token if found. Null otherwise
65       */
66      public VerifyLocalAccountToken findVerifyTokenByAccount(LocalAccount account) {
67          try {
68              return em.createNamedQuery("VerifyLocalAccountToken.findByAccountId",
69                              VerifyLocalAccountToken.class)
70  
71                      .setParameter("id", account.getId())
72  
73                      .getSingleResult();
74          } catch (NoResultException ex) {
75              return null;
76          }
77      }
78  
79      /**
80       * Find any ResetLocalAccountPasswordToken linked to the given account.
81       *
82       * @param account owner of the token
83       * @return the token if found. Null otherwise
84       */
85      public ResetLocalAccountPasswordToken findResetTokenByAccount(LocalAccount account) {
86          try {
87              return em.createNamedQuery("ResetLocalAccountPasswordToken.findByAccountId",
88                              ResetLocalAccountPasswordToken.class)
89  
90                      .setParameter("id", account.getId())
91  
92                      .getSingleResult();
93          } catch (NoResultException ex) {
94              return null;
95          }
96      }
97  
98      /**
99       * Find if a pending invitation has already been sent to recipient to join the project
100      *
101      * @param project   the project
102      * @param recipient recipient
103      * @return invitation if there is a pending one, null otherwise
104      */
105     public InvitationToken findInvitationByProjectAndRecipient(Project project, String recipient) {
106         try {
107             return em
108                     .createNamedQuery("InvitationToken.findByProjectAndRecipient",
109                             InvitationToken.class)
110 
111                     .setParameter("projectId", project.getId())
112                     .setParameter("recipient", recipient)
113 
114                     .getSingleResult();
115         } catch (NoResultException ex) {
116             return null;
117         }
118     }
119 
120     /**
121      * Find all pending invitation for a teamMember and a project
122      *
123      * @param teamMember the team member linked to the invitation token
124      * @return invitations for the team member
125      */
126     public List<InvitationToken> findInvitationByTeamMember(TeamMember teamMember) {
127         return em.createNamedQuery("InvitationToken.findByTeamMember", InvitationToken.class)
128 
129                 .setParameter("teamMemberId", teamMember.getId())
130 
131                 .getResultList();
132     }
133 
134     /**
135      * Find if a pending model sharing token has already been sent to recipient to join the project
136      *
137      * @param project   the project
138      * @param recipient recipient
139      * @return model sharing if there is a pending one, null otherwise
140      */
141     public ModelSharingToken findModelSharingByProjectAndRecipient(Project project,
142                                                                    String recipient) {
143         try {
144             return em.createNamedQuery("ModelSharingToken.findByProjectAndRecipient",
145                             ModelSharingToken.class)
146 
147                     .setParameter("projectId", project.getId())
148                     .setParameter("recipient", recipient)
149 
150                     .getSingleResult();
151         } catch (NoResultException ex) {
152             return null;
153         }
154     }
155 
156     /**
157      * Find model sharing tokens related to an instanceMaker
158      *
159      * @param instanceMaker the instanceMaker
160      * @return list of model sharing related to instanceMaker
161      */
162     public List<ModelSharingToken> findModelSharingByInstanceMaker(InstanceMaker instanceMaker) {
163         try {
164             return em.createNamedQuery("ModelSharingToken.findByInstanceMaker",
165                             ModelSharingToken.class)
166 
167                     .setParameter("instanceMakerId", instanceMaker.getId())
168                     .getResultList();
169         } catch (NoResultException ex) {
170             return null;
171         }
172     }
173 
174     /**
175      * Find sharing link tokens related to a project (regardless of if a card is specified or not)
176      *
177      * @param project the project
178      *
179      * @return All sharing link tokens related to the project
180      */
181     public List<SharingLinkToken> findSharingLinkByProject(Project project) {
182         try {
183             return em.createNamedQuery("SharingLinkToken.findByProject",
184                             SharingLinkToken.class)
185                     .setParameter("projectId", project.getId())
186                     .getResultList();
187         } catch (NoResultException ex) {
188             return new ArrayList<SharingLinkToken>();
189         }
190     }
191 
192     /**
193      * Find sharing link tokens related to a card
194      *
195      * @param card the card
196      *
197      * @return All sharing link tokens related to the card
198      */
199     public List<SharingLinkToken> findSharingLinkByCard(Card card) {
200         try {
201             return em.createNamedQuery("SharingLinkToken.findByCard", SharingLinkToken.class)
202                     .setParameter("cardId", card.getId())
203                     .getResultList();
204         } catch (NoResultException ex) {
205             return new ArrayList<SharingLinkToken>();
206         }
207     }
208 
209 //
210 //  public List<Token> findTokensByProject(Project project) {
211 //      // TODO Auto-generated method stub
212 //      return null;
213 //  }
214 
215 //    /**
216 //     * Update token. Only fields which are editable by users will be impacted.
217 //     *
218 //     * @param token the token as supplied by clients (ie not managed by JPA)
219 //     *
220 //     * @return return updated managed token
221 //     *
222 //     * @throws ColabMergeException if the update failed
223 //     */
224 //    public Token updateToken(Token token) throws ColabMergeException {
225 //        logger.trace("update token {}", token);
226 //
227 //        Token managedToken = this.findToken(token.getId());
228 //
229 //        managedToken.merge(token);
230 //
231 //        return managedToken;
232 //    }
233 
234     /**
235      * Persist the token
236      *
237      * @param token token to persist
238      */
239     public void persistToken(Token token) {
240         logger.trace("persist token {}", token);
241 
242         em.persist(token);
243     }
244 
245     /**
246      * Delete the token from database. This can't be undone
247      *
248      * @param token token to remove
249      */
250     public void deleteToken(Token token) {
251         logger.trace("delete token {}", token);
252 
253         em.remove(token);
254     }
255 
256 }