Skip to content

Commit

Permalink
HAL-1824: add confirmation check for deployment replacing
Browse files Browse the repository at this point in the history
  • Loading branch information
michpetrov committed Feb 23, 2023
1 parent e6ebac7 commit c92683d
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 2 deletions.
Expand Up @@ -473,17 +473,32 @@ protected void uploadDeployment() {
.addStep(UPLOAD, new UploadDeploymentStep(resources))
.addStep(NAMES, new NamesStep(environment, metadata, resources))

.onBack((context, currentState) -> currentState == NAMES ? UPLOAD : null)
.onBack((context, currentState) -> currentState == NAMES ? (context.name != null ? NAMES : UPLOAD) : null)
.onNext((context, currentState) -> currentState == UPLOAD ? NAMES : null)

.stayOpenAfterFinish()
.onFinish((wzd, wzdContext) -> {
String name = wzdContext.name;
wzd.showProgress(columnProps.deploymentProgressTitle, columnProps.getDeploymentProgressText.apply(name));

Task<FlowContext> confirmReplacement = context -> new Promise<>((resolve, reject) -> {
int result = context.peek();

if (result == 404) {
resolve.onInvoke(context);
return;
}

wzd.showWarning(columnProps.replaceDeploymentTitle,
resources.messages().deploymentReplaceConfirmation(name), resources.constants().replace(),
__ -> resolve.onInvoke(context), false);
});

List<Task<FlowContext>> tasks = new ArrayList<>();
tasks.add(new DeploymentTasks.CheckDeployment(dispatcher, name));
tasks.add(confirmReplacement);
tasks.add(new DeploymentTasks.UploadOrReplace(environment, dispatcher, name, wzdContext.runtimeName,
wzdContext.file, wzdContext.enabled));
wzdContext.file, wzdContext.enabled));
if (columnProps.columnType == ColumnType.SERVER_GROUP) {
tasks.add(getServerGroupDeploymentTask(name, wzdContext.runtimeName));
}
Expand Down
Expand Up @@ -24,6 +24,7 @@

import javax.inject.Provider;

import org.jboss.hal.ballroom.dialog.Dialog;
import org.jboss.hal.config.Environment;
import org.jboss.hal.core.deployment.Content;
import org.jboss.hal.core.deployment.Deployment;
Expand All @@ -47,6 +48,7 @@
import org.slf4j.LoggerFactory;

import com.google.common.collect.Lists;
import com.google.gwt.safehtml.shared.SafeHtml;
import com.google.web.bindery.event.shared.EventBus;

import elemental2.dom.File;
Expand All @@ -57,6 +59,7 @@
import static java.util.stream.Collectors.toList;
import static java.util.stream.Collectors.toMap;
import static java.util.stream.Collectors.toSet;
import static org.jboss.elemento.Elements.p;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ADD;
import static org.jboss.hal.dmr.ModelDescriptionConstants.ADDRESS;
import static org.jboss.hal.dmr.ModelDescriptionConstants.CHILD_TYPE;
Expand Down Expand Up @@ -100,6 +103,8 @@ static <T> void upload(FinderColumn<T> column, Environment environment, Dispatch
String filename = files.item(i).name;
builder.append(filename).append(" ");
tasks.add(new CheckDeployment(dispatcher, filename));
tasks.add(new ConfirmReplacement(resources.constants().replaceDeployment(), resources.constants().replace(),
resources.messages().deploymentReplaceConfirmation(filename)));
tasks.add(new UploadOrReplace(environment, dispatcher, filename, filename, files.item(i), !hasServerGroup));
if (hasServerGroup) {
tasks.add(new AddServerGroupDeployment(environment, dispatcher, filename, filename, serverGroup));
Expand Down Expand Up @@ -362,9 +367,56 @@ public Promise<FlowContext> apply(final FlowContext context) {
}
}

/**
* Check if {@code 404} is on the stack, if not asks the user for confirmation, if user confirms {@code 200} is kept on the
* stack otherwise {@code 403} is pushed on the stack.
*/
static final class ConfirmReplacement implements Task<FlowContext> {

private final String title;
private final String okButton;
private final SafeHtml message;

ConfirmReplacement(String title, String okButton, SafeHtml message) {
this.title = title;
this.okButton = okButton;
this.message = message;
}

@Override
public Promise<FlowContext> apply(FlowContext context) {
Integer result = context.peek();

if (result == 404) {
return Promise.resolve(context);
}

return new Promise<>((resolve, reject) -> {

Dialog confirmDialog = new Dialog.Builder(title)
.primary(okButton, () -> {
resolve.onInvoke(context);
return true;
})
.secondary(() -> {
context.pop();
context.push(403);
resolve.onInvoke(context);
return true;
})
.size(Dialog.Size.MEDIUM)
.add(p().innerHtml(message).element())
.build();

confirmDialog.show();
});
}
}

/**
* Creates a new deployment or replaces an existing deployment. The function looks for a status code in the context. If no
* status context or {@code 404} is found, a new deployment is created, if {@code 200} is found the deployment is replaced.
* If {@code 403} is found the task does nothing.
* <p>
* The function puts an {@link UploadStatistics} under the key {@link DeploymentTasks#UPLOAD_STATISTICS} into the context.
*/
Expand All @@ -389,6 +441,7 @@ static final class UploadOrReplace implements Task<FlowContext> {

@Override
public Promise<FlowContext> apply(final FlowContext context) {
boolean skip = false;
boolean replace;
Operation.Builder builder;

Expand All @@ -397,6 +450,16 @@ public Promise<FlowContext> apply(final FlowContext context) {
} else {
Integer status = context.peek();
replace = status == 200;
skip = status == 403;
}

if (skip) {
UploadStatistics statistics = context.get(UPLOAD_STATISTICS);
if (statistics == null) {
statistics = new UploadStatistics(environment);
context.set(UPLOAD_STATISTICS, statistics);
}
return Promise.resolve(context);
}

if (replace) {
Expand Down
6 changes: 6 additions & 0 deletions app/src/web/style/wizard.less
Expand Up @@ -27,6 +27,12 @@
line-height: (@font-size-base * 5.6);
}

.wizard-pf-warning-icon {
color: @color-pf-orange-300;
font-size: (@font-size-base * 5.6);
line-height: (@font-size-base * 5.6);
}

.wizard-hal-error-text {
text-align: left;
white-space: pre;
Expand Down
31 changes: 31 additions & 0 deletions ballroom/src/main/java/org/jboss/hal/ballroom/wizard/Wizard.java
Expand Up @@ -279,6 +279,37 @@ public void showSuccess(String title, SafeHtml text, String successButton, Succe
finishCanClose = lastStep;
}

public void showWarning(String title, SafeHtml text, String okButton, SuccessAction<C> okAction, boolean lastStep) {
blankSlate.classList.remove(wizardPfProcess);
blankSlate.classList.add(wizardPfComplete);
Elements.removeChildrenFrom(blankSlate);

blankSlate.appendChild(div().css(wizardPfWarningIcon)
.add(span().css(glyphicon("exclamation-sign"))).element());
blankSlate.appendChild(h(3).css(blankSlatePfMainAction).textContent(title).element());
blankSlate.appendChild(p().css(blankSlatePfSecondaryAction).innerHtml(text).element());

if (okButton != null && okAction != null) {
blankSlate.appendChild(button().css(btn, btnLg, btnPrimary)
.textContent(okButton)
.on(click, event -> okAction.execute(context))
.element());
}

stepElements.values().forEach(element -> Elements.setVisible(element, false));
Elements.setVisible(blankSlate, true);

cancelButton.disabled = lastStep;
backButton.disabled = lastStep;
nextButton.disabled = false;
if (lastStep) {
nextText.textContent = CONSTANTS.close();
Elements.setVisible(nextIcon, false);
}
nextButton.onclick = null;
finishCanClose = lastStep;
}

public void showError(String title, SafeHtml text) {
showError(title, text, null, true);
}
Expand Down
1 change: 1 addition & 0 deletions resources/src/main/java/org/jboss/hal/resources/CSS.java
Expand Up @@ -477,6 +477,7 @@ public interface CSS {
String wizardPfSteps = "wizard-pf-steps";
String wizardPfStepsIndicator = "wizard-pf-steps-indicator";
String wizardPfSuccessIcon = "wizard-pf-success-icon";
String wizardPfWarningIcon = "wizard-pf-warning-icon";
String wrap = "wrap";

String DASH = "-";
Expand Down
2 changes: 2 additions & 0 deletions resources/src/main/java/org/jboss/hal/resources/Messages.java
Expand Up @@ -204,6 +204,8 @@ public interface Messages extends com.google.gwt.i18n.client.Messages {

SafeHtml deploymentReadError(String deployment);

SafeHtml deploymentReplaceConfirmation(String name);

SafeHtml deploymentReplaced(@PluralCount int count);

SafeHtml deploymentStopped(String name);
Expand Down
Expand Up @@ -140,6 +140,7 @@ deploymentOpFailed=<strong>{0}</strong> deployments couldn&#39;t be processed.
deploymentOpFailed[\=1]=The deployment couldn&#39;t be processed.
deploymentPreview=This is an archived deployment. Use the button below to download a copy of this deployment. In order to add or modify files in the deployment you need to explode it.
deploymentReadError=Unable to read information for deployment <strong>{0}</strong>.
deploymentReplaceConfirmation=Deployment <strong>{0}</strong> already exists. Do you want to replace it?
deploymentReplaced=<strong>{0}</strong> deployments have been replaced.
deploymentReplaced[\=1]=The deployment has been replaced.
deploymentStandaloneColumnFilterDescription=Filter by: name or deployment status
Expand Down

0 comments on commit c92683d

Please sign in to comment.