Skip to content

Commit

Permalink
Add nodeNameValidator to field jcrName on add/edit dialogs
Browse files Browse the repository at this point in the history
  • Loading branch information
eschleb committed Mar 1, 2024
1 parent ba599c5 commit ea1baae
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.namics.oss.magnolia.appbuilder.action;

import info.magnolia.i18nsystem.I18nizer;
import info.magnolia.ui.UIComponent;
import info.magnolia.ui.ValueContext;
import info.magnolia.ui.api.i18n.I18NAuthoringSupport;
import info.magnolia.ui.dialog.DialogDefinition;
import info.magnolia.ui.dialog.DialogDefinitionRegistry;
import info.magnolia.ui.dialog.FormDialogDefinition;
import info.magnolia.ui.dialog.actions.OpenDialogAction;
import info.magnolia.ui.dialog.actions.OpenDialogActionDefinition;
import info.magnolia.ui.editor.LocaleContext;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;
import info.magnolia.ui.field.ConfiguredFieldDefinition;

import javax.inject.Inject;
import javax.jcr.Node;
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Adds a nodeNameValidator to the field jcrName if it is present in the dialog (must be unique).
*/
public class JcrNameValidatingOpenDialogAction extends OpenDialogAction<Node> {
private final NodeNameValidatorDefinition nodeNameValidatorDefinition;

@Inject
public JcrNameValidatingOpenDialogAction(
final Definition definition,
final LocaleContext localeContext,
final ValueContext<Node> valueContext,
final UIComponent parentView,
final I18NAuthoringSupport<Node> i18NAuthoringSupport,
final DialogDefinitionRegistry dialogDefinitionRegistry,
final I18nizer i18nizer
) {
super(definition, localeContext, valueContext, parentView, i18NAuthoringSupport, dialogDefinitionRegistry, i18nizer);
this.nodeNameValidatorDefinition = new NodeNameValidatorDefinition();
this.nodeNameValidatorDefinition.setMode(definition.getMode());
}

@Override
protected DialogDefinition getDialogDefinition(final DialogDefinitionRegistry dialogDefinitionRegistry, final I18nizer i18nizer) {
final DialogDefinition dialogDefinition = super.getDialogDefinition(dialogDefinitionRegistry, i18nizer);
addNodeNameValidatorToJcrNameField(dialogDefinition);
return dialogDefinition;
}

protected void addNodeNameValidatorToJcrNameField(final DialogDefinition dialogDefinition) {
Optional
.of(dialogDefinition)
.filter(FormDialogDefinition.class::isInstance)
.map(definition -> (FormDialogDefinition<Node>) definition)
.map(FormDialogDefinition::getForm)
.flatMap(formDialog -> formDialog.getFieldDefinition("jcrName"))
.filter(ConfiguredFieldDefinition.class::isInstance)
.map(definition -> (ConfiguredFieldDefinition<Node>) definition)
.ifPresent(field ->
field.setValidators(Stream.concat(
field.getValidators().stream(),
Stream.of(nodeNameValidatorDefinition)
).collect(Collectors.toList()))
);
}

public static class Definition extends OpenDialogActionDefinition {
private final NodeNameValidatorDefinition.Mode mode;

public Definition(final NodeNameValidatorDefinition.Mode mode) {
setImplementationClass(JcrNameValidatingOpenDialogAction.class);
this.mode = mode;
}

public NodeNameValidatorDefinition.Mode getMode() {
return mode;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.namics.oss.magnolia.appbuilder.action.add;

import com.namics.oss.magnolia.appbuilder.action.JcrNameValidatingOpenDialogAction;
import info.magnolia.i18nsystem.I18nizer;
import info.magnolia.ui.UIComponent;
import info.magnolia.ui.ValueContext;
Expand All @@ -8,12 +9,11 @@
import info.magnolia.ui.datasource.jcr.JcrDatasource;
import info.magnolia.ui.dialog.DialogDefinition;
import info.magnolia.ui.dialog.DialogDefinitionRegistry;
import info.magnolia.ui.dialog.actions.OpenDialogAction;
import info.magnolia.ui.editor.LocaleContext;

import javax.jcr.Node;

public class OpenCreateDialogAction extends OpenDialogAction<Node> {
public class OpenCreateDialogAction extends JcrNameValidatingOpenDialogAction {
private final OpenCreateDialogActionDefinition definition;
private final JcrDatasource jcrDatasource;
private final ValueContext<Node> valueContext;
Expand All @@ -37,6 +37,7 @@ public OpenCreateDialogAction(
@Override
protected DialogDefinition getDialogDefinition(final DialogDefinitionRegistry dialogDefinitionRegistry, final I18nizer i18nizer) {
final DialogDefinition dialogDefinition = dialogDefinitionRegistry.getProvider(getDefinition().getDialogId()).get();
super.addNodeNameValidatorToJcrNameField(dialogDefinition);
dialogDefinition.getActions().put("commit", definition.getCommitAction()); //we can't generate a new map due to byteBuddy (I18nizer), but since it is a mutable hash map this is fine
return i18nizer.decorate(dialogDefinition);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.namics.oss.magnolia.appbuilder.action.add;

import com.namics.oss.magnolia.appbuilder.action.JcrNameValidatingOpenDialogAction;
import info.magnolia.ui.contentapp.action.CommitActionDefinition;
import info.magnolia.ui.dialog.actions.OpenDialogActionDefinition;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;

public class OpenCreateDialogActionDefinition extends OpenDialogActionDefinition {
public class OpenCreateDialogActionDefinition extends JcrNameValidatingOpenDialogAction.Definition {
private final CommitActionDefinition commitAction;

public OpenCreateDialogActionDefinition(final CommitActionDefinition commitAction) {
super(NodeNameValidatorDefinition.Mode.ADD);
this.commitAction = commitAction;
setPopulate(false);
setImplementationClass(OpenCreateDialogAction.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import com.namics.oss.magnolia.appbuilder.MgnlIcon;
import com.namics.oss.magnolia.appbuilder.action.AppActionDefinition;
import com.namics.oss.magnolia.appbuilder.action.AvailabilityDefinitionBuilder;
import com.namics.oss.magnolia.appbuilder.action.JcrNameValidatingOpenDialogAction;
import com.namics.oss.magnolia.appbuilder.action.rule.JcrIsNotDeletedRuleDefinition;
import com.namics.oss.magnolia.appbuilder.action.rule.PermissionRequiredRuleDefinition;
import info.magnolia.cms.security.Permission;
import info.magnolia.ui.api.action.ConfiguredActionDefinition;
import info.magnolia.ui.dialog.actions.OpenDialogActionDefinition;
import info.magnolia.ui.editor.validator.NodeNameValidatorDefinition;

import javax.annotation.Nullable;

Expand Down Expand Up @@ -37,7 +39,7 @@ public EditAppActionDefinition(final String name, final String dialogId, final S

@Override
public ConfiguredActionDefinition action() {
final OpenDialogActionDefinition definition = new OpenDialogActionDefinition();
final OpenDialogActionDefinition definition = new JcrNameValidatingOpenDialogAction.Definition(NodeNameValidatorDefinition.Mode.EDIT);
definition.setName(name);
definition.setDialogId(dialogId);
definition.setLabel(label);
Expand Down

0 comments on commit ea1baae

Please sign in to comment.