Coder Social home page Coder Social logo

mariodavid / cuba-component-default-values Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 1.0 4.72 MB

CUBA component that lets you assign default values for entities

License: Apache License 2.0

Java 63.90% Groovy 31.62% Shell 4.49%
cuba-platform cuba-component default-values

cuba-component-default-values's Introduction

Build Status Download license

CUBA Component - Default Values

This application component lets you configure all kind of default values for entity attributes at runtime for better & faster UX... Let your application be the butler of your users - not the other way around!

Customer Editor with default values

Table of Contents

Installation

  1. default-values is available in the CUBA marketplace
  2. Select a version of the add-on which is compatible with the platform version used in your project:
Platform Version Add-on Version
7.2.x 0.2.x
7.1.x 0.1.x

The latest version is: Download

Add custom application component to your project:

  • Artifact group: de.diedavids.cuba.defaultvalues
  • Artifact name: defaultvalues-global
  • Version: add-on version
dependencies {
  appComponent("de.diedavids.cuba.defaultvalues:defaultvalues-global:*addon-version*")
}

CHANGELOG

Information on changes that happen through the different versions of the application component can be found in the CHANGELOG. The Changelog also contains information about breaking changes and tips on how to resolve them.

Supported DBMS

The following databases are supported by this application component:

  • HSQLDB
  • PostgreSQL
  • MySQL

All other DBMS systems are also possible to work with by the fact that CUBA studio generates the corresponding init / update scripts within the application.

Using the application component

Example usage

To see this application component in action, check out this example: cuba-example-using-default-values. It contains all examples of all the screenshots.

Creation of Entities with configured Default Values

Customer Editor with default values

Order Editor with default values

Default Value Configuration

Default Values can be configured for entities via Administration > Entity Default Values. It contains a list of all Entities that are registered in the application.

List of all Default Value Entities

Selecting one Entity opens the Details screen that shows the all attributes of the entity that fulfill this criteria:

  • attribute is persistent
  • attribute is not annotated with @SystemLevel or are part of System level Interfaces (like createTs e.g.)
  • attribute is not a 1:N or M:N association

Default Value Types

There are multiple types of default values that can be configured for an entity attribute.

default value type selection

Static Value

A static value is used when a globally pre-configured value should be used for an entity attribute as the default value.

default value - static value edit

Examples:

  • always set customer type to REGULAR
  • always set the Group association of a User to Employees Group

Dynamic Value

Dynamic values compared to static values are evaluated at the time when the instance is created. Those values are dependent on the environment.

Examples:

  • current date for the attribute orderDate of an order
  • current user for a association from a customer to the User via the attribute accountManager
  • locale of the current user for an attribute with a locale type

default value - dynamic value

There are different dynamic value options for different datatypes. The application component itself defines the following dynamic value options:

  • User
    • current user
    • current / substituted user
  • Date
    • Today
  • LocalDate
    • Yesterday
    • Today
    • Tomorrow

In case the datatype of the entity attribute has no options for dynamic values, it is not possible to select this option.

Custom Dynamic Values

It is also possible to create application specific dynamic value options. The application component defines an interface DynamicValueProvider that has to be implemented by a custom dynamic value.

The new dynamic value option needs to be a Spring bean in the global module of the application.

An example can be found in the example application: CustomerTypeFavoriteValueProvider

package com.company.ceudv;

import com.company.ceudv.entity.CustomerType;
import de.diedavids.cuba.defaultvalues.dynamicvalue.DynamicValueProvider;
import org.springframework.stereotype.Component;

@Component("ceudv_CustomerTypeFavoriteValueProvider")
public class CustomerTypeFavoriteValueProvider implements DynamicValueProvider<CustomerType> {

    @Override
    public String getCode() {
        return "customerTypeFavorite";
    }

    @Override
    public Class<CustomerType> getReturnType() {
        return CustomerType.class;
    }

    @Override
    public CustomerType get() {
        return CustomerType.favorite();
    }
}

The interface requires the following methods to be implemented:

  • getCode - return a unique code / name of this dynamic value provider
  • getReturnType - the type that this dynamic value is applicable for
  • get - execution of the logic to return the default value

Additionally a translation value has be set in the main message pack of the web module:

dynamicValueProvider.customerTypeFavorite = Favorite Customer Type
  • dynamicValueProvider. - prefix for translation of dynamic value providers
  • customerTypeFavorite - the code of the dynamic value provider

With that, the dynamic value appears in the default value configuration UI and can be selected for all entity attributes that have the datatype CustomerType.

Session Attribute

A session attribute can also be placed into a entity attribute as a default value.

Examples:

  • Preferred City for a new Customer based on the Access group the current user is in
  • Default Group for a new User defined by the current local admin in multi tenant application

Session attributes are a good option, if user specific options should be leveraged when assigning a default value.

The session attribute concept of CUBA integrates the whole user group hierarchy into the value that is placed into the session.

More information on session attributes can be found in the CUBA docs: Session attributes.

NOTE: in the configuration UI the datatypes of the session attributes are not checked against the datatype of the entity attribute. If the datatypes do not match, the value will not be bound.

Another usage of a session attribute based default value is when a session attribute is programmatically assigned before the new entity is created.

For example it is possible to use this variant if a user globally for its current user session (until next logout) defines that (s)he now works on the asian area of the business. This would e.g. set the default country to China in the session. With a configured session attribute default value for a new customers country is now China.

Script

The last option is a script based default value. This option allows to define a groovy script, that programmatically determines the default value at the time when a new entity instance is created.

This option gives the most flexibility of all options and with a script all above mentioned options can also be achieved.

That being said, it requires the ability to write code in order to leverage that possibility.

The following attribute are available in the script:

  • dataManager - DataManager instance for Database access from CUBA (com.haulmont.cuba.core.global.DataManager)
  • beanLocator - Provides access to all managed beans from CUBA (com.haulmont.cuba.core.global.BeanLocator)
  • timeSource - Global time source interface from CUBA (com.haulmont.cuba.core.global.TimeSource)

NOTE: The return type has to match the datatype of the entity attribute. If no value is returned, the Groovy evaluator will treat that execution as false

cuba-component-default-values's People

Contributors

mariodavid avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

sergeev-ms

cuba-component-default-values's Issues

run error

java.lang.NoSuchMethodError: com.haulmont.cuba.core.global.Metadata.create(Ljava/lang/Class;)Ljava/lang/Object;
at de.diedavids.cuba.defaultvalues.web.screens.entityattributedefaultvalue.EntityAttributeDefaultValueBrowse.createMetaClassEntity(EntityAttributeDefaultValueBrowse.java:55)
at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193)
at java.util.Iterator.forEachRemaining(Iterator.java:116)
at java.util.Spliterators$IteratorSpliterator.forEachRemaining(Spliterators.java:1801)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at de.diedavids.cuba.defaultvalues.web.screens.entityattributedefaultvalue.EntityAttributeDefaultValueBrowse.loadEntities(EntityAttributeDefaultValueBrowse.java:50)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.haulmont.cuba.gui.sys.delegates.InstalledFunction.apply(InstalledFunction.java:26)
at com.haulmont.cuba.gui.model.impl.CollectionLoaderImpl.load(CollectionLoaderImpl.java:109)
at com.haulmont.cuba.gui.model.impl.ScreenDataImpl.loadAll(ScreenDataImpl.java:80)
at com.haulmont.cuba.gui.model.impl.AnnotationBasedLoaderBeforeShow.loadData(AnnotationBasedLoaderBeforeShow.java:36)
at com.haulmont.cuba.web.sys.WebScreens.loadDataBeforeShow(WebScreens.java:554)
at com.haulmont.cuba.web.sys.WebScreens.show(WebScreens.java:446)
at com.haulmont.cuba.web.sys.WebScreens.showFromNavigation(WebScreens.java:550)
at com.haulmont.cuba.gui.config.MenuItemCommands$ScreenCommand.run(MenuItemCommands.java:338)
at com.haulmont.cuba.web.sys.SideMenuBuilder$MenuCommandExecutor.accept(SideMenuBuilder.java:264)
at com.haulmont.cuba.web.sys.SideMenuBuilder$MenuCommandExecutor.accept(SideMenuBuilder.java:249)
at com.haulmont.cuba.web.gui.components.mainwindow.WebSideMenu$MenuItemImpl.menuSelected(WebSideMenu.java:576)
at com.haulmont.cuba.web.widgets.CubaSideMenu$1.menuItemTriggered(CubaSideMenu.java:90)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:153)
at com.vaadin.server.ServerRpcManager.applyInvocation(ServerRpcManager.java:115)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocation(ServerRpcHandler.java:431)
at com.vaadin.server.communication.ServerRpcHandler.handleInvocations(ServerRpcHandler.java:396)
at com.vaadin.server.communication.ServerRpcHandler.handleRpc(ServerRpcHandler.java:260)
at com.vaadin.server.communication.UidlRequestHandler.synchronizedHandleRequest(UidlRequestHandler.java:82)
at com.vaadin.server.SynchronizedRequestHandler.handleRequest(SynchronizedRequestHandler.java:40)
at com.vaadin.server.VaadinService.handleRequest(VaadinService.java:1578)
at com.vaadin.server.VaadinServlet.service(VaadinServlet.java:425)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.serviceAppRequest(CubaApplicationServlet.java:329)
at com.haulmont.cuba.web.sys.CubaApplicationServlet.service(CubaApplicationServlet.java:215)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.springframework.web.filter.CompositeFilter$VirtualFilterChain.doFilter(CompositeFilter.java:108)
at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:74)
at com.haulmont.cuba.web.sys.CubaHttpFilter.doFilter(CubaHttpFilter.java:93)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:526)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:678)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:861)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1579)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)

Transitive dependency (metadataextensions) is not published in main repositories

A problem occurred configuring project ':app-core'.
> Could not resolve all dependencies for configuration ':appComponent'.
   > Could not find de.diedavids.cuba.metadataextensions:metadataextensions-core:0.2.1.
     Searched in the following locations:
       - https://dl.bintray.com/cuba-platform/main/de/diedavids/cuba/metadataextensions/metadataextensions-core/0.2.1/metadataextensions-core-0.2.1.pom
       - https://dl.bintray.com/cuba-platform/main/de/diedavids/cuba/metadataextensions/metadataextensions-core/0.2.1/metadataextensions-core-0.2.1.jar
       - https://jcenter.bintray.com/de/diedavids/cuba/metadataextensions/metadataextensions-core/0.2.1/metadataextensions-core-0.2.1.pom
       - https://jcenter.bintray.com/de/diedavids/cuba/metadataextensions/metadataextensions-core/0.2.1/metadataextensions-core-0.2.1.jar
     Required by:
         project : > de.diedavids.cuba.defaultvalues:defaultvalues-core:0.1.2

Currently requires adding

        maven {
            url  "https://dl.bintray.com/mariodavid/cuba-components"
        }

Which does not happen when installing via marketplace / not documented

Error after uninstalling the addon from the project

Hi,

I'm getting below error after uninstalling the addon.

Caused by: java.lang.IllegalArgumentException: Datatype EntitySoftReference is not found
at com.haulmont.cuba.core.sys.DatatypeRegistryImpl.get(DatatypeRegistryImpl.java:44) ~[cuba-global-7.2.13.jar:7.2.13]
at com.haulmont.cuba.security.app.group.AccessGroupDefinitionsComposerImpl.lambda$composeGroupDefinitionFromDb$2(AccessGroupDefinitionsComposerImpl.java:119) ~[na:na]
at com.haulmont.cuba.core.sys.TransactionImpl.execute(TransactionImpl.java:76) ~[na:na]
at com.haulmont.cuba.core.sys.TransactionImpl.execute(TransactionImpl.java:70) ~[na:na]
at com.haulmont.cuba.core.sys.PersistenceImpl.callInTransaction(PersistenceImpl.java:119) ~[na:na]
at com.haulmont.cuba.security.app.group.AccessGroupDefinitionsComposerImpl.composeGroupDefinitionFromDb(AccessGroupDefinitionsComposerImpl.java:89) ~[na:na]
at com.haulmont.cuba.security.sys.UserSessionManager.compileGroupDefinition(UserSessionManager.java:226) ~[na:na]
at com.haulmont.cuba.security.sys.UserSessionManager.createSession(UserSessionManager.java:154) ~[na:na]
at com.haulmont.cuba.security.auth.providers.AnonymousAuthenticationProvider.createSession(AnonymousAuthenticationProvider.java:108) ~[na:na]
at com.haulmont.cuba.security.auth.providers.AnonymousAuthenticationProvider.authenticate(AnonymousAuthenticationProvider.java:79) ~[na:na]
at com.haulmont.cuba.security.auth.AuthenticationManagerBean.authenticateInternal(AuthenticationManagerBean.java:252) ~[na:na]
... 49 common frames omitted

Seems like while unintall it doesn't clean up propertly.

Thanks,
Hari

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.