@Test public void getDraftShouldPatchIfNewAddressExist() throws Exception { String intygsTyp = "gdspinae-intygsTyp"; String intygsId = "gdspinae-intygsId"; setupGrundData(); setupUser(intygsTyp, false, UserOriginType.NORMAL.name(), Arrays.asList(AuthoritiesConstants.PRIVILEGE_SKRIVA_INTYG), Arrays.asList(AuthoritiesConstants.FEATURE_HANTERA_INTYGSUTKAST)); String postadress = "gdspinae-postadress"; String postort = "gdspinae-postort"; String postnummer = "gdspinae-postnummer"; Patient patientWithIncompleteAddress = buildPatient(); patientWithIncompleteAddress.setPostadress(postadress); patientWithIncompleteAddress.setPostort(postort); patientWithIncompleteAddress.setPostnummer(postnummer); when(patientDetailsResolver.resolvePatient(any(Personnummer.class), anyString(), anyString())) .thenReturn(patientWithIncompleteAddress); doReturn(buildUtkast("intygtyp", "intygsid")).when(utkastService).getDraft(anyString(), anyString()); Response response = moduleApiController.getDraft(intygsTyp, CERTIFICATE_ID, request); ArgumentCaptor<Patient> argumentCaptor = ArgumentCaptor.forClass(Patient.class); verify(moduleApi).updateBeforeSave(anyString(), argumentCaptor.capture()); assertEquals(postadress, argumentCaptor.getValue().getPostadress()); assertEquals(postort, argumentCaptor.getValue().getPostort()); assertEquals(postnummer, argumentCaptor.getValue().getPostnummer()); }
@GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod 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); draftAccessServiceHelper.validateAllowToReadUtkast(utkast, utkast.getPatientPersonnummer()); request.getSession(true).removeAttribute(LAST_SAVED_DRAFT); try { final ModuleApi moduleApi = moduleRegistry.getModuleApi(intygsTyp, utkast.getIntygTypeVersion()); Utlatande utlatande = moduleApi.getUtlatandeFromJson(utkast.getModel()); Patient savedPatientData = utlatande.getGrundData().getPatient(); Patient resolvedPatientData = getPatientDataFromPU(intygsTyp, utkast); DraftHolder newDraft = createDraftHolder(utkast, savedPatientData, resolvedPatientData); if (!resolvedPatientData.isCompleteAddressProvided()) { copyOldAddressToNewPatientData(savedPatientData, resolvedPatientData); } String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), resolvedPatientData); newDraft.setContent(updatedModel); if (utkast.getStatus().equals(UtkastStatus.DRAFT_INCOMPLETE) && utkast.getVersion() == 0 && utkast.getRelationKod() == null) { Optional<UtkastCandidateMetaData> metaData = utkastCandidateService.getCandidateMetaData(moduleApi, intygsTyp, resolvedPatientData, false); newDraft.setCandidateMetaData(metaData.orElse(null)); } resourceLinkHelper.decorateUtkastWithValidActionLinks( newDraft, intygsTyp, utlatande.getGrundData().getSkapadAv().getVardenhet(), resolvedPatientData.getPersonId()); return Response.ok(newDraft).build(); } catch (ModuleException | ModuleNotFoundException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage()); } catch (IOException e) { LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId); throw new RuntimeException("Error while using the module api to convert json to Utlatande", e); } }
UtkastModuleApiController extends AbstractApiController { @GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod 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); draftAccessServiceHelper.validateAllowToReadUtkast(utkast, utkast.getPatientPersonnummer()); request.getSession(true).removeAttribute(LAST_SAVED_DRAFT); try { final ModuleApi moduleApi = moduleRegistry.getModuleApi(intygsTyp, utkast.getIntygTypeVersion()); Utlatande utlatande = moduleApi.getUtlatandeFromJson(utkast.getModel()); Patient savedPatientData = utlatande.getGrundData().getPatient(); Patient resolvedPatientData = getPatientDataFromPU(intygsTyp, utkast); DraftHolder newDraft = createDraftHolder(utkast, savedPatientData, resolvedPatientData); if (!resolvedPatientData.isCompleteAddressProvided()) { copyOldAddressToNewPatientData(savedPatientData, resolvedPatientData); } String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), resolvedPatientData); newDraft.setContent(updatedModel); if (utkast.getStatus().equals(UtkastStatus.DRAFT_INCOMPLETE) && utkast.getVersion() == 0 && utkast.getRelationKod() == null) { Optional<UtkastCandidateMetaData> metaData = utkastCandidateService.getCandidateMetaData(moduleApi, intygsTyp, resolvedPatientData, false); newDraft.setCandidateMetaData(metaData.orElse(null)); } resourceLinkHelper.decorateUtkastWithValidActionLinks( newDraft, intygsTyp, utlatande.getGrundData().getSkapadAv().getVardenhet(), resolvedPatientData.getPersonId()); return Response.ok(newDraft).build(); } catch (ModuleException | ModuleNotFoundException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage()); } catch (IOException e) { LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId); throw new RuntimeException("Error while using the module api to convert json to Utlatande", e); } } }
UtkastModuleApiController extends AbstractApiController { @GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod 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); draftAccessServiceHelper.validateAllowToReadUtkast(utkast, utkast.getPatientPersonnummer()); request.getSession(true).removeAttribute(LAST_SAVED_DRAFT); try { final ModuleApi moduleApi = moduleRegistry.getModuleApi(intygsTyp, utkast.getIntygTypeVersion()); Utlatande utlatande = moduleApi.getUtlatandeFromJson(utkast.getModel()); Patient savedPatientData = utlatande.getGrundData().getPatient(); Patient resolvedPatientData = getPatientDataFromPU(intygsTyp, utkast); DraftHolder newDraft = createDraftHolder(utkast, savedPatientData, resolvedPatientData); if (!resolvedPatientData.isCompleteAddressProvided()) { copyOldAddressToNewPatientData(savedPatientData, resolvedPatientData); } String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), resolvedPatientData); newDraft.setContent(updatedModel); if (utkast.getStatus().equals(UtkastStatus.DRAFT_INCOMPLETE) && utkast.getVersion() == 0 && utkast.getRelationKod() == null) { Optional<UtkastCandidateMetaData> metaData = utkastCandidateService.getCandidateMetaData(moduleApi, intygsTyp, resolvedPatientData, false); newDraft.setCandidateMetaData(metaData.orElse(null)); } resourceLinkHelper.decorateUtkastWithValidActionLinks( newDraft, intygsTyp, utlatande.getGrundData().getSkapadAv().getVardenhet(), resolvedPatientData.getPersonId()); return Response.ok(newDraft).build(); } catch (ModuleException | ModuleNotFoundException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage()); } catch (IOException e) { LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId); throw new RuntimeException("Error while using the module api to convert json to Utlatande", e); } }  }
UtkastModuleApiController extends AbstractApiController { @GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod 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); draftAccessServiceHelper.validateAllowToReadUtkast(utkast, utkast.getPatientPersonnummer()); request.getSession(true).removeAttribute(LAST_SAVED_DRAFT); try { final ModuleApi moduleApi = moduleRegistry.getModuleApi(intygsTyp, utkast.getIntygTypeVersion()); Utlatande utlatande = moduleApi.getUtlatandeFromJson(utkast.getModel()); Patient savedPatientData = utlatande.getGrundData().getPatient(); Patient resolvedPatientData = getPatientDataFromPU(intygsTyp, utkast); DraftHolder newDraft = createDraftHolder(utkast, savedPatientData, resolvedPatientData); if (!resolvedPatientData.isCompleteAddressProvided()) { copyOldAddressToNewPatientData(savedPatientData, resolvedPatientData); } String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), resolvedPatientData); newDraft.setContent(updatedModel); if (utkast.getStatus().equals(UtkastStatus.DRAFT_INCOMPLETE) && utkast.getVersion() == 0 && utkast.getRelationKod() == null) { Optional<UtkastCandidateMetaData> metaData = utkastCandidateService.getCandidateMetaData(moduleApi, intygsTyp, resolvedPatientData, false); newDraft.setCandidateMetaData(metaData.orElse(null)); } resourceLinkHelper.decorateUtkastWithValidActionLinks( newDraft, intygsTyp, utlatande.getGrundData().getSkapadAv().getVardenhet(), resolvedPatientData.getPersonId()); return Response.ok(newDraft).build(); } catch (ModuleException | ModuleNotFoundException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage()); } catch (IOException e) { LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId); throw new RuntimeException("Error while using the module api to convert json to Utlatande", e); } }  @GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response getDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        @Context HttpServletRequest request); @PUT @Path("/{intygsTyp}/{intygsId}/{version}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response saveDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        @PathParam("version") long version,
        @DefaultValue("false") @QueryParam("autoSave") boolean autoSave, byte[] payload, @Context HttpServletRequest request); @POST @Path("/{intygsTyp}/{intygsId}/validate") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response validateDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        byte[] payload); @POST @Path("/{intygsTyp}/{intygsId}/copyfromcandidate") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response copyFromCandidate(
        @PathParam("intygsTyp") String intygsTyp,
        @PathParam("intygsId") String intygsId,
        CopyFromCandidateRequest request); @POST @Path("/{intygsTyp}/{intygsId}/copy") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response copyUtkast(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String orgIntygsId); @DELETE @Path("/{intygsTyp}/{intygsId}/{version}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response discardDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        @PathParam("version") long version,
        @Context HttpServletRequest request); @POST @Path("/{intygsTyp}/{intygsId}/aterkalla") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response revokeLockedDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        RevokeSignedIntygParameter param); }
UtkastModuleApiController extends AbstractApiController { @GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod 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); draftAccessServiceHelper.validateAllowToReadUtkast(utkast, utkast.getPatientPersonnummer()); request.getSession(true).removeAttribute(LAST_SAVED_DRAFT); try { final ModuleApi moduleApi = moduleRegistry.getModuleApi(intygsTyp, utkast.getIntygTypeVersion()); Utlatande utlatande = moduleApi.getUtlatandeFromJson(utkast.getModel()); Patient savedPatientData = utlatande.getGrundData().getPatient(); Patient resolvedPatientData = getPatientDataFromPU(intygsTyp, utkast); DraftHolder newDraft = createDraftHolder(utkast, savedPatientData, resolvedPatientData); if (!resolvedPatientData.isCompleteAddressProvided()) { copyOldAddressToNewPatientData(savedPatientData, resolvedPatientData); } String updatedModel = moduleApi.updateBeforeSave(utkast.getModel(), resolvedPatientData); newDraft.setContent(updatedModel); if (utkast.getStatus().equals(UtkastStatus.DRAFT_INCOMPLETE) && utkast.getVersion() == 0 && utkast.getRelationKod() == null) { Optional<UtkastCandidateMetaData> metaData = utkastCandidateService.getCandidateMetaData(moduleApi, intygsTyp, resolvedPatientData, false); newDraft.setCandidateMetaData(metaData.orElse(null)); } resourceLinkHelper.decorateUtkastWithValidActionLinks( newDraft, intygsTyp, utlatande.getGrundData().getSkapadAv().getVardenhet(), resolvedPatientData.getPersonId()); return Response.ok(newDraft).build(); } catch (ModuleException | ModuleNotFoundException e) { throw new WebCertServiceException(WebCertServiceErrorCodeEnum.MODULE_PROBLEM, e.getMessage()); } catch (IOException e) { LOG.error("Error while using the module api to convert json to Utlatande for intygsId {}", intygsId); throw new RuntimeException("Error while using the module api to convert json to Utlatande", e); } }  @GET @Path("/{intygsTyp}/{intygsId}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response getDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        @Context HttpServletRequest request); @PUT @Path("/{intygsTyp}/{intygsId}/{version}") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response saveDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        @PathParam("version") long version,
        @DefaultValue("false") @QueryParam("autoSave") boolean autoSave, byte[] payload, @Context HttpServletRequest request); @POST @Path("/{intygsTyp}/{intygsId}/validate") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response validateDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        byte[] payload); @POST @Path("/{intygsTyp}/{intygsId}/copyfromcandidate") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response copyFromCandidate(
        @PathParam("intygsTyp") String intygsTyp,
        @PathParam("intygsId") String intygsId,
        CopyFromCandidateRequest request); @POST @Path("/{intygsTyp}/{intygsId}/copy") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response copyUtkast(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String orgIntygsId); @DELETE @Path("/{intygsTyp}/{intygsId}/{version}") @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response discardDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        @PathParam("version") long version,
        @Context HttpServletRequest request); @POST @Path("/{intygsTyp}/{intygsId}/aterkalla") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON + UTF_8_CHARSET) @PrometheusTimeMethod Response revokeLockedDraft(@PathParam("intygsTyp") String intygsTyp, @PathParam("intygsId") String intygsId,
        RevokeSignedIntygParameter param); static final String LAST_SAVED_DRAFT; }