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;
}
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));
}
Aggregations