Skip to content

Commit abed121

Browse files
committed
use enum instead of string
1 parent c67e7d0 commit abed121

File tree

1 file changed

+37
-11
lines changed

1 file changed

+37
-11
lines changed

server/src/main/java/com/cloud/api/query/dao/TemplateJoinDaoImpl.java

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -162,26 +162,52 @@ protected TemplateJoinDaoImpl() {
162162
_count = "select count(distinct temp_zone_pair) from template_view WHERE ";
163163
}
164164

165+
private enum TemplateStatus {
166+
SUCCESSFULLY_INSTALLED("Successfully Installed"),
167+
INSTALLING_TEMPLATE("Installing Template"),
168+
INSTALLING_ISO("Installing ISO"),
169+
BYPASSED_SECONDARY_STORAGE("Bypassed Secondary Storage"),
170+
PROCESSING("Processing"),
171+
DOWNLOADING("%d%% Downloaded");
172+
173+
private final String status;
174+
TemplateStatus(String status) {
175+
this.status = status;
176+
}
177+
public String getStatus() {
178+
return status;
179+
}
180+
// For statuses that have dynamic details (e.g. "75% Downloaded").
181+
public String format(int percent) {
182+
return String.format(status, percent);
183+
}
184+
}
185+
165186
private String getTemplateStatus(TemplateJoinVO template) {
166-
if (template.getDownloadState() == Status.DOWNLOADED) {
167-
return "Successfully Installed";
187+
if (template == null) {
188+
return null;
168189
}
169-
String templateStatus = "Processing";
170-
if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
190+
191+
TemplateStatus templateStatus;
192+
if (template.getDownloadState() == Status.DOWNLOADED) {
193+
templateStatus = TemplateStatus.SUCCESSFULLY_INSTALLED;
194+
} else if (template.getDownloadState() == Status.DOWNLOAD_IN_PROGRESS) {
171195
if (template.getDownloadPercent() == 100) {
172-
templateStatus = "Installing Template";
173-
if (template.getFormat() == Storage.ImageFormat.ISO) {
174-
templateStatus = "Installing ISO";
196+
templateStatus = TemplateStatus.INSTALLING_TEMPLATE;
197+
if (Storage.ImageFormat.ISO == template.getFormat()) {
198+
templateStatus = TemplateStatus.INSTALLING_ISO;
175199
}
176200
} else {
177-
templateStatus = template.getDownloadPercent() + "% Downloaded";
201+
return TemplateStatus.DOWNLOADING.format(template.getDownloadPercent());
178202
}
179203
} else if (template.getDownloadState() == Status.BYPASSED) {
180-
templateStatus = "Bypassed Secondary Storage";
204+
templateStatus = TemplateStatus.BYPASSED_SECONDARY_STORAGE;
181205
} else if (StringUtils.isNotBlank(template.getErrorString())) {
182-
templateStatus = template.getErrorString().trim();
206+
return template.getErrorString().trim();
207+
} else {
208+
templateStatus = TemplateStatus.PROCESSING;
183209
}
184-
return templateStatus;
210+
return templateStatus.getStatus();
185211
}
186212

187213
@Override

0 commit comments

Comments
 (0)