Skip to content

Commit

Permalink
Remove not needed JpaDistributionSetType.compatibleToTargetTypes (#2193)
Browse files Browse the repository at this point in the history
Signed-off-by: Avgustin Marinov <[email protected]>
  • Loading branch information
avgustinmm authored Jan 10, 2025
1 parent e1f43c8 commit 0e4efe0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 70 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
package org.eclipse.hawkbit.mgmt.rest.resource;

import java.util.List;
import java.util.stream.Collectors;

import lombok.extern.slf4j.Slf4j;
import org.eclipse.hawkbit.mgmt.json.model.PagedList;
Expand Down Expand Up @@ -119,7 +118,7 @@ public ResponseEntity<Void> removeCompatibleDistributionSet(final Long targetTyp
public ResponseEntity<Void> addCompatibleDistributionSets(
final Long targetTypeId, final List<MgmtDistributionSetTypeAssignment> distributionSetTypeIds) {
targetTypeManagement.assignCompatibleDistributionSetTypes(
targetTypeId, distributionSetTypeIds.stream().map(MgmtDistributionSetTypeAssignment::getId).collect(Collectors.toList()));
targetTypeId, distributionSetTypeIds.stream().map(MgmtDistributionSetTypeAssignment::getId).toList());
return ResponseEntity.ok().build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,19 +99,10 @@ default boolean containsOptionalModuleType(final Long softwareModuleTypeId) {
return getOptionalModuleTypes().stream().anyMatch(element -> element.getId().equals(softwareModuleTypeId));
}

/**
* Compares the modules of this {@link DistributionSetType} and the given
* one.
*
* @param dsType to compare with
* @return <code>true</code> if the lists are identical.
*/
boolean areModuleEntriesIdentical(DistributionSetType dsType);

/**
* @param distributionSet to check for completeness
* @return <code>true</code> if the all mandatory software module types are
* in the system.
*/
boolean checkComplete(DistributionSet distributionSet);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.ToLongFunction;

import org.eclipse.hawkbit.repository.QuotaManagement;
import org.eclipse.hawkbit.repository.TargetTypeFields;
Expand Down Expand Up @@ -197,7 +198,7 @@ public TargetType assignCompatibleDistributionSetTypes(final long id,
}

final JpaTargetType type = getByIdAndThrowIfNotFound(id);
assertDistributionSetTypeQuota(id, distributionSetTypeIds.size());
assertDistributionSetTypeQuota(id, distributionSetTypeIds.size(), typeId -> type.getCompatibleDistributionSetTypes().size());
dsTypes.forEach(type::addCompatibleDistributionSetType);

return targetTypeRepository.save(type);
Expand All @@ -224,7 +225,8 @@ private void assertDistributionSetTypeExists(final Long typeId) {

private JpaTargetType getByIdAndThrowIfNotFound(final Long id) {
return targetTypeRepository
.findById(id).orElseThrow(() -> new EntityNotFoundException(TargetType.class, id));
.findById(id)
.orElseThrow(() -> new EntityNotFoundException(TargetType.class, id));
}

/**
Expand All @@ -235,8 +237,9 @@ private JpaTargetType getByIdAndThrowIfNotFound(final Long id) {
* @param requested number of distribution set types to check
* @throws AssignmentQuotaExceededException if the software module type quota is exceeded
*/
private void assertDistributionSetTypeQuota(final long id, final int requested) {
QuotaHelper.assertAssignmentQuota(id, requested, quotaManagement.getMaxDistributionSetTypesPerTargetType(),
DistributionSetType.class, TargetType.class, targetTypeRepository::countDsSetTypesById);
private void assertDistributionSetTypeQuota(final long id, final int requested, final ToLongFunction<Long> countFct) {
QuotaHelper.assertAssignmentQuota(
id, requested, quotaManagement.getMaxDistributionSetTypesPerTargetType(),
DistributionSetType.class, TargetType.class, countFct);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ public class JpaDistributionSetType extends AbstractJpaTypeEntity implements Dis

@OneToMany(
mappedBy = "dsType", targetEntity = DistributionSetTypeElement.class,
fetch = FetchType.EAGER, cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true)
cascade = { CascadeType.PERSIST, CascadeType.MERGE, CascadeType.REMOVE }, orphanRemoval = true)
private Set<DistributionSetTypeElement> elements = new HashSet<>();

@Setter
@Getter
@Column(name = "deleted")
private boolean deleted;

@ManyToMany(mappedBy = "distributionSetTypes", targetEntity = JpaTargetType.class, fetch = FetchType.LAZY)
private List<TargetType> compatibleToTargetTypes;

public JpaDistributionSetType(final String key, final String name, final String description) {
this(key, name, description, null);
}
Expand All @@ -95,21 +92,6 @@ public Set<SoftwareModuleType> getOptionalModuleTypes() {
.collect(Collectors.toSet());
}

@Override
public boolean areModuleEntriesIdentical(final DistributionSetType dsType) {
if (dsType instanceof JpaDistributionSetType jpaDsType) {
if (isOneModuleListEmpty(jpaDsType)) {
return false;
} else if (areBothModuleListsEmpty(jpaDsType)) {
return true;
} else {
return new HashSet<>(jpaDsType.elements).equals(elements);
}
} else {
return false;
}
}

@Override
public boolean checkComplete(final DistributionSet distributionSet) {
final List<SoftwareModuleType> smTypes = distributionSet.getModules().stream()
Expand Down Expand Up @@ -163,15 +145,6 @@ public void fireDeleteEvent() {
getTenant(), getId(), getClass(), EventPublisherHolder.getInstance().getApplicationId()));
}

private boolean isOneModuleListEmpty(final JpaDistributionSetType dsType) {
return (!CollectionUtils.isEmpty(dsType.elements) && CollectionUtils.isEmpty(elements)) ||
(CollectionUtils.isEmpty(dsType.elements) && !CollectionUtils.isEmpty(elements));
}

private boolean areBothModuleListsEmpty(final JpaDistributionSetType dsType) {
return CollectionUtils.isEmpty(dsType.elements) && CollectionUtils.isEmpty(elements);
}

private JpaDistributionSetType setModuleType(final SoftwareModuleType smType, final boolean mandatory) {
if (elements.isEmpty()) {
elements.add(new DistributionSetTypeElement(this, (JpaSoftwareModuleType) smType, mandatory));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,38 +22,17 @@
import org.springframework.transaction.annotation.Transactional;

/**
* {@link PagingAndSortingRepository} and {@link org.springframework.data.repository.CrudRepository} for
* {@link JpaTargetType}.
* {@link PagingAndSortingRepository} and {@link org.springframework.data.repository.CrudRepository} for {@link JpaTargetType}.
*/
@Transactional(readOnly = true)
public interface TargetTypeRepository
extends BaseEntityRepository<JpaTargetType> {
public interface TargetTypeRepository extends BaseEntityRepository<JpaTargetType> {

/**
* Counts the distributions set types compatible with that type
* <p/>
* No access control applied.
*
* @param id target type id
* @return the count
*/
@Query(value = "SELECT COUNT (t.id) FROM JpaDistributionSetType t JOIN t.compatibleToTargetTypes tt WHERE tt.id = :id")
long countDsSetTypesById(@Param("id") Long id);

/**
* @param dsTypeId to search for
* @return all {@link TargetType}s in the repository with given
* {@link TargetType#getName()}
*/
default List<JpaTargetType> findByDsType(@Param("id") final Long dsTypeId) {
return findAll(Specification.where(TargetTypeSpecification.hasDsSetType(dsTypeId)));
}

/**
* @param tenant Tenant
*/
@Modifying
@Transactional
@Query("DELETE FROM JpaTargetType t WHERE t.tenant = :tenant")
void deleteByTenant(@Param("tenant") String tenant);
}
}

0 comments on commit 0e4efe0

Please sign in to comment.