Skip to content

Commit

Permalink
Execute operations
Browse files Browse the repository at this point in the history
  • Loading branch information
hpehl committed Nov 18, 2024
1 parent ef65406 commit 8d7998b
Show file tree
Hide file tree
Showing 34 changed files with 652 additions and 265 deletions.
45 changes: 8 additions & 37 deletions core/src/main/java/org/jboss/hal/core/CrudOperations.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@
import static org.jboss.hal.resources.Dataset.crudMessageName;
import static org.jboss.hal.resources.Dataset.crudMessageType;
import static org.patternfly.component.alert.AlertDescription.alertDescription;
import static org.patternfly.component.button.Button.button;
import static org.patternfly.component.modal.Modal.modal;
import static org.patternfly.component.modal.ModalBody.modalBody;
import static org.patternfly.component.modal.ModalFooter.modalFooter;
import static org.patternfly.core.Tuple.tuple;
import static org.patternfly.style.Classes.util;
import static org.patternfly.style.Size.sm;

/**
* The CrudOperations class provides methods for performing create, update, and delete operations on resources represented by
Expand Down Expand Up @@ -109,37 +103,14 @@ public Promise<CompositeResult> update(AddressTemplate template, List<Operation>
// ------------------------------------------------------ delete

public Promise<ModelNode> delete(AddressTemplate template) {
return new Promise<>((resolve, reject) -> {
Tuple<String, String> typeName = typeName(template);
modal().size(sm)
.addHeader("Delete resource")
.addBody(modalBody()
.add("Do you really want to delete ")
.add(span().css(util("font-weight-bold")).textContent(typeName.value))
.add("?"))
.addFooter(modalFooter()
.addButton(button("Delete").primary(), (__, modal) -> {
modal.close();
Operation operation = new Operation.Builder(template.resolve(statementContext), REMOVE).build();
dispatcher.execute(operation)
.then(result -> {
success("Resource deleted",
description(typeName).add(" has been successfully deleted."));
resolve.onInvoke(result);
return null;
})
.catch_(error -> {
reject.onInvoke(error);
return null;
});
})
.addButton(button("Cancel").link(), (__, modal) -> {
modal.close();
resolve.onInvoke(new ModelNode());
}))
.appendToBody()
.open();
});
Tuple<String, String> typeName = typeName(template);
Operation operation = new Operation.Builder(template.resolve(statementContext), REMOVE).build();
return dispatcher.execute(operation)
.then(result -> {
success("Resource deleted",
description(typeName).add(" has been successfully deleted."));
return Promise.resolve(result);
});
}

// ------------------------------------------------------ internal
Expand Down
15 changes: 15 additions & 0 deletions core/src/main/java/org/jboss/hal/core/Notifications.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*
* Copyright 2024 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.hal.core;

import org.patternfly.component.alert.Alert;
Expand Down
10 changes: 10 additions & 0 deletions meta/src/main/java/org/jboss/hal/meta/AddressTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,16 @@ public AddressTemplate parent() {
}
}

public AddressTemplate anonymous() {
if (isEmpty()) {
return AddressTemplate.of("");
} else if (!"*".equals(last().value)) {
return parent().append(last().key, "*");
} else {
return AddressTemplate.of(this);
}
}

// ------------------------------------------------------ properties

/** @return the first segment or {@link Segment#EMPTY} if this address template is empty. */
Expand Down
6 changes: 5 additions & 1 deletion op/console/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -143,16 +143,20 @@
<compilationLevel>${j2cl.compilationLevel}</compilationLevel>
<enableSourcemaps>${j2cl.sourcemaps}</enableSourcemaps>
<initialScriptFilename>console.js</initialScriptFilename>
<!-- Keep in sync with
<!-- Keep defines for <environment.*/> in sync with
environment/src/main/java/org/jboss/hal/env/environment.js -->
<defines>
<goog.LOCALE>en</goog.LOCALE>
<environment.id>${project.artifactId}</environment.id>
<environment.name>${project.name}</environment.name>
<environment.version>${project.version}</environment.version>
<environment.base>${environment.base}</environment.base>
<environment.build>${environment.build}</environment.build>
<environment.stability>${environment.stability}</environment.stability>
</defines>
<translationsFile>
<auto>true</auto>
</translationsFile>
<webappDirectory>${project.build.directory}/console</webappDirectory>
</configuration>
<executions>
Expand Down
1 change: 0 additions & 1 deletion op/console/src/web/resource.css
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
flex-wrap: wrap;
align-items: center;
align-self: baseline;
gap: var(--pf-v5-global--spacer--xs);
}

.hal-c-resource__stability-level {
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@
<profile>
<id>prod</id>
<properties>
<j2cl.compilationLevel>ADVANCED_OPTIMIZATIONS</j2cl.compilationLevel>
<j2cl.compilationLevel>ADVANCED</j2cl.compilationLevel>
<j2cl.incremental>false</j2cl.incremental>
<j2cl.sourcemaps>false</j2cl.sourcemaps>
<npm.script>prod</npm.script>
Expand Down
12 changes: 12 additions & 0 deletions resources/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,16 @@
<packaging>jar</packaging>
<name>HAL Resources</name>
<description>HAL constants and resources</description>

<dependencies>
<dependency>
<groupId>org.kie.j2cl.tools.processors</groupId>
<artifactId>annotations</artifactId>
</dependency>
<dependency>
<groupId>org.kie.j2cl.tools.processors</groupId>
<artifactId>processors</artifactId>
<scope>provided</scope>
</dependency>
</dependencies>
</project>
26 changes: 26 additions & 0 deletions resources/src/main/java/org/jboss/hal/resources/L10nBundle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Copyright 2024 Red Hat
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.jboss.hal.resources;

import org.kie.j2cl.tools.processors.annotations.TranslationBundle;
import org.kie.j2cl.tools.processors.annotations.TranslationKey;

@TranslationBundle
public interface L10nBundle {

@TranslationKey(defaultValue = "Resource added")
String crudCreateSuccess();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#
# Copyright 2024 Red Hat
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

crudCreateSuccess=Resource added
29 changes: 29 additions & 0 deletions ui/src/main/java/org/jboss/hal/ui/BuildingBlocks.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import org.jboss.hal.meta.description.Description;
import org.jboss.hal.meta.description.OperationDescription;
import org.jboss.hal.meta.description.RestartMode;
import org.patternfly.component.codeblock.CodeBlock;
import org.patternfly.component.emptystate.EmptyState;
import org.patternfly.component.list.ListItem;
import org.patternfly.component.popover.Popover;
Expand Down Expand Up @@ -71,6 +72,7 @@
import static org.jboss.hal.resources.HalClasses.name;
import static org.jboss.hal.ui.StabilityLabel.stabilityLabel;
import static org.patternfly.component.button.Button.button;
import static org.patternfly.component.codeblock.CodeBlock.codeBlock;
import static org.patternfly.component.emptystate.EmptyState.emptyState;
import static org.patternfly.component.emptystate.EmptyStateActions.emptyStateActions;
import static org.patternfly.component.emptystate.EmptyStateBody.emptyStateBody;
Expand Down Expand Up @@ -206,6 +208,10 @@ public static Popover attributeDescriptionPopover(String header, AttributeDescri
.add(attributeDescription(attribute)));
}

public static HTMLElement nestedElementSeparator() {
return span().css(util("mx-sm")).textContent("/").element();
}

private static HTMLContainerBuilder<HTMLDivElement> description(Description description) {
HTMLContainerBuilder<HTMLDivElement> div = div();
div.add(div().textContent(description.description()));
Expand All @@ -229,6 +235,29 @@ private static void enumerate(ListItem listItem, java.util.List<ModelNode> value
}
}

// ------------------------------------------------------ code

public static CodeBlock errorCode(String error) {
return errorCode(error, 5);
}

public static CodeBlock errorCode(String error, int lines) {
return codeBlock()
.truncate(lines)
.code(error.replace("\\/", "/"));
}

public static CodeBlock modelNodeCode(ModelNode modelNode) {
return modelNodeCode(modelNode, 5);
}

public static CodeBlock modelNodeCode(ModelNode modelNode, int lines) {
String code = modelNode.isDefined() ? modelNode.toJSONString().replace("\\/", "/") : "";
return codeBlock()
.truncate(lines)
.code(code);
}

// ------------------------------------------------------ empty

public static <T> EmptyState emptyRow(Filter<T> filter) {
Expand Down
6 changes: 4 additions & 2 deletions ui/src/main/java/org/jboss/hal/ui/UIContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@
import org.jboss.hal.meta.tree.ModelTree;

/**
* Holds common classes often needed in UI elements. Use the static {@link #uic()} accessor method to get a reference of this
* class instead of passing multiple references around.
* Holds common classes often needed in UI elements.
* <p>
* In most cases UI classes take not part in CDI. In case they need one of the classes, use {@link #uic()} to get a reference
* instead.
*/
@Startup
@ApplicationScoped
Expand Down
23 changes: 20 additions & 3 deletions ui/src/main/java/org/jboss/hal/ui/modelbrowser/ModelBrowser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,12 @@

import org.jboss.elemento.IsElement;
import org.jboss.elemento.logger.Logger;
import org.jboss.hal.core.Notifications;
import org.jboss.hal.dmr.Operation;
import org.jboss.hal.dmr.ResourceAddress;
import org.jboss.hal.meta.AddressTemplate;
import org.jboss.hal.ui.modelbrowser.ModelBrowserEvents.AddResource;
import org.jboss.hal.ui.modelbrowser.ModelBrowserEvents.DeleteResource;
import org.jboss.hal.ui.modelbrowser.ModelBrowserEvents.SelectInTree;

import elemental2.dom.HTMLElement;
Expand All @@ -32,7 +34,8 @@
import static org.jboss.hal.ui.UIContext.uic;
import static org.jboss.hal.ui.modelbrowser.ModelBrowserEngine.parseChildren;
import static org.jboss.hal.ui.modelbrowser.ModelBrowserNode.Type.RESOURCE;
import static org.jboss.hal.ui.resource.AddResourceDialog.addResourceDialog;
import static org.jboss.hal.ui.resource.ResourceDialogs.addResource;
import static org.jboss.hal.ui.resource.ResourceDialogs.deleteResource;
import static org.patternfly.layout.grid.Grid.grid;
import static org.patternfly.layout.grid.GridItem.gridItem;

Expand Down Expand Up @@ -76,8 +79,9 @@ public ModelBrowser(AddressTemplate template) {
.addItem(gridItem().add(detail))
.element();

SelectInTree.listen(root, this::select);
AddResource.listen(root, this::add);
DeleteResource.listen(root, this::delete);
SelectInTree.listen(root, this::select);
load();
}

Expand Down Expand Up @@ -116,13 +120,26 @@ void load() {
}

private void add(AddResource.Details details) {
addResourceDialog(details.parent, details.child, details.singleton).add().then(__ -> {
addResource(details.parent, details.child, details.singleton).then(__ -> {
tree.select(details.parent.identifier());
tree.reload();
return null;
});
}

private void delete(DeleteResource.Details details) {
deleteResource(details.template)
.then(__ -> {
tree.select(details.template.anonymous().identifier());
tree.reload();
return null;
})
.catch_(error -> {
Notifications.error("Failed to delete resource", String.valueOf(error));
return null;
});
}

private void select(SelectInTree.Details details) {
if (rootMbn != null) {
if (details.template != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,13 +187,13 @@ static Function<ModelBrowserNode, TreeViewItem> mbn2tvi(Dispatcher dispatcher) {
}

private static Popover nonExistingSingletonPopover(ModelBrowserNode mbn) {
AddressTemplate parent = mbn.template.parent().append(mbn.template.last().key, "*"); // /a=b/c=d -> /a=b/c=*
AddressTemplate anonymous = mbn.template.anonymous(); // /a=b/c=d -> /a=b/c=*
return popover(By.data(identifier, mbn.identifier))
.addHeader(mbn.name)
.addBody(popoverBody()
.add(div().add("This resource does not yet exist."))
.add(div().add(button("Add resource").link().inline()
.onClick((e, b) -> ModelBrowserEvents.AddResource.dispatch(b.element(),
parent, mbn.name, true)))));
anonymous, mbn.name, true)))));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,43 @@ static void listen(HTMLElement element, Consumer<Details> listener) {
}
}

interface DeleteResource extends UIEvent {

String TYPE = UIEvent.type("model-browser", "delete");

class Details {

AddressTemplate template;
}

/**
* Creates and returns a custom event to delete a singleton resource.
*
* @param source the source element used to dispatch the event.
* @param template the address template to delete.
*/
@SuppressWarnings("unchecked")
static void dispatch(HTMLElement source, AddressTemplate template) {
Details details = new Details();
details.template = template;
//noinspection DuplicatedCode
CustomEventInit<Details> init = CustomEventInit.create();
init.setBubbles(true);
init.setCancelable(true);
init.setDetail(details);
CustomEvent<Details> event = new CustomEvent<>(TYPE, init);
source.dispatchEvent(event);
}

@SuppressWarnings("unchecked")
static void listen(HTMLElement element, Consumer<Details> listener) {
element.addEventListener(TYPE, event -> {
CustomEvent<Details> customEvent = (CustomEvent<Details>) event;
listener.accept(customEvent.detail);
});
}
}

// ------------------------------------------------------ select

interface SelectInTree extends UIEvent {
Expand Down
Loading

0 comments on commit 8d7998b

Please sign in to comment.