backend-api #6
@@ -1,6 +1,7 @@
 | 
			
		||||
package enseirb.myinpulse.controller;
 | 
			
		||||
 | 
			
		||||
import com.itextpdf.text.DocumentException;
 | 
			
		||||
 | 
			
		||||
import enseirb.myinpulse.model.*;
 | 
			
		||||
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.web.bind.annotation.*;
 | 
			
		||||
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.IOException;
 | 
			
		||||
import java.net.URISyntaxException;
 | 
			
		||||
 | 
			
		||||
@SpringBootApplication
 | 
			
		||||
@RestController
 | 
			
		||||
@@ -83,10 +85,12 @@ public class SharedApi {
 | 
			
		||||
            @PathVariable int appointmentId, @AuthenticationPrincipal Jwt principal) {
 | 
			
		||||
        try {
 | 
			
		||||
            sharedApiService.getPDFReport(appointmentId, principal.getClaimAsString("email"));
 | 
			
		||||
        } catch (FileNotFoundException e) {
 | 
			
		||||
            System.out.println(e + "File not found");
 | 
			
		||||
        } catch (DocumentException e) {
 | 
			
		||||
            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 enseirb.myinpulse.model.*;
 | 
			
		||||
import enseirb.myinpulse.service.database.AdministratorService;
 | 
			
		||||
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 enseirb.myinpulse.service.database.*;
 | 
			
		||||
 | 
			
		||||
import org.apache.logging.log4j.LogManager;
 | 
			
		||||
import org.apache.logging.log4j.Logger;
 | 
			
		||||
@@ -17,6 +13,9 @@ import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.web.server.ResponseStatusException;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
 | 
			
		||||
@Service
 | 
			
		||||
public class AdminApiService {
 | 
			
		||||
 | 
			
		||||
@@ -28,6 +27,7 @@ public class AdminApiService {
 | 
			
		||||
    private final UtilsService utilsService;
 | 
			
		||||
    private final AppointmentService appointmentService;
 | 
			
		||||
    private final ReportService reportService;
 | 
			
		||||
    private final SectionCellService sectionCellService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    AdminApiService(
 | 
			
		||||
@@ -36,13 +36,15 @@ public class AdminApiService {
 | 
			
		||||
            AdministratorService administratorService,
 | 
			
		||||
            UtilsService utilsService,
 | 
			
		||||
            AppointmentService appointmentService,
 | 
			
		||||
            ReportService reportService) {
 | 
			
		||||
            ReportService reportService,
 | 
			
		||||
            SectionCellService sectionCellService) {
 | 
			
		||||
        this.projectService = projectService;
 | 
			
		||||
        this.userService = userService;
 | 
			
		||||
        this.administratorService = administratorService;
 | 
			
		||||
        this.utilsService = utilsService;
 | 
			
		||||
        this.appointmentService = appointmentService;
 | 
			
		||||
        this.reportService = reportService;
 | 
			
		||||
        this.sectionCellService = sectionCellService;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO: check if tests are sufficient - peer verification required
 | 
			
		||||
@@ -52,10 +54,36 @@ public class AdminApiService {
 | 
			
		||||
                        this.userService.getUserByEmail(mail).getIdUser()));
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO
 | 
			
		||||
    public Iterable<Appointment> getUpcomingAppointments(String 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
 | 
			
		||||
 
 | 
			
		||||
@@ -13,8 +13,14 @@ import org.springframework.http.HttpStatus;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.springframework.web.server.ResponseStatusException;
 | 
			
		||||
 | 
			
		||||
import java.io.FileNotFoundException;
 | 
			
		||||
import java.io.File;
 | 
			
		||||
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.format.DateTimeFormatter;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
@@ -95,7 +101,6 @@ public class SharedApiService {
 | 
			
		||||
        return project.getProjectAdministrator();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    // TODO
 | 
			
		||||
    public Iterable<Appointment> getAppointmentsByProjectId(long projectId, String mail) {
 | 
			
		||||
        if (!utilsService.isAllowedToCheckProject(mail, projectId)) {
 | 
			
		||||
            logger.warn(
 | 
			
		||||
@@ -123,9 +128,8 @@ public class SharedApiService {
 | 
			
		||||
        return appointments;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    //
 | 
			
		||||
    public void getPDFReport(long appointmentId, String mail)
 | 
			
		||||
            throws FileNotFoundException, DocumentException {
 | 
			
		||||
            throws DocumentException, URISyntaxException, IOException {
 | 
			
		||||
        long projectId =
 | 
			
		||||
                this.appointmentService
 | 
			
		||||
                        .getAppointmentById(appointmentId)
 | 
			
		||||
@@ -202,9 +206,24 @@ public class SharedApiService {
 | 
			
		||||
        document.add(new Paragraph("\n"));
 | 
			
		||||
 | 
			
		||||
        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) {
 | 
			
		||||
        long projectId =
 | 
			
		||||
                appointment
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user