1 /*
2 * The coLAB project
3 * Copyright (C) 2022-2023 AlbaSim, MEI, HEIG-VD, HES-SO
4 *
5 * Licensed under the MIT License
6 */
7 package ch.colabproject.colab.api.persistence.jpa.project;
8
9 import ch.colabproject.colab.api.model.project.InstanceMaker;
10 import ch.colabproject.colab.api.model.project.Project;
11 import ch.colabproject.colab.api.model.user.User;
12
13 import org.slf4j.Logger;
14 import org.slf4j.LoggerFactory;
15
16 import javax.ejb.LocalBean;
17 import javax.ejb.Stateless;
18 import javax.persistence.EntityManager;
19 import javax.persistence.NoResultException;
20 import javax.persistence.PersistenceContext;
21 import javax.persistence.TypedQuery;
22
23 /**
24 * Instance maker persistence
25 * <p>
26 * Note : Most database operations are handled by managed entities and cascade.
27 *
28 * @author sandra
29 */
30 @Stateless
31 @LocalBean
32 public class InstanceMakerDao {
33
34 /**
35 * logger
36 */
37 private static final Logger logger = LoggerFactory.getLogger(InstanceMakerDao.class);
38
39 /**
40 * Access to the persistence unit
41 */
42 @PersistenceContext(unitName = "COLAB_PU")
43 private EntityManager em;
44
45 /**
46 * Find an instance maker by id
47 *
48 * @param id the id of the instance maker
49 * @return the instance maker or null if it does not exist
50 */
51 public InstanceMaker findInstanceMaker(Long id) {
52 logger.trace("find instance maker #{}", id);
53
54 return em.find(InstanceMaker.class, id);
55 }
56
57 /**
58 * Find the instance maker who match the given project and the given user.
59 *
60 * @param project the project
61 * @param user the user
62 * @return the instance maker or null
63 */
64 public InstanceMaker findInstanceMakerByProjectAndUser(Project project, User user) {
65 try {
66 TypedQuery<InstanceMaker> query = em.createNamedQuery(
67 "InstanceMaker.findByProjectAndUser",
68 InstanceMaker.class);
69
70 query.setParameter("projectId", project.getId());
71 query.setParameter("userId", user.getId());
72
73 return query.getSingleResult();
74 } catch (NoResultException ex) {
75 return null;
76 }
77 }
78
79 // /**
80 // * Find the instance makers related to the given user
81 // *
82 // * @param user the user
83 // *
84 // * @return the matching instance makers
85 // */
86 // public List<InstanceMaker> findInstanceMakersByUser(User user) {
87 // TypedQuery<InstanceMaker> query = em.createNamedQuery("InstanceMaker.findByUser",
88 // InstanceMaker.class);
89 //
90 // query.setParameter("userId", user.getId());
91 //
92 // return query.getResultList();
93 // }
94
95 // /**
96 // * Update instance maker. Only fields which are editable by users will be impacted.
97 // *
98 // * @param instanceMaker the instance maker as supplied by clients (ie not managed by JPA)
99 // *
100 // * @return return updated managed instance maker
101 // *
102 // * @throws ColabMergeException if the update failed
103 // */
104 // public InstanceMaker updateInstanceMaker(InstanceMaker instanceMaker) throws ColabMergeException {
105 // logger.trace("update instance maker {}", instanceMaker);
106 //
107 // InstanceMaker managedInstanceMaker = this.findInstanceMaker(instanceMaker.getId());
108 //
109 // managedInstanceMaker.merge(instanceMaker);
110 //
111 // return managedInstanceMaker;
112 // }
113
114 /**
115 * Persist a brand-new instance maker to database
116 *
117 * @param instanceMaker the new instance maker to persist
118 */
119 public void persistInstanceMaker(InstanceMaker instanceMaker) {
120 logger.trace("persist instance maker {}", instanceMaker);
121
122 em.persist(instanceMaker);
123
124 }
125
126 /**
127 * Delete the instance maker from database. This can't be undone
128 *
129 * @param instanceMaker the instance maker to delete
130 */
131 public void deleteInstanceMaker(InstanceMaker instanceMaker) {
132 logger.trace("delete instance maker #{}", instanceMaker);
133
134 // TODO: move to recycle bin first
135
136 em.remove(instanceMaker);
137 }
138
139 }