Search in sources :

Example 1 with PostedAllegation

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;
}
Also used : ParseException(java.text.ParseException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) Allegation(gov.ca.cwds.rest.api.domain.Allegation) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) HashSet(java.util.HashSet) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation)

Example 2 with PostedAllegation

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);
    }
}
Also used : EntityExistsException(javax.persistence.EntityExistsException) ServiceException(gov.ca.cwds.rest.services.ServiceException) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Allegation(gov.ca.cwds.data.persistence.cms.Allegation) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation)

Example 3 with PostedAllegation

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()));
}
Also used : PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Allegation(gov.ca.cwds.rest.api.domain.cms.Allegation) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Test(org.junit.Test)

Example 4 with PostedAllegation

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));
}
Also used : PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Allegation(gov.ca.cwds.rest.api.domain.cms.Allegation) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Test(org.junit.Test)

Example 5 with PostedAllegation

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());
}
Also used : InvocationOnMock(org.mockito.invocation.InvocationOnMock) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Allegation(gov.ca.cwds.rest.api.domain.cms.Allegation) PostedAllegation(gov.ca.cwds.rest.api.domain.cms.PostedAllegation) Test(org.junit.Test)

Aggregations

PostedAllegation (gov.ca.cwds.rest.api.domain.cms.PostedAllegation)9 Allegation (gov.ca.cwds.rest.api.domain.cms.Allegation)7 Test (org.junit.Test)6 ServiceException (gov.ca.cwds.rest.services.ServiceException)3 Client (gov.ca.cwds.rest.api.domain.cms.Client)2 CmsReferral (gov.ca.cwds.rest.api.domain.cms.CmsReferral)2 CrossReport (gov.ca.cwds.rest.api.domain.cms.CrossReport)2 PostedClient (gov.ca.cwds.rest.api.domain.cms.PostedClient)2 PostedCmsReferral (gov.ca.cwds.rest.api.domain.cms.PostedCmsReferral)2 PostedReferral (gov.ca.cwds.rest.api.domain.cms.PostedReferral)2 PostedReporter (gov.ca.cwds.rest.api.domain.cms.PostedReporter)2 ReferralClient (gov.ca.cwds.rest.api.domain.cms.ReferralClient)2 Reporter (gov.ca.cwds.rest.api.domain.cms.Reporter)2 LinkedHashSet (java.util.LinkedHashSet)2 Allegation (gov.ca.cwds.data.persistence.cms.Allegation)1 Allegation (gov.ca.cwds.rest.api.domain.Allegation)1 ClientUc (gov.ca.cwds.rest.api.domain.cms.ClientUc)1 Referral (gov.ca.cwds.rest.api.domain.cms.Referral)1 UnitOfWork (io.dropwizard.hibernate.UnitOfWork)1 ParseException (java.text.ParseException)1