| .. | .. |
|---|
| 3 | 3 | import java.util.Date; |
|---|
| 4 | 4 | |
|---|
| 5 | 5 | import javax.enterprise.context.ApplicationScoped; |
|---|
| 6 | +import javax.inject.Inject; |
|---|
| 6 | 7 | import javax.persistence.EntityManager; |
|---|
| 7 | | -import javax.persistence.PersistenceContext; |
|---|
| 8 | | -import javax.transaction.Transactional; |
|---|
| 9 | 8 | |
|---|
| 10 | 9 | import net.curisit.integrity.commons.Utils; |
|---|
| 11 | 10 | import net.curisit.securis.db.Settings; |
|---|
| 11 | +import net.curisit.securis.ioc.EntityManagerProvider; |
|---|
| 12 | 12 | |
|---|
| 13 | 13 | import org.apache.logging.log4j.LogManager; |
|---|
| 14 | 14 | import org.apache.logging.log4j.Logger; |
|---|
| .. | .. |
|---|
| 19 | 19 | @SuppressWarnings("unused") |
|---|
| 20 | 20 | private static final Logger LOG = LogManager.getLogger(SystemParams.class); |
|---|
| 21 | 21 | |
|---|
| 22 | | - @PersistenceContext(unitName = "localdb") |
|---|
| 23 | | - private EntityManager em; |
|---|
| 22 | + @Inject |
|---|
| 23 | + private EntityManagerProvider emProvider; |
|---|
| 24 | 24 | |
|---|
| 25 | 25 | /** |
|---|
| 26 | 26 | * Returns the system parameter value for given key |
|---|
| .. | .. |
|---|
| 109 | 109 | * @return |
|---|
| 110 | 110 | */ |
|---|
| 111 | 111 | public String getParam(String key, String defaultValue) { |
|---|
| 112 | + EntityManager em = emProvider.getEntityManager(); |
|---|
| 112 | 113 | Settings p = em.find(Settings.class, key); |
|---|
| 113 | 114 | return p == null ? defaultValue : p.getValue(); |
|---|
| 114 | 115 | } |
|---|
| .. | .. |
|---|
| 120 | 121 | * @param defaultValue |
|---|
| 121 | 122 | * @return |
|---|
| 122 | 123 | */ |
|---|
| 123 | | - @Transactional |
|---|
| 124 | 124 | public void setParam(String key, String value) { |
|---|
| 125 | | - Settings p = em.find(Settings.class, key); |
|---|
| 126 | | - if (p == null) { |
|---|
| 127 | | - p = new Settings(); |
|---|
| 128 | | - p.setKey(key); |
|---|
| 129 | | - p.setValue(value); |
|---|
| 130 | | - em.persist(p); |
|---|
| 131 | | - } else { |
|---|
| 132 | | - p.setValue(value); |
|---|
| 133 | | - em.merge(p); |
|---|
| 125 | + EntityManager em = emProvider.getEntityManager(); |
|---|
| 126 | + em.getTransaction().begin(); |
|---|
| 127 | + try { |
|---|
| 128 | + Settings p = em.find(Settings.class, key); |
|---|
| 129 | + |
|---|
| 130 | + if (p == null) { |
|---|
| 131 | + p = new Settings(); |
|---|
| 132 | + p.setKey(key); |
|---|
| 133 | + p.setValue(value); |
|---|
| 134 | + em.persist(p); |
|---|
| 135 | + } else { |
|---|
| 136 | + p.setValue(value); |
|---|
| 137 | + em.merge(p); |
|---|
| 138 | + } |
|---|
| 139 | + em.flush(); |
|---|
| 140 | + em.getTransaction().commit(); |
|---|
| 141 | + } finally { |
|---|
| 142 | + em.getTransaction().rollback(); |
|---|
| 134 | 143 | } |
|---|
| 135 | | - em.flush(); |
|---|
| 136 | 144 | } |
|---|
| 137 | 145 | |
|---|
| 138 | 146 | /** |
|---|
| .. | .. |
|---|
| 181 | 189 | * @param key |
|---|
| 182 | 190 | * @return |
|---|
| 183 | 191 | */ |
|---|
| 184 | | - @Transactional |
|---|
| 185 | 192 | public void removeParam(String key) { |
|---|
| 186 | | - Settings p = em.find(Settings.class, key); |
|---|
| 187 | | - if (p != null) { |
|---|
| 188 | | - em.remove(p); |
|---|
| 193 | + EntityManager em = emProvider.getEntityManager(); |
|---|
| 194 | + em.getTransaction().begin(); |
|---|
| 195 | + try { |
|---|
| 196 | + Settings p = em.find(Settings.class, key); |
|---|
| 197 | + if (p != null) { |
|---|
| 198 | + em.remove(p); |
|---|
| 199 | + } |
|---|
| 200 | + em.getTransaction().commit(); |
|---|
| 201 | + } finally { |
|---|
| 202 | + em.getTransaction().rollback(); |
|---|
| 189 | 203 | } |
|---|
| 190 | 204 | } |
|---|
| 191 | 205 | |
|---|