Skip to content
Snippets Groups Projects
Commit ce0184fc authored by OZGCloud's avatar OZGCloud
Browse files

WIP: OZG-6275 added poc code for saving MUK Antwoerten

parent beec6d70
Branches
Tags
No related merge requests found
......@@ -24,6 +24,7 @@ package de.ozgcloud.nachrichten.postfach.muk.transfer;
import static de.ozgcloud.nachrichten.postfach.muk.transfer.MukPostfachMessageMapper.*;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
......@@ -36,8 +37,10 @@ import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Stream;
import org.apache.commons.lang3.StringUtils;
import org.bson.types.ObjectId;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.stereotype.Service;
......@@ -67,20 +70,39 @@ import lombok.extern.log4j.Log4j2;
@ConditionalOnProperty(prefix = MukPostfachProperties.PREFIX, name = { "server" })
public class ElsterTransferRemoteService {
public static final String POSTFACH = "postfach";
private final ElsterRestClient restClient;
private final MukPostfachProperties postfachProperties;
private final MukPostfachMessageMapper mapper;
private final MukOzgCloudFileMapper fileMapper;
private final AttachmentService attachmentService;
public static final String VORGANG_ID_REFIX = "VorgangId[";
public static final String ANTWORTEN_ENDPOINT = "/rest/eingang/antworten";
public static final String BEREITSTELLUNGS_ENDPUNKT = "/rest/bereitstellungsauftrag/v3";
public static final String BEREITSTELLUNG_UPLOAD_ENDPUNKT = "/rest/bereitstellungsauftrag/file-upload";
static final Set<PostfachNachricht.ReplyOption> REPLY_POSSIBLE_OPTION = EnumSet.of(PostfachNachricht.ReplyOption.POSSIBLE,
PostfachNachricht.ReplyOption.MANDATORY);
static final Set<String> MITTEILUNG_DATENARTEN = Set.of(DATENART_KURZMITTEILUNG, DATENART_MITTEILUNG);
static final Set<String> NACHRICHTEN_MIT_ANHANG = Set.of(DATENART_BESCHEID, DATENART_MITTEILUNG);
private final ElsterRestClient restClient;
private final MukPostfachProperties postfachProperties;
private final MukPostfachMessageMapper mapper;
private final MukOzgCloudFileMapper fileMapper;
private final AttachmentService attachmentService;
private static boolean anhangNeedToBeUploaded(final BereitstellungAuftragNeuV3 bereitstellungAuftrag) {
return NACHRICHTEN_MIT_ANHANG.contains(bereitstellungAuftrag.getDatenart());
}
private static boolean isMitteilung(final BereitstellungAuftragNeuV3 bereitstellungAuftrag) {
return MITTEILUNG_DATENARTEN.contains(bereitstellungAuftrag.getDatenart());
}
private static boolean isReplyPossible(final PostfachNachricht nachricht) {
return REPLY_POSSIBLE_OPTION.contains(nachricht.getReplyOption());
}
static OzgCloudUploadFile createOzgCloudUploadFile(final DownloadAnhangDetails downloadAnhangDetails, final String vorgangId) {
return OzgCloudUploadFile.builder()
.fileName(downloadAnhangDetails.getDatei())
.fieldName(POSTFACH)
.vorgangId(vorgangId)
.contentType(downloadAnhangDetails.getMimeType()).build();
}
public AuftragAngelegt send(PostfachNachricht nachricht) {
var auftrag = createBereitstellungsAuftrag(nachricht);
......@@ -93,7 +115,8 @@ public class ElsterTransferRemoteService {
bereitstellungAuftrag.setAbsender(getAbsender());
if (anhangNeedToBeUploaded(bereitstellungAuftrag)) {
bereitstellungAuftrag.setAnhaenge(nachricht.getAttachments().stream().map(fileId -> uploadAnhang(fileId, nachricht.getVorgangId())).toList());
bereitstellungAuftrag.setAnhaenge(
nachricht.getAttachments().stream().map(fileId -> uploadAnhang(fileId, nachricht.getVorgangId())).toList());
}
if (isMitteilung(bereitstellungAuftrag)) {
......@@ -107,18 +130,6 @@ public class ElsterTransferRemoteService {
return bereitstellungAuftrag;
}
private static boolean anhangNeedToBeUploaded(final BereitstellungAuftragNeuV3 bereitstellungAuftrag) {
return NACHRICHTEN_MIT_ANHANG.contains(bereitstellungAuftrag.getDatenart());
}
private static boolean isMitteilung(final BereitstellungAuftragNeuV3 bereitstellungAuftrag) {
return MITTEILUNG_DATENARTEN.contains(bereitstellungAuftrag.getDatenart());
}
private static boolean isReplyPossible(final PostfachNachricht nachricht) {
return REPLY_POSSIBLE_OPTION.contains(nachricht.getReplyOption());
}
String getAbsender() {
return postfachProperties.getSender();
}
......@@ -152,9 +163,17 @@ public class ElsterTransferRemoteService {
BereitstellungAntwort createAntwort(PostfachNachricht nachricht) {
var antwort = new BereitstellungAntwort();
// FIXME: nachricht needs to have a nachricht id already set. Needs to be fixed in de.ozgcloud.nachrichten.postfach.PostfachService or de.ozgcloud.nachrichten.postfach.PostfachEventListener.buildNachricht
antwort.setZuordnungskriterium(ObjectIdToUUIDConverter.toUUID(Objects.isNull(nachricht.getId()) ? ObjectId.get().toHexString() : nachricht.getId()));
antwort.setAntwortBetreffe(Set.of(postfachProperties.getReplySubjectPrefix() + nachricht.getSubject() + " VorgangId: " + nachricht.getVorgangId()));
if (Objects.isNull(nachricht.getId())) {
antwort.setZuordnungskriterium(Objects.isNull(nachricht.getId()) ? UUID.randomUUID() : ObjectIdToUUIDConverter.toUUID(nachricht.getId()));
antwort.setAntwortBetreffe(
Set.of(VORGANG_ID_REFIX + nachricht.getVorgangId() + "]" + postfachProperties.getReplySubjectPrefix() + nachricht.getSubject()));
} else {
antwort.setZuordnungskriterium(ObjectIdToUUIDConverter.toUUID(nachricht.getId()));
antwort.setAntwortBetreffe(Set.of(postfachProperties.getReplySubjectPrefix() + nachricht.getSubject()));
}
antwort.setAntwortFrist(LocalDate.now().plusDays(postfachProperties.getReplyInDays()));
return antwort;
}
......@@ -199,7 +218,7 @@ public class ElsterTransferRemoteService {
if (Objects.nonNull(antwortDetails.getAnhaenge())) {
var idList = antwortDetails.getAnhaenge().stream()
.filter(Objects::nonNull)
.map(downloadAnhangDetails -> saveAnhang(downloadAnhang(downloadAnhangDetails), downloadAnhangDetails, nachrichtId.toHexString()))
.map(downloadAnhangDetails -> saveAnhang(downloadAnhang(downloadAnhangDetails), downloadAnhangDetails, nachrichtId))
.filter(Optional::isPresent)
.map(Optional::get)
.toList();
......@@ -216,13 +235,24 @@ public class ElsterTransferRemoteService {
return restClient.getForObject(url, byte[].class);
}
Optional<String> saveAnhang(byte[] data, DownloadAnhangDetails downloadAnhangDetails, String nachrichtId) {
Optional<String> saveAnhang(byte[] data, DownloadAnhangDetails downloadAnhangDetails, ObjectId nachrichtId) {
LOG.info("Saving Attachment from DownloadAnhangDetails {}", downloadAnhangDetails);
Optional<String> fileIdOptional = Optional.empty();
if (Objects.nonNull(data)) {
// TODO update to new API as soon the new api is available. Save to file here and then upload attachment
try (var dataIn = new ByteArrayInputStream(data)) {
fileIdOptional = storeToOzgCloud(downloadAnhangDetails, nachrichtId, dataIn);
if (Objects.isNull(nachrichtId) && Objects.nonNull(downloadAnhangDetails) && StringUtils.isNotEmpty(
downloadAnhangDetails.getDateiBezeichnung())) {
// can be removed when new api is available
var dateiBezeichnung = downloadAnhangDetails.getDateiBezeichnung();
var vorgangId = dateiBezeichnung.substring(VORGANG_ID_REFIX.length(),
VORGANG_ID_REFIX.length() + ObjectId.get().toHexString().length());
fileIdOptional = storeToOzgCloud(downloadAnhangDetails, vorgangId, dataIn);
} else {
// use new api here
// fileIdOptional = storeToOzgCloud(downloadAnhangDetails, nachrichtId, dataIn);
LOG.warn("Saving with NachrichtId not implemented yet.");
}
} catch (IOException e) {
LOG.error("Error transferring attachment body from Elster-Transfer to OZG-Cloud.", e);
throw new TechnicalException("Error downloading anhang", e);
......@@ -237,8 +267,8 @@ public class ElsterTransferRemoteService {
Optional<String> storeToOzgCloud(DownloadAnhangDetails downloadAnhangDetails, String vorgangId, ByteArrayInputStream dataIn) throws IOException {
OzgCloudFileId ozgCloudFileId;
// TODO update to new API as soon the new api is available. Use saved temp file
/*try (BufferedInputStream dataStream = new BufferedInputStream(dataIn)) {
try (BufferedInputStream dataStream = new BufferedInputStream(dataIn)) {
ozgCloudFileId = attachmentService.store(createOzgCloudUploadFile(downloadAnhangDetails, vorgangId), dataStream);
}
......@@ -247,16 +277,8 @@ public class ElsterTransferRemoteService {
}
LOG.error("Received no OzgCloudFileId when saving attachment {} to vorgangId {}", downloadAnhangDetails, vorgangId);
*/
return Optional.empty();
}
static OzgCloudUploadFile createOzgCloudUploadFile(final DownloadAnhangDetails downloadAnhangDetails, final String nachrichtId) {
return OzgCloudUploadFile.builder()
.fileName(downloadAnhangDetails.getDatei())
.fieldName(POSTFACH)
.contentType(downloadAnhangDetails.getMimeType()).build();
}
}
This diff is collapsed.
......@@ -28,13 +28,16 @@ import java.net.URI;
import java.util.List;
import java.util.UUID;
import org.bson.types.ObjectId;
import de.ozgcloud.muk.elster.transfer.AntwortDetails;
import de.ozgcloud.muk.elster.transfer.AntwortDetailsList;
import de.ozgcloud.muk.elster.transfer.DownloadAnhangDetails;
public class AntwortDetailsTestFactory {
public static final String DATEI = "test-data.csv";
public static final String DATEI_BEZEICHNUNG = "test daten";
public static final String VORGANG_OBJECT_ID = ObjectId.get().toHexString();
public static final String DATEI_BEZEICHNUNG = "VorgangId[" + VORGANG_OBJECT_ID + "] Test";
public static final URI DOWNLOAD_URI = URI.create("/download");
public static final String TEXT_CSV = "text/csv";
public static final UUID ZUORDNUNG = ObjectIdToUUIDConverter.toUUID(NACHRICHT_ID);
......
......@@ -307,7 +307,7 @@ class ElsterTransferRemoteServiceTest {
downloadAnhangDetails.setDateiBezeichnung(OzgCloudFileTestFactory.NAME);
downloadAnhangDetails.setDatei("");
var res = elsterTransferService.saveAnhang(data, downloadAnhangDetails, ObjectId.get().toHexString());
var res = elsterTransferService.saveAnhang(data, downloadAnhangDetails, new ObjectId(NACHRICHT_ID));
assertThat(res).isNotPresent();
......@@ -391,7 +391,7 @@ class ElsterTransferRemoteServiceTest {
doThrow(IOException.class).when(elsterTransferService).storeToOzgCloud(any(), any(), any());
assertThatExceptionOfType(TechnicalException.class).isThrownBy(
() -> elsterTransferService.saveAnhang(data, downloadAnhangDetails, NACHRICHT_ID)
() -> elsterTransferService.saveAnhang(data, downloadAnhangDetails, null)
);
}
}
......@@ -405,7 +405,7 @@ class ElsterTransferRemoteServiceTest {
void setUp() {
downloadAnhangDetails.setMimeType(OzgCloudFileTestFactory.CONTENT_TYPE);
downloadAnhangDetails.setDownloadUrl(URI.create("/download"));
downloadAnhangDetails.setDateiBezeichnung(OzgCloudFileTestFactory.NAME);
downloadAnhangDetails.setDateiBezeichnung(DATEI_BEZEICHNUNG);
downloadAnhangDetails.setDatei(OzgCloudFileTestFactory.NAME);
}
......@@ -424,7 +424,6 @@ class ElsterTransferRemoteServiceTest {
}
@Test
@Disabled("Needs to be reimplemented when the new File API is available")
void shouldHaveVorgangId() {
var file = ElsterTransferRemoteService.createOzgCloudUploadFile(downloadAnhangDetails, vorgangId);
......@@ -444,5 +443,27 @@ class ElsterTransferRemoteServiceTest {
assertThat(file.getFieldName()).isEqualTo(POSTFACH);
}
}
@Nested
class TestSaveFile {
@Test
void shouldExtractVorgangIdFromDateibezeichnung() throws IOException {
when(attachmentService.store(any(), any())).thenReturn(new OzgCloudFileId(UUID.randomUUID().toString()));
elsterTransferService.saveAnhang("Test,Data".getBytes(), AntwortDetailsTestFactory.createDownloadAnhangDetails(), null);
verify(elsterTransferService).storeToOzgCloud(any(), eq(VORGANG_OBJECT_ID), any());
}
@Test
void shouldSaveAttachment() {
when(attachmentService.store(any(), any())).thenReturn(new OzgCloudFileId(UUID.randomUUID().toString()));
var fileIdOpt = elsterTransferService.saveAnhang("Test,Data".getBytes(), AntwortDetailsTestFactory.createDownloadAnhangDetails(), null);
assertThat(fileIdOpt).isPresent();
}
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment