Skip to content

Commit

Permalink
Implement ColumnDefinitionKeyGenerator
Browse files Browse the repository at this point in the history
 - Useful in chooser dialogs
  • Loading branch information
eschleb committed Jun 6, 2024
1 parent 1865d08 commit 1a9b306
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
43 changes: 30 additions & 13 deletions key-generator/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,33 +13,50 @@
validators.<VALIDATOR_NAME>.errorMessage
```

## ColumnDefinitionKeyGenerator
```properties
<APP_NAME>.<SUBAPP_NAME>.views.<COLUMN_NAME>.label
<APP_NAME>.<SUBAPP_NAME>.views.<COLUMN_NAME>
<MODULE_NAME>.dialogs.<DIALOG_NAME>.views.<COLUMN_NAME>.label
<MODULE_NAME>.dialogs.<DIALOG_NAME>.views.<COLUMN_NAME>
chooser.views.<COLUMN_NAME>.label
chooser.views.<COLUMN_NAME>
views.<COLUMN_NAME>.label
views.<COLUMN_NAME>
```

## Setup

```java
import com.merkle.oss.magnolia.definition.key.generator.ColumnDefinitionKeyGenerator;
import com.merkle.oss.magnolia.definition.key.generator.EditorPropertyDefinitionKeyGenerator;
import com.merkle.oss.magnolia.definition.key.generator.FieldValidatorDefinitionKeyGenerator;
import com.merkle.oss.magnolia.definition.key.generator.KeyGeneratorUpdater;

import info.magnolia.module.ModuleLifecycle;
import info.magnolia.module.ModuleLifecycleContext;
import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;
import info.magnolia.ui.field.EditorPropertyDefinition;
import info.magnolia.ui.field.FieldValidatorDefinition;
import info.magnolia.ui.form.field.definition.FieldDefinition;

import javax.inject.Inject;

public class SomeModule implements ModuleLifecycle {
private final KeyGeneratorUpdater keyGeneratorUpdater;

@Inject
public SomeModule(final KeyGeneratorUpdater keyGeneratorUpdater) {
this.keyGeneratorUpdater = keyGeneratorUpdater;
}

@Override
public void start(ModuleLifecycleContext moduleLifecycleContext) {
keyGeneratorUpdater.update(FieldDefinition.class, EditorPropertyDefinitionKeyGenerator.class);
keyGeneratorUpdater.update(EditorPropertyDefinition.class, EditorPropertyDefinitionKeyGenerator.class);
keyGeneratorUpdater.update(FieldValidatorDefinition.class, FieldValidatorDefinitionKeyGenerator.class);
}
private final KeyGeneratorUpdater keyGeneratorUpdater;

@Inject
public SomeModule(final KeyGeneratorUpdater keyGeneratorUpdater) {
this.keyGeneratorUpdater = keyGeneratorUpdater;
}

@Override
public void start(ModuleLifecycleContext moduleLifecycleContext) {
keyGeneratorUpdater.update(FieldDefinition.class, EditorPropertyDefinitionKeyGenerator.class);
keyGeneratorUpdater.update(EditorPropertyDefinition.class, EditorPropertyDefinitionKeyGenerator.class);
keyGeneratorUpdater.update(FieldValidatorDefinition.class, FieldValidatorDefinitionKeyGenerator.class);
keyGeneratorUpdater.update(ColumnDefinition.class, ColumnDefinitionKeyGenerator.class);
}
}
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.merkle.oss.magnolia.definition.key.generator;

import info.magnolia.ui.chooser.definition.ChooserDefinition;
import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;

import java.lang.reflect.AnnotatedElement;
import java.util.List;
import java.util.Optional;

public class ColumnDefinitionKeyGenerator extends info.magnolia.ui.contentapp.configuration.column.ColumnDefinitionKeyGenerator {

@Override
protected void keysFor(
final List<String> list,
final ColumnDefinition definition,
final AnnotatedElement el
) {
super.keysFor(list, definition, el);
getChooserDialogDefinition(definition).ifPresent(dialogName ->
addKey(list, getKeys(dialogName, definition, el))
);
}

private Optional<? extends ChooserDefinition<?, ?>> getChooserDialogDefinition(final ColumnDefinition<?> definition) {
return getAncestors(definition).stream()
.filter(ChooserDefinition.class::isInstance)
.map(chooser -> (ChooserDefinition<?,?>) chooser)
.filter(chooser -> chooser.getId() != null)
.findAny();
}

protected String[] getKeys(final ChooserDefinition<?, ?> chooser, final ColumnDefinition<?> definition, final AnnotatedElement el) {
return new String[] {
getModuleName(chooser.getId()),
getIdWithoutModuleName(chooser.getId()),
"views",
replaceColons(definition.getName()),
fieldOrGetterName(el)
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package com.merkle.oss.magnolia.definition.key.generator;

import static org.junit.jupiter.api.Assertions.assertEquals;

import info.magnolia.context.Context;
import info.magnolia.i18nsystem.bytebudddy.ByteBuddyI18nizer;
import info.magnolia.ui.chooser.definition.WorkbenchChooserDefinition;
import info.magnolia.ui.contentapp.configuration.ListViewDefinition;
import info.magnolia.ui.contentapp.configuration.WorkbenchDefinition;
import info.magnolia.ui.contentapp.configuration.column.ColumnDefinition;
import info.magnolia.ui.contentapp.configuration.column.ConfiguredColumnDefinition;

import java.util.Collection;
import java.util.List;
import java.util.Objects;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

class ColumnDefinitionKeyGeneratorTest extends AbstractKeyGeneratorTest {
private ColumnDefinitionKeyGenerator keyGenerator;

@BeforeEach
void setUp() {
keyGenerator = new ColumnDefinitionKeyGenerator();
}

@Test
<T> void keysFor() throws NoSuchMethodException {
ConfiguredColumnDefinition<T> column = new ConfiguredColumnDefinition<>();
column.setName("someColumn");
final ConfiguredColumnDefinition<?> columnDefinition = i18nIfy("idPrefix:dialogs/SomeDialog", List.of(column), column);

assertEquals(
List.of(
"workbenchchooser.views.someColumn.label",
"workbenchchooser.views.someColumn",
"views.someColumn.label",
"views.someColumn",
"idPrefix.dialogs.SomeDialog.views.someColumn.label",
"idPrefix.dialogs.SomeDialog.views.someColumn"
),
List.of(keyGenerator.keysFor((String)null, columnDefinition, ColumnDefinition.class.getMethod("getLabel")))
);
}

private <T, D extends ColumnDefinition<T>> D i18nIfy(final String dialogId, final List<ColumnDefinition<T>> columns, final D definition) {
final ListViewDefinition<T> listView = new ListViewDefinition<>();
listView.setColumns(columns);
final WorkbenchDefinition<T> workbench = new WorkbenchDefinition<>();
workbench.setContentViews(List.of(listView));
final WorkbenchChooserDefinition<T> dialog = new WorkbenchChooserDefinition<>();
dialog.setWorkbench(workbench);
dialog.setId(dialogId);
final WorkbenchChooserDefinition<T> decoratedDialog = new ByteBuddyI18nizer(new TranslationServiceMock(), () -> Mockito.mock(Context.class)).decorate(dialog);

return decoratedDialog.getWorkbench().getContentViews().stream()
.filter(ListViewDefinition.class::isInstance)
.map(viewDefinition -> (ListViewDefinition<T>)viewDefinition)
.map(ListViewDefinition::getColumns)
.flatMap(Collection::stream)
.filter(column -> Objects.equals(column.getName(), definition.getName()))
.findFirst()
.map(d -> (D)d)
.orElseThrow();
}
}

0 comments on commit 1a9b306

Please sign in to comment.