generalizhed database connection

This commit is contained in:
2019-01-16 09:50:21 +01:00
parent 12da8c2d8c
commit 7790a6fe50
5 changed files with 231 additions and 3 deletions

View File

@ -44,7 +44,6 @@
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-jeeutil</artifactId>
<type>jar</type>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>junit</groupId>
@ -69,7 +68,6 @@
<dependency>
<groupId>de.muehlencord.shared</groupId>
<artifactId>shared-util</artifactId>
<version>1.1-SNAPSHOT</version>
<type>jar</type>
</dependency>
<dependency>
@ -83,6 +81,8 @@
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>7.0</version>
<type>jar</type>
<scope>provided</scope>
</dependency>
<dependency>

View File

@ -0,0 +1,119 @@
/*
* Copyright 2016 Joern Muehlencord <joern at muehlencord.de>.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.account.business;
import de.muehlencord.shared.account.business.account.entity.Account;
import de.muehlencord.shared.account.util.ApplicationPU;
import de.muehlencord.shared.account.util.Updateable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import javax.ejb.Lock;
import javax.ejb.LockType;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Order;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;
/**
*
* @author Joern Muehlencord <joern at muehlencord.de>
* @param <T>
*/
public abstract class AbstractController<T> {
@Inject
@ApplicationPU
protected EntityManager em;
@Inject
Account account;
private final Class<T> entityClass;
public AbstractController(Class clazz) {
this.entityClass = clazz;
}
public T attach(T entity) {
return em.merge(entity);
}
@TransactionAttribute (TransactionAttributeType.REQUIRED)
@Transactional
@Lock(LockType.WRITE)
public void create(T entity) {
if (Updateable.class.isAssignableFrom(entity.getClass())) {
Updateable updateable = (Updateable) entity;
updateable.setCreatedBy(account.getUsername());
updateable.setCreatedOn(new Date());
updateable.setLastUpdatedBy(account.getUsername());
updateable.setLastUpdatedOn(new Date());
}
em.persist(entity);
}
@TransactionAttribute (TransactionAttributeType.REQUIRED)
@Transactional
@Lock(LockType.WRITE)
public T update(T entity) {
if (Updateable.class.isAssignableFrom(entity.getClass())) {
Updateable updateable = (Updateable) entity;
updateable.setLastUpdatedBy(account.getUsername());
updateable.setLastUpdatedOn(new Date());
}
return em.merge(entity);
}
@TransactionAttribute (TransactionAttributeType.REQUIRED)
@Transactional
@Lock(LockType.WRITE)
public void delete(T entity) {
em.remove(attach(entity));
}
@Lock(LockType.READ)
public List<T> findAll() {
return findAll(new ArrayList<>());
}
@Lock(LockType.READ)
public List<T> findAll(String... orderFields) {
return findAll(Arrays.asList(orderFields));
}
@Lock(LockType.READ)
public List<T> findAll(List<String> orderFields) {
final CriteriaBuilder cb = em.getCriteriaBuilder();
final CriteriaQuery<T> criteria = cb.createQuery(entityClass);
final Root<T> r = criteria.from(entityClass);
List<Order> orderList = new ArrayList<>();
orderFields.stream().forEachOrdered(field -> orderList.add(cb.asc(r.get(field))));
final TypedQuery<T> query = em.createQuery(criteria.orderBy(orderList));
return query.getResultList();
}
}

View File

@ -0,0 +1,44 @@
package de.muehlencord.shared.account.business;
import de.muehlencord.shared.account.util.ApplicationPU;
import javax.annotation.Priority;
import javax.inject.Inject;
import javax.interceptor.AroundInvoke;
import javax.interceptor.Interceptor;
import javax.interceptor.InvocationContext;
import javax.persistence.EntityManager;
import javax.transaction.Transactional;
import static javax.transaction.Transactional.TxType.REQUIRED;
import javax.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
@Transactional(value = REQUIRED)
@Interceptor
@Priority(value = TransactionJoinInterceptor.PRIORITY)
public class TransactionJoinInterceptor {
private static final Logger LOGGER = LoggerFactory.getLogger(TransactionJoinInterceptor.class);
// attach behind the interceptor of the container
public static final int PRIORITY = Interceptor.Priority.PLATFORM_BEFORE + 250;
@Inject
@NotNull
@ApplicationPU
private EntityManager em;
@AroundInvoke
public Object joinTransaction(InvocationContext context) throws Exception {
if (em.isJoinedToTransaction()) {
LOGGER.trace("transaction already joined");
} else {
LOGGER.trace("joining transaction");
em.joinTransaction();
}
return context.proceed();
}
}

View File

@ -0,0 +1,36 @@
/*
* Copyright 2018 joern.muehlencord.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.muehlencord.shared.account.util;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.ElementType.TYPE;
import java.lang.annotation.Retention;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Target;
import javax.inject.Qualifier;
/**
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
@Qualifier
@Retention(RUNTIME)
@Target({METHOD, FIELD, PARAMETER, TYPE})
public @interface ApplicationPU {
}

View File

@ -0,0 +1,29 @@
package de.muehlencord.shared.account.util;
import java.util.Date;
/**
* This interface is used for Entities which provide createdOn / createdBy
* lastUpatedBy / lastUpdatedOn fields. The AbstractController uses this interface
* to automatically update the fields on creation / update.
*
* @author Joern Muehlencord <joern at muehlencord.de>
*/
public interface Updateable {
void setCreatedBy(String createdBy);
String getCreatedBy();
void setCreatedOn(Date createdOn);
Date getCreatedOn();
void setLastUpdatedBy(String lastUpdatedBy);
String getLastUpdatedBy();
void setLastUpdatedOn(Date lastUpdatedOn);
Date getLastUpdatedOn();
}