BrightSide Workbench Full Report + Source Code
DaoManager.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2021 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 package org.turro.jpa;
20 
21 import java.io.Closeable;
22 import java.util.Map;
23 import java.util.logging.Level;
24 import java.util.logging.Logger;
25 import javax.persistence.EntityManager;
26 import javax.persistence.Query;
27 import javax.persistence.criteria.CriteriaDelete;
28 import javax.persistence.criteria.CriteriaQuery;
29 import javax.persistence.criteria.CriteriaUpdate;
30 import org.turro.elephant.context.ElephantContext;
31 import org.turro.util.AtomicCounter;
32 
37 public class DaoManager implements Closeable {
38 
39  private final Dao dao;
40  private final EntityManager manager;
41 
42  public DaoManager(Dao dao) {
43  this.dao = dao;
44  this.manager = dao.createEntityManager();
45  openManagers.increment();
46  }
47 
48  public <T> T saveObject(T obj) {
49  try {
50  obj = manager.merge(obj);
51  } catch (Exception ex) {
52  Logger.getLogger(DaoManager.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
53  } finally {
54  return obj;
55  }
56  }
57 
58  public void persist(Object obj) {
59  try {
60  manager.persist(obj);
61  } catch (Exception ex) {
62  Logger.getLogger(DaoManager.class.getName()).log(Level.SEVERE, ElephantContext.logMsg(null), ex);
63  }
64  }
65 
66  public <T> T find(Class<T> javaClass, Object arg) {
67  return manager.find(javaClass, arg);
68  }
69 
70  public void remove(Object obj) {
71  manager.remove(obj);
72  }
73 
74  public <T> Query createQuery(CriteriaQuery<T> query) {
75  return manager.createQuery(query);
76  }
77 
78  public <T> Query createQuery(String clause, Class<T> javaClass) {
79  return manager.createQuery(clause, javaClass);
80  }
81 
82  public Query createQuery(String clause) {
83  return manager.createQuery(clause);
84  }
85 
86  public Query createQuery(CriteriaDelete delete) {
87  return manager.createQuery(delete);
88  }
89 
90  public Query createQuery(CriteriaUpdate update) {
91  return manager.createQuery(update);
92  }
93 
94  public Query createNativeQuery(String clause, Class resultClass) {
95  return manager.createNativeQuery(clause, resultClass);
96  }
97 
98  public Query createNativeQuery(String clause) {
99  return manager.createNativeQuery(clause);
100  }
101 
102  public void setNamedParameters(Query q, Map<String, Object> parameters) {
103  for (Map.Entry<String, Object> e : parameters.entrySet()) {
104  q.setParameter(e.getKey(), e.getValue());
105  }
106  }
107 
108  public void clear() {
109  manager.clear();
110  }
111 
112  public EntityManager getManager() {
113  return manager;
114  }
115 
116  public Dao getDao() {
117  return dao;
118  }
119 
120  @Override
121  public void close() {
122  manager.close();
123  openManagers.decrement();
124  }
125 
126  public boolean isClosed() {
127  return !manager.isOpen();
128  }
129 
130  /* Debug */
131 
132  private static final AtomicCounter openManagers = new AtomicCounter();
133 
134  public static AtomicCounter getCounter() {
135  return openManagers;
136  }
137 
138 }
void persist(Object obj)
Definition: DaoManager.java:58
Query createNativeQuery(String clause)
Definition: DaoManager.java:98
Query createNativeQuery(String clause, Class resultClass)
Definition: DaoManager.java:94
static AtomicCounter getCounter()
void setNamedParameters(Query q, Map< String, Object > parameters)
Query createQuery(CriteriaDelete delete)
Definition: DaoManager.java:86
EntityManager getManager()
Query createQuery(String clause)
Definition: DaoManager.java:82
Query createQuery(CriteriaUpdate update)
Definition: DaoManager.java:90
abstract EntityManager createEntityManager()