Andrew Grant 4 mesi fa
parent
commit
fecb14505f

+ 1 - 0
src/main/java/scot/carricksoftware/grantswriter/constants/ViewConstants.java

@@ -19,5 +19,6 @@ public class ViewConstants {
     @SuppressWarnings("unused")
     public static final String FILES = "files";
     public static final String TEX = "tex";
+    public static final String HOME = "index";
 
 }

+ 3 - 0
src/main/java/scot/carricksoftware/grantswriter/controllers/FilesController.java

@@ -13,4 +13,7 @@ public interface FilesController {
 
     @SuppressWarnings("SameReturnValue")
     String getFiles(Model model);
+
+    @SuppressWarnings({"SameReturnValue", "unused"})
+    String gotFiles(Model model);
 }

+ 10 - 1
src/main/java/scot/carricksoftware/grantswriter/controllers/FilesControllerImpl.java

@@ -10,6 +10,7 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
 import scot.carricksoftware.grantswriter.constants.AttributeConstants;
 import scot.carricksoftware.grantswriter.constants.MappingConstants;
 import scot.carricksoftware.grantswriter.constants.ViewConstants;
@@ -31,11 +32,19 @@ public class FilesControllerImpl implements FilesController {
     @GetMapping(MappingConstants.FILES)
     @Override
     public String getFiles(Model model) {
-        logger.debug("FilesControllerImpl::getSelectionPage");
+        logger.debug("FilesControllerImpl::getFiles");
         if (isNull(writerFiles.getLatexFileName())) {
             writerFiles.init();
         }
         model.addAttribute(AttributeConstants.WRITER_FILES, writerFiles);
         return ViewConstants.FILES;
     }
+
+    @PostMapping(MappingConstants.FILES)
+    @Override
+    public String gotFiles(Model model) {
+        logger.debug("FilesControllerImpl::gotFiles");
+        model.addAttribute(AttributeConstants.WRITER_FILES, writerFiles);
+        return ViewConstants.HOME;
+    }
 }

+ 0 - 1
src/main/java/scot/carricksoftware/grantswriter/controllers/IndexControllerImpl.java

@@ -20,7 +20,6 @@ public class IndexControllerImpl implements IndexController {
     private static final Logger logger = LogManager.getLogger(IndexControllerImpl.class);
 
 
-
     @SuppressWarnings("SameReturnValue")
     @GetMapping(MappingConstants.BUILD_PDF)
     @Override

+ 6 - 1
src/main/java/scot/carricksoftware/grantswriter/controllers/TexController.java

@@ -7,8 +7,13 @@ package scot.carricksoftware.grantswriter.controllers;
 
 import org.springframework.ui.Model;
 
+import java.io.IOException;
+
 public interface TexController {
 
     @SuppressWarnings({"SameReturnValue", "unused"})
-    String start(Model model);
+    String screen(Model model);
+
+    @SuppressWarnings({"SameReturnValue", "unused"})
+    String start(Model model) throws IOException;
 }

+ 20 - 3
src/main/java/scot/carricksoftware/grantswriter/controllers/TexControllerImpl.java

@@ -10,10 +10,14 @@ import org.apache.logging.log4j.Logger;
 import org.springframework.stereotype.Controller;
 import org.springframework.ui.Model;
 import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
 import scot.carricksoftware.grantswriter.constants.AttributeConstants;
 import scot.carricksoftware.grantswriter.constants.MappingConstants;
 import scot.carricksoftware.grantswriter.constants.ViewConstants;
 import scot.carricksoftware.grantswriter.files.WriterFiles;
+import scot.carricksoftware.grantswriter.writer.TexWriter;
+
+import java.io.IOException;
 
 import static java.util.Objects.isNull;
 
@@ -24,19 +28,32 @@ public class TexControllerImpl implements TexController {
 
     private final WriterFiles writerFiles;
 
-    public TexControllerImpl(WriterFiles writerFiles) {
+    private final TexWriter texWriter;
+
+    public TexControllerImpl(WriterFiles writerFiles, TexWriter texWriter) {
         this.writerFiles = writerFiles;
+        this.texWriter = texWriter;
     }
 
     @GetMapping(MappingConstants.TEX)
     @Override
-    public String start(Model model) {
-        logger.debug("FilesControllerImpl::getSelectionPage");
+    public String screen(Model model) {
+        logger.debug("FilesControllerImpl::screen");
         if (isNull(writerFiles.getLatexFileName())) {
             writerFiles.init();
         }
         model.addAttribute(AttributeConstants.WRITER_FILES, writerFiles);
         writerFiles.setStatus("Running");
+
         return ViewConstants.TEX;
     }
+
+    @PostMapping(MappingConstants.TEX)
+    @Override
+    public String start(Model model) throws IOException {
+        logger.debug("FilesControllerImpl::start");
+        texWriter.write(writerFiles.getLatexFileName());
+
+        return ViewConstants.HOME;
+    }
 }

+ 2 - 2
src/main/java/scot/carricksoftware/grantswriter/files/WriterFilesImpl.java

@@ -54,8 +54,8 @@ public String getStatus() {
     @Override
     public void init() {
         logger.debug("WriterFiles::init");
-        setLatexFileName(System.getProperty("user.home") + File.separator + "carricksoftware" + File.separator + "grants.tex");
-        setPdfFileName(System.getProperty("user.home") + File.separator + "carricksoftware" + File.separator + "grants.pdf");
+        setLatexFileName(System.getProperty("user.home")  + File.separator + "grants.tex");
+        setPdfFileName(System.getProperty("user.home") + File.separator + "grants.pdf");
         setStatus("Ready");
     }
 }

+ 16 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/FileWriter.java

@@ -0,0 +1,16 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer;
+
+import java.io.IOException;
+
+public interface FileWriter {
+
+    void init(String texFilename) throws IOException;
+    void close() throws IOException;
+
+    void writeLine(String line);
+}

+ 45 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/FileWriterImpl.java

@@ -0,0 +1,45 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer;
+
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.springframework.stereotype.Component;
+
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.OutputStream;
+
+@Component
+public class FileWriterImpl implements FileWriter {
+
+    private static final Logger logger = LogManager.getLogger(FileWriterImpl.class);
+
+    private OutputStream os = null;
+
+    public void init(String fileName) throws IOException {
+        logger.debug("FileWriterImpl::init");
+        os = new FileOutputStream(fileName);
+    }
+
+    @Override
+    public void close() throws IOException {
+        logger.debug("FileWriterImpl::close");
+        os.close();
+    }
+
+    @Override
+    public void writeLine(String line) {
+        logger.debug("FileWriterImpl::writeLine");
+        try {
+            os.write(line.getBytes());
+        } catch (IOException e) {
+            throw new RuntimeException(e);
+        }
+    }
+}
+
+

+ 12 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/TexWriter.java

@@ -0,0 +1,12 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer;
+
+import java.io.IOException;
+
+public interface TexWriter {
+    void write(String filename) throws IOException;
+}

+ 27 - 0
src/main/java/scot/carricksoftware/grantswriter/writer/TexWriterImpl.java

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer;
+
+import org.springframework.stereotype.Component;
+
+import java.io.IOException;
+
+@Component
+public class TexWriterImpl implements TexWriter {
+
+    private final FileWriter fileWriter;
+
+    public TexWriterImpl(FileWriter fileWriter) {
+        this.fileWriter = fileWriter;
+    }
+
+    @Override
+    public void write(String filename) throws IOException {
+        fileWriter.init(filename);
+        fileWriter.writeLine("Archie");
+        fileWriter.close();
+    }
+}

+ 27 - 0
src/test/java/scot/carricksoftware/grantswriter/writer/FileWriterTest.java

@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2025.  Andrew Grant Carrick Software. All rights reserved
+ *
+ */
+
+package scot.carricksoftware.grantswriter.writer;
+
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+
+class FileWriterTest {
+
+    private FileWriter writer;
+
+    @BeforeEach
+    void setUp() {
+        writer = new FileWriterImpl();
+    }
+
+    @Test
+    public void dummyTest() {
+        assertNotNull(writer);
+    }
+}