use of se.inera.intyg.webcert.web.web.controller.moduleapi.dto.DraftHolder in project webcert by sklintyg.
the class UtkastModuleApiController method getDraft.
/**
* Returns the draft certificate as JSON identified by the intygId.
*
* @param intygsId The id of the certificate
* @return a JSON object
*/
@GET
@Path("/{intygsTyp}/{intygsId}")
@Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET)
public Response getDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId, @Context HttpServletRequest request) {
LOG.debug("Retrieving Intyg with id {} and type {}", intygsId, intygsTyp);
Utkast utkast = utkastService.getDraft(intygsId, intygsTyp);
Patient resolvedPatient = patientDetailsResolver.resolvePatient(utkast.getPatientPersonnummer(), intygsTyp);
if (resolvedPatient == null) {
throw new WebCertServiceException(WebCertServiceErrorCodeEnum.PU_PROBLEM, "Could not resolve Patient in PU-service when opening draft.");
}
authoritiesValidator.given(getWebCertUserService().getUser(), intygsTyp).features(AuthoritiesConstants.FEATURE_HANTERA_INTYGSUTKAST).privilege(AuthoritiesConstants.PRIVILEGE_SKRIVA_INTYG).orThrow();
verifySekretessmarkering(intygsTyp, utkast.getEnhetsId(), resolvedPatient);
request.getSession(true).removeAttribute(LAST_SAVED_DRAFT);
DraftHolder draftHolder = new DraftHolder();
draftHolder.setVersion(utkast.getVersion());
draftHolder.setVidarebefordrad(utkast.getVidarebefordrad());
draftHolder.setStatus(utkast.getStatus());
draftHolder.setEnhetsNamn(utkast.getEnhetsNamn());
draftHolder.setVardgivareNamn(utkast.getVardgivarNamn());
draftHolder.setLatestTextVersion(intygTextsService.getLatestVersion(utkast.getIntygsTyp()));
Relations relations1 = certificateRelationService.getRelations(utkast.getIntygsId());
draftHolder.setRelations(relations1);
draftHolder.setKlartForSigneringDatum(utkast.getKlartForSigneringDatum());
draftHolder.setCreated(utkast.getSkapad());
// The patientResolved is unnecessary?
draftHolder.setPatientResolved(true);
draftHolder.setSekretessmarkering(resolvedPatient.isSekretessmarkering());
draftHolder.setAvliden(resolvedPatient.isAvliden());
// Businesss logic below should not be here inside a controller.. Should preferably be moved in the future.
try {
try {
Utlatande utlatande = moduleRegistry.getModuleApi(intygsTyp).getUtlatandeFromJson(utkast.getModel());
draftHolder.setPatientNameChangedInPU(patientDetailsResolver.isPatientNamedChanged(utlatande.getGrundData().getPatient(), resolvedPatient));
draftHolder.setPatientAddressChangedInPU(patientDetailsResolver.isPatientAddressChanged(utlatande.getGrundData().getPatient(), resolvedPatient));
} catch (IOException e) {
LOG.error("Failed to getUtlatandeFromJson intygsId {} while checking for updated patient information", intygsId);
}
if (!completeAddressProvided(resolvedPatient)) {
// Overwrite retrieved address data with saved one.
Patient oldPatientData = null;
try {
oldPatientData = moduleRegistry.getModuleApi(intygsTyp).getUtlatandeFromJson(utkast.getModel()).getGrundData().getPatient();
} catch (IOException e) {
LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId);
}
copyOldAddressToNewPatientData(oldPatientData, resolvedPatient);
}
// Update the internal model with the resolved patient. This means the draft may be updated
// with new patient info on the next auto-save!
String updatedModel = moduleRegistry.getModuleApi(intygsTyp).updateBeforeSave(utkast.getModel(), resolvedPatient);
utkast.setModel(updatedModel);
draftHolder.setContent(utkast.getModel());
return Response.ok(draftHolder).build();
} catch (ModuleException | ModuleNotFoundException e) {
throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage());
}
}
Aggregations