package nuclibook.routes;

import nuclibook.constants.P;
import nuclibook.entity_utils.ExportUtils;
import nuclibook.entity_utils.PatientUtils;
import nuclibook.entity_utils.SecurityUtils;
import nuclibook.models.Patient;
import nuclibook.server.HtmlRenderer;
import spark.Request;
import spark.Response;

import java.util.List;

public class ExportRoute extends DefaultRoute {

    @Override
    public Object handle(Request request, Response response) throws Exception {
        // necessary prelim routine
        prepareToHandle();

        String[] fileSplit = request.params(":file:").split("\\.", 2);
        String table = fileSplit[0];

        String type = "";
        try {
            type = fileSplit[1];
        } catch (ArrayIndexOutOfBoundsException e) {
            return null;
        }

        String exportData = null;

        if (table.equals("patients")) {
            if (SecurityUtils.requirePermission(P.VIEW_PATIENT_LIST, response)) {
                if (type.equals("csv")) {
                    exportData = ExportUtils.exportCSV(Patient.class);
                }
            }
        }

        if (exportData != null) {
            response.header("Content-Disposition", "attachment");
        }

        return exportData;
    }
}

@@ -50,16 +50,60 @@
 ants.P;%0A
+import nuclibook.entity_utils.ActionLogger;%0A
 import n
@@ -974,20 +974,22 @@
 n(P.
-VIEW
+EXPORT
 _PATIENT
 _LIS
@@ -988,13 +988,9 @@
 IENT
-_LIST
+S
 , re
@@ -1113,16 +1113,16 @@
 class);%0A
-
         
@@ -1123,32 +1123,267 @@
               %7D%0A
+                ActionLogger.logAction(ActionLogger.EXPORT_PATIENTS, 0);%0A            %7D else %7B%0A                ActionLogger.logAction(ActionLogger.ATTEMPT_EXPORT_PATIENTS, 0, %22Failed as user does not have permissions for this action%22);%0A
             %7D%0A  

Add export log + permissions