Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,18 @@
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.time.StopWatch;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import uk.gov.hmcts.reform.ccd.client.model.CaseDetails;
import uk.gov.hmcts.reform.ccd.client.model.SearchResult;
import uk.gov.hmcts.reform.migration.ccd.CoreCaseDataService;
import uk.gov.hmcts.reform.migration.ccd.MigrationEvent;
import uk.gov.hmcts.reform.migration.service.DataMigrationService;

import static uk.gov.hmcts.reform.migration.queries.CcdElasticSearchQueries.fetchAllCaseNameInternalCasesQuery;
import static uk.gov.hmcts.reform.migration.queries.CcdElasticSearchQueries.fetchAllUnmigratedGlobalSearchCasesQuery;
import static uk.gov.hmcts.reform.migration.queries.CcdElasticSearchQueries.*;

@Component
@RequiredArgsConstructor
Expand All @@ -33,7 +34,7 @@ public class CaseMigrationProcessor {

private final Logger infoLogger = LoggerFactory.getLogger("ccd-migration-info");

private static final String EVENT_ID = "migrateWorkAllocationR3";
private MigrationEvent eventId = MigrationEvent.UNKNOWN_MIGRATION;
private static final String EVENT_SUMMARY = "Migrate Case";
private static final String EVENT_DESCRIPTION = "Migrate Case";

Expand Down Expand Up @@ -66,13 +67,24 @@ public void processSingleCase(String userToken, String caseId, boolean dryRun) {
}
}

public void fetchAndProcessCases(String userToken, boolean dryRun, int numThreads, MigrationPageParams pageParams, boolean migrateCaseNameInternalFlag)
public void fetchAndProcessCases(String userToken, boolean dryRun, int numThreads, MigrationPageParams pageParams,
MigrationEvent migrationEvent, boolean migrateCaseNameInternalFlag,
boolean migrateCaseFlagsInternalFlag)
throws InterruptedException {

eventId = migrationEvent;

SearchSourceBuilder currentQuery = fetchAllUnmigratedGlobalSearchCasesQuery();
BoolQueryBuilder queryBuilder = (BoolQueryBuilder) fetchAllUnmigratedGlobalSearchCasesQuery().query();

if (migrateCaseNameInternalFlag){
if (migrationEvent.equals(MigrationEvent.MIGRATE_WORK_ALLOCATION_R3) && migrateCaseNameInternalFlag){
currentQuery = fetchAllCaseNameInternalCasesQuery();
queryBuilder = (BoolQueryBuilder) fetchAllCaseNameInternalCasesQuery().query();
}

if (migrationEvent.equals(MigrationEvent.MIGRATE_CASE_FLAGS) && migrateCaseFlagsInternalFlag){
currentQuery = fetchAllUnmigratedCaseFlagsInternalCasesQuery();
queryBuilder = (BoolQueryBuilder) fetchAllUnmigratedCaseFlagsInternalCasesQuery().query();
}

SearchResult initialSearch = coreCaseDataService.searchCases(userToken,
Expand All @@ -91,7 +103,7 @@ public void fetchAndProcessCases(String userToken, boolean dryRun, int numThread
ExecutorService executorService = Executors.newFixedThreadPool(numThreads);

fetchAndSubmitTasks(userToken, dryRun, totalCasesToProcess, pageParams.getPageSize(), searchFrom,
executorService, migrateCaseNameInternalFlag);
executorService, queryBuilder);

executorService.shutdown();
executorService.awaitTermination(Integer.MAX_VALUE, TimeUnit.DAYS);
Expand All @@ -112,15 +124,15 @@ private int resolveTotalCasesToProcess(SearchResult initialSearch, int maxCasesT
}

private void fetchAndSubmitTasks(String userToken, boolean dryRun, int totalCasesToProcess, int pageSize,
Long searchFrom, ExecutorService executorService, boolean migrateCaseNameInternalFlag) {
Long searchFrom, ExecutorService executorService, BoolQueryBuilder queryBuilder) {
int casesFetched = 1;
int numCasesToFetch = pageSize;

while (casesFetched < totalCasesToProcess) {
numCasesToFetch = resolvePageSize(totalCasesToProcess, casesFetched, numCasesToFetch, pageSize);

List<CaseDetails> caseDetails =
coreCaseDataService.fetchNCases(userToken, numCasesToFetch, searchFrom);
coreCaseDataService.fetchNCases(userToken, numCasesToFetch, searchFrom, queryBuilder);

if (caseDetails.isEmpty()) {
break;
Expand Down Expand Up @@ -177,7 +189,7 @@ private void updateCase(String authorisation, Long id, Map<String, Object> data,
coreCaseDataService.update(
authorisation,
id.toString(),
EVENT_ID,
eventId.toString(),
EVENT_SUMMARY,
EVENT_DESCRIPTION,
migratedData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import uk.gov.hmcts.reform.idam.client.IdamClient;
import uk.gov.hmcts.reform.migration.ccd.MigrationEvent;

@SpringBootApplication
@RequiredArgsConstructor
Expand All @@ -32,6 +33,8 @@ public class CaseMigrationRunner implements CommandLineRunner {
private int numThreads;
@Value("${migration.migrateCaseNameInternal}")
private boolean migrateCaseNameInternalFlag;
@Value("${migration.migrateCaseFlagsInternal}")
private boolean migrateCaseFlagsInternalFlag;
private final IdamClient idamClient;
private final CaseMigrationProcessor caseMigrationProcessor;

Expand All @@ -48,12 +51,20 @@ public void run(String... args) {

StopWatch stopWatch = StopWatch.createStarted();

log.info("-----------------------------------------");
log.info("Is this DryRun: {}", dryrun);
log.info("-----------------------------------------");
log.info("-----------------------------------------");
log.info("Migration Event: {}", getMigrationEvent());
log.info("-----------------------------------------");

if (ccdCaseId != null && !ccdCaseId.isBlank()) {
log.info("Data migration of single case started");
caseMigrationProcessor.processSingleCase(userToken, ccdCaseId, dryrun);
} else {
log.info("Data migration of cases started");
caseMigrationProcessor.fetchAndProcessCases(userToken, dryrun, numThreads, pageParams, migrateCaseNameInternalFlag);
caseMigrationProcessor.fetchAndProcessCases(userToken, dryrun, numThreads, pageParams,
getMigrationEvent(), migrateCaseNameInternalFlag, migrateCaseFlagsInternalFlag);
}

stopWatch.stop();
Expand All @@ -76,4 +87,17 @@ public void run(String... args) {
log.error("Migration failed with the following reason: {}", e.getMessage(), e);
}
}

private MigrationEvent getMigrationEvent(){

if (migrateCaseNameInternalFlag){
return MigrationEvent.MIGRATE_WORK_ALLOCATION_R3;
}
else if (migrateCaseFlagsInternalFlag) {
return MigrationEvent.MIGRATE_CASE_FLAGS;
}
else {
return MigrationEvent.MIGRATE_WORK_ALLOCATION_R3;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package uk.gov.hmcts.reform.migration.ccd;

import static java.util.Collections.emptyList;
import static uk.gov.hmcts.reform.migration.queries.CcdElasticSearchQueries.oldestCaseQuery;
import static uk.gov.hmcts.reform.migration.queries.CcdElasticSearchQueries.pageForUnsetCaseAccessManagementFieldsFieldsQuery;
import static uk.gov.hmcts.reform.migration.queries.CcdElasticSearchQueries.*;

import feign.FeignException;

Expand All @@ -16,6 +15,7 @@
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.StopWatch;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -36,7 +36,7 @@
@RequiredArgsConstructor
public class CoreCaseDataService {

private static final String SSCS_CASE_TYPE = "Asylum";
private static final String IAC_CASE_TYPE = "Asylum";

@Value("${migration.jurisdiction}")
private String jurisdiction;
Expand All @@ -52,12 +52,12 @@ public CaseDetails fetchOne(String authorisation, String caseId) {
return coreCaseDataApi.getCase(authorisation, authTokenGenerator.generate(), caseId);
}

public List<CaseDetails> fetchNCases(String authorisation, int casesToFetch, long searchFrom) {
public List<CaseDetails> fetchNCases(String authorisation, int casesToFetch, long searchFrom, BoolQueryBuilder queryBuilder) {

StopWatch stopWatch = StopWatch.createStarted();

List<CaseDetails> page = fetchPage(authorisation,
pageForUnsetCaseAccessManagementFieldsFieldsQuery(searchFrom, casesToFetch));
pageForUnsetCaseFieldsFieldsQuery(searchFrom, casesToFetch, queryBuilder));

stopWatch.stop();

Expand Down Expand Up @@ -125,7 +125,7 @@ public SearchResult searchCases(String authorisation, SearchSourceBuilder search
return coreCaseDataApi.searchCases(
authorisation,
authTokenGenerator.generate(),
SSCS_CASE_TYPE,
IAC_CASE_TYPE,
searchBuilder.toString());
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package uk.gov.hmcts.reform.migration.ccd;

import com.fasterxml.jackson.annotation.JsonEnumDefaultValue;
import com.fasterxml.jackson.annotation.JsonValue;

public enum MigrationEvent {
MIGRATE_WORK_ALLOCATION_R3("migrateWorkAllocationR3"),
MIGRATE_CASE_FLAGS("migrateCaseFlags"),

@JsonEnumDefaultValue
UNKNOWN_MIGRATION("unknownMigration");

@JsonValue
private final String id;

MigrationEvent(String id) {
this.id = id;
}

@Override
public String toString() {
return id;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,13 @@ public static SearchSourceBuilder fetchAllCaseNameInternalCasesQuery() {
.sort(REFERENCE_KEYWORD, SortOrder.ASC);
}

public static SearchSourceBuilder fetchAllUnmigratedCaseFlagsInternalCasesQuery() {
return SearchSourceBuilder.searchSource()
.size(1)
.query(caseFlagsInternalFieldsQuery())
.sort(REFERENCE_KEYWORD, SortOrder.ASC);
}

public static BoolQueryBuilder missingSearchCriteriaFieldsQuery() {
return QueryBuilders.boolQuery()
.mustNot(
Expand All @@ -60,6 +67,25 @@ public static BoolQueryBuilder missingCaseNameInternalFieldsQuery() {
.should(existsQuery("data.caseNameHmctsInternal")));
}

public static BoolQueryBuilder caseFlagsInternalFieldsQuery() {

return QueryBuilders.boolQuery()
.must(existsQuery("data.caseFlags"))
.must(QueryBuilders.boolQuery()
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "anonymity"))
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "complexCase"))
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "detainedImmigrationAppeal"))
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "foreignNationalOffender"))
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "potentiallyViolentPerson"))
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "unacceptableCustomerBehaviour"))
.should(QueryBuilders.matchQuery("data.caseFlags.value.caseFlagType", "unaccompaniedMinor"))
.minimumShouldMatch(1))
.mustNot(
QueryBuilders.boolQuery()
.should(existsQuery("data.caseLevelFlags"))
.should(existsQuery("data.appellantLevelFlags")));
}

public static SearchSourceBuilder pageForUnsetCaseAccessManagementFieldsFieldsQuery(int pageSize) {
return SearchSourceBuilder.searchSource()
.size(pageSize)
Expand All @@ -76,4 +102,14 @@ public static SearchSourceBuilder pageForUnsetCaseAccessManagementFieldsFieldsQu
.searchAfter(new Object[] { lastCaseId})
.sort(REFERENCE_KEYWORD, SortOrder.ASC);
}

public static SearchSourceBuilder pageForUnsetCaseFieldsFieldsQuery(Long lastCaseId, int pageSize,
BoolQueryBuilder queryBuilder) {

return SearchSourceBuilder.searchSource()
.size(pageSize)
.query(queryBuilder)
.searchAfter(new Object[] { lastCaseId})
.sort(REFERENCE_KEYWORD, SortOrder.ASC);
}
}