Skip to content

Commit

Permalink
OK-418: Refactored the logic for data -> inputstream creation
Browse files Browse the repository at this point in the history
  • Loading branch information
pitkamak committed Apr 29, 2024
1 parent a8d6016 commit cb33b51
Showing 1 changed file with 24 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.stream.JsonWriter;
import fi.vm.sade.service.valintaperusteet.dto.ValintaperusteetDTO;
import fi.vm.sade.valinta.dokumenttipalvelu.SiirtotiedostoPalvelu;
import fi.vm.sade.valinta.dokumenttipalvelu.dto.ObjectMetadata;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -35,20 +38,26 @@ public SiirtotiedostoS3Client(
}

public String createSiirtotiedosto(List<ValintaperusteetDTO> data) {
try {
JsonArray jsonArray = new JsonArray(data.size());
data.forEach(item -> jsonArray.add(gson.toJsonTree(item)));
ObjectMetadata result =
siirtotiedostoPalvelu.saveSiirtotiedosto(
"valintaperusteet",
"hakukohde",
"",
new ByteArrayInputStream(jsonArray.toString().getBytes()),
2);
return result.key;
} catch (Exception e) {
logger.error("Siirtotiedoston luonti epäonnistui; ", e);
throw new RuntimeException(e);
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
try (OutputStreamWriter outputStreamWriter = new OutputStreamWriter(outputStream)) {
JsonWriter jsonWriter = new JsonWriter(outputStreamWriter);
jsonWriter.beginArray();
for (ValintaperusteetDTO dto : data) {
gson.toJson(gson.toJsonTree(dto), jsonWriter);
}
jsonWriter.endArray();
jsonWriter.close();

try (ByteArrayInputStream inputStream =
new ByteArrayInputStream(outputStream.toByteArray())) {
ObjectMetadata result =
siirtotiedostoPalvelu.saveSiirtotiedosto(
"valintaperusteet", "hakukohde", "", inputStream, 2);
return result.key;
}
}
} catch (IOException ioe) {
throw new RuntimeException("Siirtotiedoston luonti epäonnistui; ", ioe);
}
}

Expand Down

0 comments on commit cb33b51

Please sign in to comment.