From a2d5e08f3c76746e51955b43510f066b9e1443e6 Mon Sep 17 00:00:00 2001 From: Francesco-Campagna Date: Wed, 11 Dec 2024 15:21:29 +0100 Subject: [PATCH] [RSJ-76] aggiunto import degli scheduling esterni in json --- .../demacs/rdm/controller/JsonController.java | 17 ++++++++ .../implementation/JsonServiceImpl.java | 42 +++++++++++++++++++ .../service/interfaces/IJsonService.java | 3 ++ .../rdm/service/JsonServiceImplTest.java | 14 ++++++- .../schedule/schedule.component.html | 2 + .../components/schedule/schedule.component.ts | 34 +++++++++++++++ 6 files changed, 111 insertions(+), 1 deletion(-) diff --git a/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java b/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java index 24750dd..8b0f03a 100644 --- a/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java +++ b/Backend/src/main/java/unical/demacs/rdm/controller/JsonController.java @@ -5,6 +5,7 @@ import lombok.AllArgsConstructor; import org.springframework.http.*; import org.springframework.web.bind.annotation.*; +import org.springframework.web.multipart.MultipartFile; import unical.demacs.rdm.config.ModelMapperExtended; import unical.demacs.rdm.persistence.dto.*; import unical.demacs.rdm.persistence.entities.Job; @@ -140,4 +141,20 @@ public ResponseEntity> downloadSchedules() { return ResponseEntity.ok(modelMapperExtended.mapList(schedules, ScheduleDTO.class)); } + @Operation(summary = "Import Schedules from JSON", description = "Upload and import Schedules from a JSON file.", + tags = {"json-controller"}) + @PostMapping(value = "/upload-schedules", consumes = MediaType.MULTIPART_FORM_DATA_VALUE) + public ResponseEntity> importSchedules(@RequestParam("file") MultipartFile file) { + try { + if (file.isEmpty()) { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Map.of("message", "File is empty.")); + } + jsonService.importSchedules(file); + return ResponseEntity.ok(Map.of("message", "Schedules imported successfully.")); + } catch (RuntimeException e) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) + .body(Map.of("message", "Error importing schedules: " + e.getMessage())); + } + } + } \ No newline at end of file diff --git a/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/JsonServiceImpl.java b/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/JsonServiceImpl.java index eb446a2..a4c9937 100644 --- a/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/JsonServiceImpl.java +++ b/Backend/src/main/java/unical/demacs/rdm/persistence/service/implementation/JsonServiceImpl.java @@ -3,7 +3,12 @@ import com.fasterxml.jackson.databind.ObjectMapper; import lombok.AllArgsConstructor; import org.springframework.stereotype.Service; +import org.springframework.web.multipart.MultipartFile; import unical.demacs.rdm.persistence.dto.*; +import unical.demacs.rdm.persistence.entities.Schedule; +import unical.demacs.rdm.persistence.repository.JobRepository; +import unical.demacs.rdm.persistence.repository.MachineRepository; +import unical.demacs.rdm.persistence.repository.MachineTypeRepository; import unical.demacs.rdm.persistence.repository.ScheduleRepository; import unical.demacs.rdm.persistence.service.interfaces.IJsonService; @@ -17,6 +22,9 @@ public class JsonServiceImpl implements IJsonService { private final ObjectMapper objectMapper; private final ScheduleRepository scheduleRepository; + private final JobRepository jobRepository; + private final MachineRepository machineRepository; + private final MachineTypeRepository machineTypeRepository; public List readScheduleFile(String fileName) { File file = new File(fileName); @@ -28,4 +36,38 @@ public List readScheduleFile(String fileName) { } } + @Override + public void importSchedules(MultipartFile file) { + try { + String jsonContent = new String(file.getBytes()); + List schedules = objectMapper.readValue( + jsonContent, + objectMapper.getTypeFactory().constructCollectionType(List.class, ScheduleWithMachineDTO.class) + ); + for (ScheduleWithMachineDTO dto : schedules) { + Schedule schedule = mapDtoToEntity(dto); + scheduleRepository.save(schedule); + } + } catch (IOException e) { + throw new RuntimeException("Errore durante la lettura del file JSON: " + e.getMessage(), e); + } + } + + private Schedule mapDtoToEntity(ScheduleWithMachineDTO dto) { + return Schedule.scheduleBuilder() + .job(jobRepository.findById(dto.getJobId()) + .orElseThrow(() -> new RuntimeException("Job non trovato"))) + .machineType(machineTypeRepository.findById(dto.getMachineTypeId()) + .orElseThrow(() -> new RuntimeException("MachineType non trovato"))) + .machine(dto.getMachineId() != null ? + machineRepository.findById(dto.getMachineId()) + .orElseThrow(() -> new RuntimeException("Machine non trovata")) + : null) + .dueDate(dto.getDueDate()) + .startTime(dto.getStartTime()) + .duration(dto.getDuration()) + .status(dto.getStatus()) + .build(); + } + } \ No newline at end of file diff --git a/Backend/src/main/java/unical/demacs/rdm/persistence/service/interfaces/IJsonService.java b/Backend/src/main/java/unical/demacs/rdm/persistence/service/interfaces/IJsonService.java index d80feb4..85cae00 100644 --- a/Backend/src/main/java/unical/demacs/rdm/persistence/service/interfaces/IJsonService.java +++ b/Backend/src/main/java/unical/demacs/rdm/persistence/service/interfaces/IJsonService.java @@ -1,10 +1,13 @@ package unical.demacs.rdm.persistence.service.interfaces; +import org.springframework.web.multipart.MultipartFile; import unical.demacs.rdm.persistence.dto.ScheduleWithMachineDTO; import java.util.List; public interface IJsonService { public List readScheduleFile(String fileName); + void importSchedules(MultipartFile file); + } \ No newline at end of file diff --git a/Backend/src/test/java/unical/demacs/rdm/service/JsonServiceImplTest.java b/Backend/src/test/java/unical/demacs/rdm/service/JsonServiceImplTest.java index 2594c99..52fc050 100644 --- a/Backend/src/test/java/unical/demacs/rdm/service/JsonServiceImplTest.java +++ b/Backend/src/test/java/unical/demacs/rdm/service/JsonServiceImplTest.java @@ -9,6 +9,9 @@ import unical.demacs.rdm.persistence.dto.ScheduleDTO; import unical.demacs.rdm.persistence.dto.ScheduleWithMachineDTO; import unical.demacs.rdm.persistence.enums.ScheduleStatus; +import unical.demacs.rdm.persistence.repository.JobRepository; +import unical.demacs.rdm.persistence.repository.MachineRepository; +import unical.demacs.rdm.persistence.repository.MachineTypeRepository; import unical.demacs.rdm.persistence.repository.ScheduleRepository; import unical.demacs.rdm.persistence.service.implementation.JsonServiceImpl; import unical.demacs.rdm.persistence.service.interfaces.IJsonService; @@ -29,13 +32,22 @@ public class JsonServiceImplTest { @Mock private IJsonService jsonService; + @Mock + private MachineRepository machineRepository; + + @Mock + private MachineTypeRepository machineTypeRepository; + + @Mock + private JobRepository jobRepository; + @Mock private ScheduleRepository scheduleRepository; @BeforeEach void setUp() { objectMapper = new ObjectMapper(); - jsonService = new JsonServiceImpl(objectMapper, scheduleRepository); + jsonService = new JsonServiceImpl(objectMapper, scheduleRepository, jobRepository, machineRepository, machineTypeRepository); } @Test diff --git a/Frontend/src/app/components/schedule/schedule.component.html b/Frontend/src/app/components/schedule/schedule.component.html index a3bc136..d37e2f6 100644 --- a/Frontend/src/app/components/schedule/schedule.component.html +++ b/Frontend/src/app/components/schedule/schedule.component.html @@ -55,6 +55,8 @@

Job partecipanti alla schedulazione

+ +
diff --git a/Frontend/src/app/components/schedule/schedule.component.ts b/Frontend/src/app/components/schedule/schedule.component.ts index e4dc5de..6cf3004 100644 --- a/Frontend/src/app/components/schedule/schedule.component.ts +++ b/Frontend/src/app/components/schedule/schedule.component.ts @@ -448,5 +448,39 @@ export class ScheduleComponent implements OnInit { machineLink.click(); }); } + + triggerFileInput(): void { + const fileInput = document.getElementById('hiddenFileInput') as HTMLInputElement; + if (fileInput) { + fileInput.value = ''; + fileInput.click(); + } else { + console.error('Elemento file input non trovato.'); + } + } + + importSchedules(event: Event): void { + const input = event.target as HTMLInputElement; + const file = input.files?.[0]; + if (file) { + console.log('File selezionato:', file); + if (file.type === 'application/json' || file.name.endsWith('.json')) { + console.log('Il file è un JSON valido.'); + const formData = new FormData(); + formData.append('file', file); + this.jsonService.importSchedulesForm(file).subscribe({ + next:() => { + Swal.fire('Importato', 'Il file selezionato è stato importato con successo.', 'success'); + }, + error:() => { + Swal.fire('Errore', 'Errore durante l\'importazione. Riprova.', 'error'); + } + }); + } else { + Swal.fire('Errore', 'Il file selezionato non è un file JSON valido. Riprova.', 'error'); + console.error('Formato file non valido:', file.type); + } + } + } }