From edc91b1b34d6bff9e5f74af0d22b2cb2b036c0c3 Mon Sep 17 00:00:00 2001 From: Remo Date: Thu, 20 Jun 2019 10:54:40 +0200 Subject: [PATCH] editor presentation model for crnk-ui #517 --- .../io/crnk/meta/model/MetaPrimitiveType.java | 9 + .../java/io/crnk/meta/model/MetaType.java | 4 +- crnk-ui/build.gradle | 4 +- .../src/main/java/io/crnk/ui/UIModule.java | 188 ++++++++++-------- .../presentation/PresentationEnvironment.java | 125 +++++++----- .../ui/presentation/PresentationManager.java | 119 +++++------ .../ui/presentation/PresentationType.java | 3 +- .../presentation/element/EditorElement.java | 105 ++++++---- .../ui/presentation/element/FormElement.java | 40 ++-- .../ui/presentation/element/FormElements.java | 33 +-- .../element/PlainTextElement.java | 17 ++ .../element/PresentationElement.java | 30 +-- .../factory/DefaultDisplayElementFactory.java | 18 -- .../factory/DefaultEditorFactory.java | 61 ++++++ .../factory/DefaultExplorerFactory.java | 66 ++++++ .../factory/DefaultFormFactory.java | 86 ++++++++ .../DefaultPlainTextElementFactory.java | 40 ++++ .../factory/DefaultTableElementFactory.java | 169 ++++++++-------- .../repository/EditorRepository.java | 27 +++ .../java/io/crnk/ui/EditorRepositoryTest.java | 64 ++++++ .../io/crnk/ui/ExplorerRepositoryTest.java | 73 ++++--- 21 files changed, 866 insertions(+), 415 deletions(-) create mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/element/PlainTextElement.java delete mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultDisplayElementFactory.java create mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultEditorFactory.java create mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultExplorerFactory.java create mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultFormFactory.java create mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultPlainTextElementFactory.java create mode 100644 crnk-ui/src/main/java/io/crnk/ui/presentation/repository/EditorRepository.java create mode 100644 crnk-ui/src/test/java/io/crnk/ui/EditorRepositoryTest.java diff --git a/crnk-meta/src/main/java/io/crnk/meta/model/MetaPrimitiveType.java b/crnk-meta/src/main/java/io/crnk/meta/model/MetaPrimitiveType.java index 7e36abfc5..b3ef79d45 100644 --- a/crnk-meta/src/main/java/io/crnk/meta/model/MetaPrimitiveType.java +++ b/crnk-meta/src/main/java/io/crnk/meta/model/MetaPrimitiveType.java @@ -5,4 +5,13 @@ @JsonApiResource(type = "meta/primitiveType") public class MetaPrimitiveType extends MetaType { + public static final String ID_STRING = "base.string"; + + public static final String ID_INT = "base.int"; + + public static final String ID_BYTE = "base.byte"; + + public static final String ID_SHORT = "base.short"; + + public static final String ID_LONG = "base.long"; } diff --git a/crnk-meta/src/main/java/io/crnk/meta/model/MetaType.java b/crnk-meta/src/main/java/io/crnk/meta/model/MetaType.java index 0c37e9bd9..c5237c754 100644 --- a/crnk-meta/src/main/java/io/crnk/meta/model/MetaType.java +++ b/crnk-meta/src/main/java/io/crnk/meta/model/MetaType.java @@ -1,5 +1,7 @@ package io.crnk.meta.model; +import java.lang.reflect.Type; + import com.fasterxml.jackson.annotation.JsonIgnore; import io.crnk.core.engine.internal.utils.ClassUtils; import io.crnk.core.engine.internal.utils.PreconditionUtil; @@ -8,8 +10,6 @@ import io.crnk.core.resource.annotations.LookupIncludeBehavior; import io.crnk.core.resource.annotations.SerializeType; -import java.lang.reflect.Type; - @JsonApiResource(type = "meta/type") public class MetaType extends MetaElement { diff --git a/crnk-ui/build.gradle b/crnk-ui/build.gradle index ab0edfcb5..b4101ed80 100644 --- a/crnk-ui/build.gradle +++ b/crnk-ui/build.gradle @@ -36,7 +36,9 @@ task npmRunBuild(type: NpmTask) { npmCommand = ['run', 'build'] args = ['--', '--base-href', './'] + NG_BUILD_ARGS - inputs.dir 'src' + inputs.dir 'src/app' + inputs.dir 'src/assets' + inputs.dir 'src/environments' inputs.files 'angular-cli.json', 'package.json' outputs.dir 'build/npm/io/crnk/ui' } diff --git a/crnk-ui/src/main/java/io/crnk/ui/UIModule.java b/crnk-ui/src/main/java/io/crnk/ui/UIModule.java index efcbbe023..ca21f928a 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/UIModule.java +++ b/crnk-ui/src/main/java/io/crnk/ui/UIModule.java @@ -1,6 +1,12 @@ package io.crnk.ui; +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.List; +import java.util.Optional; +import java.util.function.Supplier; + import io.crnk.core.engine.internal.utils.ClassUtils; import io.crnk.core.module.Module; import io.crnk.core.module.ModuleExtension; @@ -12,97 +18,103 @@ import io.crnk.ui.internal.UIHttpRequestProcessor; import io.crnk.ui.presentation.PresentationManager; import io.crnk.ui.presentation.PresentationService; +import io.crnk.ui.presentation.repository.EditorRepository; import io.crnk.ui.presentation.repository.ExplorerRepository; -import java.lang.reflect.Method; -import java.util.Arrays; -import java.util.List; -import java.util.Optional; -import java.util.function.Supplier; - public class UIModule implements Module { - private final UIModuleConfig config; - - private ExplorerRepository explorerRepository; - - private ModuleContext context; - - // protected for CDI - protected UIModule() { - config = null; - } - - protected UIModule(UIModuleConfig config) { - this.config = config; - } - - public static UIModule create(UIModuleConfig config) { - return new UIModule(config); - } - - public String getModuleName() { - return "ui"; - } - - private MetaLookup metaLookup; - - private MetaLookup initMetaModule() { - if (metaLookup == null) { - Optional optMetaModule = context.getModuleRegistry().getModule(MetaModule.class); - if (optMetaModule.isPresent()) { - metaLookup = optMetaModule.get().getLookup(); - } else { - MetaModuleConfig config = new MetaModuleConfig(); - config.addMetaProvider(new ResourceMetaProvider()); - - MetaLookupImpl impl = new MetaLookupImpl(); - impl.setModuleContext(context); - config.apply(impl); - impl.initialize(); - metaLookup = impl; - } - } - return metaLookup; - } - - @Override - public void setupModule(ModuleContext context) { - this.context = context; - context.addHttpRequestProcessor(new UIHttpRequestProcessor(config)); - setupHomeExtension(context); - - if (config != null) { - Supplier> servicesSupplier = config.getServices(); - if (servicesSupplier == null) { - servicesSupplier = () -> Arrays.asList(new PresentationService("local", null, initMetaModule())); - } - - PresentationManager manager = new PresentationManager(servicesSupplier); - explorerRepository = new ExplorerRepository(manager); - context.addRepository(explorerRepository); - } - } - - public ExplorerRepository getExplorerRepository() { - return explorerRepository; - } - - public UIModuleConfig getConfig() { - return config; - } - - private void setupHomeExtension(ModuleContext context) { - if (ClassUtils.existsClass("io.crnk.home.HomeModuleExtension")) { - try { - Class clazz = Class.forName("io.crnk.ui.internal.UiHomeModuleExtensionFactory"); - Method method = clazz.getMethod("create", UIModuleConfig.class); - ModuleExtension homeExtension = (ModuleExtension) method.invoke(clazz, config); - context.addExtension(homeExtension); - } catch (Exception e) { - throw new IllegalStateException(e); - } - } - } + private final UIModuleConfig config; + + private ExplorerRepository explorerRepository; + + private EditorRepository editorRepository; + + private ModuleContext context; + + // protected for CDI + protected UIModule() { + config = null; + } + + protected UIModule(UIModuleConfig config) { + this.config = config; + } + + public static UIModule create(UIModuleConfig config) { + return new UIModule(config); + } + + public String getModuleName() { + return "ui"; + } + + private MetaLookup metaLookup; + + private MetaLookup initMetaModule() { + if (metaLookup == null) { + Optional optMetaModule = context.getModuleRegistry().getModule(MetaModule.class); + if (optMetaModule.isPresent()) { + metaLookup = optMetaModule.get().getLookup(); + } + else { + MetaModuleConfig config = new MetaModuleConfig(); + config.addMetaProvider(new ResourceMetaProvider()); + + MetaLookupImpl impl = new MetaLookupImpl(); + impl.setModuleContext(context); + config.apply(impl); + impl.initialize(); + metaLookup = impl; + } + } + return metaLookup; + } + + @Override + public void setupModule(ModuleContext context) { + this.context = context; + context.addHttpRequestProcessor(new UIHttpRequestProcessor(config)); + setupHomeExtension(context); + + if (config != null) { + Supplier> servicesSupplier = config.getServices(); + if (servicesSupplier == null) { + servicesSupplier = () -> Arrays.asList(new PresentationService("local", null, initMetaModule())); + } + + PresentationManager manager = new PresentationManager(servicesSupplier); + explorerRepository = new ExplorerRepository(manager); + context.addRepository(explorerRepository); + editorRepository = new EditorRepository(manager); + context.addRepository(editorRepository); + } + } + + public ExplorerRepository getExplorerRepository() { + return explorerRepository; + } + + + public EditorRepository getEditorRepository() { + return editorRepository; + } + + public UIModuleConfig getConfig() { + return config; + } + + private void setupHomeExtension(ModuleContext context) { + if (ClassUtils.existsClass("io.crnk.home.HomeModuleExtension")) { + try { + Class clazz = Class.forName("io.crnk.ui.internal.UiHomeModuleExtensionFactory"); + Method method = clazz.getMethod("create", UIModuleConfig.class); + ModuleExtension homeExtension = (ModuleExtension) method.invoke(clazz, config); + context.addExtension(homeExtension); + } + catch (Exception e) { + throw new IllegalStateException(e); + } + } + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationEnvironment.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationEnvironment.java index e216d6f3e..7478e5722 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationEnvironment.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationEnvironment.java @@ -1,78 +1,105 @@ package io.crnk.ui.presentation; +import java.util.ArrayDeque; +import java.util.Collections; +import java.util.List; + import io.crnk.meta.model.MetaAttribute; import io.crnk.meta.model.MetaElement; import io.crnk.meta.model.MetaType; import io.crnk.ui.presentation.element.PresentationElement; -import java.util.ArrayDeque; -import java.util.Collections; -import java.util.List; - public class PresentationEnvironment { - private MetaElement element; + private MetaElement element; + + private MetaType type; + + private ArrayDeque attributePath; + + private boolean editable; + + private List acceptedTypes = Collections.emptyList(); - private MetaType type; + private PresentationManager manager; - private ArrayDeque attributePath; + private PresentationService service; - private boolean editable; + public PresentationElement createElement(PresentationEnvironment env) { + return manager.createElement(env); + } - private List acceptedTypes = Collections.emptyList(); + public MetaElement getElement() { + return element; + } - private PresentationManager factory; + public void setElement(MetaElement element) { + this.element = element; + } - public PresentationElement createElement(PresentationEnvironment env) { - return factory.createElement(env); - } + public MetaType getType() { + return type; + } - public MetaElement getElement() { - return element; - } + public void setType(MetaType type) { + this.type = type; + } - public void setElement(MetaElement element) { - this.element = element; - } + public ArrayDeque getAttributePath() { + return attributePath; + } - public MetaType getType() { - return type; - } + public PresentationEnvironment setAttributePath(ArrayDeque attributePath) { + this.attributePath = attributePath; + return this; + } - public void setType(MetaType type) { - this.type = type; - } + public boolean isEditable() { + return editable; + } - public ArrayDeque getAttributePath() { - return attributePath; - } + public PresentationEnvironment setEditable(boolean editable) { + this.editable = editable; + return this; + } - public void setAttributePath(ArrayDeque attributePath) { - this.attributePath = attributePath; - } + public List getAcceptedTypes() { + return acceptedTypes; + } - public boolean isEditable() { - return editable; - } + public PresentationEnvironment setAcceptedTypes(List acceptedTypes) { + this.acceptedTypes = acceptedTypes; + return this; + } - public void setEditable(boolean editable) { - this.editable = editable; - } + public PresentationManager getManager() { + return manager; + } - public List getAcceptedTypes() { - return acceptedTypes; - } + public PresentationEnvironment setManager(PresentationManager manager) { + this.manager = manager; + return this; + } - public void setAcceptedTypes(List acceptedTypes) { - this.acceptedTypes = acceptedTypes; - } + public PresentationService getService() { + return service; + } - public PresentationManager getFactory() { - return factory; - } + public PresentationEnvironment setService(PresentationService service) { + this.service = service; + return this; + } - public void setFactory(PresentationManager factory) { - this.factory = factory; - } + public PresentationEnvironment clone() { + PresentationEnvironment duplicate = new PresentationEnvironment(); + duplicate.setElement(element); + duplicate.setType(type); + duplicate.setAttributePath(attributePath); + duplicate.setEditable(editable); + duplicate.setAcceptedTypes(acceptedTypes); + duplicate.setManager(manager); + duplicate.setService(service); + return duplicate; + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationManager.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationManager.java index 7c593553d..cb5a525e7 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationManager.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationManager.java @@ -12,16 +12,16 @@ import io.crnk.core.exception.ResourceNotFoundException; import io.crnk.meta.MetaLookup; import io.crnk.meta.model.resource.MetaResource; -import io.crnk.ui.presentation.element.ActionElement; -import io.crnk.ui.presentation.element.DataTableElement; +import io.crnk.ui.presentation.element.EditorElement; import io.crnk.ui.presentation.element.ExplorerElement; import io.crnk.ui.presentation.element.MenuElement; import io.crnk.ui.presentation.element.MenuElements; import io.crnk.ui.presentation.element.PresentationElement; -import io.crnk.ui.presentation.element.QueryElement; -import io.crnk.ui.presentation.factory.DefaultDisplayElementFactory; +import io.crnk.ui.presentation.factory.DefaultPlainTextElementFactory; +import io.crnk.ui.presentation.factory.DefaultEditorFactory; +import io.crnk.ui.presentation.factory.DefaultExplorerFactory; +import io.crnk.ui.presentation.factory.DefaultFormFactory; import io.crnk.ui.presentation.factory.DefaultTableElementFactory; -import io.crnk.ui.presentation.factory.PresentationBuilderUtils; import io.crnk.ui.presentation.factory.PresentationElementFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -37,12 +37,33 @@ public class PresentationManager { public PresentationManager(Supplier> services) { this.services = services; - this.factories.add(new DefaultDisplayElementFactory()); this.factories.add(new DefaultTableElementFactory()); - + this.factories.add(new DefaultExplorerFactory()); + this.factories.add(new DefaultEditorFactory()); + this.factories.add(new DefaultFormFactory()); + this.factories.add(new DefaultPlainTextElementFactory()); } public ExplorerElement getExplorer(String id) { + ResourceRef ref = findResourceRef(id); + return (ExplorerElement) createViewer(ref.service, ref.resource, PresentationType.EXPLORER); + } + + + public EditorElement getEditor(String id) { + ResourceRef ref = findResourceRef(id); + return (EditorElement) createViewer(ref.service, ref.resource, PresentationType.EDITOR); + } + + + class ResourceRef { + + PresentationService service; + + MetaResource resource; + } + + private ResourceRef findResourceRef(String id) { int index = id.lastIndexOf("-"); String serviceName = id.substring(0, index); String resourceMetaId = id.substring(index + 1); @@ -52,23 +73,50 @@ public ExplorerElement getExplorer(String id) { throw new ResourceNotFoundException("no presentation service found with name " + serviceName); } PresentationService service = optService.get(); - MetaLookup lookup = service.getLookup(); MetaResource resource = lookup.findElement(MetaResource.class, resourceMetaId); - return createExplorer(service, resource); + + ResourceRef ref = new ResourceRef(); + ref.service = service; + ref.resource = resource; + return ref; + } + + + private PresentationElement createViewer(PresentationService service, MetaResource resource, PresentationType type) { + PresentationEnvironment env = createEnv(service, resource); + env.setAcceptedTypes(Arrays.asList(type)); + return createElement(env); + } + + private PresentationEnvironment createEnv(PresentationService service, MetaResource resource) { + PresentationEnvironment env = new PresentationEnvironment(); + env.setEditable(false); + env.setElement(resource); + env.setType(resource); + env.setService(service); + env.setManager(this); + return env; + } + + public Map getEditors() { + return (Map) getViewers(PresentationType.EDITOR); } public Map getExplorers() { - HashMap map = new HashMap<>(); + return (Map) getViewers(PresentationType.EXPLORER); + } + public Map getViewers(PresentationType type) { + HashMap map = new HashMap<>(); for (PresentationService service : services.get()) { try { MetaLookup lookup = service.getLookup(); List resources = lookup.findElements(MetaResource.class); for (MetaResource resource : resources) { if (!isIgnored(resource)) { - ExplorerElement explorer = createExplorer(service, resource); - map.put(explorer.getId(), explorer); + PresentationElement element = createViewer(service, resource, type); + map.put(element.getId(), element); } } } @@ -83,53 +131,6 @@ private boolean isIgnored(MetaResource resource) { return resource.getResourceType().startsWith("meta/") && resource.getRepository() != null && resource.getRepository().isExposed(); } - private ExplorerElement createExplorer(PresentationService service, MetaResource resource) { - ExplorerElement explorerElement = new ExplorerElement(); - explorerElement.setId(toId(service, resource)); - explorerElement.setTable(createTable(resource)); - explorerElement.addAction(createRefreshAction()); - explorerElement.setPath(service.getPath() + resource.getResourcePath()); - explorerElement.setServiceName(service.getServiceName()); - explorerElement.setServicePath(service.getPath()); - - if (resource.isInsertable()) { - explorerElement.addAction(createPostAction()); - } - - QueryElement query = explorerElement.getBaseQuery(); - query.setResourceType(resource.getResourceType()); - query.setInclusions(PresentationBuilderUtils.computeIncludes(explorerElement.getTable())); - return explorerElement; - } - - private String toId(PresentationService service, MetaResource resource) { - return service.getServiceName() + "-" + resource.getId(); - } - - - private DataTableElement createTable(MetaResource resource) { - DefaultTableElementFactory builder = new DefaultTableElementFactory(); - PresentationEnvironment env = new PresentationEnvironment(); - env.setElement(resource); - env.setType(resource); - env.setEditable(false); - env.setAcceptedTypes(Arrays.asList(PresentationType.TABLE)); - env.setFactory(this); - return builder.create(env); - } - - private ActionElement createRefreshAction() { - ActionElement element = new ActionElement(); - element.setId("refresh"); - return element; - } - - private ActionElement createPostAction() { - ActionElement element = new ActionElement(); - element.setId("create"); - return element; - } - private MenuElements createMenu() { MenuElements menuElements = new MenuElements(); diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationType.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationType.java index 3c805ec25..b14c2bb56 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationType.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/PresentationType.java @@ -1,5 +1,6 @@ package io.crnk.ui.presentation; public enum PresentationType { - TABLE, FORM_ELEMENT, FORM, COLUMN, ACTION, DISPLAY, CELL; + TABLE, FORM_ELEMENT, FORM, COLUMN, ACTION, DISPLAY, CELL, EXPLORER, EDITOR; + } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/EditorElement.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/EditorElement.java index da288a41e..95866cb42 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/EditorElement.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/EditorElement.java @@ -1,56 +1,91 @@ package io.crnk.ui.presentation.element; import com.fasterxml.jackson.databind.node.ObjectNode; +import io.crnk.core.resource.annotations.JsonApiResource; +import io.crnk.data.facet.annotation.Facet; + +@JsonApiResource(type = "uiEditor", resourcePath = "ui/editor") public class EditorElement extends PresentationElement { - private ViewHeaderElement header = new ViewHeaderElement(); + @Facet + private String serviceName; + + private String servicePath; + + private ViewHeaderElement header = new ViewHeaderElement(); + + private QueryElement baseQuery = new QueryElement(); + + private ActionContainerElement actions = new ActionContainerElement(); + + private FormContainerElement form = new FormContainerElement(); + + private ObjectNode newResourceTemplate; + + private String path; + + public String getServiceName() { + return serviceName; + } + + public void setServiceName(String serviceName) { + this.serviceName = serviceName; + } - private QueryElement baseQuery = new QueryElement(); + public String getServicePath() { + return servicePath; + } - private ActionContainerElement actions = new ActionContainerElement(); + public void setServicePath(String servicePath) { + this.servicePath = servicePath; + } - private FormContainerElement form = new FormContainerElement(); + public ViewHeaderElement getHeader() { + return header; + } - private ObjectNode newResourceTemplate; + public void setHeader(ViewHeaderElement header) { + this.header = header; + } - public ViewHeaderElement getHeader() { - return header; - } + public QueryElement getBaseQuery() { + return baseQuery; + } - public void setHeader(ViewHeaderElement header) { - this.header = header; - } + public void setBaseQuery(QueryElement baseQuery) { + this.baseQuery = baseQuery; + } - public QueryElement getBaseQuery() { - return baseQuery; - } + public ActionContainerElement getActions() { + return actions; + } - public void setBaseQuery(QueryElement baseQuery) { - this.baseQuery = baseQuery; - } + public void setActions(ActionContainerElement actions) { + this.actions = actions; + } - public ActionContainerElement getActions() { - return actions; - } + public FormContainerElement getForm() { + return form; + } - public void setActions(ActionContainerElement actions) { - this.actions = actions; - } + public void setForm(FormContainerElement form) { + this.form = form; + } - public FormContainerElement getForm() { - return form; - } + public ObjectNode getNewResourceTemplate() { + return newResourceTemplate; + } - public void setForm(FormContainerElement form) { - this.form = form; - } + public void setNewResourceTemplate(ObjectNode newResourceTemplate) { + this.newResourceTemplate = newResourceTemplate; + } - public ObjectNode getNewResourceTemplate() { - return newResourceTemplate; - } + public String getPath() { + return path; + } - public void setNewResourceTemplate(ObjectNode newResourceTemplate) { - this.newResourceTemplate = newResourceTemplate; - } + public void setPath(String path) { + this.path = path; + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElement.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElement.java index 97b6ed1e4..08e9d700c 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElement.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElement.java @@ -1,25 +1,35 @@ package io.crnk.ui.presentation.element; -public class FormElement extends PresentationElement { +public class FormElement extends SingularValueElement { - private PresentationElement component; + private PresentationElement component; - private String label; + private String label; - public PresentationElement getComponent() { - return component; - } + private boolean editable; - public void setComponent(PresentationElement component) { - this.component = component; - } + public PresentationElement getComponent() { + return component; + } - public String getLabel() { - return label; - } + public void setComponent(PresentationElement component) { + this.component = component; + } - public void setLabel(String label) { - this.label = label; - } + public String getLabel() { + return label; + } + + public void setLabel(String label) { + this.label = label; + } + + public boolean isEditable() { + return editable; + } + + public void setEditable(boolean editable) { + this.editable = editable; + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElements.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElements.java index 72d4fb738..2c0232ae6 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElements.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/FormElements.java @@ -7,23 +7,28 @@ public class FormElements extends PresentationElement { - private Map elements = new HashMap<>(); + private Map elements = new HashMap<>(); - private List elementIds = new ArrayList<>(); + private List elementIds = new ArrayList<>(); - public Map getElements() { - return elements; - } + public Map getElements() { + return elements; + } - public void setElements(Map elements) { - this.elements = elements; - } + public void setElements(Map elements) { + this.elements = elements; + } - public List getElementIds() { - return elementIds; - } + public List getElementIds() { + return elementIds; + } - public void setElementIds(List elementIds) { - this.elementIds = elementIds; - } + public void setElementIds(List elementIds) { + this.elementIds = elementIds; + } + + public void add(FormElement element) { + this.elementIds.add(element.getId()); + this.elements.put(element.getId(), element); + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PlainTextElement.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PlainTextElement.java new file mode 100644 index 000000000..12e96f5eb --- /dev/null +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PlainTextElement.java @@ -0,0 +1,17 @@ +package io.crnk.ui.presentation.element; + +import java.util.HashMap; +import java.util.Map; + +public class PlainTextElement extends SingularValueElement { + + private Map style = new HashMap<>(); + + public Map getStyle() { + return style; + } + + public void setStyle(Map style) { + this.style = style; + } +} diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PresentationElement.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PresentationElement.java index 08709d568..229ce22b8 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PresentationElement.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/element/PresentationElement.java @@ -4,24 +4,24 @@ public class PresentationElement { - @JsonApiId - private String id; + @JsonApiId + private String id; - private String componentId; + private String componentId; - public String getId() { - return id; - } + public String getId() { + return id; + } - public void setId(String id) { - this.id = id; - } + public void setId(String id) { + this.id = id; + } - public String getComponentId() { - return componentId; - } + public String getComponentId() { + return componentId; + } - public void setComponentId(String componentId) { - this.componentId = componentId; - } + public void setComponentId(String componentId) { + this.componentId = componentId; + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultDisplayElementFactory.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultDisplayElementFactory.java deleted file mode 100644 index a87157b49..000000000 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultDisplayElementFactory.java +++ /dev/null @@ -1,18 +0,0 @@ -package io.crnk.ui.presentation.factory; - -import io.crnk.ui.presentation.PresentationEnvironment; -import io.crnk.ui.presentation.element.PresentationElement; - -public class DefaultDisplayElementFactory implements PresentationElementFactory { - @Override - public boolean accepts(PresentationEnvironment env) { - return true; - } - - @Override - public PresentationElement create(PresentationEnvironment env) { - PresentationElement element = new PresentationElement(); - element.setComponentId("display"); - return element; - } -} diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultEditorFactory.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultEditorFactory.java new file mode 100644 index 000000000..be32f41f2 --- /dev/null +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultEditorFactory.java @@ -0,0 +1,61 @@ +package io.crnk.ui.presentation.factory; + +import java.util.Collections; + +import io.crnk.meta.model.resource.MetaResource; +import io.crnk.ui.presentation.PresentationEnvironment; +import io.crnk.ui.presentation.PresentationService; +import io.crnk.ui.presentation.PresentationType; +import io.crnk.ui.presentation.element.ActionElement; +import io.crnk.ui.presentation.element.EditorElement; +import io.crnk.ui.presentation.element.FormContainerElement; +import io.crnk.ui.presentation.element.QueryElement; + +public class DefaultEditorFactory implements PresentationElementFactory { + + @Override + public boolean accepts(PresentationEnvironment env) { + return env.getAcceptedTypes().contains(PresentationType.EDITOR); + } + + @Override + public EditorElement create(PresentationEnvironment env) { + MetaResource resource = (MetaResource) env.getElement(); + PresentationService service = env.getService(); + + EditorElement editorElement = new EditorElement(); + editorElement.setId(toId(service, resource)); + editorElement.setForm(createForm(env)); + editorElement.setPath(service.getPath() + resource.getResourcePath()); + editorElement.setServiceName(service.getServiceName()); + editorElement.setServicePath(service.getPath()); + + QueryElement query = editorElement.getBaseQuery(); + query.setResourceType(resource.getResourceType()); + query.setInclusions(PresentationBuilderUtils.computeIncludes(editorElement.getForm())); + return editorElement; + } + + private String toId(PresentationService service, MetaResource resource) { + return service.getServiceName() + "-" + resource.getId(); + } + + + private FormContainerElement createForm(PresentationEnvironment env) { + PresentationEnvironment formEnv = env.clone().setAcceptedTypes(Collections.singletonList(PresentationType.FORM)); + return (FormContainerElement) env.createElement(formEnv); + + } + + private ActionElement createRefreshAction() { + ActionElement element = new ActionElement(); + element.setId("refresh"); + return element; + } + + private ActionElement createPostAction() { + ActionElement element = new ActionElement(); + element.setId("create"); + return element; + } +} diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultExplorerFactory.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultExplorerFactory.java new file mode 100644 index 000000000..d16efbfa0 --- /dev/null +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultExplorerFactory.java @@ -0,0 +1,66 @@ +package io.crnk.ui.presentation.factory; + +import java.util.Collections; + +import io.crnk.meta.model.resource.MetaResource; +import io.crnk.ui.presentation.PresentationEnvironment; +import io.crnk.ui.presentation.PresentationService; +import io.crnk.ui.presentation.PresentationType; +import io.crnk.ui.presentation.element.ActionElement; +import io.crnk.ui.presentation.element.DataTableElement; +import io.crnk.ui.presentation.element.ExplorerElement; +import io.crnk.ui.presentation.element.QueryElement; + +public class DefaultExplorerFactory implements PresentationElementFactory { + + @Override + public boolean accepts(PresentationEnvironment env) { + return env.getAcceptedTypes().contains(PresentationType.EXPLORER); + } + + @Override + public ExplorerElement create(PresentationEnvironment env) { + MetaResource resource = (MetaResource) env.getElement(); + PresentationService service = env.getService(); + + ExplorerElement explorerElement = new ExplorerElement(); + explorerElement.setId(toId(service, resource)); + explorerElement.setTable(createTable(env)); + explorerElement.addAction(createRefreshAction()); + explorerElement.setPath(service.getPath() + resource.getResourcePath()); + explorerElement.setServiceName(service.getServiceName()); + explorerElement.setServicePath(service.getPath()); + + if (resource.isInsertable()) { + explorerElement.addAction(createPostAction()); + } + + QueryElement query = explorerElement.getBaseQuery(); + query.setResourceType(resource.getResourceType()); + query.setInclusions(PresentationBuilderUtils.computeIncludes(explorerElement.getTable())); + return explorerElement; + } + + private String toId(PresentationService service, MetaResource resource) { + return service.getServiceName() + "-" + resource.getId(); + } + + + private DataTableElement createTable(PresentationEnvironment env) { + PresentationEnvironment tableEnv = env.clone().setAcceptedTypes(Collections.singletonList(PresentationType.TABLE)); + return (DataTableElement) env.createElement(tableEnv); + + } + + private ActionElement createRefreshAction() { + ActionElement element = new ActionElement(); + element.setId("refresh"); + return element; + } + + private ActionElement createPostAction() { + ActionElement element = new ActionElement(); + element.setId("create"); + return element; + } +} diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultFormFactory.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultFormFactory.java new file mode 100644 index 000000000..f2ce43d78 --- /dev/null +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultFormFactory.java @@ -0,0 +1,86 @@ +package io.crnk.ui.presentation.factory; + +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.stream.Collectors; + +import io.crnk.core.queryspec.PathSpec; +import io.crnk.meta.model.MetaAttribute; +import io.crnk.meta.model.MetaDataObject; +import io.crnk.meta.model.MetaType; +import io.crnk.meta.model.resource.MetaResource; +import io.crnk.ui.presentation.PresentationEnvironment; +import io.crnk.ui.presentation.PresentationType; +import io.crnk.ui.presentation.element.FormContainerElement; +import io.crnk.ui.presentation.element.FormElement; +import io.crnk.ui.presentation.element.FormElements; +import io.crnk.ui.presentation.element.PresentationElement; + +public class DefaultFormFactory implements PresentationElementFactory { + + @Override + public boolean accepts(PresentationEnvironment env) { + return env.getAcceptedTypes().contains(PresentationType.FORM); + } + + @Override + public FormContainerElement create(PresentationEnvironment env) { + MetaResource resource = (MetaResource) env.getElement(); + + FormContainerElement form = new FormContainerElement(); + for (MetaAttribute attribute : resource.getAttributes()) { + buildElement(env, form.getElements(), new ArrayDeque(Arrays.asList(attribute))); + } + return form; + } + + private void buildElement(PresentationEnvironment env, FormElements elements, ArrayDeque attributePath) { + MetaAttribute lastAttribute = attributePath.getLast(); + MetaType type = lastAttribute.getType(); + if (isIgnored(attributePath)) { + return; + } + + String label = PresentationBuilderUtils.getLabel(attributePath); + + if (type instanceof MetaDataObject && !lastAttribute.isAssociation()) { + for (MetaAttribute nestedAttribute : type.asDataObject().getAttributes()) { + if (!attributePath.contains(nestedAttribute)) { + ArrayDeque nestedPath = new ArrayDeque(); + nestedPath.addAll(attributePath); + nestedPath.add(nestedAttribute); + buildElement(env, elements, nestedPath); + } + } + } + else { + PresentationEnvironment elementEnv = env.clone(); + elementEnv.setAttributePath(attributePath); + elementEnv.setAcceptedTypes(Arrays.asList(PresentationType.FORM_ELEMENT, PresentationType.DISPLAY)); + elementEnv.setType(type); + PresentationElement element = env.createElement(elementEnv); + + PathSpec pathSpec = PathSpec.of(attributePath.stream().map(it -> it.getName()).collect(Collectors.toList())); + + //String valuePath = PresentationBuilderUtils.getValuePath(attributePath); + String id = pathSpec.toString(); //valuePath.join(valuePath, '.'); + + FormElement formElement = new FormElement(); + formElement.setId(id); + formElement.setLabel(label); + formElement.setAttributePath(pathSpec); + formElement.setEditable(env.isEditable()); + formElement.setComponent(element); + //column.setEditComponent(); + // column.setFilter + // column.setWidth + // column.setTyleClass + elements.add(formElement); + } + } + + private boolean isIgnored(ArrayDeque attributePath) { + return attributePath.getLast().isVersion(); + } + +} diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultPlainTextElementFactory.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultPlainTextElementFactory.java new file mode 100644 index 000000000..1cdf5da19 --- /dev/null +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultPlainTextElementFactory.java @@ -0,0 +1,40 @@ +package io.crnk.ui.presentation.factory; + +import io.crnk.meta.model.MetaPrimitiveType; +import io.crnk.meta.model.MetaType; +import io.crnk.ui.presentation.PresentationEnvironment; +import io.crnk.ui.presentation.PresentationType; +import io.crnk.ui.presentation.element.PlainTextElement; +import io.crnk.ui.presentation.element.PresentationElement; + +public class DefaultPlainTextElementFactory implements PresentationElementFactory { + + @Override + public boolean accepts(PresentationEnvironment env) { + return env.getType() != null; + } + + @Override + public PresentationElement create(PresentationEnvironment env) { + MetaType type = env.getType(); + String id = type.getId(); + boolean isNumber = MetaPrimitiveType.ID_SHORT.equals(id) || + MetaPrimitiveType.ID_BYTE.equals(id) || + MetaPrimitiveType.ID_INT.equals(id) || + MetaPrimitiveType.ID_LONG.equals(id); + boolean isCell = env.getAcceptedTypes().contains(PresentationType.CELL); + + PlainTextElement element = new PlainTextElement(); + element.setComponentId("display"); + + if (isNumber) { + element.getStyle().put("text-align", "right"); + } + if (isCell) { + element.getStyle().put("white-space", "nowrap"); + element.getStyle().put("overflow", "hidden"); + element.getStyle().put("text-overflow", "ellipsis"); + } + return element; + } +} diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultTableElementFactory.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultTableElementFactory.java index 31dff2aad..601f6667a 100644 --- a/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultTableElementFactory.java +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/factory/DefaultTableElementFactory.java @@ -1,5 +1,9 @@ package io.crnk.ui.presentation.factory; +import java.util.ArrayDeque; +import java.util.Arrays; +import java.util.stream.Collectors; + import io.crnk.core.queryspec.PathSpec; import io.crnk.meta.model.MetaAttribute; import io.crnk.meta.model.MetaDataObject; @@ -12,92 +16,87 @@ import io.crnk.ui.presentation.element.TableColumnElement; import io.crnk.ui.presentation.element.TableColumnsElement; -import java.util.ArrayDeque; -import java.util.Arrays; -import java.util.stream.Collectors; - public class DefaultTableElementFactory implements PresentationElementFactory { - @Override - public boolean accepts(PresentationEnvironment env) { - return false; - } - - @Override - public DataTableElement create(PresentationEnvironment env) { - boolean displayAssociation = env.getAttributePath() != null; - boolean sortingEnabled = !displayAssociation; - boolean filtersEnabled = !displayAssociation; - boolean pagingEnabled = !displayAssociation; - - MetaResource resource = (MetaResource) env.getElement(); - String resourceType = resource.getResourceType(); - - DataTableElement table = new DataTableElement(); - table.setEditable(env.isEditable()); - table.getPagination().setEnabled(true); - - for (MetaAttribute attribute : resource.getAttributes()) { - buildColumn(env, table.getColumns(), resource, filtersEnabled, sortingEnabled, new ArrayDeque(Arrays.asList(attribute))); - } - return table; - } - - private void buildColumn(PresentationEnvironment env, TableColumnsElement columns, MetaResource resource, boolean filtersEnabled, boolean sortingEnabled, ArrayDeque attributePath) { - MetaAttribute lastAttribute = attributePath.getLast(); - MetaType type = lastAttribute.getType(); - if (isIgnored(attributePath) || type.isCollection()) { - return; - } - - String label = PresentationBuilderUtils.getLabel(attributePath); - - if (type instanceof MetaDataObject && !lastAttribute.isAssociation()) { - for (MetaAttribute nestedAttribute : type.asDataObject().getAttributes()) { - if (!attributePath.contains(nestedAttribute)) { - ArrayDeque nestedPath = new ArrayDeque(); - nestedPath.addAll(attributePath); - nestedPath.add(nestedAttribute); - buildColumn(env, columns, resource, filtersEnabled, sortingEnabled, nestedPath); - } - } - } else { - - PresentationEnvironment cellEnv = new PresentationEnvironment(); - cellEnv.setEditable(false); - cellEnv.setAttributePath(attributePath); - cellEnv.setAcceptedTypes(Arrays.asList(PresentationType.CELL, PresentationType.DISPLAY)); - cellEnv.setType(type); - cellEnv.setElement(resource); - - PresentationElement cellElement = env.createElement(cellEnv); - - - boolean sortable = sortingEnabled && lastAttribute.isSortable(); - PathSpec pathSpec = PathSpec.of(attributePath.stream().map(it -> it.getName()).collect(Collectors.toList())); - - //String valuePath = PresentationBuilderUtils.getValuePath(attributePath); - String id = pathSpec.toString(); //valuePath.join(valuePath, '.'); - - TableColumnElement column = new TableColumnElement(); - column.setId(id); - column.setLabel(label); - column.setAttributePath(pathSpec); - column.setEditable(env.isEditable()); - column.setComponent(cellElement); - //column.setEditComponent(); - // column.setFilter - column.setSortable(sortable); - // column.setWidth - // column.setTyleClass - columns.add(column); - } - - - } - - private boolean isIgnored(ArrayDeque attributePath) { - return attributePath.getLast().isVersion(); - } + @Override + public boolean accepts(PresentationEnvironment env) { + return env.getAcceptedTypes().contains(PresentationType.TABLE); + } + + @Override + public DataTableElement create(PresentationEnvironment env) { + boolean displayAssociation = env.getAttributePath() != null; + boolean sortingEnabled = !displayAssociation; + boolean filtersEnabled = !displayAssociation; + boolean pagingEnabled = !displayAssociation; + + MetaResource resource = (MetaResource) env.getElement(); + String resourceType = resource.getResourceType(); + + DataTableElement table = new DataTableElement(); + table.setEditable(env.isEditable()); + table.getPagination().setEnabled(true); + + for (MetaAttribute attribute : resource.getAttributes()) { + buildColumn(env, table.getColumns(), resource, filtersEnabled, sortingEnabled, new ArrayDeque(Arrays.asList(attribute))); + } + return table; + } + + private void buildColumn(PresentationEnvironment env, TableColumnsElement columns, MetaResource resource, boolean filtersEnabled, boolean sortingEnabled, + ArrayDeque attributePath) { + MetaAttribute lastAttribute = attributePath.getLast(); + MetaType type = lastAttribute.getType(); + if (isIgnored(attributePath) || type.isCollection()) { + return; + } + + String label = PresentationBuilderUtils.getLabel(attributePath); + + if (type instanceof MetaDataObject && !lastAttribute.isAssociation()) { + for (MetaAttribute nestedAttribute : type.asDataObject().getAttributes()) { + if (!attributePath.contains(nestedAttribute)) { + ArrayDeque nestedPath = new ArrayDeque(); + nestedPath.addAll(attributePath); + nestedPath.add(nestedAttribute); + buildColumn(env, columns, resource, filtersEnabled, sortingEnabled, nestedPath); + } + } + } + else { + PresentationEnvironment cellEnv = env.clone(); + cellEnv.setAttributePath(attributePath); + cellEnv.setAcceptedTypes(Arrays.asList(PresentationType.CELL, PresentationType.DISPLAY)); + cellEnv.setType(type); + + PresentationElement cellElement = env.createElement(cellEnv); + + + boolean sortable = sortingEnabled && lastAttribute.isSortable(); + PathSpec pathSpec = PathSpec.of(attributePath.stream().map(it -> it.getName()).collect(Collectors.toList())); + + //String valuePath = PresentationBuilderUtils.getValuePath(attributePath); + String id = pathSpec.toString(); //valuePath.join(valuePath, '.'); + + TableColumnElement column = new TableColumnElement(); + column.setId(id); + column.setLabel(label); + column.setAttributePath(pathSpec); + column.setEditable(env.isEditable()); + column.setComponent(cellElement); + //column.setEditComponent(); + // column.setFilter + column.setSortable(sortable); + // column.setWidth + // column.setTyleClass + columns.add(column); + } + + + } + + private boolean isIgnored(ArrayDeque attributePath) { + return attributePath.getLast().isVersion(); + } } diff --git a/crnk-ui/src/main/java/io/crnk/ui/presentation/repository/EditorRepository.java b/crnk-ui/src/main/java/io/crnk/ui/presentation/repository/EditorRepository.java new file mode 100644 index 000000000..c176b5e05 --- /dev/null +++ b/crnk-ui/src/main/java/io/crnk/ui/presentation/repository/EditorRepository.java @@ -0,0 +1,27 @@ +package io.crnk.ui.presentation.repository; + +import io.crnk.core.queryspec.QuerySpec; +import io.crnk.core.repository.ResourceRepositoryBase; +import io.crnk.core.resource.list.ResourceList; +import io.crnk.ui.presentation.PresentationManager; +import io.crnk.ui.presentation.element.EditorElement; + +public class EditorRepository extends ResourceRepositoryBase { + + private final PresentationManager manager; + + public EditorRepository(PresentationManager manager) { + super(EditorElement.class); + this.manager = manager; + } + + @Override + public EditorElement findOne(String id, QuerySpec querySpec) { + return manager.getEditor(id); + } + + @Override + public ResourceList findAll(QuerySpec querySpec) { + return querySpec.apply(manager.getEditors().values()); + } +} diff --git a/crnk-ui/src/test/java/io/crnk/ui/EditorRepositoryTest.java b/crnk-ui/src/test/java/io/crnk/ui/EditorRepositoryTest.java new file mode 100644 index 000000000..7b616241e --- /dev/null +++ b/crnk-ui/src/test/java/io/crnk/ui/EditorRepositoryTest.java @@ -0,0 +1,64 @@ +package io.crnk.ui; + + +import io.crnk.core.boot.CrnkBoot; +import io.crnk.core.queryspec.PathSpec; +import io.crnk.core.queryspec.QuerySpec; +import io.crnk.core.resource.list.ResourceList; +import io.crnk.test.mock.TestModule; +import io.crnk.ui.presentation.element.EditorElement; +import io.crnk.ui.presentation.element.FormContainerElement; +import io.crnk.ui.presentation.element.FormElement; +import io.crnk.ui.presentation.element.FormElements; +import io.crnk.ui.presentation.element.PlainTextElement; +import io.crnk.ui.presentation.repository.EditorRepository; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + +public class EditorRepositoryTest { + + + private UIModule uiModule; + + @Before + public void setup() { + uiModule = UIModule.create(new UIModuleConfig()); + CrnkBoot boot = new CrnkBoot(); + boot.addModule(uiModule); + boot.addModule(new TestModule()); + boot.boot(); + + } + + @Test + public void checkFindAll() { + EditorRepository repository = uiModule.getEditorRepository(); + ResourceList editors = repository.findAll(new QuerySpec(EditorElement.class)); + Assert.assertNotNull(editors); + Assert.assertTrue(editors.size() > 0); + } + + + @Test + public void checkFindOne() { + EditorRepository repository = uiModule.getEditorRepository(); + EditorElement editor = repository.findOne("local-resources.tasks", new QuerySpec(EditorElement.class)); + Assert.assertNotNull(editor); + + Assert.assertEquals("tasks", editor.getBaseQuery().getResourceType()); + + FormContainerElement form = editor.getForm(); + + FormElements elements = form.getElements(); + Assert.assertNotEquals(0, elements.getElementIds().size()); + + FormElement idFormElement = elements.getElements().get("id"); + Assert.assertEquals("id", idFormElement.getId()); + Assert.assertEquals("id", idFormElement.getLabel()); + Assert.assertFalse(idFormElement.isEditable()); + Assert.assertEquals(PathSpec.of("id"), idFormElement.getAttributePath()); + PlainTextElement idComponent = (PlainTextElement) idFormElement.getComponent(); + Assert.assertEquals("right", idComponent.getStyle().get("text-align")); + } +} diff --git a/crnk-ui/src/test/java/io/crnk/ui/ExplorerRepositoryTest.java b/crnk-ui/src/test/java/io/crnk/ui/ExplorerRepositoryTest.java index 3d908f20c..f40a93d05 100644 --- a/crnk-ui/src/test/java/io/crnk/ui/ExplorerRepositoryTest.java +++ b/crnk-ui/src/test/java/io/crnk/ui/ExplorerRepositoryTest.java @@ -8,6 +8,7 @@ import io.crnk.test.mock.TestModule; import io.crnk.ui.presentation.element.DataTableElement; import io.crnk.ui.presentation.element.ExplorerElement; +import io.crnk.ui.presentation.element.PlainTextElement; import io.crnk.ui.presentation.element.TableColumnElement; import io.crnk.ui.presentation.element.TableColumnsElement; import io.crnk.ui.presentation.repository.ExplorerRepository; @@ -18,46 +19,52 @@ public class ExplorerRepositoryTest { - private UIModule uiModule; + private UIModule uiModule; - @Before - public void setup() { - uiModule = UIModule.create(new UIModuleConfig()); - CrnkBoot boot = new CrnkBoot(); - boot.addModule(uiModule); - boot.addModule(new TestModule()); - boot.boot(); + @Before + public void setup() { + uiModule = UIModule.create(new UIModuleConfig()); + CrnkBoot boot = new CrnkBoot(); + boot.addModule(uiModule); + boot.addModule(new TestModule()); + boot.boot(); - } + } - @Test - public void checkFindAll() { - ExplorerRepository repository = uiModule.getExplorerRepository(); - ResourceList explorers = repository.findAll(new QuerySpec(ExplorerElement.class)); - Assert.assertNotNull(explorers); - Assert.assertTrue(explorers.size() > 0); - } + @Test + public void checkFindAll() { + ExplorerRepository repository = uiModule.getExplorerRepository(); + ResourceList explorers = repository.findAll(new QuerySpec(ExplorerElement.class)); + Assert.assertNotNull(explorers); + Assert.assertTrue(explorers.size() > 0); + } - @Test - public void checkFindOne() { - ExplorerRepository repository = uiModule.getExplorerRepository(); - ExplorerElement explorer = repository.findOne("local-resources.tasks", new QuerySpec(ExplorerElement.class)); - Assert.assertNotNull(explorer); + @Test + public void checkFindOne() { + ExplorerRepository repository = uiModule.getExplorerRepository(); + ExplorerElement explorer = repository.findOne("local-resources.tasks", new QuerySpec(ExplorerElement.class)); + Assert.assertNotNull(explorer); - Assert.assertEquals("tasks", explorer.getBaseQuery().getResourceType()); + Assert.assertEquals("tasks", explorer.getBaseQuery().getResourceType()); - DataTableElement table = explorer.getTable(); + DataTableElement table = explorer.getTable(); - TableColumnsElement columns = table.getColumns(); - Assert.assertNotEquals(0, columns.getElementIds().size()); + TableColumnsElement columns = table.getColumns(); + Assert.assertNotEquals(0, columns.getElementIds().size()); - TableColumnElement idColumn = columns.getElements().get("id"); - Assert.assertEquals("id", idColumn.getId()); - Assert.assertEquals("id", idColumn.getLabel()); - Assert.assertFalse(idColumn.isEditable()); - Assert.assertTrue(idColumn.isSortable()); - Assert.assertEquals(PathSpec.of("id"), idColumn.getAttributePath()); - Assert.assertEquals("display", idColumn.getComponent().getComponentId()); - } + TableColumnElement idColumn = columns.getElements().get("id"); + Assert.assertEquals("id", idColumn.getId()); + Assert.assertEquals("id", idColumn.getLabel()); + Assert.assertFalse(idColumn.isEditable()); + Assert.assertTrue(idColumn.isSortable()); + Assert.assertEquals(PathSpec.of("id"), idColumn.getAttributePath()); + Assert.assertEquals("display", idColumn.getComponent().getComponentId()); + + PlainTextElement idComponent = (PlainTextElement) idColumn.getComponent(); + Assert.assertEquals("right", idComponent.getStyle().get("text-align")); + Assert.assertEquals("hidden", idComponent.getStyle().get("overflow")); + Assert.assertEquals("ellipsis", idComponent.getStyle().get("text-overflow")); + Assert.assertEquals("nowrap", idComponent.getStyle().get("white-space")); + } }