Search in sources :

Example 1 with CrudsService

use of gov.ca.cwds.rest.services.CrudsService in project API by ca-cwds.

the class CmsNSHelper method handleResponse.

@SuppressWarnings("javadoc")
public Map<String, Map<CrudsService, Response>> handleResponse(Map<CrudsService, Request> cmsRequests, Map<CrudsService, Request> nsRequests) {
    Map<CrudsService, Response> cmsResponse = new HashMap<>();
    Map<CrudsService, Response> nsResponse = new HashMap<>();
    Map<String, Map<CrudsService, Response>> response = new HashMap<>();
    Response referral = null;
    Response person;
    org.hibernate.Session sessionCMS = cmsSessionFactory.openSession();
    org.hibernate.Session sessionNS = nsSessionFactory.openSession();
    try {
        // NOSONAR
        ManagedSessionContext.bind(sessionCMS);
        Transaction transactionCMS = sessionCMS.beginTransaction();
        for (CrudsService service : cmsRequests.keySet()) {
            try {
                referral = service.create(cmsRequests.get(service));
                cmsResponse.put(service, referral);
                sessionCMS.flush();
            } catch (Exception e) {
                transactionCMS.rollback();
                throw e;
            }
        }
        // NOSONAR
        ManagedSessionContext.bind(sessionNS);
        Transaction transactionNS = sessionNS.beginTransaction();
        for (CrudsService service : nsRequests.keySet()) {
            try {
                person = service.create(nsRequests.get(service));
                nsResponse.put(service, person);
                sessionNS.flush();
            } catch (Exception e) {
                transactionNS.rollback();
                transactionCMS.rollback();
                throw e;
            }
            try {
                transactionCMS.commit();
                transactionNS.commit();
            } catch (Exception e) {
                throw e;
            }
        }
    } finally {
        sessionCMS.close();
        sessionNS.close();
        // NOSONAR
        ManagedSessionContext.unbind(cmsSessionFactory);
        // NOSONAR
        ManagedSessionContext.unbind(nsSessionFactory);
    }
    response.put("cms", cmsResponse);
    response.put("ns", nsResponse);
    return response;
}
Also used : Response(gov.ca.cwds.rest.api.Response) Transaction(org.hibernate.Transaction) HashMap(java.util.HashMap) CrudsService(gov.ca.cwds.rest.services.CrudsService) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with CrudsService

use of gov.ca.cwds.rest.services.CrudsService in project API by ca-cwds.

the class CmsNSReferralService method create.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
   */
@Override
public Response create(Request request) {
    assert request instanceof CmsReferral;
    CmsNSReferral cmsReferral = (CmsNSReferral) request;
    CmsNSHelper helper = new CmsNSHelper(cmsSessionFactory, nsSessionFactory);
    Map<CrudsService, Request> cmsRequest = new HashMap<>();
    Map<CrudsService, Request> nsRequest = new HashMap<>();
    cmsRequest.put(referralService, cmsReferral.getReferral());
    nsRequest.put(personService, cmsReferral.getPerson());
    Map<String, Map<CrudsService, Response>> response = helper.handleResponse(cmsRequest, nsRequest);
    return new PostedCmsNSReferral((PostedReferral) response.get("cms").get(referralService), (PostedPerson) response.get("ns").get(personService));
}
Also used : CmsReferral(gov.ca.cwds.rest.api.domain.cms.CmsReferral) PostedCmsNSReferral(gov.ca.cwds.rest.api.domain.cms.PostedCmsNSReferral) HashMap(java.util.HashMap) CrudsService(gov.ca.cwds.rest.services.CrudsService) CmsNSReferral(gov.ca.cwds.rest.api.domain.cms.CmsNSReferral) PostedCmsNSReferral(gov.ca.cwds.rest.api.domain.cms.PostedCmsNSReferral) Request(gov.ca.cwds.rest.api.Request) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

CrudsService (gov.ca.cwds.rest.services.CrudsService)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Request (gov.ca.cwds.rest.api.Request)1 Response (gov.ca.cwds.rest.api.Response)1 CmsNSReferral (gov.ca.cwds.rest.api.domain.cms.CmsNSReferral)1 CmsReferral (gov.ca.cwds.rest.api.domain.cms.CmsReferral)1 PostedCmsNSReferral (gov.ca.cwds.rest.api.domain.cms.PostedCmsNSReferral)1 Transaction (org.hibernate.Transaction)1