Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerald Unterrainer committed Jul 30, 2021
2 parents c84e1a9 + 31f9158 commit cdd2a15
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 41 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

<modelVersion>4.0.0</modelVersion>
<artifactId>http-server</artifactId>
<version>0.2.28</version>
<version>0.2.29</version>
<name>HttpServer</name>
<packaging>jar</packaging>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

import javax.persistence.EntityManagerFactory;

import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import info.unterrainer.commons.rdbutils.entities.BasicAsyncJpa;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;

public class AsyncJpqlDao<P extends BasicAsyncJpa> extends BasicJpqlDao<P> {

Expand All @@ -21,7 +21,7 @@ public class AsyncJpqlDao<P extends BasicAsyncJpa> extends BasicJpqlDao<P> {
* @param tenantJpaType the JPA of the tenant-permission table associated
*/
public AsyncJpqlDao(final EntityManagerFactory emf, final Class<P> type,
final Class<? extends BasicJpa> tenantJpaType) {
final Class<? extends BasicPermissionJpa> tenantJpaType) {
super(emf, type);
this.coreDao.tenantData = new TenantData(tenantJpaType);
}
Expand All @@ -42,7 +42,7 @@ public AsyncJpqlDao(final EntityManagerFactory emf, final Class<P> type,
* @param tenantIdFieldName the name of the field holding the tenant-ID
*/
public AsyncJpqlDao(final EntityManagerFactory emf, final Class<P> type,
final Class<? extends BasicJpa> tenantJpaType, final String tenantReferenceFieldName,
final Class<? extends BasicPermissionJpa> tenantJpaType, final String tenantReferenceFieldName,
final String tenantIdFieldName) {
super(emf, type);
this.coreDao.tenantData = new TenantData(tenantJpaType, tenantReferenceFieldName, tenantIdFieldName);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
package info.unterrainer.commons.httpserver.daos;

import java.time.LocalDateTime;
import java.util.function.Function;

import javax.persistence.EntityManager;

import info.unterrainer.commons.jreutils.DateUtils;
import info.unterrainer.commons.rdbutils.Transactions;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import lombok.Getter;
Expand All @@ -31,12 +29,6 @@ protected <V> V withEntityManager(final Function<EntityManager, V> func) {
* @return the entity after insertion
*/
public P execute() {
return withEntityManager(em -> {
LocalDateTime time = DateUtils.nowUtc();
entity.setCreatedOn(time);
entity.setEditedOn(time);
em.persist(entity);
return entity;
});
return withEntityManager(em -> dao.coreDao.create(em, entity, writeTenantIds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import info.unterrainer.commons.httpserver.exceptions.ForbiddenException;
import info.unterrainer.commons.httpserver.exceptions.InternalServerErrorException;
import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import info.unterrainer.commons.httpserver.jsons.ListJson;
import info.unterrainer.commons.jreutils.DateUtils;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
Expand Down Expand Up @@ -61,7 +62,7 @@ public P create(final EntityManager em, final P entity, final Set<Long> tenantId
if (hasTenantData())
try {
for (Long tenantId : tenantIds) {
BasicJpa tenantJpa = tenantData.getType().getConstructor().newInstance();
BasicPermissionJpa tenantJpa = tenantData.getPermissionJpaType().getConstructor().newInstance();
tenantData.getReferenceSetMethod().invoke(tenantJpa, entity.getId());
tenantData.getTenantIdSetMethod().invoke(tenantJpa, tenantId);

Expand All @@ -71,8 +72,8 @@ public P create(final EntityManager em, final P entity, final Set<Long> tenantId
}
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new InternalServerErrorException(
String.format("Error creating permission-entry in [%s]", tenantData.getType().getSimpleName()));
throw new InternalServerErrorException(String.format("Error creating permission-entry in [%s]",
tenantData.getPermissionJpaType().getSimpleName()));
}
return entity;
}
Expand Down Expand Up @@ -233,8 +234,8 @@ private String addTenantJoin(final String joinClause) {
if (joinClause == null || joinClause.isBlank())
r = "";

r += String.format(" LEFT JOIN %s tenantTable on o.id = tenantTable.%s", tenantData.getType().getSimpleName(),
tenantData.getMainTableIdReferenceField());
r += String.format(" LEFT JOIN %s tenantTable on o.id = tenantTable.%s",
tenantData.getPermissionJpaType().getSimpleName(), tenantData.getMainTableIdReferenceField());
return r;
}

Expand Down Expand Up @@ -266,8 +267,8 @@ private Map<String, Object> addTenantParams(final Map<String, Object> map, final
}

private boolean hasTenantData() {
return tenantData != null && tenantData.getType() != null && tenantData.getMainTableIdReferenceField() != null
&& tenantData.getTenantIdField() != null;
return tenantData != null && tenantData.getPermissionJpaType() != null
&& tenantData.getMainTableIdReferenceField() != null && tenantData.getTenantIdField() != null;
}

private boolean isSet(final String str) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import javax.persistence.EntityManagerFactory;

import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import io.javalin.http.Context;

Expand All @@ -22,7 +23,8 @@ public class JpqlDao<P extends BasicJpa> extends BasicJpqlDao<P> {
* @param type the return-type of the query (the underlying JPA)
* @param tenantJpaType the JPA of the tenant-permission table associated
*/
public JpqlDao(final EntityManagerFactory emf, final Class<P> type, final Class<? extends BasicJpa> tenantJpaType) {
public JpqlDao(final EntityManagerFactory emf, final Class<P> type,
final Class<? extends BasicPermissionJpa> tenantJpaType) {
super(emf, type);
this.coreDao.tenantData = new TenantData(tenantJpaType);
}
Expand All @@ -45,7 +47,7 @@ public JpqlDao(final EntityManagerFactory emf, final Class<P> type, final Class<
* associated
*/
public JpqlDao(final Function<Context, EntityManagerFactory> entityManagerFactorySupplier, final Class<P> type,
final Class<? extends BasicJpa> tenantJpaType) {
final Class<? extends BasicPermissionJpa> tenantJpaType) {
super(entityManagerFactorySupplier, type);
this.coreDao.tenantData = new TenantData(tenantJpaType);
}
Expand All @@ -65,8 +67,9 @@ public JpqlDao(final Function<Context, EntityManagerFactory> entityManagerFactor
* to the main-table-id
* @param tenantIdFieldName the name of the field holding the tenant-ID
*/
public JpqlDao(final EntityManagerFactory emf, final Class<P> type, final Class<? extends BasicJpa> tenantJpaType,
final String tenantReferenceFieldName, final String tenantIdFieldName) {
public JpqlDao(final EntityManagerFactory emf, final Class<P> type,
final Class<? extends BasicPermissionJpa> tenantJpaType, final String tenantReferenceFieldName,
final String tenantIdFieldName) {
super(emf, type);
this.coreDao.tenantData = new TenantData(tenantJpaType, tenantReferenceFieldName, tenantIdFieldName);
}
Expand All @@ -90,7 +93,7 @@ public JpqlDao(final EntityManagerFactory emf, final Class<P> type, final Class<
* tenant-ID
*/
public JpqlDao(final Function<Context, EntityManagerFactory> entityManagerFactorySupplier, final Class<P> type,
final Class<? extends BasicJpa> tenantJpaType, final String tenantReferenceFieldName,
final Class<? extends BasicPermissionJpa> tenantJpaType, final String tenantReferenceFieldName,
final String tenantIdFieldName) {
super(entityManagerFactorySupplier, type);
this.coreDao.tenantData = new TenantData(tenantJpaType, tenantReferenceFieldName, tenantIdFieldName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.lang.reflect.Method;

import info.unterrainer.commons.httpserver.exceptions.InternalServerErrorException;
import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import lombok.Getter;

public class TenantData {

@Getter
private Class<? extends BasicJpa> type;
private Class<? extends BasicPermissionJpa> permissionJpaType;
@Getter
private String mainTableIdReferenceField = "referenceId";
@Getter
Expand All @@ -18,37 +18,38 @@ public class TenantData {
private Method referenceSetMethod;
private Method tenantIdSetMethod;

public TenantData(final Class<? extends BasicJpa> type) {
public TenantData(final Class<? extends BasicPermissionJpa> permissionJpaType) {
super();
this.type = type;
this.permissionJpaType = permissionJpaType;
}

public TenantData(final Class<? extends BasicJpa> type, final String mainTableIdReferenceField,
final String tenantIdField) {
public TenantData(final Class<? extends BasicPermissionJpa> permissionJpaType,
final String mainTableIdReferenceField, final String tenantIdField) {
super();
this.type = type;
this.permissionJpaType = permissionJpaType;
this.mainTableIdReferenceField = mainTableIdReferenceField;
this.tenantIdField = tenantIdField;
}

public Method getReferenceSetMethod() {
if (referenceSetMethod == null)
try {
referenceSetMethod = type.getMethod("set" + capitalize(mainTableIdReferenceField), Long.class);
referenceSetMethod = permissionJpaType.getMethod("set" + capitalize(mainTableIdReferenceField),
Long.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new InternalServerErrorException(
String.format("Error creating permission-entry in [%s]", type.getSimpleName()), e);
String.format("Error creating permission-entry in [%s]", permissionJpaType.getSimpleName()), e);
}
return referenceSetMethod;
}

public Method getTenantIdSetMethod() {
if (tenantIdSetMethod == null)
try {
tenantIdSetMethod = type.getMethod("set" + capitalize(tenantIdField), Long.class);
tenantIdSetMethod = permissionJpaType.getMethod("set" + capitalize(tenantIdField), Long.class);
} catch (NoSuchMethodException | SecurityException e) {
throw new InternalServerErrorException(
String.format("Error creating permission-entry in [%s]", type.getSimpleName()), e);
String.format("Error creating permission-entry in [%s]", permissionJpaType.getSimpleName()), e);
}
return tenantIdSetMethod;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package info.unterrainer.commons.httpserver.jpas;

import javax.persistence.MappedSuperclass;

import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

@Data
@NoArgsConstructor
@SuperBuilder(toBuilder = true)
@MappedSuperclass
public class BasicPermissionJpa extends BasicJpa {

private Long referenceId;
private Long tenantId;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import javax.persistence.Entity;
import javax.persistence.Table;

import info.unterrainer.commons.rdbutils.entities.BasicJpa;
import info.unterrainer.commons.httpserver.jpas.BasicPermissionJpa;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
Expand All @@ -15,8 +15,5 @@
@SuperBuilder(toBuilder = true)
@Entity
@Table(name = "test_permission")
public class TestPermissionJpa extends BasicJpa {

private Long referenceId;
private Long tenantId;
public class TestPermissionJpa extends BasicPermissionJpa {
}

0 comments on commit cdd2a15

Please sign in to comment.