Skip to content

Commit

Permalink
modificato metodo download scheduling
Browse files Browse the repository at this point in the history
  • Loading branch information
Francesco-Campagna committed Dec 11, 2024
1 parent 8d31ac4 commit dc53f8f
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,15 @@
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.ScheduleWithMachineDTO;
import unical.demacs.rdm.persistence.dto.*;
import unical.demacs.rdm.persistence.entities.Job;
import unical.demacs.rdm.persistence.entities.Machine;
import unical.demacs.rdm.persistence.entities.MachineType;
import unical.demacs.rdm.persistence.service.interfaces.IJobService;
import unical.demacs.rdm.persistence.service.interfaces.IJsonService;
import unical.demacs.rdm.persistence.service.interfaces.IMachineService;
import unical.demacs.rdm.persistence.service.interfaces.IMachineTypeService;
import unical.demacs.rdm.persistence.entities.Schedule;
import unical.demacs.rdm.persistence.service.interfaces.*;

import java.util.List;
import java.util.Map;
import java.util.Optional;

@RestController
@RequestMapping(value = "/api/v1/json", produces = "application/json")
Expand All @@ -33,6 +27,7 @@ public class JsonController {
private final IMachineService machineService;
private final IMachineTypeService machineTypeService;
private final IJsonService jsonService;
private final IScheduleService scheduleService;
private final ModelMapperExtended modelMapperExtended;

@Operation(summary = "Import Job data from JSON", description = "Import Job data into the system from JSON content.",
Expand Down Expand Up @@ -140,15 +135,9 @@ public ResponseEntity<List<ScheduleWithMachineDTO>> exportJobScheduledRO() {
@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());
}
public ResponseEntity<List<ScheduleDTO>> downloadSchedules() {
List<Schedule> schedules = scheduleService.getAllSchedules();
return ResponseEntity.ok(modelMapperExtended.mapList(schedules, ScheduleDTO.class));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,12 @@
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
Expand All @@ -30,30 +28,4 @@ public List<ScheduleWithMachineDTO> readScheduleFile(String fileName) {
}
}

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,12 +1,10 @@
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();
}
1 change: 0 additions & 1 deletion Frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 13 additions & 4 deletions Frontend/src/app/components/schedule/schedule.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -431,13 +431,22 @@ export class ScheduleComponent implements OnInit {
protected readonly Number = Number;

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

this.jsonService.exportMachine('body').subscribe(machineResponse => {
const machineJsonResponse = JSON.stringify(machineResponse);
const machineBlob = new Blob([machineJsonResponse], { type: 'application/json' });
const machineLink = document.createElement('a');
machineLink.href = URL.createObjectURL(machineBlob);
machineLink.download = 'machines.json';
machineLink.click();
});
}

}
111 changes: 88 additions & 23 deletions Frontend/src/app/generated-api/api/jsonController.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Observable } from 'rxjs';
import { JobDTO } from '../model/jobDTO';
import { MachineDTO } from '../model/machineDTO';
import { MachineTypeDTO } from '../model/machineTypeDTO';
import { ScheduleDTO } from '../model/scheduleDTO';
import { ScheduleWithMachineDTO } from '../model/scheduleWithMachineDTO';

import { BASE_PATH, COLLECTION_FORMATS } from '../variables';
Expand Down Expand Up @@ -59,15 +60,15 @@ export class JsonControllerService {


/**
* Export Job data to JSON
* Export all Job data to JSON.
* Download all Schedules as JSON
* Download all Schedules as a JSON file.
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public exportJob(observe?: 'body', reportProgress?: boolean): Observable<Array<JobDTO>>;
public exportJob(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Array<JobDTO>>>;
public exportJob(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Array<JobDTO>>>;
public exportJob(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {
public downloadSchedules(observe?: 'body', reportProgress?: boolean): Observable<Array<ScheduleDTO>>;
public downloadSchedules(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Array<ScheduleDTO>>>;
public downloadSchedules(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Array<ScheduleDTO>>>;
public downloadSchedules(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {

let headers = this.defaultHeaders;

Expand All @@ -84,7 +85,7 @@ export class JsonControllerService {
const consumes: string[] = [
];

return this.httpClient.request<Array<JobDTO>>('get',`${this.basePath}/api/v1/json/exportJob`,
return this.httpClient.request<Array<ScheduleDTO>>('get',`${this.basePath}/api/v1/json/download-schedules`,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
Expand All @@ -95,18 +96,19 @@ 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.
* Export Job data to JSON
* Export all Job data to JSON.
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @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> {
public exportJob(observe?: 'body', reportProgress?: boolean): Observable<Array<JobDTO>>;
public exportJob(observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<Array<JobDTO>>>;
public exportJob(observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<Array<JobDTO>>>;
public exportJob(observe: any = 'body', reportProgress: boolean = false ): Observable<any> {

let headers = this.defaultHeaders;

// to determine the Accept header
let httpHeaderAccepts: string[] = [
'application/json'
];
Expand All @@ -115,13 +117,18 @@ export class JsonControllerService {
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
});
// to determine the Content-Type header
const consumes: string[] = [
];

return this.httpClient.request<Array<JobDTO>>('get',`${this.basePath}/api/v1/json/exportJob`,
{
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}

/**
Expand Down Expand Up @@ -233,8 +240,8 @@ export class JsonControllerService {
}

/**
* Export Job data to JSON
* Export all Job data to JSON.
* Export RO scheduled jobs
* Export all RO scheduled jobs to JSON.
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
Expand Down Expand Up @@ -492,4 +499,62 @@ export class JsonControllerService {
);
}

/**
* Import Schedules from JSON
* Upload and import Schedules from a JSON file.
* @param file
* @param observe set whether or not to return the data Observable as the body, response or events. defaults to returning the body.
* @param reportProgress flag to report request and response progress.
*/
public importSchedulesForm(file?: Blob, observe?: 'body', reportProgress?: boolean): Observable<{ [key: string]: string; }>;
public importSchedulesForm(file?: Blob, observe?: 'response', reportProgress?: boolean): Observable<HttpResponse<{ [key: string]: string; }>>;
public importSchedulesForm(file?: Blob, observe?: 'events', reportProgress?: boolean): Observable<HttpEvent<{ [key: string]: string; }>>;
public importSchedulesForm(file?: Blob, observe: any = 'body', reportProgress: boolean = false ): Observable<any> {


let headers = this.defaultHeaders;

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

// to determine the Content-Type header
const consumes: string[] = [
'multipart/form-data'
];

const canConsumeForm = this.canConsumeForm(consumes);

let formParams: { append(param: string, value: any): void; };
let useForm = false;
let convertFormParamsToString = false;
// use FormData to transmit files using content-type "multipart/form-data"
// see https://stackoverflow.com/questions/4007969/application-x-www-form-urlencoded-or-multipart-form-data
useForm = canConsumeForm;
if (useForm) {
formParams = new FormData();
} else {
formParams = new HttpParams({encoder: new CustomHttpUrlEncodingCodec()});
}

if (file !== undefined) {
formParams = formParams.append('file', <any>file) as any || formParams;
}

return this.httpClient.request<{ [key: string]: string; }>('post',`${this.basePath}/api/v1/json/upload-schedules`,
{
body: convertFormParamsToString ? formParams.toString() : formParams,
withCredentials: this.configuration.withCredentials,
headers: headers,
observe: observe,
reportProgress: reportProgress
}
);
}

}
15 changes: 15 additions & 0 deletions Frontend/src/app/generated-api/model/jsonUploadschedulesBody.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
/**
* OpenAPI definition
* No description provided (generated by Swagger Codegen https://github.com/swagger-api/swagger-codegen)
*
* OpenAPI spec version: v0
*
*
* NOTE: This class is auto generated by the swagger code generator program.
* https://github.com/swagger-api/swagger-codegen.git
* Do not edit the class manually.
*/

export interface JsonUploadschedulesBody {
file: Blob;
}
1 change: 1 addition & 0 deletions Frontend/src/app/generated-api/model/models.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from './job';
export * from './jobDTO';
export * from './jsonUploadschedulesBody';
export * from './machine';
export * from './machineDTO';
export * from './machineType';
Expand Down

0 comments on commit dc53f8f

Please sign in to comment.