Setting/resetting the index item of a category only worked for the root category, now works for all categories.

pull/20/head
Jens Pelzetter 2022-02-26 17:55:16 +01:00
parent 35ce5479fd
commit f5d3481c9a
2 changed files with 40 additions and 29 deletions

View File

@ -524,7 +524,7 @@
<td class="actions-setindex-col"> <td class="actions-setindex-col">
<c:choose> <c:choose>
<c:when test="#{object.indexObject}"> <c:when test="#{object.indexObject}">
<form action="#{mvc.basePath}/#{ContentSectionModel.sectionName}/categorysystems/#{CategorySystemModel.selectedCategorySystem.context}/categories#{category.path}/@index-element/reset" <form action="#{mvc.basePath}/#{ContentSectionModel.sectionName}/categorysystems/#{CategorySystemModel.selectedCategorySystem.context}/categories#{CategorySystemModel.selectedCategory.path}/@index-element/reset"
method="post"> method="post">
<button class="btn btn-danger" <button class="btn btn-danger"
type="submit"> type="submit">
@ -534,7 +534,7 @@
</form> </form>
</c:when> </c:when>
<c:otherwise> <c:otherwise>
<form action="#{mvc.basePath}/#{ContentSectionModel.sectionName}/categorysystems/#{CategorySystemModel.selectedCategorySystem.context}/categories#{category.path}/@index-element/#{object.objectUuid}" <form action="#{mvc.basePath}/#{ContentSectionModel.sectionName}/categorysystems/#{CategorySystemModel.selectedCategorySystem.context}/categories#{CategorySystemModel.selectedCategory.path}/@index-element/#{object.objectUuid}"
method="post"> method="post">
<button class="btn btn-info" <button class="btn btn-info"
type="submit"> type="submit">

View File

@ -47,7 +47,7 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
private static final long serialVersionUID = 8462548002420023652L; private static final long serialVersionUID = 8462548002420023652L;
protected static final String FETCH_GRAPH_HINT_KEY protected static final String FETCH_GRAPH_HINT_KEY
= "javax.persistence.fetchgraph"; = "javax.persistence.fetchgraph";
/** /**
* The {@link EntityManager} instance to use. Provided by the container via * The {@link EntityManager} instance to use. Provided by the container via
@ -113,7 +113,8 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public EntityGraph<E> createEntityGraph(final String entityGraphName) { public EntityGraph<E> createEntityGraph(final String entityGraphName) {
return (EntityGraph<E>) entityManager.createEntityGraph( return (EntityGraph<E>) entityManager.createEntityGraph(
entityGraphName); entityGraphName
);
} }
/** /**
@ -153,16 +154,18 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
*/ */
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public Optional<E> findById(final K entityId) { public Optional<E> findById(final K entityId) {
Objects.requireNonNull(entityId); Objects.requireNonNull(entityId);
return Optional.ofNullable(entityManager.find(getEntityClass(), return Optional.ofNullable(
entityId)); entityManager.find(
getEntityClass(),
entityId
)
);
} }
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public Optional<E> findById(final K entityId, final String entityGraphName) { public Optional<E> findById(final K entityId, final String entityGraphName) {
Objects.requireNonNull(entityId); Objects.requireNonNull(entityId);
Objects.requireNonNull(entityGraphName); Objects.requireNonNull(entityGraphName);
@ -175,15 +178,18 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public Optional<E> findById(final K entityId, public Optional<E> findById(final K entityId,
final EntityGraph<E> entityGraph) { final EntityGraph<E> entityGraph) {
Objects.requireNonNull(entityId); Objects.requireNonNull(entityId);
Objects.requireNonNull(entityGraph); Objects.requireNonNull(entityGraph);
final Map<String, Object> hints = new HashMap<>(); final Map<String, Object> hints = new HashMap<>();
hints.put(FETCH_GRAPH_HINT_KEY, entityGraph); hints.put(FETCH_GRAPH_HINT_KEY, entityGraph);
return Optional.ofNullable(entityManager.find(getEntityClass(), return Optional.ofNullable(
entityId, entityManager.find(
hints)); getEntityClass(),
entityId,
hints
)
);
} }
/** /**
@ -206,16 +212,18 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
Objects.requireNonNull(entityId); Objects.requireNonNull(entityId);
final CriteriaBuilder builder = entityManager.getCriteriaBuilder(); final CriteriaBuilder builder = entityManager.getCriteriaBuilder();
final CriteriaQuery<E> criteriaQuery = builder final CriteriaQuery<E> criteriaQuery = builder.createQuery(
.createQuery(getEntityClass()); getEntityClass()
);
final Root<E> from = criteriaQuery.from(getEntityClass()); final Root<E> from = criteriaQuery.from(getEntityClass());
criteriaQuery.from(getEntityClass()); criteriaQuery.from(getEntityClass());
for (final String fetchJoin : fetchJoins) { for (final String fetchJoin : fetchJoins) {
from.fetch(fetchJoin); from.fetch(fetchJoin);
} }
criteriaQuery criteriaQuery.where(
.where(builder.equal(from.get(getIdAttributeName()), entityId)); builder.equal(from.get(getIdAttributeName()), entityId)
);
final TypedQuery<E> query = entityManager.createQuery(criteriaQuery); final TypedQuery<E> query = entityManager.createQuery(criteriaQuery);
try { try {
@ -229,10 +237,15 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
public E reload(final E entity, final String... fetchJoins) { public E reload(final E entity, final String... fetchJoins) {
return findById(getIdOfEntity(entity), fetchJoins) return findById(getIdOfEntity(entity), fetchJoins)
.orElseThrow(() -> new IllegalArgumentException(String .orElseThrow(
.format("No Entity of type \"%s\" with ID %s in the database.", () -> new IllegalArgumentException(
getEntityClass().getName(), String.format(
Objects.toString(getIdOfEntity(entity))))); "No Entity of type \"%s\" with ID %s in the database.",
getEntityClass().getName(),
Objects.toString(getIdOfEntity(entity))
)
)
);
} }
/** /**
@ -273,7 +286,8 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
final CriteriaBuilder criteriaBuilder = entityManager final CriteriaBuilder criteriaBuilder = entityManager
.getCriteriaBuilder(); .getCriteriaBuilder();
final CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery( final CriteriaQuery<E> criteriaQuery = criteriaBuilder.createQuery(
getEntityClass()); getEntityClass()
);
final Root<E> root = criteriaQuery.from(getEntityClass()); final Root<E> root = criteriaQuery.from(getEntityClass());
return criteriaQuery.select(root); return criteriaQuery.select(root);
} }
@ -303,8 +317,7 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
final String graphName) { final String graphName) {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
final EntityGraph<E> entityGraph = (EntityGraph< E>) entityManager final EntityGraph<E> entityGraph = (EntityGraph< E>) entityManager
.getEntityGraph( .getEntityGraph(graphName);
graphName);
return executeCriteriaQuery(criteriaQuery, entityGraph); return executeCriteriaQuery(criteriaQuery, entityGraph);
} }
@ -334,7 +347,6 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
*/ */
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public void save(final E entity) { public void save(final E entity) {
Objects.requireNonNull(entity, "Can't save null."); Objects.requireNonNull(entity, "Can't save null.");
if (isNew(entity)) { if (isNew(entity)) {
@ -363,12 +375,11 @@ public abstract class AbstractEntityRepository<K, E> implements Serializable {
*/ */
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public void delete(final E entity) { public void delete(final E entity) {
Objects.requireNonNull(
entity, "Can't delete a null entity."
);
Objects.requireNonNull(entity, entityManager.remove(entity);
"Can't delete a null entity.");
//We need to make sure we use a none detached entity, therefore the merge
entityManager.remove(entityManager.merge(entity));
} }
} }