From 2add1babf0b6f562c03d24f22eada57f2a96e3da Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 4 Nov 2025 12:14:03 +0530 Subject: [PATCH 1/5] Initialize template status='Processing' --- .../java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java index 05c024e8f9b9..c50344ad9d45 100644 --- a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java @@ -77,6 +77,7 @@ import com.cloud.user.Account; import com.cloud.user.AccountService; import com.cloud.utils.Pair; +import com.cloud.utils.StringUtils; import com.cloud.utils.db.Filter; import com.cloud.utils.db.SearchBuilder; import com.cloud.utils.db.SearchCriteria; @@ -162,9 +163,8 @@ protected TemplateJoinDaoImpl() { } private String getTemplateStatus(TemplateJoinVO template) { - String templateStatus = null; + String templateStatus = "Processing"; if (template.getDownloadState() != Status.DOWNLOADED) { - templateStatus = "Processing"; if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { if (template.getDownloadPercent() == 100) { templateStatus = "Installing Template"; @@ -173,10 +173,8 @@ private String getTemplateStatus(TemplateJoinVO template) { } } else if (template.getDownloadState() == Status.BYPASSED) { templateStatus = "Bypassed Secondary Storage"; - } else if (template.getErrorString() == null) { + } else if (StringUtils.isNotBlank(template.getErrorString())) { templateStatus = template.getTemplateState().toString(); - } else { - templateStatus = template.getErrorString().trim(); } } else if (template.getDownloadState() == Status.DOWNLOADED) { templateStatus = "Download Complete"; From ef29e99917dae6d0cc033c85e293a1e9d7146847 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 4 Nov 2025 13:32:30 +0530 Subject: [PATCH 2/5] remove else block and fix the error string --- .../java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java index c50344ad9d45..8857aef755d9 100644 --- a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java @@ -174,12 +174,10 @@ private String getTemplateStatus(TemplateJoinVO template) { } else if (template.getDownloadState() == Status.BYPASSED) { templateStatus = "Bypassed Secondary Storage"; } else if (StringUtils.isNotBlank(template.getErrorString())) { - templateStatus = template.getTemplateState().toString(); + templateStatus = template.getErrorString().trim(); } } else if (template.getDownloadState() == Status.DOWNLOADED) { templateStatus = "Download Complete"; - } else { - templateStatus = "Successfully Installed"; } return templateStatus; } From a4cca72d33c76b7e9623959cee6e6223e202aeee Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 4 Nov 2025 13:57:30 +0530 Subject: [PATCH 3/5] restructure if-else --- .../api/query/dao/TemplateJoinDaoImpl.java | 25 +++++++++---------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java index 8857aef755d9..e380330f2401 100644 --- a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java @@ -163,21 +163,20 @@ protected TemplateJoinDaoImpl() { } private String getTemplateStatus(TemplateJoinVO template) { + if (template.getDownloadState() == Status.DOWNLOADED) { + return "Download Complete"; + } String templateStatus = "Processing"; - if (template.getDownloadState() != Status.DOWNLOADED) { - if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { - if (template.getDownloadPercent() == 100) { - templateStatus = "Installing Template"; - } else { - templateStatus = template.getDownloadPercent() + "% Downloaded"; - } - } else if (template.getDownloadState() == Status.BYPASSED) { - templateStatus = "Bypassed Secondary Storage"; - } else if (StringUtils.isNotBlank(template.getErrorString())) { - templateStatus = template.getErrorString().trim(); + if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { + if (template.getDownloadPercent() == 100) { + templateStatus = "Installing Template"; + } else { + templateStatus = template.getDownloadPercent() + "% Downloaded"; } - } else if (template.getDownloadState() == Status.DOWNLOADED) { - templateStatus = "Download Complete"; + } else if (template.getDownloadState() == Status.BYPASSED) { + templateStatus = "Bypassed Secondary Storage"; + } else if (StringUtils.isNotBlank(template.getErrorString())) { + templateStatus = template.getErrorString().trim(); } return templateStatus; } From c67e7d02ed27e80dfe3eca9ba6f4ebb3cf6eff77 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Tue, 4 Nov 2025 16:44:58 +0530 Subject: [PATCH 4/5] standardize register ISO response --- .../api/query/dao/TemplateJoinDaoImpl.java | 26 +++++-------------- 1 file changed, 7 insertions(+), 19 deletions(-) diff --git a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java index e380330f2401..b0ee2e1426cf 100644 --- a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java @@ -164,12 +164,15 @@ protected TemplateJoinDaoImpl() { private String getTemplateStatus(TemplateJoinVO template) { if (template.getDownloadState() == Status.DOWNLOADED) { - return "Download Complete"; + return "Successfully Installed"; } String templateStatus = "Processing"; if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { if (template.getDownloadPercent() == 100) { templateStatus = "Installing Template"; + if (template.getFormat() == Storage.ImageFormat.ISO) { + templateStatus = "Installing ISO"; + } } else { templateStatus = template.getDownloadPercent() + "% Downloaded"; } @@ -506,24 +509,9 @@ public TemplateResponse newIsoResponse(TemplateJoinVO iso, ResponseView view) { // If the user is an admin, add the template download status if (isAdmin || caller.getId() == iso.getAccountId()) { // add download status - if (iso.getDownloadState() != Status.DOWNLOADED) { - String isoStatus = "Processing"; - if (iso.getDownloadState() == Status.DOWNLOADED) { - isoStatus = "Download Complete"; - } else if (iso.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { - if (iso.getDownloadPercent() == 100) { - isoStatus = "Installing ISO"; - } else { - isoStatus = iso.getDownloadPercent() + "% Downloaded"; - } - } else if (iso.getDownloadState() == Status.BYPASSED) { - isoStatus = "Bypassed Secondary Storage"; - } else { - isoStatus = iso.getErrorString(); - } - isoResponse.setStatus(isoStatus); - } else { - isoResponse.setStatus("Successfully Installed"); + String templateStatus = getTemplateStatus(iso); + if (templateStatus != null) { + isoResponse.setStatus(templateStatus); } isoResponse.setUrl(iso.getUrl()); List isosInStore = _templateStoreDao.listByTemplateNotBypassed(iso.getId()); From abed1212e51a24ae9f644021d50c6cfe2e2d29e3 Mon Sep 17 00:00:00 2001 From: Manoj Kumar Date: Wed, 5 Nov 2025 11:26:18 +0530 Subject: [PATCH 5/5] use enum instead of string --- .../api/query/dao/TemplateJoinDaoImpl.java | 48 ++++++++++++++----- 1 file changed, 37 insertions(+), 11 deletions(-) diff --git a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java index b0ee2e1426cf..3a2b309f1d11 100644 --- a/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java +++ b/server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java @@ -162,26 +162,52 @@ protected TemplateJoinDaoImpl() { _count = "select count(distinct temp_zone_pair) from template_view WHERE "; } + private enum TemplateStatus { + SUCCESSFULLY_INSTALLED("Successfully Installed"), + INSTALLING_TEMPLATE("Installing Template"), + INSTALLING_ISO("Installing ISO"), + BYPASSED_SECONDARY_STORAGE("Bypassed Secondary Storage"), + PROCESSING("Processing"), + DOWNLOADING("%d%% Downloaded"); + + private final String status; + TemplateStatus(String status) { + this.status = status; + } + public String getStatus() { + return status; + } + // For statuses that have dynamic details (e.g. "75% Downloaded"). + public String format(int percent) { + return String.format(status, percent); + } + } + private String getTemplateStatus(TemplateJoinVO template) { - if (template.getDownloadState() == Status.DOWNLOADED) { - return "Successfully Installed"; + if (template == null) { + return null; } - String templateStatus = "Processing"; - if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { + + TemplateStatus templateStatus; + if (template.getDownloadState() == Status.DOWNLOADED) { + templateStatus = TemplateStatus.SUCCESSFULLY_INSTALLED; + } else if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) { if (template.getDownloadPercent() == 100) { - templateStatus = "Installing Template"; - if (template.getFormat() == Storage.ImageFormat.ISO) { - templateStatus = "Installing ISO"; + templateStatus = TemplateStatus.INSTALLING_TEMPLATE; + if (Storage.ImageFormat.ISO == template.getFormat()) { + templateStatus = TemplateStatus.INSTALLING_ISO; } } else { - templateStatus = template.getDownloadPercent() + "% Downloaded"; + return TemplateStatus.DOWNLOADING.format(template.getDownloadPercent()); } } else if (template.getDownloadState() == Status.BYPASSED) { - templateStatus = "Bypassed Secondary Storage"; + templateStatus = TemplateStatus.BYPASSED_SECONDARY_STORAGE; } else if (StringUtils.isNotBlank(template.getErrorString())) { - templateStatus = template.getErrorString().trim(); + return template.getErrorString().trim(); + } else { + templateStatus = TemplateStatus.PROCESSING; } - return templateStatus; + return templateStatus.getStatus(); } @Override