TargetType management over common RepositoryManagement (#2581)

Signed-off-by: Avgustin Marinov <Avgustin.Marinov@bosch.com>
This commit is contained in:
Avgustin Marinov
2025-08-01 19:24:49 +03:00
committed by GitHub
parent c79e35b9de
commit a689733d4c
40 changed files with 435 additions and 762 deletions

View File

@@ -25,13 +25,12 @@ import org.eclipse.hawkbit.mgmt.rest.api.MgmtTargetTypeRestApi;
import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtDistributionSetTypeMapper;
import org.eclipse.hawkbit.mgmt.rest.resource.mapper.MgmtTargetTypeMapper;
import org.eclipse.hawkbit.mgmt.rest.resource.util.PagingUtility;
import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.TargetTypeManagement;
import org.eclipse.hawkbit.repository.TargetTypeManagement.Update;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.model.TargetType;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Slice;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
@@ -43,30 +42,28 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class MgmtTargetTypeResource implements MgmtTargetTypeRestApi {
private final TargetTypeManagement targetTypeManagement;
private final EntityFactory entityFactory;
private final TargetTypeManagement<? extends TargetType> targetTypeManagement;
private final MgmtTargetTypeMapper mgmtTargetTypeMapper;
public MgmtTargetTypeResource(final TargetTypeManagement targetTypeManagement, final EntityFactory entityFactory) {
public MgmtTargetTypeResource(
final TargetTypeManagement<? extends TargetType> targetTypeManagement, final MgmtTargetTypeMapper mgmtTargetTypeMapper) {
this.targetTypeManagement = targetTypeManagement;
this.entityFactory = entityFactory;
this.mgmtTargetTypeMapper = mgmtTargetTypeMapper;
}
@Override
public ResponseEntity<PagedList<MgmtTargetType>> getTargetTypes(
final String rsqlParam, final int pagingOffsetParam, final int pagingLimitParam, final String sortParam) {
final Pageable pageable = PagingUtility.toPageable(pagingOffsetParam, pagingLimitParam, sanitizeTargetTypeSortParam(sortParam));
final Slice<TargetType> findTargetTypesAll;
long countTargetTypesAll;
final Page<? extends TargetType> findTargetTypesAll;
if (rsqlParam != null) {
findTargetTypesAll = targetTypeManagement.findByRsql(rsqlParam, pageable);
countTargetTypesAll = ((Page<TargetType>) findTargetTypesAll).getTotalElements();
} else {
findTargetTypesAll = targetTypeManagement.findAll(pageable);
countTargetTypesAll = targetTypeManagement.count();
}
final List<MgmtTargetType> rest = MgmtTargetTypeMapper.toListResponse(findTargetTypesAll.getContent());
return ResponseEntity.ok(new PagedList<>(rest, countTargetTypesAll));
return ResponseEntity.ok(new PagedList<>(rest, targetTypeManagement.count()));
}
@Override
@@ -88,8 +85,9 @@ public class MgmtTargetTypeResource implements MgmtTargetTypeRestApi {
@Override
public ResponseEntity<MgmtTargetType> updateTargetType(final Long targetTypeId, final MgmtTargetTypeRequestBodyPut restTargetType) {
final TargetType updated = targetTypeManagement
.update(entityFactory.targetType().update(targetTypeId).name(restTargetType.getName())
.description(restTargetType.getDescription()).colour(restTargetType.getColour()));
.update(Update.builder().id(targetTypeId)
.name(restTargetType.getName()).description(restTargetType.getDescription()).colour(restTargetType.getColour())
.build());
final MgmtTargetType response = MgmtTargetTypeMapper.toResponse(updated);
MgmtTargetTypeMapper.addLinks(response);
return ResponseEntity.ok(response);
@@ -97,15 +95,14 @@ public class MgmtTargetTypeResource implements MgmtTargetTypeRestApi {
@Override
public ResponseEntity<List<MgmtTargetType>> createTargetTypes(final List<MgmtTargetTypeRequestBodyPost> targetTypes) {
final List<TargetType> createdTargetTypes = targetTypeManagement
.create(MgmtTargetTypeMapper.targetFromRequest(entityFactory, targetTypes));
final List<? extends TargetType> createdTargetTypes = targetTypeManagement.create(mgmtTargetTypeMapper.targetFromRequest(targetTypes));
return ResponseEntity.status(HttpStatus.CREATED).body(MgmtTargetTypeMapper.toListResponse(createdTargetTypes));
}
@Override
public ResponseEntity<List<MgmtDistributionSetType>> getCompatibleDistributionSets(final Long targetTypeId) {
final TargetType foundType = findTargetTypeWithExceptionIfNotFound(targetTypeId);
return ResponseEntity.ok(MgmtDistributionSetTypeMapper.toListResponse(foundType.getCompatibleDistributionSetTypes()));
return ResponseEntity.ok(MgmtDistributionSetTypeMapper.toListResponse(foundType.getDistributionSetTypes()));
}
@Override

View File

@@ -16,36 +16,46 @@ import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
import org.eclipse.hawkbit.mgmt.json.model.distributionsettype.MgmtDistributionSetTypeAssignment;
import org.eclipse.hawkbit.mgmt.json.model.targettype.MgmtTargetType;
import org.eclipse.hawkbit.mgmt.json.model.targettype.MgmtTargetTypeRequestBodyPost;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtRestConstants;
import org.eclipse.hawkbit.mgmt.rest.api.MgmtTargetTypeRestApi;
import org.eclipse.hawkbit.repository.EntityFactory;
import org.eclipse.hawkbit.repository.builder.TargetTypeCreate;
import org.eclipse.hawkbit.repository.DistributionSetTypeManagement;
import org.eclipse.hawkbit.repository.TargetTypeManagement;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
import org.eclipse.hawkbit.repository.model.TargetType;
import org.eclipse.hawkbit.rest.json.model.ResponseList;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
/**
* A mapper which maps repository model to RESTful model representation and back.
*/
@NoArgsConstructor(access = AccessLevel.PRIVATE)
@Service
public final class MgmtTargetTypeMapper {
public static List<TargetTypeCreate> targetFromRequest(
final EntityFactory entityFactory, final Collection<MgmtTargetTypeRequestBodyPost> targetTypesRest) {
private final DistributionSetTypeManagement<? extends DistributionSetType> distributionSetTypeManagement;
public MgmtTargetTypeMapper(final DistributionSetTypeManagement<? extends DistributionSetType> distributionSetTypeManagement) {
this.distributionSetTypeManagement = distributionSetTypeManagement;
}
public List<TargetTypeManagement.Create> targetFromRequest(final Collection<MgmtTargetTypeRequestBodyPost> targetTypesRest) {
if (targetTypesRest == null) {
return Collections.emptyList();
}
return targetTypesRest.stream()
.map(targetRest -> fromRequest(entityFactory, targetRest))
.map(this::fromRequest)
.toList();
}
public static List<MgmtTargetType> toListResponse(final List<TargetType> types) {
public static List<MgmtTargetType> toListResponse(final List<? extends TargetType> types) {
if (types == null) {
return Collections.emptyList();
}
@@ -67,17 +77,31 @@ public final class MgmtTargetTypeMapper {
.withRel(MgmtRestConstants.TARGETTYPE_V1_DS_TYPES).expand());
}
private static TargetTypeCreate fromRequest(final EntityFactory entityFactory,
final MgmtTargetTypeRequestBodyPost targetTypesRest) {
return entityFactory.targetType().create()
private TargetTypeManagement.Create fromRequest(final MgmtTargetTypeRequestBodyPost targetTypesRest) {
return TargetTypeManagement.Create.builder()
.name(targetTypesRest.getName()).description(targetTypesRest.getDescription())
.key(targetTypesRest.getKey()).colour(targetTypesRest.getColour())
.compatible(getDistributionSets(targetTypesRest));
.distributionSetTypes(getDistributionSets(targetTypesRest))
.build();
}
private static Collection<Long> getDistributionSets(final MgmtTargetTypeRequestBodyPost targetTypesRest) {
private Set<DistributionSetType> getDistributionSets(final MgmtTargetTypeRequestBodyPost targetTypesRest) {
return Optional.ofNullable(targetTypesRest.getCompatibledistributionsettypes())
.map(ds -> ds.stream().map(MgmtDistributionSetTypeAssignment::getId).toList())
.orElse(Collections.emptyList());
.map(compatibleDs -> compatibleDs.stream().map(MgmtDistributionSetTypeAssignment::getId).toList())
.map(this::getDistributionSetTypes)
.orElse(Collections.emptySet());
}
private Set<DistributionSetType> getDistributionSetTypes(final Collection<Long> distributionSetTypeId) {
if (CollectionUtils.isEmpty(distributionSetTypeId)) {
return Collections.emptySet();
}
final Collection<? extends DistributionSetType> type = distributionSetTypeManagement.get(distributionSetTypeId);
if (type.size() < distributionSetTypeId.size()) {
throw new EntityNotFoundException(SoftwareModuleType.class, distributionSetTypeId);
}
return type.stream().map(DistributionSetType.class::cast).collect(Collectors.toSet());
}
}