Skip to content

Commit

Permalink
repair pre-update jpa passed to extensions
Browse files Browse the repository at this point in the history
add getWithException and repair get() in singleQuery
  • Loading branch information
Gerald Unterrainer committed Aug 12, 2021
1 parent 0ab94e6 commit 6535023
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ private void fullUpdate(final Context ctx) throws IOException {
DaoTransaction<E> transaction = dao.getTransactionManager().beginTransaction(ctx);
P jpa = hu.getJpaById(ctx, transaction.getManager(), dao);
try {
P detachedJpa = orikaMapper.map(jpa, jpaType);
J json = jsonMapper.fromStringTo(jsonType, ctx.attribute(Attribute.REQUEST_BODY));
P mappedJpa = orikaMapper.map(json, jpaType);
mappedJpa.setId(jpa.getId());
Expand All @@ -192,11 +193,12 @@ private void fullUpdate(final Context ctx) throws IOException {

mappedJpa = extensions.runPreModify(ctx, makeAsyncExtensionContextFor(ctx), transaction.getManager(),
jpa.getId(), json, jpa, mappedJpa, executorService);

P persistedJpa = dao.update(transaction.getManager(), mappedJpa, hu.getReadTenantIdsFrom(ctx));

J r = orikaMapper.map(persistedJpa, jsonType);
r = extensions.runPostModify(ctx, makeAsyncExtensionContextFor(ctx), transaction.getManager(), jpa.getId(),
json, jpa, mappedJpa, persistedJpa, r, executorService);
json, detachedJpa, mappedJpa, persistedJpa, r, executorService);

ctx.attribute(Attribute.RESPONSE_OBJECT, r);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package info.unterrainer.commons.httpserver.daos;

import java.util.List;
import java.util.Map;
import java.util.function.Function;

Expand Down Expand Up @@ -30,6 +31,22 @@ protected <V> V withEntityManager(final Function<EntityManager, V> func) {
* @return the selected entity
*/
public P get() {
return withEntityManager(em -> {
List<P> resultList = dao.coreDao
.getQuery(em, "o", null, "o.id = :id", Map.of("id", id), dao.type, null, false, null, readTenantIds)
.getResultList();
if (resultList.isEmpty())
return null;
return resultList.get(0);
});
}

/**
* Get the selected entity or throw an exception if it wasn't found.
*
* @return the selected entity
*/
public P getOrException() {
return withEntityManager(em -> dao.coreDao
.getQuery(em, "o", null, "o.id = :id", Map.of("id", id), dao.type, null, false, null, readTenantIds)
.getSingleResult());
Expand Down

0 comments on commit 6535023

Please sign in to comment.