Skip to content

Commit

Permalink
save
Browse files Browse the repository at this point in the history
  • Loading branch information
lwih committed Nov 28, 2024
1 parent ec07de2 commit 6c2668b
Show file tree
Hide file tree
Showing 32 changed files with 430 additions and 521 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export

// if you update this enum, update it too in the API and frontend
enum class ExportReportTypeEnum {
ALL, // all at once
AEM, // aka tableaux AEM
PATROL // aka rapport de patrouille
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export
import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.aem.AEMTableExport
import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionAEMExportEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission
import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FillAEMExcelRow
import fr.gouv.dgampa.rapportnav.infrastructure.utils.Base64Converter
Expand All @@ -27,7 +27,7 @@ class ExportMissionAEM(
) {

private val logger: Logger = LoggerFactory.getLogger(ExportMissionAEM::class.java)
fun execute(missionId: Int): MissionAEMExportEntity? {
fun execute(missionId: Int): MissionExportEntity? {
return try {

val inputStream = javaClass.getResourceAsStream(aemTemplatePath)
Expand All @@ -54,7 +54,7 @@ class ExportMissionAEM(

logger.info("ODS file created and converted to Base64")

return MissionAEMExportEntity(
return MissionExportEntity(
fileName = "Rapport_AEM.ods",
fileContent = base64Content
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2

import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission
import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FormatDateTime
import org.slf4j.LoggerFactory

@UseCase
class ExportMissionAEMCombined(
private val formatDateTime: FormatDateTime,
private val exportMissionAEMSingle: ExportMissionAEMSingle,
private val getMissionById: GetMission,
) {

private val logger = LoggerFactory.getLogger(ExportMissionAEMCombined::class.java)


/**
* Returns a merged Rapport de Patrouille
* Taking several missions and combining them into one
*
* @param missionIds a list of Mission Ids
* @return a MissionExportEntity with file name and content
*/
fun execute(missionIds: List<Int>): MissionExportEntity? {

try {

// retrieve missions
var missions = mutableListOf<MissionEntity>()

for (missionId in missionIds) {
val mission = getMissionById.execute(missionId)
if (mission != null) {
missions.add(mission)
}
}

// bundle actions and other stuff
val firstMission = missions.first() // Take all other fields from the first mission
val combinedActions = missions.flatMap { it.actions.orEmpty() } // Aggregate all actions from all missions
val mission = firstMission.copy(actions = combinedActions) // Create a new instance with aggregated actions

// create file
val output = exportMissionAEMSingle.createFile(mission = mission)


return MissionExportEntity(
fileName = "Rapport de patrouille combiné - ${formatDateTime.formatDate(mission.startDateTimeUtc)}.odt",
fileContent = output?.fileContent.orEmpty()
)

} catch (e: Exception) {
logger.error("[AEM] - error while generating report : ${e.message}")
return null
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2

import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.mission.CompletenessForStatsStatusEnum
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission
import org.slf4j.LoggerFactory
import java.io.File

@UseCase
class ExportMissionAEMMultipleZipped(
private val exportMissionAEMSingle: ExportMissionAEMSingle,
private val getMissionById: GetMission,
private val zipFiles: ZipFiles,
) {

private val logger = LoggerFactory.getLogger(ExportMissionAEMMultipleZipped::class.java)


/**
* Returns a zip with several Rapports de Patrouille
* There will be one file per complete mission
*
* @param missionIds a list of Mission Ids
* @return a MissionExportEntity with file name and content
*/
fun execute(missionIds: List<Int>): MissionExportEntity? {
try {
val filesToZip = mutableListOf<File>();

// retrieve missions
for (missionId in missionIds) {
val mission = getMissionById.execute(missionId)

// only keep complete missions
if (mission != null && mission.completenessForStats?.status === CompletenessForStatsStatusEnum.COMPLETE) {
val output = exportMissionAEMSingle.createFile(mission = mission)
filesToZip.add(File(output?.fileContent.orEmpty()))

}
}

// zip all files together
val output = zipFiles.execute(filesToZip)

return MissionExportEntity(
fileName = "Rapports de patrouille.odt",
fileContent = output
)

} catch (e: Exception) {
logger.error("[ExportMissionAEMMultipleZipped] - error building zipped mission export ${e.message}")
return null
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,79 +3,76 @@ package fr.gouv.dgampa.rapportnav.domain.use_cases.mission.export.v2
import fr.gouv.dgampa.rapportnav.config.UseCase
import fr.gouv.dgampa.rapportnav.domain.entities.aem.AEMTableExport
import fr.gouv.dgampa.rapportnav.domain.entities.mission.MissionEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionAEMExportEntity
import fr.gouv.dgampa.rapportnav.domain.entities.mission.nav.export.MissionExportEntity
import fr.gouv.dgampa.rapportnav.domain.use_cases.mission.GetMission
import fr.gouv.dgampa.rapportnav.domain.use_cases.utils.FillAEMExcelRow
import fr.gouv.dgampa.rapportnav.infrastructure.utils.Base64Converter
import fr.gouv.dgampa.rapportnav.infrastructure.utils.office.ExportExcelFile
import fr.gouv.dgampa.rapportnav.infrastructure.utils.office.OfficeConverter
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Value
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.StandardCopyOption

@UseCase
class ExportMissionsAEM(
class ExportMissionAEMSingle(
private val getMission: GetMission,
private val fillAEMExcelRow: FillAEMExcelRow,
@Value("\${rapportnav.aem.template.path}") private val aemTemplatePath: String,
@Value("\${rapportnav.aem.tmp_xlsx.path}") private val aemTmpXLSXPath: String,
@Value("\${rapportnav.aem.tmp_ods.path}") private val aemTmpODSPath: String,
private val fillAEMExcelRow: FillAEMExcelRow,
private val getMissionById: GetMission,

) {
private val logger: Logger = LoggerFactory.getLogger(ExportMissionsAEM::class.java)
) {

fun execute(missionIds: List<Int>): MissionAEMExportEntity? {
private val logger = LoggerFactory.getLogger(ExportMissionAEMSingle::class.java)

var missions = mutableListOf<MissionEntity>()

for (missionId in missionIds) {
val mission = getMissionById.execute(missionId)
if (mission != null) {
missions.add(mission)
}
fun execute(missionId: Int): MissionExportEntity? {
val mission: MissionEntity? = getMission.execute(missionId = missionId)
if (mission == null) {
logger.error("[ExportAEM] - Mission not found for missionId: $missionId")
return null
}
return createFile(mission)
}

fun createFile(mission: MissionEntity?): MissionExportEntity? {
if (mission == null) return null
return try {

val inputStream = javaClass.getResourceAsStream(aemTemplatePath)
?: throw IllegalArgumentException("Template file not found: $aemTemplatePath")

val tmpPath = Path.of(aemTmpXLSXPath)
val tmpPath = java.nio.file.Path.of(aemTmpXLSXPath)
Files.copy(inputStream, tmpPath, StandardCopyOption.REPLACE_EXISTING)
inputStream.close()

logger.info("Template file copied to temporary path: $tmpPath")

val excelFile = ExportExcelFile(tmpPath.toString())
val tableExportList = AEMTableExport.fromMissionList(missions)

if (tableExportList.isNotEmpty()) {
var rowStart = 3
val excelFile: ExportExcelFile = ExportExcelFile(tmpPath.toString())

for (tableExport in tableExportList) {
fillAEMExcelRow.fill(tableExport, excelFile, "Synthese", rowStart)
rowStart++
}
if (!mission.actions.isNullOrEmpty()) {
val tableExport = AEMTableExport.fromMission(mission)
fillAEMExcelRow.fill(tableExport, excelFile, "Synthese", 3)
excelFile.save()

logger.info("Excel file processed and saved for export on missions list")
logger.info("Excel file processed and saved")

val odsFile = OfficeConverter().convert(tmpPath.toString(), aemTmpODSPath)
val base64Content = Base64Converter().convertToBase64(odsFile)

return MissionAEMExportEntity(
logger.info("ODS file created and converted to Base64")

return MissionExportEntity(
fileName = "Rapport_AEM.ods",
fileContent = base64Content
)
} else {
logger.error("Mission or mission actions are null for missionId: ${mission.id}")
null
}

logger.error("Actions in missions list are null")
null
} catch (e: Exception) {
logger.error("An error occurred during mission processing", e)
null
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class ExportMissionPatrolCombined(
val mission = firstMission.copy(actions = combinedActions) // Create a new instance with aggregated actions

// create file
val output = exportMissionPatrolSingle.exportOdt(mission = mission)
val output = exportMissionPatrolSingle.createFile(mission = mission)


return MissionExportEntity(
Expand All @@ -53,7 +53,7 @@ class ExportMissionPatrolCombined(
)

} catch (e: Exception) {
logger.error("[RapportDePatrouille] - Error building data before sending it to RapportNav1 : ${e.message}")
logger.error("[RapportDePatrouille] - Error while generating report : ${e.message}")
return null
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class ExportMissionPatrolMultipleZipped(

// only keep complete missions
if (mission != null && mission.completenessForStats?.status === CompletenessForStatsStatusEnum.COMPLETE) {
val output = exportMissionPatrolSingle.exportOdt(mission = mission)
val output = exportMissionPatrolSingle.createFile(mission = mission)
filesToZip.add(File(output?.fileContent.orEmpty()))

}
Expand Down
Loading

0 comments on commit 6c2668b

Please sign in to comment.