RED-9891: Re-creation of component-definition does not work
This commit is contained in:
parent
dc8abbba58
commit
2b7be87985
@ -9,6 +9,7 @@ import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
@ -47,7 +48,16 @@ public class ComponentDefinitionService {
|
||||
validateAddRequest(componentDefinitionAddRequests);
|
||||
|
||||
List<ComponentDefinitionEntity> componentEntities = new ArrayList<>();
|
||||
componentDefinitionAddRequests.forEach(componentDefinitionAddRequest -> componentEntities.add(componentDefinitionPersistenceService.insert(componentDefinitionAddRequest, dossierTemplateId)));
|
||||
componentDefinitionAddRequests.forEach(componentDefinitionAddRequest -> {
|
||||
Optional<String> optionalIdOfSoftDeleted = componentDefinitionPersistenceService.findIdByTechnicalNameAndSoftDeleted(componentDefinitionAddRequest.getTechnicalName());
|
||||
ComponentDefinitionEntity entity;
|
||||
if (optionalIdOfSoftDeleted.isEmpty()) {
|
||||
entity = componentDefinitionPersistenceService.insert(componentDefinitionAddRequest, dossierTemplateId);
|
||||
} else {
|
||||
entity = componentDefinitionPersistenceService.restoreComponent(dossierTemplateId, optionalIdOfSoftDeleted.get());
|
||||
}
|
||||
componentEntities.add(entity);
|
||||
});
|
||||
return componentEntities.stream()
|
||||
.map(componentDefinitionEntity -> MagicConverter.convert(componentDefinitionEntity, ComponentDefinition.class))
|
||||
.sorted(Comparator.comparing(ComponentDefinition::getRank))
|
||||
@ -57,7 +67,9 @@ public class ComponentDefinitionService {
|
||||
|
||||
private void validateAddRequest(List<ComponentDefinitionAddRequest> componentDefinitionAddRequests) {
|
||||
|
||||
long nrOfComponentsWithEmptyTechnicalName = componentDefinitionAddRequests.stream().filter(c -> StringUtils.isEmpty(c.getTechnicalName())).count();
|
||||
long nrOfComponentsWithEmptyTechnicalName = componentDefinitionAddRequests.stream()
|
||||
.filter(c -> StringUtils.isEmpty(c.getTechnicalName()))
|
||||
.count();
|
||||
if (nrOfComponentsWithEmptyTechnicalName != 0) {
|
||||
throw new BadRequestException("Technical name can not be empty for a component.");
|
||||
}
|
||||
@ -84,7 +96,7 @@ public class ComponentDefinitionService {
|
||||
if (includeSoftDeleted) {
|
||||
componentDefinitionEntities = componentDefinitionPersistenceService.findComponentsByDossierTemplateId(dossierTemplateId);
|
||||
} else {
|
||||
componentDefinitionEntities = componentDefinitionPersistenceService.findComponentsByDossierTemplateIdExcludeSoftDeleted(dossierTemplateId);
|
||||
componentDefinitionEntities = componentDefinitionPersistenceService.findByDossierTemplateIdAndNotSoftDeleted(dossierTemplateId);
|
||||
}
|
||||
|
||||
return componentDefinitionEntities.stream()
|
||||
@ -156,7 +168,7 @@ public class ComponentDefinitionService {
|
||||
validateDossierTemplateExists(dossierTemplateId);
|
||||
|
||||
List<ComponentDefinition> orderedComponents = new ArrayList<>();
|
||||
List<ComponentDefinitionEntity> existingComponents = componentDefinitionPersistenceService.findComponentsByDossierTemplateIdExcludeSoftDeleted(dossierTemplateId);
|
||||
List<ComponentDefinitionEntity> existingComponents = componentDefinitionPersistenceService.findByDossierTemplateIdAndNotSoftDeleted(dossierTemplateId);
|
||||
|
||||
Map<String, ComponentDefinitionEntity> componentMap = existingComponents.stream()
|
||||
.collect(Collectors.toMap(ComponentDefinitionEntity::getId, component -> component));
|
||||
@ -193,4 +205,5 @@ public class ComponentDefinitionService {
|
||||
throw new NotFoundException(String.format("DossierTemplate with Id %s not found.", dossierTemplateId));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -2,6 +2,7 @@ package com.iqser.red.service.persistence.management.v1.processor.service.persis
|
||||
|
||||
import java.time.OffsetDateTime;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
@ -36,6 +37,7 @@ public class ComponentDefinitionPersistenceService {
|
||||
return componentDefinitionRepository.saveAndFlush(componentDefinition);
|
||||
}
|
||||
|
||||
|
||||
public ComponentDefinitionEntity insert(ComponentDefinitionAddRequest component, int rank, String dossierTemplateId) {
|
||||
|
||||
ComponentDefinitionEntity componentDefinitionEntity = new ComponentDefinitionEntity();
|
||||
@ -90,6 +92,15 @@ public class ComponentDefinitionPersistenceService {
|
||||
}
|
||||
|
||||
|
||||
@Transactional
|
||||
public ComponentDefinitionEntity restoreComponent(String dossierTemplateId, String componentId) {
|
||||
|
||||
componentDefinitionRepository.updateSoftDeleteForIds(List.of(componentId), dossierTemplateId, null);
|
||||
return componentDefinitionRepository.findById(componentId)
|
||||
.orElseThrow(() -> new NotFoundException("Component with id: " + componentId + " not found"));
|
||||
}
|
||||
|
||||
|
||||
public ComponentDefinitionEntity findComponentByDossierTemplateIdAndComponentId(String dossierTemplateId, String componentId) {
|
||||
|
||||
return componentDefinitionRepository.findByIdAndDossierTemplateId(componentId, dossierTemplateId)
|
||||
@ -103,15 +114,15 @@ public class ComponentDefinitionPersistenceService {
|
||||
}
|
||||
|
||||
|
||||
public List<ComponentDefinitionEntity> findComponentsByDossierTemplateIdExcludeSoftDeleted(String dossierTemplateId) {
|
||||
|
||||
return componentDefinitionRepository.findByDossierTemplateIdAndSoftDeletedTimeIsNull(dossierTemplateId);
|
||||
}
|
||||
|
||||
|
||||
public List<ComponentDefinitionEntity> findByDossierTemplateIdAndNotSoftDeleted(String dossierTemplateId) {
|
||||
|
||||
return componentDefinitionRepository.findByDossierTemplateIdAndSoftDeletedTimeIsNull(dossierTemplateId);
|
||||
}
|
||||
|
||||
|
||||
public Optional<String> findIdByTechnicalNameAndSoftDeleted(String technicalName) {
|
||||
|
||||
return componentDefinitionRepository.findIdByTechnicalNameAndIsSoftDeleted(technicalName);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@ -37,4 +37,8 @@ public interface ComponentDefinitionRepository extends JpaRepository<ComponentDe
|
||||
@Query("SELECT COUNT(c) from ComponentDefinitionEntity c WHERE c.dossierTemplateId = :dossierTemplateId AND c.softDeleteTime IS NULL")
|
||||
int countByDossierTemplateId(@Param("dossierTemplateId") String dossierTemplateId);
|
||||
|
||||
@Query("SELECT c.id FROM ComponentDefinitionEntity c WHERE c.technicalName = :technicalName AND c.softDeleteTime IS NOT NULL")
|
||||
Optional<String> findIdByTechnicalNameAndIsSoftDeleted(@Param("technicalName") String technicalName);
|
||||
|
||||
|
||||
}
|
||||
|
||||
@ -15,6 +15,7 @@ import com.iqser.red.service.peristence.v1.server.integration.client.DossierTemp
|
||||
import com.iqser.red.service.peristence.v1.server.integration.service.DossierTemplateTesterAndProvider;
|
||||
import com.iqser.red.service.peristence.v1.server.integration.utils.AbstractPersistenceServerServiceTest;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.DossierTemplateModel;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.component.ComponentDefinition;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.component.ComponentDefinitionAddRequest;
|
||||
import com.iqser.red.service.persistence.service.v1.api.shared.model.component.ComponentDefinitionUpdateRequest;
|
||||
|
||||
@ -145,6 +146,27 @@ public class ComponentDefinitionTests extends AbstractPersistenceServerServiceTe
|
||||
assertTrue(unknownDossierError.getMessage().contains("DossierTemplate with Id 123 not found."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddComponentDefinitionAfterSoftDeletion() {
|
||||
|
||||
var dossierTemplate = dossierTemplateTesterAndProvider.provideTestTemplate();
|
||||
var componentDefinitionAddRequest = buildComponentDefinitionAddRequest(dossierTemplate);
|
||||
|
||||
var response = dossierTemplateExternalClient.createComponents(dossierTemplate.getId(), List.of(componentDefinitionAddRequest));
|
||||
assertEquals(response.size(), 1);
|
||||
|
||||
dossierTemplateExternalClient.deleteComponents(dossierTemplate.getId(), List.of(response.get(0).getId()));
|
||||
|
||||
var softDeletedComponent = dossierTemplateExternalClient.getComponent(dossierTemplate.getId(), response.get(0).getId());
|
||||
assertNotNull(softDeletedComponent.getSoftDeleteTime());
|
||||
List<ComponentDefinition> componentsIncludingSoftDeleted = dossierTemplateExternalClient.getComponents(dossierTemplate.getId(), true);
|
||||
List<ComponentDefinition> componentsExcludingSoftDeleted = dossierTemplateExternalClient.getComponents(dossierTemplate.getId(), false);
|
||||
assertEquals(componentsIncludingSoftDeleted.size(), componentsExcludingSoftDeleted.size() + 1);
|
||||
|
||||
response = dossierTemplateExternalClient.createComponents(dossierTemplate.getId(), List.of(componentDefinitionAddRequest));
|
||||
assertEquals(response.size(), 1);
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testRestoreDeletedComponentDefinitions() {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user