Skip to content

Commit

Permalink
Fix delete draft removing original questions (#1205)
Browse files Browse the repository at this point in the history
* Fix delete draft removing original questions

* test

---------

Co-authored-by: Michael Peels <[email protected]>
  • Loading branch information
mpeels and Michael Peels authored Mar 19, 2024
1 parent 95399a8 commit ce8549c
Show file tree
Hide file tree
Showing 3 changed files with 132 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,43 +14,41 @@
@Service
public class PageDeletor {

private final EntityManager entityManager;

private final PageUidFinder pageUidFinder;

public PageDeletor(
final EntityManager entityManager,
final PageUidFinder pageUidFinder) {
this.entityManager = entityManager;
this.pageUidFinder = pageUidFinder;
private final EntityManager entityManager;
private final PageUidFinder pageUidFinder;

public PageDeletor(
final EntityManager entityManager,
final PageUidFinder pageUidFinder) {
this.entityManager = entityManager;
this.pageUidFinder = pageUidFinder;
}

@Transactional
public PageDeleteResponse deletePageDraft(Long id) {
WaTemplate page = entityManager.find(WaTemplate.class, id);

if (page == null) {
throw new PageNotFoundException();
}

if (page.getTemplateType().equals(PageConstants.DRAFT)) {

@Transactional
public PageDeleteResponse deletePageDraft(Long id) {
WaTemplate page = entityManager.find(WaTemplate.class, id);

if (page == null) {
throw new PageNotFoundException();
}

if (page.getTemplateType().equals(PageConstants.DRAFT)) {

Long publishedWithDraft = pageUidFinder.findTemplateByType(page.getFormCd(), PageConstants.PUBLISHED_WITH_DRAFT);
Long publishedWithDraft = pageUidFinder.findTemplateByType(page.getFormCd(), PageConstants.PUBLISHED_WITH_DRAFT);

WaTemplate publishedWithDraftPage = entityManager.find(WaTemplate.class, publishedWithDraft);
WaTemplate publishedWithDraftPage = entityManager.find(WaTemplate.class, publishedWithDraft);

if (publishedWithDraftPage == null) {
throw new PageNotFoundException();
}
publishedWithDraftPage.setTemplateType(PageConstants.PUBLISHED);
if (publishedWithDraftPage == null) {
throw new PageNotFoundException();
}
publishedWithDraftPage.setTemplateType(PageConstants.PUBLISHED);

entityManager.remove(page);
entityManager.remove(page);

} else {
throw new PageUpdateException(PageConstants.DELETE_DRAFT_FAIL);
}
return new PageDeleteResponse(page.getId(), PageConstants.DRAFT_DELETE_SUCCESS);
} else {
throw new PageUpdateException(PageConstants.DELETE_DRAFT_FAIL);
}
return new PageDeleteResponse(page.getId(), PageConstants.DRAFT_DELETE_SUCCESS);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,63 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Optional;
import java.util.Set;


import org.springframework.stereotype.Service;

import gov.cdc.nbs.questionbank.entity.PageCondMapping;
import gov.cdc.nbs.questionbank.entity.WaTemplate;
import gov.cdc.nbs.questionbank.entity.WaUiMetadata;
import gov.cdc.nbs.questionbank.entity.pagerule.WaRuleMetadata;
import gov.cdc.nbs.questionbank.entity.repository.PageCondMappingRepository;
import gov.cdc.nbs.questionbank.entity.repository.WaTemplateRepository;
import gov.cdc.nbs.questionbank.entity.repository.WaUiMetadataRepository;
import gov.cdc.nbs.questionbank.page.exception.PageUpdateException;
import gov.cdc.nbs.questionbank.page.response.PageStateResponse;
import gov.cdc.nbs.questionbank.page.util.PageConstants;
import lombok.RequiredArgsConstructor;
import gov.cdc.nbs.questionbank.pagerules.repository.WaRuleMetaDataRepository;

@Service
@RequiredArgsConstructor
public class PageStateChanger {


private final WaTemplateRepository templateRepository;


private final WaUiMetadataRepository waUiMetadataRepository;


private final PageCondMappingRepository pageConMappingRepository;


private final WaRuleMetaDataRepository waRuleMetaDataRepository;

public PageStateChanger(
final WaTemplateRepository templateRepository,
final WaUiMetadataRepository waUiMetadataRepository,
final PageCondMappingRepository pageConMappingRepository,
final WaRuleMetaDataRepository waRuleMetaDataRepository) {
this.templateRepository = templateRepository;
this.waUiMetadataRepository = waUiMetadataRepository;
this.pageConMappingRepository = pageConMappingRepository;
this.waRuleMetaDataRepository = waRuleMetaDataRepository;
}

public PageStateResponse savePageAsDraft(Long id) {
PageStateResponse response = new PageStateResponse();
try {
Optional<WaTemplate> result = templateRepository.findById(id);
if (result.isPresent()) {
WaTemplate page = result.get();
if (page.getTemplateType().equals(PageConstants.PUBLISHED_WITH_DRAFT)) {
throw new PageUpdateException(PageConstants.SAVE_DRAFT_NOCHANGE);
}
WaTemplate draftPage = createDraftCopy(page);
page.setTemplateType(PageConstants.PUBLISHED_WITH_DRAFT);

page = templateRepository.save(page);
draftPage = templateRepository.save(draftPage);
pageConMappingRepository.saveAll(draftPage.getConditionMappings());

List<WaUiMetadata> draftMappings = copyWaTemplateUIMetaData(page, draftPage);

waUiMetadataRepository.saveAll(draftMappings);
response.setMessage(PageConstants.SAVE_DRAFT_SUCCESS);
response.setTemplateId(draftPage.getId());
} else {
throw new PageUpdateException(PageConstants.SAVE_DRAFT_FAIL);
WaTemplate page =
templateRepository.findById(id).orElseThrow(() -> new PageUpdateException(PageConstants.SAVE_DRAFT_FAIL));
if (page.getTemplateType().equals(PageConstants.PUBLISHED_WITH_DRAFT)) {
throw new PageUpdateException(PageConstants.SAVE_DRAFT_NOCHANGE);
}
WaTemplate draftPage = createDraftCopy(page);
page.setTemplateType(PageConstants.PUBLISHED_WITH_DRAFT);

page = templateRepository.save(page);
draftPage = templateRepository.save(draftPage);
pageConMappingRepository.saveAll(draftPage.getConditionMappings());

List<WaUiMetadata> draftMappings = copyWaTemplateUIMetaData(page, draftPage);
waUiMetadataRepository.saveAll(draftMappings);

List<WaRuleMetadata> rules = copyRules(id, draftPage.getId());
waRuleMetaDataRepository.saveAll(rules);

response.setMessage(PageConstants.SAVE_DRAFT_SUCCESS);
response.setTemplateId(draftPage.getId());

} catch (PageUpdateException e) {
throw e;
} catch (Exception e) {
Expand All @@ -67,13 +68,22 @@ public PageStateResponse savePageAsDraft(Long id) {
return response;
}


List<WaRuleMetadata> copyRules(long publishedPage, long newPage) {
List<WaRuleMetadata> initialRuleMappings = new ArrayList<>();
List<WaRuleMetadata> rules = waRuleMetaDataRepository.findByWaTemplateUid(publishedPage);
for (WaRuleMetadata original : rules) {
WaRuleMetadata clone = WaRuleMetadata.clone(original);
clone.setWaTemplateUid(newPage);
initialRuleMappings.add(clone);
}
return initialRuleMappings;
}

public List<WaUiMetadata> copyWaTemplateUIMetaData(WaTemplate page, WaTemplate clonePage) {
List<WaUiMetadata> draftMappings = new ArrayList<>();
List<WaUiMetadata> pages = waUiMetadataRepository.findAllByWaTemplateUid(page);
for (WaUiMetadata original : pages) {
WaUiMetadata clone = original;
WaUiMetadata clone = WaUiMetadata.clone(original);
clone.setWaTemplateUid(clonePage);
draftMappings.add(clone);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package gov.cdc.nbs.questionbank.page;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertThrows;
import static org.mockito.Mockito.when;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import gov.cdc.nbs.questionbank.entity.WaTemplate;
import gov.cdc.nbs.questionbank.entity.pagerule.WaRuleMetadata;
import gov.cdc.nbs.questionbank.entity.repository.PageCondMappingRepository;
import gov.cdc.nbs.questionbank.entity.repository.WaTemplateRepository;
import gov.cdc.nbs.questionbank.entity.repository.WaUiMetadataRepository;
import gov.cdc.nbs.questionbank.page.exception.PageUpdateException;
import gov.cdc.nbs.questionbank.pagerules.repository.WaRuleMetaDataRepository;

@ExtendWith(MockitoExtension.class)
class PageStateChangerTest {

@Mock
private WaTemplateRepository templateRepository;
@Mock
private WaUiMetadataRepository waUiMetadataRepository;
@Mock
private PageCondMappingRepository pageConMappingRepository;
@Mock
private WaRuleMetaDataRepository waRuleMetaDataRepository;
@InjectMocks
private PageStateChanger changer;


@Test
void should_copy_rules() {
when(waRuleMetaDataRepository.findByWaTemplateUid(1l)).thenReturn(rules());
List<WaRuleMetadata> newRules = changer.copyRules(1l, 2l);
assertThat(newRules).hasSize(2).allSatisfy(c -> assertThat(c.getWaTemplateUid()).isEqualTo(2));
}

@Test
void should_throw_exception() {
WaTemplate page = new WaTemplate();
page.setTemplateType("Published With Draft");
when(templateRepository.findById(1l)).thenReturn(Optional.of(page));
assertThrows(PageUpdateException.class, () -> changer.savePageAsDraft(1l));
}

List<WaRuleMetadata> rules() {
WaRuleMetadata rule1 = new WaRuleMetadata();
rule1.setId(1l);
WaRuleMetadata rule2 = new WaRuleMetadata();
rule2.setId(2l);
return Arrays.asList(rule1, rule2);
}
}

0 comments on commit ce8549c

Please sign in to comment.