diff --git a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/SoftwareModuleManagement.java b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/SoftwareModuleManagement.java index b305fc7b7..7ec58403f 100644 --- a/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/SoftwareModuleManagement.java +++ b/hawkbit-repository/hawkbit-repository-api/src/main/java/org/eclipse/hawkbit/repository/SoftwareModuleManagement.java @@ -44,23 +44,6 @@ import org.springframework.security.access.prepost.PreAuthorize; public interface SoftwareModuleManagement extends RepositoryManagement { - /** - * Counts {@link SoftwareModule}s with given - * {@link SoftwareModule#getName()} or {@link SoftwareModule#getVersion()} - * and {@link SoftwareModule#getType()} that are not marked as deleted. - * - * @param searchText - * to search for in name and version - * @param typeId - * to filter the result by type - * @return number of found {@link SoftwareModule}s - * - * @throws EntityNotFoundException - * if software module type with given ID does not exist - */ - @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY) - long countByTextAndType(String searchText, Long typeId); - /** * Creates a list of software module meta data entries. * @@ -281,37 +264,6 @@ public interface SoftwareModuleManagement Page findMetaDataByRsql(@NotNull Pageable pageable, long id, @NotNull String rsqlParam); - /** - * Filter {@link SoftwareModule}s with given - * {@link SoftwareModule#getName()} or {@link SoftwareModule#getVersion()} - * search text and {@link SoftwareModule#getType()} that are not marked as - * deleted and sort them by means of given distribution set related modules - * on top of the list. - * - * After that the modules are sorted by by default by - * {@link SoftwareModule#getName()} and {@link SoftwareModule#getVersion()} - * in ascending order if no other sorting is provided in {@link Pageable}. - * If sorting is provided in {@link Pageable} parameter the provided sorting - * is used. - * - * @param pageable - * page parameter - * @param orderByDistributionSetId - * the ID of distribution set to be ordered on top - * @param searchText - * filtered as "like" on {@link SoftwareModule#getName()} - * @param typeId - * filtered as "equal" on {@link SoftwareModule#getType()} - * - * @return the page of found {@link SoftwareModule} - * - * @throws EntityNotFoundException - * if given software module type does not exist - */ - @PreAuthorize(SpringEvalExpressions.HAS_AUTH_READ_REPOSITORY) - Slice findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc( - @NotNull Pageable pageable, long orderByDistributionSetId, String searchText, Long typeId); - /** * Retrieves the {@link SoftwareModule}s by their {@link SoftwareModuleType} * . diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaSoftwareModuleManagement.java b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaSoftwareModuleManagement.java index d62e1843a..4025172a8 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaSoftwareModuleManagement.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/main/java/org/eclipse/hawkbit/repository/jpa/management/JpaSoftwareModuleManagement.java @@ -93,7 +93,6 @@ import org.springframework.validation.annotation.Validated; /** * JPA implementation of {@link SoftwareModuleManagement}. - * */ @Transactional(readOnly = true) @Validated @@ -332,129 +331,6 @@ public class JpaSoftwareModuleManagement implements SoftwareModuleManagement { smFilterNameAndVersionEntries[1]); } - /** - * @deprecated Used only in UI which is to be removed - */ - @Deprecated(forRemoval = true) - @Override - // In the interface org.springframework.data.domain.Pageable.getSort the - // return value is not guaranteed to be non-null, therefore a null check is - // necessary otherwise we rely on the implementation but this could change. - @SuppressWarnings({ "squid:S2583", "squid:S2589" }) - public Slice findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc( - final Pageable pageable, final long orderByDistributionSetId, final String searchText, final Long typeId) { - final CriteriaBuilder cb = entityManager.getCriteriaBuilder(); - final CriteriaQuery query = cb.createTupleQuery(); - final Root smRoot = query.from(JpaSoftwareModule.class); - - final ListJoin assignedDsList = smRoot - .join(JpaSoftwareModule_.assignedTo, JoinType.LEFT); - - final Expression assignedCaseMax = cb.max( - cb. selectCase(assignedDsList.get(JpaDistributionSet_.id)).when(orderByDistributionSetId, 1).otherwise(0)); - - query.multiselect(smRoot.alias("sm"), assignedCaseMax.alias("assigned")); - - final Predicate[] specPredicate = specificationsToPredicate(buildSpecificationList(searchText, typeId), - smRoot, query, cb); - - if (specPredicate.length > 0) { - query.where(specPredicate); - } - - query.groupBy(smRoot); - - final Sort sort = pageable.getSort(); - final List orders = new ArrayList<>(); - orders.add(cb.desc(assignedCaseMax)); - if (sort == null || sort.isEmpty()) { - orders.add(cb.asc(smRoot.get(JpaSoftwareModule_.name))); - orders.add(cb.asc(smRoot.get(JpaSoftwareModule_.version))); - } else { - orders.addAll(QueryUtils.toOrders(sort, smRoot, cb)); - } - query.orderBy(orders); - - final int pageSize = pageable.getPageSize(); - final List smWithAssignedFlagList = entityManager.createQuery(query) - .setFirstResult((int) pageable.getOffset()).setMaxResults(pageSize).getResultList(); - final boolean hasNext = smWithAssignedFlagList.size() > pageSize; - - final List resultList = new ArrayList<>(); - - smWithAssignedFlagList.forEach(smWithAssignedFlag -> { - final JpaSoftwareModule softwareModule = smWithAssignedFlag.get("sm", JpaSoftwareModule.class); - try { - // check read access - if (softwareModuleRepository.getAccessController().map(accessController -> { - try { - accessController.assertOperationAllowed(AccessController.Operation.READ, softwareModule); - return true; - } catch (final InsufficientPermissionException e) { - return false; - } - }).orElse(true)) { - resultList.add(new AssignedSoftwareModule(softwareModule, - smWithAssignedFlag.get("assigned", Number.class).longValue() == 1)); - } - } catch (final InsufficientPermissionException e) { - // skip the entry - } - }); - - return new SliceImpl<>(Collections.unmodifiableList(resultList), pageable, hasNext); - } - - private List> buildSpecificationList(final String searchText, final Long typeId) { - final List> specList = new ArrayList<>(3); - if (!ObjectUtils.isEmpty(searchText)) { - specList.add(buildSmSearchQuerySpec(searchText)); - } - - if (typeId != null) { - assertSoftwareModuleTypeExists(typeId); - - specList.add(SoftwareModuleSpecification.equalType(typeId)); - } - specList.add(SoftwareModuleSpecification.isNotDeleted()); - return specList; - } - - private Predicate[] specificationsToPredicate(final List> specifications, - final Root root, final CriteriaQuery query, final CriteriaBuilder cb, - final Predicate... additionalPredicates) { - - return Stream.concat(specifications.stream().map(spec -> spec.toPredicate(root, query, cb)), - Arrays.stream(additionalPredicates)).toArray(Predicate[]::new); - } - - /** - * No access control applied - * - * @deprecated Used only in UI which is to be removed - */ - @Deprecated(forRemoval = true) - @Override - public long countByTextAndType(final String searchText, final Long typeId) { - final List> specList = new ArrayList<>(3); - - Specification spec = SoftwareModuleSpecification.isNotDeleted(); - specList.add(spec); - - if (!ObjectUtils.isEmpty(searchText)) { - specList.add(buildSmSearchQuerySpec(searchText)); - } - - if (null != typeId) { - assertSoftwareModuleTypeExists(typeId); - - spec = SoftwareModuleSpecification.equalType(typeId); - specList.add(spec); - } - - return JpaManagementHelper.countBySpec(softwareModuleRepository, specList); - } - @Override public Page findByAssignedTo(final Pageable pageable, final long distributionSetId) { assertDistributionSetExists(distributionSetId); diff --git a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleManagementTest.java b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleManagementTest.java index 4b361da2d..2e0658cbe 100644 --- a/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleManagementTest.java +++ b/hawkbit-repository/hawkbit-repository-jpa/src/test/java/org/eclipse/hawkbit/repository/jpa/management/SoftwareModuleManagementTest.java @@ -528,105 +528,6 @@ public class SoftwareModuleManagementTest extends AbstractJpaIntegrationTest { } } - @Test - @Description("Test verifies that results are returned based on given filter parameters and in the specified order.") - public void findSoftwareModuleOrderByDistributionModuleNameAscModuleVersionAsc() { - // test meta data - final SoftwareModuleType testType = softwareModuleTypeManagement - .create(entityFactory.softwareModuleType().create().key("thetype").name("thename").maxAssignments(100)); - DistributionSetType testDsType = distributionSetTypeManagement - .create(entityFactory.distributionSetType().create().key("key").name("name")); - - distributionSetTypeManagement.assignMandatorySoftwareModuleTypes(testDsType.getId(), - Collections.singletonList(osType.getId())); - testDsType = distributionSetTypeManagement.assignOptionalSoftwareModuleTypes(testDsType.getId(), - Collections.singletonList(testType.getId())); - - // found in test - final SoftwareModule unassigned = testdataFactory.createSoftwareModule("thetype", "unassignedfound", false); - final SoftwareModule one = testdataFactory.createSoftwareModule("thetype", "bfound", false); - final SoftwareModule two = testdataFactory.createSoftwareModule("thetype", "cfound", false); - final SoftwareModule differentName = testdataFactory.createSoftwareModule("thetype", "a", false); - - // ignored - final SoftwareModule deleted = testdataFactory.createSoftwareModule("thetype", "deleted", false); - final SoftwareModule four = testdataFactory.createSoftwareModuleOs("e"); - - final DistributionSet set = distributionSetManagement - .create(entityFactory.distributionSet().create().name("set").version("1").type(testDsType).modules( - List.of(one.getId(), two.getId(), deleted.getId(), four.getId(), differentName.getId()))); - softwareModuleManagement.delete(deleted.getId()); - - // with filter on name, version and module type - assertThat(softwareModuleManagement.findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc(PAGE, - set.getId(), "%found%", testType.getId()).getContent()) - .as("Found modules with given name, given module type and the assigned ones first") - .containsExactly(new AssignedSoftwareModule(one, true), new AssignedSoftwareModule(two, true), - new AssignedSoftwareModule(unassigned, false)); - - // with filter on name, version and module type, sorting defined by - // Pagerequest - assertThat(softwareModuleManagement.findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc( - PageRequest.of(0, 500, Sort.by(Direction.DESC, "name")), set.getId(), "%found%", testType.getId()) - .getContent()) - .as("Found modules with given name, given module type, the assigned ones first, ordered by name DESC") - .containsExactly(new AssignedSoftwareModule(two, true), new AssignedSoftwareModule(one, true), - new AssignedSoftwareModule(unassigned, false)); - - // with filter on module type only - assertThat(softwareModuleManagement - .findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc(PAGE, set.getId(), null, testType.getId()) - .getContent()).as("Found modules with given module type and the assigned ones first") - .containsExactly(new AssignedSoftwareModule(one, true), new AssignedSoftwareModule(two, true), - new AssignedSoftwareModule(differentName, true), new AssignedSoftwareModule(unassigned, false)); - - // without any filter - assertThat(softwareModuleManagement - .findAllOrderBySetAssignmentAndModuleNameAscModuleVersionAsc(PAGE, set.getId(), null, null) - .getContent()).as("Found modules with the assigned ones first") - .containsExactly(new AssignedSoftwareModule(one, true), new AssignedSoftwareModule(two, true), - new AssignedSoftwareModule(differentName, true), new AssignedSoftwareModule(four, true), - new AssignedSoftwareModule(unassigned, false)); - } - - @Test - @Description("Checks that number of modules is returned as expected based on given filters.") - public void countSoftwareModuleByFilters() { - // test meta data - final SoftwareModuleType testType = softwareModuleTypeManagement - .create(entityFactory.softwareModuleType().create().key("thetype").name("thename").maxAssignments(100)); - DistributionSetType testDsType = distributionSetTypeManagement - .create(entityFactory.distributionSetType().create().key("key").name("name")); - - distributionSetTypeManagement.assignMandatorySoftwareModuleTypes(testDsType.getId(), - Collections.singletonList(osType.getId())); - testDsType = distributionSetTypeManagement.assignOptionalSoftwareModuleTypes(testDsType.getId(), - Collections.singletonList(testType.getId())); - - // found in test - testdataFactory.createSoftwareModule("thetype", "unassignedfound", false); - final SoftwareModule one = testdataFactory.createSoftwareModule("thetype", "bfound", false); - final SoftwareModule two = testdataFactory.createSoftwareModule("thetype", "cfound", false); - final SoftwareModule differentName = testdataFactory.createSoftwareModule("thetype", "d", false); - - // ignored - final SoftwareModule deleted = testdataFactory.createSoftwareModule("thetype", "deleted", false); - final SoftwareModule four = testdataFactory.createSoftwareModuleOs("e"); - - distributionSetManagement - .create(entityFactory.distributionSet().create().name("set").version("1").type(testDsType).modules( - List.of(one.getId(), two.getId(), deleted.getId(), four.getId(), differentName.getId()))); - softwareModuleManagement.delete(deleted.getId()); - - // test - assertThat(softwareModuleManagement.countByTextAndType("%found%", testType.getId())) - .as("Number of modules with given name or version and type").isEqualTo(3); - assertThat(softwareModuleManagement.countByTextAndType(null, testType.getId())) - .as("Number of modules with given type").isEqualTo(4); - assertThat(softwareModuleManagement.countByTextAndType(null, null)).as("Number of modules overall") - .isEqualTo(5); - } - @Test @Description("Verfies that all undeleted software modules are found in the repository.") public void countSoftwareModuleTypesAll() {