CCM NG/ccm-core: Added methods for setting the index object of a category to the CategoryManager

git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4565 8810af33-2d31-482b-a856-94f89814c4df

Former-commit-id: 90b8b206e5
pull/2/head
jensp 2017-02-15 09:30:54 +00:00
parent 2a9884b592
commit 97c3f05a9e
2 changed files with 258 additions and 154 deletions

View File

@ -56,12 +56,36 @@ import org.libreccm.security.Relation;
name = "Categorization.find", name = "Categorization.find",
query = "SELECT c FROM Categorization c " query = "SELECT c FROM Categorization c "
+ "WHERE c.category = :category " + "WHERE c.category = :category "
+ "AND c.categorizedObject = :object"), + "AND c.categorizedObject = :object")
,
@NamedQuery(
name = "Categorization.isAssignedTo",
query = "SELECT (CASE WHEN COUNT(c) > 0 THEN true ELSE false END) "
+ "FROM Categorization c "
+ "WHERE c.category = :category "
+ "AND c.categorizedObject = :object")
,
@NamedQuery(
name = "Categorization.isAssignedToWithType",
query = "SELECT (CASE WHEN COUNT(c) > 0 THEN true ELSE false END) "
+ "FROM Categorization c "
+ "WHERE c.category = :category "
+ "AND c.categorizedObject = :object "
+ "AND c.type = :type")
,
@NamedQuery( @NamedQuery(
name = "Categorization.findIndexObject", name = "Categorization.findIndexObject",
query = "SELECT c.categorizedObject FROM Categorization c " query = "SELECT c.categorizedObject FROM Categorization c "
+ "WHERE c.category = :category " + "WHERE c.category = :category "
+ "AND c.index = TRUE"), + "AND c.index = TRUE")
,
@NamedQuery(
name = "Categorization.findIndexObjectCategorization",
query = "SELECT c FROM Categorization c "
+ "WHERE c.category = :category "
+ "AND c.index = TRUE"
)
,
@NamedQuery( @NamedQuery(
name = "Categorization.hasIndexObject", name = "Categorization.hasIndexObject",
query = "SELECT (CASE WHEN COUNT(c.categorizedObject) > 0 THEN true " query = "SELECT (CASE WHEN COUNT(c.categorizedObject) > 0 THEN true "

View File

@ -32,6 +32,7 @@ import org.libreccm.security.Shiro;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.Optional; import java.util.Optional;
import java.util.StringJoiner; import java.util.StringJoiner;
@ -148,7 +149,7 @@ public class CategoryManager {
// Saving a category requires the manage_category privilege which // Saving a category requires the manage_category privilege which
// may has not been granted to a user which is allowed to assign objects // may has not been granted to a user which is allowed to assign objects
// to a category. Therefore we bypass the this authorisation check here // to a category. Therefore we bypass the authorisation check here
// by executing CategoryRepository#save(Category) as the system user. // by executing CategoryRepository#save(Category) as the system user.
shiro.getSystemUser().execute(() -> { shiro.getSystemUser().execute(() -> {
entityManager.persist(categorization); entityManager.persist(categorization);
@ -157,6 +158,28 @@ public class CategoryManager {
}); });
} }
public boolean isAssignedToCategory(final Category category,
final CcmObject object) {
final TypedQuery<Boolean> query = entityManager.createNamedQuery(
"Categorization.isAssignedTo", Boolean.class);
query.setParameter("category", category);
query.setParameter("object", object);
return query.getSingleResult();
}
public boolean isAssignedToCategoryWithType(final Category category,
final CcmObject object,
final String type) {
final TypedQuery<Boolean> query = entityManager.createNamedQuery(
"Categorization.isAssignedTo", Boolean.class);
query.setParameter("category", category);
query.setParameter("object", object);
query.setParameter("type", type);
return query.getSingleResult();
}
/** /**
* Removes a object from a category. Additionally to removing the object * Removes a object from a category. Additionally to removing the object
* from the category this method also upgrades the order of all objects * from the category this method also upgrades the order of all objects
@ -239,14 +262,13 @@ public class CategoryManager {
* The value of the {@code order} property of the object after the provided * The value of the {@code order} property of the object after the provided
* is decreased by one. Effectively the two objects are swapped. * is decreased by one. Effectively the two objects are swapped.
* *
* @param object The object which {@code order} property is decreased. * @param object The object which {@code order} property is decreased. Can't
* Can't be {@code null}. * be {@code null}.
* @param category The category to which the object is assigned. Can't be * @param category The category to which the object is assigned. Can't be
* {@code null}. * {@code null}.
* *
* @throws ObjectNotAssignedToCategoryException Throws if the provided * @throws ObjectNotAssignedToCategoryException Throws if the provided
* object is not assigned to * object is not assigned to the provided category.
* the provided category.
*/ */
@AuthorizationRequired @AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@ -313,14 +335,13 @@ public class CategoryManager {
* The value of the {@code order} property of the object before the provided * The value of the {@code order} property of the object before the provided
* is increased by one. Effectively the two objects are swapped. * is increased by one. Effectively the two objects are swapped.
* *
* @param object The object which {@code order} property is decreased. * @param object The object which {@code order} property is decreased. Can't
* Can't be {@code null}. * be {@code null}.
* @param category The category to which the object is assigned. Can't be * @param category The category to which the object is assigned. Can't be
* {@code null}. * {@code null}.
* *
* @throws ObjectNotAssignedToCategoryException Throws if the provided * @throws ObjectNotAssignedToCategoryException Throws if the provided
* object is not assigned to * object is not assigned to the provided category.
* the provided category.
*/ */
@AuthorizationRequired @AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@ -393,9 +414,7 @@ public class CategoryManager {
* are assigned. Can't be {@code null}. * are assigned. Can't be {@code null}.
* *
* @throws ObjectNotAssignedToCategoryException Thrown if one or both of the * @throws ObjectNotAssignedToCategoryException Thrown if one or both of the
* provided objects are not * provided objects are not assigned to the provided category.
* assigned to the provided
* category.
*/ */
// public void swapObjects(final CcmObject objectA, // public void swapObjects(final CcmObject objectA,
// final CcmObject objectB, // final CcmObject objectB,
@ -471,8 +490,7 @@ public class CategoryManager {
* @param parentCategory The parent category. Can't be {@code null}. * @param parentCategory The parent category. Can't be {@code null}.
* *
* @throws IllegalArgumentException If the provided subcategory is not * @throws IllegalArgumentException If the provided subcategory is not
* assigned to the provided parent * assigned to the provided parent category.
* category.
*/ */
@AuthorizationRequired @AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@ -510,14 +528,13 @@ public class CategoryManager {
* objects is decreased by one. If the object is the last one this method * objects is decreased by one. If the object is the last one this method
* has not effect. * has not effect.
* *
* @param subCategory The category which order property is increased. * @param subCategory The category which order property is increased. Can't
* Can't be {@code null}. * be {@code null}.
* @param parentCategory The parent category of the category. Can't be * @param parentCategory The parent category of the category. Can't be
* {@code null}. * {@code null}.
* *
* @throws IllegalArgumentException If the provided subcategory is not a * @throws IllegalArgumentException If the provided subcategory is not a
* subcategory of the provided parent * subcategory of the provided parent category.
* category.
*/ */
@AuthorizationRequired @AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@ -585,14 +602,13 @@ public class CategoryManager {
* preceeding objects is increased by one. If the object is the last one * preceeding objects is increased by one. If the object is the last one
* this method has not effect. * this method has not effect.
* *
* @param subCategory The category which order property is increased. * @param subCategory The category which order property is increased. Can't
* Can't be {@code null}. * be {@code null}.
* @param parentCategory The parent category of the category. Can't be * @param parentCategory The parent category of the category. Can't be
* {@code null}. * {@code null}.
* *
* @throws IllegalArgumentException If the provided subcategory is not a * @throws IllegalArgumentException If the provided subcategory is not a
* subcategory of the provided parent * subcategory of the provided parent category.
* category.
*/ */
@AuthorizationRequired @AuthorizationRequired
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
@ -680,11 +696,6 @@ public class CategoryManager {
} }
public boolean hasIndexObject(final Category category) { public boolean hasIndexObject(final Category category) {
// final TypedQuery<Long> hasIndexItemQuery = entityManager
// .createNamedQuery("Categorization.hasIndexObject", Long.class);
// hasIndexItemQuery.setParameter("category", category);
// final long indexItems = hasIndexItemQuery.getSingleResult();
// return indexItems > 0;
final TypedQuery<Boolean> query = entityManager final TypedQuery<Boolean> query = entityManager
.createNamedQuery("Categorization.hasIndexObject", Boolean.class); .createNamedQuery("Categorization.hasIndexObject", Boolean.class);
query.setParameter("category", category); query.setParameter("category", category);
@ -715,4 +726,73 @@ public class CategoryManager {
} }
} }
@Transactional(Transactional.TxType.REQUIRED)
public void setIndexObject(final Category category,
final CcmObject object)
throws ObjectNotAssignedToCategoryException{
Objects.requireNonNull(category);
Objects.requireNonNull(object);
// First, ensure that the provided object is assigned to the provided
// category.
if (!isAssignedToCategory(category, object)) {
throw new ObjectNotAssignedToCategoryException(String.format(
"The provided object %s is not assigned to the provided category %s "
+ "and can therefore not be an index object of the category.",
Objects.toString(category),
Objects.toString(object)));
}
// If the category has already an index object we need to reset the
// index for the categorisation to ensure that the category has only
// one index object
resetIndexObject(category);
// Now find the categorization for the provided object and set
// index = true
final TypedQuery<Categorization> query = entityManager.createNamedQuery(
"Categorization.find", Categorization.class);
query.setParameter("category", category);
query.setParameter("object", object);
final Categorization categorization;
try {
categorization = query.getSingleResult();
} catch(NoResultException ex) {
throw new ObjectNotAssignedToCategoryException(String.format(
"Strange. The previous check if the provided object %s is "
+ "assigned to the provided category %s returned "
+ "true, but the query for the categorization "
+ "object returned no result. This should not happen. "
+ "Please report a bug.",
Objects.toString(object),
Objects.toString(category)),
ex);
}
categorization.setIndex(true);
entityManager.merge(categorization);
}
/**
* Resets the index object of a category to none. This methods retrieves all
* categorisations with {@link Categorization#index}{@code == true} of the
* provided category and set them to {@code false}. Therefore this method
* can also be used if a category has got multiple index objects due to some
* circumstances which may causes problems.
*
* @param category The category which index object should be reset.
*/
@Transactional(Transactional.TxType.REQUIRED)
public void resetIndexObject(final Category category) {
final TypedQuery<Categorization> query = entityManager.createNamedQuery(
"Categorization.findIndexObjectCategorization",
Categorization.class);
query.setParameter("category", category);
final List<Categorization> result = query.getResultList();
result.forEach(categorization -> categorization.setIndex(false));
result.forEach(categorization -> entityManager.merge(categorization));
}
} }