|
| 1 | +package backend.fullstack.export.application; |
| 2 | + |
| 3 | +import java.nio.charset.StandardCharsets; |
| 4 | +import java.time.LocalDate; |
| 5 | +import java.time.LocalDateTime; |
| 6 | +import java.time.format.DateTimeFormatter; |
| 7 | +import java.util.LinkedHashMap; |
| 8 | +import java.util.List; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.springframework.stereotype.Component; |
| 12 | + |
| 13 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 14 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 15 | +import com.fasterxml.jackson.databind.SerializationFeature; |
| 16 | + |
| 17 | +import backend.fullstack.checklist.domain.ChecklistInstance; |
| 18 | +import backend.fullstack.checklist.domain.ChecklistInstanceItem; |
| 19 | +import backend.fullstack.deviations.domain.Deviation; |
| 20 | +import backend.fullstack.temperature.domain.TemperatureReading; |
| 21 | + |
| 22 | +/** |
| 23 | + * Generates JSON export files from domain data. |
| 24 | + */ |
| 25 | +@Component |
| 26 | +public class JsonExportGenerator { |
| 27 | + |
| 28 | + private static final DateTimeFormatter DT_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); |
| 29 | + private static final DateTimeFormatter D_FMT = DateTimeFormatter.ofPattern("yyyy-MM-dd"); |
| 30 | + |
| 31 | + private final ObjectMapper objectMapper; |
| 32 | + |
| 33 | + public JsonExportGenerator() { |
| 34 | + this.objectMapper = new ObjectMapper(); |
| 35 | + this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT); |
| 36 | + } |
| 37 | + |
| 38 | + public byte[] generateTemperatureLogsJson(List<TemperatureReading> readings) { |
| 39 | + List<Map<String, Object>> items = readings.stream().map(r -> { |
| 40 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 41 | + map.put("id", r.getId()); |
| 42 | + map.put("temperature", r.getTemperature()); |
| 43 | + map.put("unitName", r.getUnit() != null ? r.getUnit().getName() : null); |
| 44 | + map.put("recordedAt", formatDateTime(r.getRecordedAt())); |
| 45 | + map.put("recordedBy", r.getRecordedBy() != null |
| 46 | + ? r.getRecordedBy().getFirstName() + " " + r.getRecordedBy().getLastName() |
| 47 | + : null); |
| 48 | + map.put("note", r.getNote()); |
| 49 | + map.put("isDeviation", r.isDeviation()); |
| 50 | + return map; |
| 51 | + }).toList(); |
| 52 | + |
| 53 | + Map<String, Object> root = new LinkedHashMap<>(); |
| 54 | + root.put("exportType", "TEMPERATURE_LOGS"); |
| 55 | + root.put("exportedAt", formatDateTime(LocalDateTime.now())); |
| 56 | + root.put("totalRecords", items.size()); |
| 57 | + root.put("data", items); |
| 58 | + |
| 59 | + return toBytes(root); |
| 60 | + } |
| 61 | + |
| 62 | + public byte[] generateDeviationsJson(List<Deviation> deviations) { |
| 63 | + List<Map<String, Object>> items = deviations.stream().map(d -> { |
| 64 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 65 | + map.put("id", d.getId()); |
| 66 | + map.put("title", d.getTitle()); |
| 67 | + map.put("description", d.getDescription()); |
| 68 | + map.put("status", d.getStatus() != null ? d.getStatus().name() : null); |
| 69 | + map.put("severity", d.getSeverity() != null ? d.getSeverity().name() : null); |
| 70 | + map.put("moduleType", d.getModuleType() != null ? d.getModuleType().name() : null); |
| 71 | + map.put("reportedBy", d.getReportedByName()); |
| 72 | + map.put("createdAt", formatDateTime(d.getCreatedAt())); |
| 73 | + map.put("resolvedBy", d.getResolvedByName()); |
| 74 | + map.put("resolvedAt", formatDateTime(d.getResolvedAt())); |
| 75 | + map.put("resolution", d.getResolution()); |
| 76 | + return map; |
| 77 | + }).toList(); |
| 78 | + |
| 79 | + Map<String, Object> root = new LinkedHashMap<>(); |
| 80 | + root.put("exportType", "DEVIATIONS"); |
| 81 | + root.put("exportedAt", formatDateTime(LocalDateTime.now())); |
| 82 | + root.put("totalRecords", items.size()); |
| 83 | + root.put("data", items); |
| 84 | + |
| 85 | + return toBytes(root); |
| 86 | + } |
| 87 | + |
| 88 | + public byte[] generateChecklistsJson(List<ChecklistInstance> checklists) { |
| 89 | + List<Map<String, Object>> items = checklists.stream().map(c -> { |
| 90 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 91 | + map.put("id", c.getId()); |
| 92 | + map.put("title", c.getTitle()); |
| 93 | + map.put("frequency", c.getFrequency() != null ? c.getFrequency().name() : null); |
| 94 | + map.put("date", formatDate(c.getDate())); |
| 95 | + map.put("status", c.getStatus() != null ? c.getStatus().name() : null); |
| 96 | + map.put("items", c.getItems().stream().map(this::mapChecklistItem).toList()); |
| 97 | + return map; |
| 98 | + }).toList(); |
| 99 | + |
| 100 | + Map<String, Object> root = new LinkedHashMap<>(); |
| 101 | + root.put("exportType", "CHECKLISTS"); |
| 102 | + root.put("exportedAt", formatDateTime(LocalDateTime.now())); |
| 103 | + root.put("totalRecords", items.size()); |
| 104 | + root.put("data", items); |
| 105 | + |
| 106 | + return toBytes(root); |
| 107 | + } |
| 108 | + |
| 109 | + private Map<String, Object> mapChecklistItem(ChecklistInstanceItem item) { |
| 110 | + Map<String, Object> map = new LinkedHashMap<>(); |
| 111 | + map.put("id", item.getId()); |
| 112 | + map.put("text", item.getText()); |
| 113 | + map.put("completed", item.isCompleted()); |
| 114 | + map.put("completedByUserId", item.getCompletedByUserId()); |
| 115 | + map.put("completedAt", item.getCompletedAt() != null ? item.getCompletedAt().toString() : null); |
| 116 | + return map; |
| 117 | + } |
| 118 | + |
| 119 | + private String formatDateTime(LocalDateTime dt) { |
| 120 | + return dt != null ? dt.format(DT_FMT) : null; |
| 121 | + } |
| 122 | + |
| 123 | + private String formatDate(LocalDate d) { |
| 124 | + return d != null ? d.format(D_FMT) : null; |
| 125 | + } |
| 126 | + |
| 127 | + private byte[] toBytes(Object value) { |
| 128 | + try { |
| 129 | + return objectMapper.writeValueAsString(value).getBytes(StandardCharsets.UTF_8); |
| 130 | + } catch (JsonProcessingException e) { |
| 131 | + throw new IllegalStateException("Failed to generate JSON export", e); |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments