Custom Validation in Edit Screen Controller - Error

Hello community,

I’m new to Java and CUBA Studio, and have some requirements that I don’t seem to be able to find the answers to.

Job Screen Overview:
There is a selector called ‘Job Type’ that determines which of a bunch of ‘Assignment’ fields is visible to the user. There is also a Composition table called ‘Notes’ which contains note records that each include a noteCompletedBy selector and a noteCompletedDate. These two fields are filled in once a note has been received and followed up on.

Goal:
I need custom validation in the Java Controller to check these fields are filled in with data prior to saving the data if the user has marked the jobStatus attribute as ‘Complete’.

Work So Far:
I’ve subscribed to onBeforeCommitChanges in order to run the validation, and here is the code as it stands.

JobEdit.java (25.8 KB)

I’ve started in the onBeforeCommitChanges method, which checks if the jobStatus is Closed, determines the jobType, and in this case, because it’s the only method of the jobType selection I’ve written so far, run the checkValuationsJobAssignmentFieldsNotNull method. This method checks the various Assignment fields for this type, and then runs the checkNotesFieldsNull method.

And finally, the error and stacktrace is here:

DevelopmentException: Target com.haulmont.cuba.web.gui.components.WebLookupField does not support event type class com.haulmont.cuba.gui.screen.StandardEditor$BeforeCommitChangesEvent

com.haulmont.cuba.core.global.DevelopmentException: Target com.haulmont.cuba.web.gui.components.WebLookupField does not support event type class com.haulmont.cuba.gui.screen.StandardEditor$BeforeCommitChangesEvent
at com.haulmont.cuba.gui.sys.UiControllerDependencyInjector.initSubscribeListeners(UiControllerDependencyInjector.java:372)
at com.haulmont.cuba.gui.sys.UiControllerDependencyInjector.inject(UiControllerDependencyInjector.java:114)
at com.haulmont.cuba.web.sys.WebScreens.createScreen(WebScreens.java:241)
at com.haulmont.cuba.web.sys.WebScreens.create(WebScreens.java:175)
at com.haulmont.cuba.gui.builders.EditorBuilderProcessor.createScreen(EditorBuilderProcessor.java:287)
at com.haulmont.cuba.gui.builders.EditorBuilderProcessor.buildEditor(EditorBuilderProcessor.java:89)
at com.haulmont.cuba.gui.builders.EditorBuilder.build(EditorBuilder.java:359)
at com.haulmont.cuba.gui.actions.list.CreateAction.actionPerform(CreateAction.java:110)
at com.haulmont.cuba.web.gui.components.WebButton.buttonClicked(WebButton.java:64)
at com.haulmont.cuba.web.widgets.CubaButton.fireClick(CubaButton.java:76)
at com.vaadin.ui.Button$1.click(Button.java:57)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
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:1577)
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:107)
at org.springframework.web.filter.CompositeFilter.doFilter(CompositeFilter.java:73)
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:200)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
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:836)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1747)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.base/java.lang.Thread.run(Thread.java:834)

That’s where I’m at right now. Thank you in advance for your help!

Hi @adam5sbass!

To subscribe to BeforeCommitChangesEvent, you don’t need to specify a @Subscribe annotation value (i.e. @Subscribe("jobStatusField")). If you want to prevent a commit, you need to call event.preventCommit() function.
I can advise you to change the signature of the verification methods so that functions return boolean values. When the check fails, you prevent the commit.

Example of a modified onBeforeCommitChanges function:

    @Subscribe
    private void onBeforeCommitChanges(BeforeCommitChangesEvent event) {
        if (jobStatusField.getValue() == JobStatusEnum.CLOSED) {
            Enum<JobTypeEnum> jobType = jobDc.getItem().getJobType();
            if (jobType != null) {
                boolean commitChanges;
                switch (jobType) {
                    case JobTypeEnum.VALUATIONS:
                        commitChanges = checkValuationsJobAssignmentFieldsNotNull();
                    case JobTypeEnum.OTHER:
                        commitChanges = checkOtherJobAssignmentFieldsNotNull();
                    case JobTypeEnum.FINANCIALS:
                        commitChanges = checkFinancialsJobAssignmentFieldsNotNull();
                    case JobTypeEnum.TAX:
                        commitChanges = checkTaxJobAssignmentFieldsNotNull();
                }
                if (!commitChanges) {
                    event.preventCommit();
                }
            }
        }
    }

Regards,
Gleb

1 Like

Thank you for this! I’m going to test it in a little bit.