From d4c545828f0d3f1d8c13654f4b409e4811a375c5 Mon Sep 17 00:00:00 2001
From: mherman22 <hermanmuhereza22@gmail.com>
Date: Wed, 8 Jan 2025 19:21:56 +0300
Subject: [PATCH 1/3] SDK-368: SDK should prompt for database uri again if an
 invalid uri was entered

---
 .../java/org/openmrs/maven/plugins/Setup.java | 26 ++++++-----
 .../maven/plugins/utility/DBConnector.java    |  2 +
 .../maven/plugins/utility/DefaultWizard.java  | 45 +++++++++++++++++++
 .../openmrs/maven/plugins/utility/Wizard.java |  4 +-
 4 files changed, 64 insertions(+), 13 deletions(-)

diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
index 133afbbe..1ad64b99 100644
--- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
+++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
@@ -428,34 +428,36 @@ private void setupDatabaseForServer(Server server) throws MojoExecutionException
 		}
 
 		if (server.isMySqlDb() || server.isPostgreSqlDb()) {
-			String uri = getUriWithoutDb(server);
 			boolean connectionEstablished = false;
 			int maxAttempts = 3;
 			int attempts = 0;
 
 			while (!connectionEstablished && attempts < maxAttempts) {
 				attempts++;
+				String uri = getUriWithoutDb(server);
 				try (DBConnector connector = new DBConnector(uri, server.getDbUser(), server.getDbPassword(), server.getDbName())) {
 					connector.checkAndCreate(server);
 					wizard.showMessage("Connected to the database.");
 					connectionEstablished = true;
 				}
 				catch (SQLException e) {
-					if (e.getMessage().contains("Invalid database credentials")) {
+					String message = e.getMessage();
+					wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, message));
+					if (message.contains("Invalid database credentials")) {
 						if (attempts == maxAttempts) {
-							throw new MojoExecutionException(
-									String.format("Failed to connect to database after %d attempts. Please verify your credentials and try again.", maxAttempts),
-									e
-							);
+							throw new MojoExecutionException(String.format("Failed to connect to database after %d attempts. " +
+									"Please verify your credentials and try again.", maxAttempts), e);
 						}
 
-						wizard.showMessage(String.format("Database connection failed (attempt %d of %d): %s", attempts, maxAttempts, e.getMessage()));
-						String newUser = wizard.promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser, "dbUser", "root");
-						String newPassword = wizard.promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword, "dbPassword", "");
-
-						server.setDbUser(newUser);
-						server.setDbPassword(newPassword);
+						wizard.promptForDbCredentialsAgain(server, dbUser, dbPassword);
+						continue;
+					} else if (message.contains("Incorrect Database Uri")) {
+						if (attempts == maxAttempts) {
+							throw new MojoExecutionException(String.format("Failed to connect to database after %d attempts. " +
+									"Please verify your database uri.", maxAttempts), e);
+						}
 
+						wizard.promptForNewUriAndCredentials(server, dbUser, dbPassword, dbUri);
 						continue;
 					}
 					throw new MojoExecutionException("Failed to connect to the specified database " + server.getDbUri(), e);
diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java
index 274ad1c9..5280bfb0 100644
--- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java
+++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java
@@ -27,6 +27,8 @@ public DBConnector(String url, String user, String pass, String dbName) throws S
 			} catch (SQLException e2) {
 				if (e2.getMessage().contains("Access denied")) {
 					throw new SQLException("Invalid database credentials. Please check your username and password.", e2);
+				} else if (e2.getMessage().contains("No suitable driver found for")) {
+					throw new SQLException("Incorrect Database Uri. Please provide a correct database uri.", e2);
 				}
 				throw e2;
 			}
diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java
index 3a43b4a2..eb011650 100644
--- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java
+++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java
@@ -950,6 +950,51 @@ public void promptForDbCredentialsIfMissing(Server server) throws MojoExecutionE
 		server.setDbPassword(dbPassword);
 	}
 
+	@Override
+	public void promptForDbCredentialsAgain(Server server, String dbUser, String dbPassword) throws MojoExecutionException {
+		String newUser = promptForValueIfMissingWithDefault("Please specify correct database username (-D%s)", dbUser,
+				"dbUser", "root");
+
+		String newPassword = promptForPasswordIfMissingWithDefault("Please specify correct database password (-D%s)", dbPassword,
+				"dbPassword", "");
+
+		server.setDbUser(newUser);
+		server.setDbPassword(newPassword);
+	}
+
+	@Override
+	public void promptForNewUriAndCredentials(Server server, String dbUser, String dbPassword, String dbUri) throws MojoExecutionException {
+		showMessage("Prompting for new database URI and credentials...");
+		String uriTemplate;
+		if (server.isMySqlDb()) {
+			uriTemplate = SDKConstants.URI_MYSQL;
+		} else if (server.isPostgreSqlDb()) {
+			uriTemplate = SDKConstants.URI_POSTGRESQL;
+		} else {
+			return;
+		}
+
+		String newUri = promptForValueIfMissingWithDefault(
+				"The distribution requires a " +
+						(server.isMySqlDb() ? "MySQL" : "PostgreSQL") +
+						" database. Please specify a valid database uri (-D%s)",
+				dbUri, "dbUri", uriTemplate);
+
+		if (server.isMySqlDb()) {
+			newUri = addMySQLParamsIfMissing(newUri);
+		} else if (server.isPostgreSqlDb()) {
+			newUri = addPostgreSQLParamsIfMissing(newUri);
+		}
+
+		newUri = newUri.replace(DBNAME_URL_VARIABLE, server.getServerId());
+		server.setDbUri(newUri);
+
+		String newUser = promptForValueIfMissingWithDefault("Please enter the database username (-D%s):", dbUser, "dbUser", "root");
+		String newPassword = promptForPasswordIfMissingWithDefault("Please enter the database password (-D%s):", dbPassword, "dbPassword", "");
+		server.setDbUser(newUser);
+		server.setDbPassword(newPassword);
+	}
+
 	/**
 	 * Get servers with recently used first
 	 */
diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java
index 7f3609b8..9d0a81d7 100644
--- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java
+++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/Wizard.java
@@ -56,6 +56,8 @@ public interface Wizard {
 
     String promptForExistingServerIdIfMissing(String serverId) throws MojoExecutionException;
 
+    void promptForNewUriAndCredentials(Server server, String dbUser, String dbPassword, String dbUri) throws MojoExecutionException;
+
     List<String> getListOfServers() throws MojoExecutionException;
 
     void showJdkErrorMessage(String jdk, String platform, String recommendedJdk, String pathToProps);
@@ -64,5 +66,5 @@ public interface Wizard {
 
     void setAnswers(ArrayDeque<String> batchAnswers);
 
-    String promptForPasswordIfMissingWithDefault(String s, String dbPassword, String dbPassword1, String s1) throws MojoExecutionException;
+    void promptForDbCredentialsAgain(Server server, String dbUser, String dbPassword) throws MojoExecutionException;
 }

From c0748f285d0ede69c1f4255c59f4ead8d324c297 Mon Sep 17 00:00:00 2001
From: mherman22 <hermanmuhereza22@gmail.com>
Date: Wed, 15 Jan 2025 02:09:03 +0300
Subject: [PATCH 2/3] more

---
 .../java/org/openmrs/maven/plugins/utility/DBConnector.java     | 2 +-
 .../java/org/openmrs/maven/plugins/utility/DefaultWizard.java   | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java
index 5280bfb0..cb9d3fc1 100644
--- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java
+++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/utility/DBConnector.java
@@ -27,7 +27,7 @@ public DBConnector(String url, String user, String pass, String dbName) throws S
 			} catch (SQLException e2) {
 				if (e2.getMessage().contains("Access denied")) {
 					throw new SQLException("Invalid database credentials. Please check your username and password.", e2);
-				} else if (e2.getMessage().contains("No suitable driver found for")) {
+				} else if (e2.getMessage().contains("No suitable driver found for") || e2.getMessage().contains("Communications link failure")) {
 					throw new SQLException("Incorrect Database Uri. Please provide a correct database uri.", e2);
 				}
 				throw e2;
diff --git a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java
index eb011650..9eb49c3b 100644
--- a/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java
+++ b/sdk-commons/src/main/java/org/openmrs/maven/plugins/utility/DefaultWizard.java
@@ -1036,7 +1036,7 @@ private URIBuilder addDefaultParamsIfMissing(String dbUri) throws MojoExecutionE
 			uri = new URIBuilder(noJdbc);
 		}
 		catch (URISyntaxException e) {
-			throw new MojoExecutionException("Database URI \"" + noJdbc + "\" is not a valid URI " + e.getMessage(),  e);
+			throw new MojoExecutionException("Database URI \"" + noJdbc + "\" is not a valid URI. " + e.getMessage(),  e);
 		}
 		uri.setParameter("autoReconnect", "true");
 		uri.setParameter("useUnicode", "true");

From 1751f0f9b9af748d4989be29509690487b6a1fde Mon Sep 17 00:00:00 2001
From: mherman22 <hermanmuhereza22@gmail.com>
Date: Thu, 16 Jan 2025 22:10:22 +0300
Subject: [PATCH 3/3] add serverport parameter

---
 .../java/org/openmrs/maven/plugins/Setup.java | 29 +++++++++++--------
 1 file changed, 17 insertions(+), 12 deletions(-)

diff --git a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
index 1ad64b99..04297d90 100644
--- a/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
+++ b/maven-plugin/src/main/java/org/openmrs/maven/plugins/Setup.java
@@ -139,6 +139,9 @@ public class Setup extends AbstractServerTask {
 	@Parameter(property = "debug")
 	private String debug;
 
+	@Parameter(property = "serverPort")
+	private String serverPort;
+
 	@Parameter(defaultValue = "false", property = "run")
 	private boolean run;
 
@@ -367,18 +370,20 @@ private void wipeDatabase(Server server) throws MojoExecutionException {
 	}
 
 	private void setServerPort(Server server) throws MojoExecutionException {
-		String message = "What port would you like your server to use?";
-		String port = wizard.promptForValueIfMissingWithDefault(
-				message,
-				server.getParam("tomcat.port"),
-				"port number",
-				String.valueOf(Setup.DEFAULT_PORT));
-		if (!StringUtils.isNumeric(port) || !this.serverHelper.isPort(Integer.parseInt(port))) {
-			wizard.showMessage("Port must be numeric and less or equal 65535.");
-			this.setServerPort(server);
-			return;
-		}
-		server.setPort(port);
+		if (StringUtils.isBlank(serverPort)) {
+			String message = "What port (-D%s) would you like your server to use?";
+			serverPort = wizard.promptForValueIfMissingWithDefault(
+					message,
+					server.getParam("tomcat.port"),
+					"serverPort",
+					String.valueOf(Setup.DEFAULT_PORT));
+			if (!StringUtils.isNumeric(serverPort) || !this.serverHelper.isPort(Integer.parseInt(serverPort))) {
+				wizard.showMessage("Port must be numeric and less or equal 65535.");
+				this.setServerPort(server);
+				return;
+			}
+			server.setPort(serverPort);
+		}
 	}
 
 	private void setDebugPort(Server server) throws MojoExecutionException {