Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RSJ-78] aggiunto export degli schedules #20

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.http.*;
import org.springframework.web.bind.annotation.*;
import unical.demacs.rdm.config.ModelMapperExtended;
import unical.demacs.rdm.persistence.dto.JobDTO;
import unical.demacs.rdm.persistence.dto.MachineDTO;
import unical.demacs.rdm.persistence.dto.MachineTypeDTO;
import unical.demacs.rdm.persistence.dto.ScheduleDTO;
import unical.demacs.rdm.persistence.dto.ScheduleWithMachineDTO;
import unical.demacs.rdm.persistence.entities.Job;
import unical.demacs.rdm.persistence.entities.Machine;
Expand Down Expand Up @@ -141,4 +139,19 @@ public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledRO() {
List<ScheduleWithMachineDTO> schedules = jsonService.readScheduleFile("./data/job-scheduled-ro.json");
return ResponseEntity.ok(schedules);
}

@Operation(summary = "Download all Schedules as JSON", description = "Download all Schedules as a JSON file.",
tags = {"json-controller"})
@GetMapping(value = "/download-schedules", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<byte[]> downloadSchedules() {
try {
byte[] jsonContent = jsonService.exportSchedulesToJson();
HttpHeaders headers = new HttpHeaders();
headers.setContentDisposition(ContentDisposition.builder("attachment").filename("schedules.json").build());
headers.setContentType(MediaType.APPLICATION_JSON);
return new ResponseEntity<>(jsonContent, headers, HttpStatus.OK);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Error exporting schedules: ".getBytes());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,21 @@
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import unical.demacs.rdm.persistence.dto.*;
import unical.demacs.rdm.persistence.entities.Schedule;
import unical.demacs.rdm.persistence.repository.ScheduleRepository;
import unical.demacs.rdm.persistence.service.interfaces.IJsonService;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

@Service
@AllArgsConstructor
public class JsonServiceImpl implements IJsonService {

private final ObjectMapper objectMapper;
private final ScheduleRepository scheduleRepository;

public List<ScheduleWithMachineDTO> readScheduleFile(String fileName) {
File file = new File(fileName);
Expand All @@ -25,4 +29,31 @@ public List<ScheduleWithMachineDTO> readScheduleFile(String fileName) {
throw new RuntimeException(e);
}
}

public List<ScheduleDTO> getAllSchedules() {
List<Schedule> schedules = scheduleRepository.findAll();
return schedules.stream()
.map(schedule -> new ScheduleDTO(
schedule.getId(),
schedule.getJob().getId(),
schedule.getMachineType().getId(),
schedule.getDueDate(),
schedule.getStartTime(),
schedule.getDuration(),
schedule.getStatus(),
schedule.getMachine() != null ? schedule.getMachine().getId() : null,
schedule.getMachine() != null ? schedule.getMachine().getName() : null
))
.collect(Collectors.toList());
}

public byte[] exportSchedulesToJson() {
try {
List<ScheduleDTO> schedules = getAllSchedules();
return objectMapper.writeValueAsBytes(schedules);
} catch (IOException e) {
throw new RuntimeException("Errore durante l'esportazione degli schedule in JSON", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package unical.demacs.rdm.persistence.service.interfaces;

import unical.demacs.rdm.persistence.dto.ScheduleDTO;
import unical.demacs.rdm.persistence.dto.ScheduleWithMachineDTO;

import java.util.List;

public interface IJsonService {
public List<ScheduleWithMachineDTO> readScheduleFile(String fileName);
public List<ScheduleDTO> getAllSchedules();
public byte[] exportSchedulesToJson();
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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.ScheduleRepository;
import unical.demacs.rdm.persistence.service.implementation.JsonServiceImpl;
import unical.demacs.rdm.persistence.service.interfaces.IJsonService;

Expand All @@ -28,10 +29,13 @@ public class JsonServiceImplTest {
@Mock
private IJsonService jsonService;

@Mock
private ScheduleRepository scheduleRepository;

@BeforeEach
void setUp() {
objectMapper = new ObjectMapper();
jsonService = new JsonServiceImpl(objectMapper);
jsonService = new JsonServiceImpl(objectMapper, scheduleRepository);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ <h2>Tutti i Job</h2>
<h2>Job partecipanti alla schedulazione</h2>
<div class="schedule-actions">
<button class="button" (click)="openSchedulingDialog()">Avvia Scheduling</button>
<button class="button" (click)="downloadSchedules()">Download Scheduling</button>
</div>
</div>
<div class="table-responsive">
Expand Down
13 changes: 13 additions & 0 deletions Frontend/src/app/components/schedule/schedule.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
ScheduleControllerService,
JobControllerService,
SchedulerEngineService,
JsonControllerService,
} from '../../generated-api';
import { MatDialog, MatDialogRef } from '@angular/material/dialog';
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
Expand Down Expand Up @@ -51,6 +52,7 @@ export class ScheduleComponent implements OnInit {
constructor(
private jobService: JobControllerService,
private scheduleService: ScheduleControllerService,
private jsonService: JsonControllerService,
private dialog: MatDialog,
private fb: FormBuilder,
private schedulerEngineService: SchedulerEngineService
Expand Down Expand Up @@ -427,4 +429,15 @@ export class ScheduleComponent implements OnInit {
}

protected readonly Number = Number;

downloadSchedules() {
this.jsonService.downloadSchedules().subscribe(response => {
const blob = new Blob([response], { type: 'application/json' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = 'schedules.json';
link.click();
});
}

}
30 changes: 30 additions & 0 deletions Frontend/src/app/generated-api/api/jsonController.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,36 @@ export class JsonControllerService {
);
}

/**
* Download schedules as a JSON file
* This method triggers the download of the schedules JSON file.
* @param observe set whether or not to return the data Observable as the body, response or events.
* @param reportProgress flag to report request and response progress.
*/
public downloadSchedules(observe?: 'body', reportProgress?: boolean): Observable<any>;
public downloadSchedules(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<any>>;
public downloadSchedules(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<any>>;
public downloadSchedules(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {

let headers = this.defaultHeaders;

let httpHeaderAccepts: string[] = [
'application/json'
];
const httpHeaderAcceptSelected: string | undefined = this.configuration.selectHeaderAccept(httpHeaderAccepts);
if (httpHeaderAcceptSelected != undefined) {
headers = headers.set('Accept', httpHeaderAcceptSelected);
}

return this.httpClient.request('get', `${this.basePath}/api/v1/json/download-schedules`, {
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
responseType: 'blob',
reportProgress: reportProgress
});
}

/**
* Export Job data to JSON
* Export all Job data to JSON.
Expand Down
Loading