From 1db7ddb39b5297396122d70433a2e09400efaf4d Mon Sep 17 00:00:00 2001 From: Wikum Weerakutti Date: Sat, 3 Aug 2024 16:40:38 +0530 Subject: [PATCH] O3-3438: Update SPA module to load the bundled frontend --- .../org/openmrs/module/spa/SpaActivator.java | 22 ++++++++++++++++++- .../org/openmrs/module/spa/SpaConstants.java | 2 ++ .../module/spa/SpaDirectoryResolver.java | 2 ++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java b/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java index f806403..25bfde6 100644 --- a/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java +++ b/omod/src/main/java/org/openmrs/module/spa/SpaActivator.java @@ -14,16 +14,35 @@ import org.openmrs.api.context.Context; import org.openmrs.module.BaseModuleActivator; +import javax.servlet.ServletContext; +import java.nio.file.Path; +import java.nio.file.Paths; + +import static org.openmrs.module.spa.SpaConstants.BUNDLED_FRONTEND_DIRECTORY; +import static org.openmrs.module.spa.SpaConstants.GP_LOCAL_DIRECTORY; + @Slf4j public class SpaActivator extends BaseModuleActivator { // here so we can register the listener on load and de-register it when stopped private GlobalPropertyListener spaDirectoryResolver = null; + public static ServletContext servletContext; + @Override public void started() { log.info("SPA module started"); - spaDirectoryResolver = new SpaDirectoryResolver(); + } + + @Override + public void contextRefreshed() { + SpaDirectoryResolver spaDirectoryResolver = new SpaDirectoryResolver(); + if (!Paths.get(SpaDirectoryResolver.getSpaDirectory(), "index.html").toFile().exists()) { + Path bundledFrontend = Paths.get(servletContext.getRealPath("/"), "WEB-INF", BUNDLED_FRONTEND_DIRECTORY); + if (bundledFrontend.resolve("index.html").toFile().exists()) { + Context.getAdministrationService().setGlobalProperty(GP_LOCAL_DIRECTORY, bundledFrontend.toAbsolutePath().toString()); + } + } Context.getAdministrationService().addGlobalPropertyListener(spaDirectoryResolver); } @@ -33,4 +52,5 @@ public void stopped() { spaDirectoryResolver = null; log.info("SPA module stopped"); } + } \ No newline at end of file diff --git a/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java b/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java index 795d130..8ea11e5 100644 --- a/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java +++ b/omod/src/main/java/org/openmrs/module/spa/SpaConstants.java @@ -19,6 +19,8 @@ private SpaConstants() {} public static final String DEFAULT_FRONTEND_DIRECTORY = "frontend"; + public static final String BUNDLED_FRONTEND_DIRECTORY = "bundledFrontend"; + public static final String GP_LOCAL_DIRECTORY = "spa.local.directory"; } diff --git a/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java b/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java index 6a3ab3c..24b10cb 100644 --- a/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java +++ b/omod/src/main/java/org/openmrs/module/spa/SpaDirectoryResolver.java @@ -84,6 +84,8 @@ private static void resolveDirectory(String spaDirectory) { SpaDirectoryResolver.spaDirectory.set(Paths.get(OpenmrsUtil.getApplicationDataDirectory(), DEFAULT_FRONTEND_DIRECTORY).normalize().toAbsolutePath().toString()); } SpaDirectoryResolver.spaDirectory.set(spaDirectoryPath.normalize().toString()); + } else { + SpaDirectoryResolver.spaDirectory.set(spaDirectory); } } }