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
@@ -1,14 +1,17 @@
package uk.gov.moj.cpp.hearing.domain.aggregate.hearing;

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Optional.ofNullable;
import static org.apache.commons.collections.CollectionUtils.isNotEmpty;
import static uk.gov.justice.core.courts.IndicatedPleaValue.INDICATED_GUILTY;
import static uk.gov.moj.cpp.hearing.domain.aggregate.util.PleaVerdictUtil.isGuiltyVerdict;
import static uk.gov.moj.cpp.hearing.domain.event.ConvictionDateAdded.convictionDateAdded;
import static uk.gov.moj.cpp.hearing.domain.event.ConvictionDateRemoved.convictionDateRemoved;

import uk.gov.justice.core.courts.CourtApplication;
import uk.gov.justice.core.courts.CourtOrderOffence;
import uk.gov.justice.core.courts.Hearing;
import uk.gov.justice.core.courts.IndicatedPlea;
import uk.gov.justice.core.courts.Offence;
import uk.gov.justice.core.courts.Plea;
Expand Down Expand Up @@ -132,28 +135,33 @@ public Stream<Object> updatePlea(final UUID hearingId, final PleaModel pleaModel
}
setOriginatingHearingId(hearingId, pleaModel);

final boolean isCivil = isCivilCase();
final PleaModel pleaToUpdate = isCivil ? pleaModel.setAllocationDecision(null) : pleaModel;

final Plea plea = pleaToUpdate.getPlea();
final List<Object> events = new ArrayList<>();
final Plea plea = pleaModel.getPlea();
events.add(PleaUpsert.pleaUpsert()
.setHearingId(hearingId)
.setPleaModel(pleaModel));

if (nonNull(plea)) {
addConvictionDateEventForPlea(hearingId, guiltyPleaTypes, offenceId, prosecutionCaseId, courtApplicationId, events, plea);
} else if (nonNull(pleaModel.getIndicatedPlea()) ) {
// indicated plea logic for updating conviction dates is not changing as it is out of scope for DD-2825
// and will be covered under a separate CR
addConvictionDateEventForIndicatedPlea(hearingId, pleaModel, offenceId, prosecutionCaseId, courtApplicationId, events);
} else if (canRemoveConvictionDate(offenceId, courtApplicationId)){
events.add(
convictionDateRemoved()
.setCaseId(prosecutionCaseId)
.setHearingId(hearingId)
.setOffenceId(offenceId)
.setCourtApplicationId(courtApplicationId));
.setPleaModel(pleaToUpdate));

if (!isCivil) {
if (nonNull(plea)) {
addConvictionDateEventForPlea(hearingId, guiltyPleaTypes, offenceId, prosecutionCaseId, courtApplicationId, events, plea);
} else if (nonNull(pleaToUpdate.getIndicatedPlea()) ) {
// indicated plea logic for updating conviction dates is not changing as it is out of scope for DD-2825
// and will be covered under a separate CR
addConvictionDateEventForIndicatedPlea(hearingId, pleaToUpdate, offenceId, prosecutionCaseId, courtApplicationId, events);
} else if (canRemoveConvictionDate(offenceId, courtApplicationId)) {
events.add(
convictionDateRemoved()
.setCaseId(prosecutionCaseId)
.setHearingId(hearingId)
.setOffenceId(offenceId)
.setCourtApplicationId(courtApplicationId));
}
}

return events.stream();
return events.stream();
}

private void addConvictionDateEventForIndicatedPlea(final UUID hearingId, final PleaModel pleaModel, final UUID offenceId, final UUID prosecutionCaseId, final UUID courtApplicationId, final List<Object> events) {
Expand Down Expand Up @@ -244,4 +252,16 @@ private void setOffence(final Offence offence) {
private void setApplicationPlea(final CourtApplication courtApplication) {
courtApplication.setPlea(this.momento.getPleas().get(courtApplication.getId()));
}

private boolean isCivilCase() {
final Hearing hearing = momento.getHearing();
if (isNull(hearing)) {
return false;
}

return isNotEmpty(hearing.getProsecutionCases())
&& hearing.getProsecutionCases().stream()
.filter(Objects::nonNull)
.anyMatch(prosecutionCase -> Boolean.TRUE.equals(prosecutionCase.getIsCivil()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,9 @@ public void shouldAddConvictionDateAddedEventWhenIndicatedPleaIsGuilty() {
.withProsecutionCaseId(CASE_ID)
.withDefendantId(DEFENDANT_ID)
.withOffenceId(OFFENCE_ID)
.withAllocationDecision(AllocationDecision.allocationDecision()
.withOffenceId(OFFENCE_ID)
.build())
.withIndicatedPlea(IndicatedPlea.indicatedPlea()
.withOffenceId(OFFENCE_ID)
.withIndicatedPleaDate(indicatedPleaDate)
Expand All @@ -487,6 +490,7 @@ public void shouldAddConvictionDateAddedEventWhenIndicatedPleaIsGuilty() {
final PleaUpsert pleaUpsert = (PleaUpsert) events.get(0);
assertThat(pleaUpsert, is(notNullValue()));
assertThat(pleaUpsert.getHearingId(), is(HEARING_ID));
assertThat(pleaUpsert.getPleaModel().getAllocationDecision(), is(notNullValue()));

final ConvictionDateAdded convictionDateAdded = (ConvictionDateAdded) events.get(1);
assertThat(convictionDateAdded, is(notNullValue()));
Expand Down Expand Up @@ -590,6 +594,64 @@ public void shouldNotAddConvictionDateRemovedEventWhenIndicatedPleaIsNotGuilty()
assertThat(pleaUpsert.getHearingId(), is(HEARING_ID));
}

@Test
public void shouldNotAddConvictionDateIfCaseIsCivil() {
final Hearing hearing = getHearing(OFFENCE_ID, DEFENDANT_ID, CASE_ID, HEARING_ID);
hearing.getProsecutionCases().get(0).setIsCivil(true);

this.hearingAggregateMomento.setHearing(hearing);

final PleaModel pleaModel = PleaModel.pleaModel()
.withProsecutionCaseId(CASE_ID)
.withDefendantId(DEFENDANT_ID)
.withOffenceId(OFFENCE_ID)
.withPlea(Plea.plea()
.withOffenceId(OFFENCE_ID)
.withPleaDate(PAST_LOCAL_DATE.next())
.withPleaValue(GUILTY)
.build())
.build();

final List<Object> events = pleaDelegate.updatePlea(HEARING_ID, pleaModel, guiltyPleaTypes()).toList();

assertThat(events.size(), is(1));
final PleaUpsert pleaUpsert = (PleaUpsert) events.get(0);
assertThat(pleaUpsert, is(notNullValue()));
assertThat(pleaUpsert.getHearingId(), is(HEARING_ID));

assertThat(events.stream().filter(ConvictionDateAdded.class::isInstance).count(), is(0L));
}

@Test
public void shouldRemoveAllocationDecisionValuesWhenCaseIsCivil() {
final Hearing hearing = getHearing(OFFENCE_ID, DEFENDANT_ID, CASE_ID, HEARING_ID);
hearing.getProsecutionCases().get(0).setIsCivil(true);

this.hearingAggregateMomento.setHearing(hearing);

final PleaModel pleaModel = PleaModel.pleaModel()
.withProsecutionCaseId(CASE_ID)
.withDefendantId(DEFENDANT_ID)
.withOffenceId(OFFENCE_ID)
.withAllocationDecision(AllocationDecision.allocationDecision()
.withOffenceId(OFFENCE_ID)
.build())
.withPlea(Plea.plea()
.withOffenceId(OFFENCE_ID)
.withPleaDate(PAST_LOCAL_DATE.next())
.withPleaValue(GUILTY)
.build())
.build();

final List<Object> events = pleaDelegate.updatePlea(HEARING_ID, pleaModel, guiltyPleaTypes()).toList();

assertThat(events.size(), is(1));
final PleaUpsert pleaUpsert = (PleaUpsert) events.get(0);
assertThat(pleaUpsert, is(notNullValue()));
assertThat(pleaUpsert.getHearingId(), is(HEARING_ID));
assertThat(pleaUpsert.getPleaModel().getAllocationDecision(), is(nullValue()));
}

@Test
public void shouldAddConvictionDateWhenPleaIsGuiltyType() {
GUILTY_PLEA_LIST.forEach(this::shouldAddConvictionDateAddedWhenPleaIsGuiltyType);
Expand Down
6 changes: 3 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
<datafactory.version>0.8</datafactory.version>
<commons.fileupload.version>1.3.3</commons.fileupload.version>
<authorisation.service.version>0.1.96</authorisation.service.version>
<coredomain.version>17.103.11</coredomain.version>
<referencedata.version>17.103.128</referencedata.version>
<coredomain.version>17.103.12</coredomain.version>
<referencedata.version>17.103.129</referencedata.version>
<material.version>17.0.57</material.version>
<results.version>17.0.58</results.version>
<notification.notify.version>17.0.1</notification.notify.version>
<stagingenforcement.version>17.103.73</stagingenforcement.version>
<progression.version>17.0.215</progression.version>
<progression.version>17.0.240</progression.version>
<jaxb.version>2.2.11</jaxb.version>
<org.xmlunit.version>2.6.3</org.xmlunit.version>
<sardine.version>5.7</sardine.version>
Expand Down
Loading