backend-api #6
@@ -1,6 +1,7 @@
 | 
				
			|||||||
package enseirb.myinpulse.controller;
 | 
					package enseirb.myinpulse.controller;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.itextpdf.text.DocumentException;
 | 
					import com.itextpdf.text.DocumentException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import enseirb.myinpulse.model.*;
 | 
					import enseirb.myinpulse.model.*;
 | 
				
			||||||
import enseirb.myinpulse.service.SharedApiService;
 | 
					import enseirb.myinpulse.service.SharedApiService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -10,7 +11,8 @@ import org.springframework.security.core.annotation.AuthenticationPrincipal;
 | 
				
			|||||||
import org.springframework.security.oauth2.jwt.Jwt;
 | 
					import org.springframework.security.oauth2.jwt.Jwt;
 | 
				
			||||||
import org.springframework.web.bind.annotation.*;
 | 
					import org.springframework.web.bind.annotation.*;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.FileNotFoundException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.net.URISyntaxException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@SpringBootApplication
 | 
					@SpringBootApplication
 | 
				
			||||||
@RestController
 | 
					@RestController
 | 
				
			||||||
@@ -83,10 +85,12 @@ public class SharedApi {
 | 
				
			|||||||
            @PathVariable int appointmentId, @AuthenticationPrincipal Jwt principal) {
 | 
					            @PathVariable int appointmentId, @AuthenticationPrincipal Jwt principal) {
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            sharedApiService.getPDFReport(appointmentId, principal.getClaimAsString("email"));
 | 
					            sharedApiService.getPDFReport(appointmentId, principal.getClaimAsString("email"));
 | 
				
			||||||
        } catch (FileNotFoundException e) {
 | 
					 | 
				
			||||||
            System.out.println(e + "File not found");
 | 
					 | 
				
			||||||
        } catch (DocumentException e) {
 | 
					        } catch (DocumentException e) {
 | 
				
			||||||
            System.out.println(e + "Document exception");
 | 
					            System.out.println(e + "Document exception");
 | 
				
			||||||
 | 
					        } catch (URISyntaxException e) {
 | 
				
			||||||
 | 
					            System.out.println(e + "Error with URI");
 | 
				
			||||||
 | 
					        } catch (IOException e) {
 | 
				
			||||||
 | 
					            System.out.println(e + "Failed to access file");
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,11 +4,7 @@ import static enseirb.myinpulse.model.ProjectDecisionValue.ACTIVE;
 | 
				
			|||||||
import static enseirb.myinpulse.model.ProjectDecisionValue.REJECTED;
 | 
					import static enseirb.myinpulse.model.ProjectDecisionValue.REJECTED;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import enseirb.myinpulse.model.*;
 | 
					import enseirb.myinpulse.model.*;
 | 
				
			||||||
import enseirb.myinpulse.service.database.AdministratorService;
 | 
					import enseirb.myinpulse.service.database.*;
 | 
				
			||||||
import enseirb.myinpulse.service.database.AppointmentService;
 | 
					 | 
				
			||||||
import enseirb.myinpulse.service.database.ProjectService;
 | 
					 | 
				
			||||||
import enseirb.myinpulse.service.database.ReportService;
 | 
					 | 
				
			||||||
import enseirb.myinpulse.service.database.UserService;
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
import org.apache.logging.log4j.LogManager;
 | 
					import org.apache.logging.log4j.LogManager;
 | 
				
			||||||
import org.apache.logging.log4j.Logger;
 | 
					import org.apache.logging.log4j.Logger;
 | 
				
			||||||
@@ -17,6 +13,9 @@ import org.springframework.http.HttpStatus;
 | 
				
			|||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import org.springframework.web.server.ResponseStatusException;
 | 
					import org.springframework.web.server.ResponseStatusException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@Service
 | 
					@Service
 | 
				
			||||||
public class AdminApiService {
 | 
					public class AdminApiService {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -28,6 +27,7 @@ public class AdminApiService {
 | 
				
			|||||||
    private final UtilsService utilsService;
 | 
					    private final UtilsService utilsService;
 | 
				
			||||||
    private final AppointmentService appointmentService;
 | 
					    private final AppointmentService appointmentService;
 | 
				
			||||||
    private final ReportService reportService;
 | 
					    private final ReportService reportService;
 | 
				
			||||||
 | 
					    private final SectionCellService sectionCellService;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    @Autowired
 | 
					    @Autowired
 | 
				
			||||||
    AdminApiService(
 | 
					    AdminApiService(
 | 
				
			||||||
@@ -36,13 +36,15 @@ public class AdminApiService {
 | 
				
			|||||||
            AdministratorService administratorService,
 | 
					            AdministratorService administratorService,
 | 
				
			||||||
            UtilsService utilsService,
 | 
					            UtilsService utilsService,
 | 
				
			||||||
            AppointmentService appointmentService,
 | 
					            AppointmentService appointmentService,
 | 
				
			||||||
            ReportService reportService) {
 | 
					            ReportService reportService,
 | 
				
			||||||
 | 
					            SectionCellService sectionCellService) {
 | 
				
			||||||
        this.projectService = projectService;
 | 
					        this.projectService = projectService;
 | 
				
			||||||
        this.userService = userService;
 | 
					        this.userService = userService;
 | 
				
			||||||
        this.administratorService = administratorService;
 | 
					        this.administratorService = administratorService;
 | 
				
			||||||
        this.utilsService = utilsService;
 | 
					        this.utilsService = utilsService;
 | 
				
			||||||
        this.appointmentService = appointmentService;
 | 
					        this.appointmentService = appointmentService;
 | 
				
			||||||
        this.reportService = reportService;
 | 
					        this.reportService = reportService;
 | 
				
			||||||
 | 
					        this.sectionCellService = sectionCellService;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: check if tests are sufficient - peer verification required
 | 
					    // TODO: check if tests are sufficient - peer verification required
 | 
				
			||||||
@@ -52,10 +54,36 @@ public class AdminApiService {
 | 
				
			|||||||
                        this.userService.getUserByEmail(mail).getIdUser()));
 | 
					                        this.userService.getUserByEmail(mail).getIdUser()));
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO
 | 
					 | 
				
			||||||
    public Iterable<Appointment> getUpcomingAppointments(String mail) {
 | 
					    public Iterable<Appointment> getUpcomingAppointments(String mail) {
 | 
				
			||||||
        logger.info("User {} check their upcoming appointments", mail);
 | 
					        logger.info("User {} check their upcoming appointments", mail);
 | 
				
			||||||
        throw new ResponseStatusException(HttpStatus.NOT_IMPLEMENTED, "Not implemented yet");
 | 
					        User user = this.userService.getUserByEmail(mail);
 | 
				
			||||||
 | 
					        List<Appointment> appointments = new ArrayList<>();
 | 
				
			||||||
 | 
					        if (user instanceof Administrator) {
 | 
				
			||||||
 | 
					            List<Project> projects = new ArrayList<>(((Administrator) user).getListProject());
 | 
				
			||||||
 | 
					            projects.forEach(
 | 
				
			||||||
 | 
					                    project -> {
 | 
				
			||||||
 | 
					                        project.getListSectionCell()
 | 
				
			||||||
 | 
					                                .forEach(
 | 
				
			||||||
 | 
					                                        sectionCell -> {
 | 
				
			||||||
 | 
					                                            appointments.addAll(
 | 
				
			||||||
 | 
					                                                    this.sectionCellService
 | 
				
			||||||
 | 
					                                                            .getAppointmentsBySectionCellId(
 | 
				
			||||||
 | 
					                                                                    sectionCell
 | 
				
			||||||
 | 
					                                                                            .getIdSectionCell()));
 | 
				
			||||||
 | 
					                                        });
 | 
				
			||||||
 | 
					                    });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (user instanceof Entrepreneur) {
 | 
				
			||||||
 | 
					            Project project = ((Entrepreneur) user).getProjectParticipation();
 | 
				
			||||||
 | 
					            project.getListSectionCell()
 | 
				
			||||||
 | 
					                    .forEach(
 | 
				
			||||||
 | 
					                            sectionCell -> {
 | 
				
			||||||
 | 
					                                appointments.addAll(
 | 
				
			||||||
 | 
					                                        this.sectionCellService.getAppointmentsBySectionCellId(
 | 
				
			||||||
 | 
					                                                sectionCell.getIdSectionCell()));
 | 
				
			||||||
 | 
					                            });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return appointments;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO: check if tests are sufficient - peer verification required
 | 
					    // TODO: check if tests are sufficient - peer verification required
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -13,8 +13,14 @@ import org.springframework.http.HttpStatus;
 | 
				
			|||||||
import org.springframework.stereotype.Service;
 | 
					import org.springframework.stereotype.Service;
 | 
				
			||||||
import org.springframework.web.server.ResponseStatusException;
 | 
					import org.springframework.web.server.ResponseStatusException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.FileNotFoundException;
 | 
					import java.io.File;
 | 
				
			||||||
import java.io.FileOutputStream;
 | 
					import java.io.FileOutputStream;
 | 
				
			||||||
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					import java.net.URI;
 | 
				
			||||||
 | 
					import java.net.URISyntaxException;
 | 
				
			||||||
 | 
					import java.nio.file.Files;
 | 
				
			||||||
 | 
					import java.nio.file.Paths;
 | 
				
			||||||
 | 
					import java.nio.file.StandardCopyOption;
 | 
				
			||||||
import java.time.LocalDateTime;
 | 
					import java.time.LocalDateTime;
 | 
				
			||||||
import java.time.format.DateTimeFormatter;
 | 
					import java.time.format.DateTimeFormatter;
 | 
				
			||||||
import java.util.ArrayList;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
@@ -95,7 +101,6 @@ public class SharedApiService {
 | 
				
			|||||||
        return project.getProjectAdministrator();
 | 
					        return project.getProjectAdministrator();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO
 | 
					 | 
				
			||||||
    public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
 | 
					    public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
 | 
				
			||||||
        if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
 | 
					        if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
 | 
				
			||||||
            logger.warn(
 | 
					            logger.warn(
 | 
				
			||||||
@@ -123,9 +128,8 @@ public class SharedApiService {
 | 
				
			|||||||
        return appointments;
 | 
					        return appointments;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    //
 | 
					 | 
				
			||||||
    public void getPDFReport(long appointmentId, String mail)
 | 
					    public void getPDFReport(long appointmentId, String mail)
 | 
				
			||||||
            throws FileNotFoundException, DocumentException {
 | 
					            throws DocumentException, URISyntaxException, IOException {
 | 
				
			||||||
        long projectId =
 | 
					        long projectId =
 | 
				
			||||||
                this.appointmentService
 | 
					                this.appointmentService
 | 
				
			||||||
                        .getAppointmentById(appointmentId)
 | 
					                        .getAppointmentById(appointmentId)
 | 
				
			||||||
@@ -202,9 +206,24 @@ public class SharedApiService {
 | 
				
			|||||||
        document.add(new Paragraph("\n"));
 | 
					        document.add(new Paragraph("\n"));
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        document.close();
 | 
					        document.close();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // Replace uri with website address
 | 
				
			||||||
 | 
					        Files.copy(
 | 
				
			||||||
 | 
					                new URI(
 | 
				
			||||||
 | 
					                                "http://localhost:8080/shared/projects/appointments/report/"
 | 
				
			||||||
 | 
					                                        + appointmentId)
 | 
				
			||||||
 | 
					                        .toURL()
 | 
				
			||||||
 | 
					                        .openStream(),
 | 
				
			||||||
 | 
					                Paths.get("Report" + appointmentId + ".pdf"),
 | 
				
			||||||
 | 
					                StandardCopyOption.REPLACE_EXISTING);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        // delete file, we don't want to stock all reports on the server
 | 
				
			||||||
 | 
					        File file = new File("Report" + appointmentId + ".pdf");
 | 
				
			||||||
 | 
					        if (!file.delete()) {
 | 
				
			||||||
 | 
					            logger.warn("Failed to delete report {}", file.getAbsolutePath());
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    // TODO
 | 
					 | 
				
			||||||
    public void createAppointmentRequest(Appointment appointment, String mail) {
 | 
					    public void createAppointmentRequest(Appointment appointment, String mail) {
 | 
				
			||||||
        long projectId =
 | 
					        long projectId =
 | 
				
			||||||
                appointment
 | 
					                appointment
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user