-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement ColumnDefinitionKeyGenerator
- Useful in chooser dialogs
- Loading branch information
eschleb
committed
Jun 6, 2024
1 parent
1865d08
commit 1a9b306
Showing
3 changed files
with
139 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
...n/java/com/merkle/oss/magnolia/definition/key/generator/ColumnDefinitionKeyGenerator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
...va/com/merkle/oss/magnolia/definition/key/generator/ColumnDefinitionKeyGeneratorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |