use of gov.ca.cwds.rest.api.domain.cms.PostedAllegation in project API by ca-cwds.
the class ScreeningToReferralService method processAllegations.
/*
* CMS Allegation - one for each allegation
*/
private Set<Allegation> processAllegations(ScreeningToReferral scr, String referralId, HashMap<Long, String> perpatratorClient, HashMap<Long, String> victimClient, Set<ErrorMessage> messages) throws ServiceException {
Set<Allegation> processedAllegations = new HashSet<>();
Set<Allegation> allegations;
String victimClientId = "";
String perpatratorClientId = "";
allegations = scr.getAllegations();
if (allegations == null || allegations.isEmpty()) {
String message = " Referral must have at least one Allegation ";
ServiceException exception = new ServiceException(message);
logError(message, exception, messages);
return processedAllegations;
}
for (Allegation allegation : allegations) {
try {
if (!ParticipantValidator.isVictimParticipant(scr, allegation.getVictimPersonId())) {
String message = " Allegation/Victim Person Id does not contain a Participant with a role of Victim ";
ServiceException exception = new ServiceException(message);
logError(message, exception, messages);
}
} catch (Exception e) {
logError(e.getMessage(), e, messages);
// next allegation
continue;
}
if (victimClient.containsKey(allegation.getVictimPersonId())) {
// this is the legacy Id (CLIENT) of the victime
victimClientId = victimClient.get(allegation.getVictimPersonId());
}
if (allegation.getPerpetratorPersonId() != 0) {
try {
if (!ParticipantValidator.isPerpetratorParticipant(scr, allegation.getPerpetratorPersonId())) {
String message = " Allegation/Perpetrator Person Id does not contain a Participant with a role of Perpetrator ";
ServiceException exception = new ServiceException(message);
logError(message, exception, messages);
}
} catch (Exception e) {
logError(e.getMessage(), e, messages);
// next allegation
continue;
}
}
if (perpatratorClient.containsKey(allegation.getPerpetratorPersonId())) {
// this is the legacy Id (CLIENT) of the perpetrator
perpatratorClientId = perpatratorClient.get(allegation.getPerpetratorPersonId());
}
if (victimClientId.isEmpty()) {
String message = " Victim could not be determined for an allegation ";
ServiceException exception = new ServiceException(message);
logError(message, exception, messages);
// next allegation
continue;
}
if (allegation.getLegacyId() == null || allegation.getLegacyId().isEmpty()) {
// create an allegation in CMS legacy database
gov.ca.cwds.rest.api.domain.cms.Allegation cmsAllegation = new gov.ca.cwds.rest.api.domain.cms.Allegation("", DEFAULT_CODE, "", scr.getLocationType(), "", DEFAULT_CODE, allegationTypeCode, scr.getReportNarrative(), "", false, DEFAULT_NON_PROTECTING_PARENT_CODE, false, victimClientId, perpatratorClientId, referralId, DEFAULT_COUNTY_SPECIFIC_CODE, false, DEFAULT_CODE);
buildErrors(messages, validator.validate(cmsAllegation));
PostedAllegation postedAllegation = this.allegationService.create(cmsAllegation);
allegation.setLegacyId(postedAllegation.getId());
allegation.setLegacySourceTable(ALLEGATION_TABLE_NAME);
processedAllegations.add(allegation);
} else {
gov.ca.cwds.rest.api.domain.cms.Allegation foundAllegation = this.allegationService.find(allegation.getLegacyId());
if (foundAllegation == null) {
String message = " Legacy Id on Allegation does not correspond to an existing CMS/CWS Allegation ";
ServiceException se = new ServiceException(message);
logError(message, se, messages);
// next allegation
continue;
}
}
}
return processedAllegations;
}
use of gov.ca.cwds.rest.api.domain.cms.PostedAllegation in project API by ca-cwds.
the class AllegationService method create.
/**
* {@inheritDoc}
*
* @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
*/
@Override
public PostedAllegation create(Request request) {
assert request instanceof gov.ca.cwds.rest.api.domain.cms.Allegation;
gov.ca.cwds.rest.api.domain.cms.Allegation allegation = (gov.ca.cwds.rest.api.domain.cms.Allegation) request;
try {
String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
Allegation managed = new Allegation(CmsKeyIdGenerator.cmsIdGenertor(lastUpdatedId), allegation, lastUpdatedId);
managed = allegationDao.create(managed);
return new PostedAllegation(managed);
} catch (EntityExistsException e) {
LOGGER.info("Allegation already exists : {}", allegation);
throw new ServiceException(e);
}
}
use of gov.ca.cwds.rest.api.domain.cms.PostedAllegation in project API by ca-cwds.
the class AllegationServiceTest method testCreateReturnsNonNull.
@SuppressWarnings("javadoc")
@Test
public void testCreateReturnsNonNull() throws Exception {
String id = "Aaeae9r0F4";
Allegation allegationDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/Allegation/valid/valid.json"), Allegation.class);
gov.ca.cwds.data.persistence.cms.Allegation toCreate = new gov.ca.cwds.data.persistence.cms.Allegation(id, allegationDomain, "ABC");
Allegation request = new Allegation(toCreate);
when(allegationDao.create(any(gov.ca.cwds.data.persistence.cms.Allegation.class))).thenReturn(toCreate);
PostedAllegation postedAllegation = allegationService.create(request);
assertThat(postedAllegation, is(notNullValue()));
}
use of gov.ca.cwds.rest.api.domain.cms.PostedAllegation in project API by ca-cwds.
the class AllegationServiceTest method testCreateReturnsCorrectEntity.
@Override
@Test
public void testCreateReturnsCorrectEntity() throws Exception {
String id = "Aaeae9r0F4";
Allegation allegationDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/Allegation/valid/valid.json"), Allegation.class);
gov.ca.cwds.data.persistence.cms.Allegation toCreate = new gov.ca.cwds.data.persistence.cms.Allegation(id, allegationDomain, "ABC");
Allegation request = new Allegation(toCreate);
when(allegationDao.create(any(gov.ca.cwds.data.persistence.cms.Allegation.class))).thenReturn(toCreate);
PostedAllegation expected = new PostedAllegation(toCreate);
PostedAllegation returned = allegationService.create(request);
assertThat(returned, is(expected));
}
use of gov.ca.cwds.rest.api.domain.cms.PostedAllegation in project API by ca-cwds.
the class AllegationServiceTest method createReturnsGeneratedId.
/*
* Test for checking the new Allegation Id generated and lenght is 10
*/
@SuppressWarnings("javadoc")
@Test
public void createReturnsGeneratedId() throws Exception {
Allegation allegationDomain = MAPPER.readValue(fixture("fixtures/domain/legacy/Allegation/valid/valid.json"), Allegation.class);
when(allegationDao.create(any(gov.ca.cwds.data.persistence.cms.Allegation.class))).thenAnswer(new Answer<gov.ca.cwds.data.persistence.cms.Allegation>() {
@Override
public gov.ca.cwds.data.persistence.cms.Allegation answer(InvocationOnMock invocation) throws Throwable {
gov.ca.cwds.data.persistence.cms.Allegation report = (gov.ca.cwds.data.persistence.cms.Allegation) invocation.getArguments()[0];
return report;
}
});
PostedAllegation returned = allegationService.create(allegationDomain);
assertEquals(returned.getId().length(), 10);
PostedAllegation newReturned = allegationService.create(allegationDomain);
Assert.assertNotEquals(returned.getId(), newReturned.getId());
}
Aggregations