Skip to content

Commit 988ebde

Browse files
STAND-123: Automatically trigger a search index rebuild after loading demo or empty database Platform (#80)
* move Demo and CIEL Database Packaging from Build-Time to Runtime * fixing test failures * adding support for mysqldump via MySQL on CI * removing the wildcard* * addressing linux and win install mysql * finding mysqldump on ci * revert to mysqldump * adding ci linux mysqldump checks * adding arg column-statistics=0 to pass the linux build * cleanup and updating README.md * adding cleaning step for linux * using bin/mariadb-dump instead of mysqldump * using bin/mariadb-dump instead of mysqldump * using scripts to solve mysqldump for windows * using java DatabaseDumper to solve mysqldump for windows * adding skip-extended-insert * added rebuild the search index on database * fixing windows ci failure * fixing windows ci failure * addressing the mvn package failure * re-correcting the update search index * STAND-123: Automatically trigger a search index rebuild after loading demo or empty database Platform
1 parent 06a02f3 commit 988ebde

File tree

2 files changed

+45
-2
lines changed

2 files changed

+45
-2
lines changed

src/main/java/org/openmrs/standalone/ApplicationController.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@
1616
import ch.vorburger.exec.ManagedProcessException;
1717
import org.apache.commons.io.FileUtils;
1818

19-
20-
2119
import java.io.BufferedInputStream;
2220
import java.io.BufferedOutputStream;
2321
import java.io.File;
@@ -151,6 +149,7 @@ public Object construct() {
151149
}
152150

153151
public void finished() {
152+
String resourceUrl = "http://localhost:" + userInterface.getTomcatPort() + "/" + contextName;
154153
Object value = workerThread.get();
155154

156155
userInterface.enableStart(value == null);
@@ -162,6 +161,9 @@ public void finished() {
162161
//else block with the await call such that we do not exit tomcat
163162
if (!commandLineMode) {
164163
StandaloneUtil.launchBrowser(userInterface.getTomcatPort(), contextName);
164+
if (applyDatabaseChange != DatabaseMode.USE_INITIALIZATION_WIZARD) {
165+
OpenmrsUtil.rebuildEntireSearchIndex(resourceUrl);
166+
}
165167
}
166168

167169
//if in non interactive mode, block such that tomcat does not exit

src/main/java/org/openmrs/standalone/OpenmrsUtil.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@
2424
import java.io.File;
2525
import java.io.Reader;
2626
import java.io.FileReader;
27+
import java.io.OutputStream;
28+
import java.net.HttpURLConnection;
29+
import java.net.URL;
30+
import java.nio.charset.StandardCharsets;
2731
import java.nio.file.Path;
2832
import java.nio.file.Paths;
2933
import java.sql.Connection;
3034
import java.sql.DriverManager;
3135
import java.util.Properties;
36+
import java.util.Base64;
3237

3338
public class OpenmrsUtil {
3439

@@ -258,4 +263,40 @@ public static String getTitle() {
258263
return "OpenMRS Platform " + PLATFORM_VERSION + " Standalone";
259264
}
260265

266+
public static void rebuildEntireSearchIndex(String resourceUrl) {
267+
final String SEARCH_INDEX_URL = resourceUrl + "/ws/rest/v1/searchindexupdate";
268+
try {
269+
URL url = new URL(SEARCH_INDEX_URL);
270+
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
271+
272+
// Basic Auth
273+
String username = "admin";
274+
String password = "test";
275+
String auth = username + ":" + password;
276+
String encodedAuth = Base64.getEncoder().encodeToString(auth.getBytes(StandardCharsets.UTF_8));
277+
String authHeader = "Basic " + encodedAuth;
278+
279+
conn.setRequestMethod("POST");
280+
conn.setRequestProperty("Content-Type", "application/json");
281+
conn.setRequestProperty("Authorization", authHeader);
282+
conn.setDoOutput(true);
283+
284+
// Prepare the request body
285+
String body = "{}";
286+
try (OutputStream os = conn.getOutputStream()) {
287+
byte[] input = body.getBytes("utf-8");
288+
os.write(input, 0, input.length);
289+
}
290+
291+
int responseCode = conn.getResponseCode();
292+
if (responseCode == HttpURLConnection.HTTP_NO_CONTENT) {
293+
System.out.println("✅ Search index rebuild triggered successfully on startup.");
294+
} else {
295+
System.err.println("❌ Failed to trigger rebuild. Status: " + responseCode);
296+
}
297+
conn.disconnect();
298+
} catch (Exception e) {
299+
e.printStackTrace();
300+
}
301+
}
261302
}

0 commit comments

Comments
 (0)