Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jasonpaige committed Sep 29, 2023
1 parent 091cb78 commit 095508a
Show file tree
Hide file tree
Showing 2 changed files with 166 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import org.apache.http.HttpStatus;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import uk.gov.hmcts.ccd.definition.store.rest.endpoint.TestingSupportController;
import uk.gov.hmcts.net.ccd.definition.store.BaseTest;

import java.io.InputStream;
Expand All @@ -20,6 +23,7 @@ public class TestingSupportControllerIT extends BaseTest {
private static final String CLEANUP_CASE_TYPE_URL = "/api/testing-support/cleanup-case-type/%s?caseTypeIds=%s";

private static final String CASE_TYPE_URL = "/api/data/case-type/%s";
private static final Logger LOG = LoggerFactory.getLogger(TestingSupportControllerIT.class);

@Test
public void shouldReturnCaseType() throws Exception {
Expand All @@ -32,10 +36,11 @@ public void shouldReturnCaseType() throws Exception {
.andReturn();
assertResponseCode(mvcResult, HttpStatus.SC_CREATED);

mockMvc.perform(MockMvcRequestBuilders.delete(
var deleteResult = mockMvc.perform(MockMvcRequestBuilders.delete(
String.format(CLEANUP_CASE_TYPE_URL, new BigInteger("123"), "TestAddressBookCase")))
.andExpect(MockMvcResultMatchers.status().isNoContent())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn();
assertResponseCode(deleteResult, HttpStatus.SC_OK);

mockMvc.perform(MockMvcRequestBuilders.get(String.format(CASE_TYPE_URL, "TestAddressBookCase-123")))
.andExpect(MockMvcResultMatchers.status().isNotFound())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import uk.gov.hmcts.ccd.definition.store.domain.service.casetype.CaseTypeService;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


@RestController
Expand Down Expand Up @@ -48,127 +50,169 @@ public void dataCaseTypeIdDelete(
var caseIdList = Arrays.stream(caseTypeIds.split(",")).toList();
var caseTypesWithChangeIds = caseIdList.stream().map(caseTypeId -> caseTypeId + "-" + changeId).toList();

var caseTypes = caseTypeService.findAllByCaseTypeIds(caseTypesWithChangeIds);
if (caseTypes.size() != caseIdList.size()) {
throw new NotFoundException("Unable to find case type");
}

Session session = sessionFactory.openSession();
session.beginTransaction();
String sql = """
DELETE FROM event_case_field_complex_type
WHERE event_case_field_id IN (SELECT id
FROM event_case_field
WHERE event_id IN (SELECT id
FROM event
WHERE
case_type_id IN :caseTypeIds)
);
DELETE FROM event_case_field
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN :caseTypeIds);
DELETE FROM challenge_question
WHERE case_type_id IN :caseTypeIds;
DELETE FROM display_group_case_field
WHERE display_group_id IN (SELECT id
FROM display_group
WHERE case_type_id IN :caseTypeIds);
DELETE FROM case_field_acl
WHERE case_field_id IN (SELECT id
FROM case_field
WHERE case_type_id IN :caseTypeIds);
DELETE FROM workbasket_case_field
WHERE case_type_id IN :caseTypeIds;
DELETE FROM workbasket_input_case_field
WHERE case_type_id IN :caseTypeIds;
DELETE FROM search_alias_field
WHERE case_type_id IN :caseTypeIds;
DELETE FROM search_result_case_field
WHERE case_type_id IN :caseTypeIds;
DELETE FROM search_input_case_field
WHERE case_type_id IN :caseTypeIds;
DELETE FROM search_cases_result_fields
WHERE case_type_id IN :caseTypeIds;
DELETE FROM complex_field_acl
WHERE case_field_id IN (SELECT id
FROM case_field
WHERE case_type_id IN :caseTypeIds);
DELETE FROM case_field
WHERE case_type_id IN :caseTypeIds;
DELETE FROM display_group
WHERE case_type_id IN :caseTypeIds;
ArrayList<String> sql = new ArrayList<String> (
Arrays.asList(
"""
DELETE FROM event_case_field_complex_type
WHERE event_case_field_id IN (SELECT id
FROM event_case_field
WHERE event_id IN (SELECT id
FROM event
WHERE
case_type_id IN ( :caseTypeIds ) )
)
""",
"""
DELETE FROM event_case_field
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM challenge_question
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM display_group_case_field
WHERE display_group_id IN (SELECT id
FROM display_group
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM case_field_acl
WHERE case_field_id IN (SELECT id
FROM case_field
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM workbasket_case_field
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM workbasket_input_case_field
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM search_alias_field
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM search_result_case_field
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM search_input_case_field
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM search_cases_result_fields
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM complex_field_acl
WHERE case_field_id IN (SELECT id
FROM case_field
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM case_field
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM display_group
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM event_webhook
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM event_pre_state
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM event_acl
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM event_post_state
WHERE case_event_id IN (SELECT id
FROM event
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM state_acl
WHERE state_id IN (SELECT id
FROM state
WHERE case_type_id IN ( :caseTypeIds ) )
""",
"""
DELETE FROM state
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM case_type_acl
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM ROLE
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM role_to_access_profiles
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM search_criteria
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM search_party
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM category
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM event
WHERE case_type_id IN ( :caseTypeIds )
""",
"""
DELETE FROM case_type
WHERE id IN ( :caseTypeIds )
"""
)
);

DELETE FROM event_webhook
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN :caseTypeIds);

DELETE FROM event_pre_state
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN :caseTypeIds);

DELETE FROM event_acl
WHERE event_id IN (SELECT id
FROM event
WHERE case_type_id IN :caseTypeIds);
DELETE FROM event_post_state
WHERE case_event_id IN (SELECT id
FROM event
WHERE case_type_id IN :caseTypeIds);
DELETE FROM state_acl
WHERE state_id IN (SELECT id
FROM state
WHERE case_type_id IN :caseTypeIds);
DELETE FROM state
WHERE case_type_id IN :caseTypeIds;
DELETE FROM case_type_acl
WHERE case_type_id IN :caseTypeIds;
DELETE FROM ROLE
WHERE case_type_id IN :caseTypeIds;
DELETE FROM role_to_access_profiles
WHERE case_type_id IN :caseTypeIds;
DELETE FROM search_criteria
WHERE case_type_id IN :caseTypeIds;
DELETE FROM search_party
WHERE case_type_id IN :caseTypeIds;
DELETE FROM category
WHERE case_type_id IN :caseTypeIds;
DELETE FROM event
WHERE case_type_id IN :caseTypeIds;
DELETE FROM case_type
WHERE id IN :caseTypeIds;
""";
Session session = sessionFactory.openSession();
session.beginTransaction();

session.createNativeQuery(sql)
.setParameter("caseTypeIds", caseTypes.stream().map(CaseType::getId).toList())
var ids = session.createNativeQuery("SELECT id FROM case_type WHERE reference IN ( :caseTypesWithChangeIds );")
.setParameterList("caseTypesWithChangeIds", caseTypesWithChangeIds)
.list();
session.getTransaction().commit();
if (ids.size() != caseIdList.size()) {
throw new NotFoundException("Unable to find case type");
}

for (String sqlStatement : sql) {
LOG.info("Executing SQL: " + sqlStatement);
session.beginTransaction();
session.createNativeQuery(sqlStatement)
.setParameterList("caseTypeIds", ids, org.hibernate.type.IntegerType.INSTANCE)
.executeUpdate();
session.getTransaction().commit();
}

session.close();
}
}

0 comments on commit 095508a

Please sign in to comment.