Screen data migration from v 6

Hi Cuba team
What is the most easy way to convert data source to data containers when a legacy screen is migrated to the new platform?

By hand.

Unless there is a specific reason to convert to data containers, you might consider leaving it as it is.

But here are a few things to remember:

Remove the existing data sources from your screen’s xml file first, but make a note of all the SQL that they’re using. It’ll save you having to think how to write them again.

In your screen’s xml file, change the definition from

xmlns="http://schemas.haulmont.com/cuba/window.xsd"

to

xmlns="http://schemas.haulmont.com/cuba/screen/window.xsd"

Once you have changed the xmlns definition then the editor will offer you the right components to add data containers and data loaders, but it is better to add them by hand if you can.

Make sure that you’re inheriting your screen’s Java file from one of the new Version 7.0 classes; StandardEditor for example.

Remove the old screen entry details from web-screens.xml

Remember that version 7+ does not support declarative dependencies on screen lookups, so you will have to implement this yourself, which in itself is a good reason to think about staying put. Having said that, there are a couple of pages that shows you how to add the same functionality to your own code:

https://doc.cuba-platform.com/manual-7.0/gui_data_comp_dep.html
https://doc.cuba-platform.com/manual-7.0/screen_mixins.html

If you use the mixing method then you need to make a bit of a change:

String query = loader.getQuery();

After executing this line, check that the returned string query isn’t null before you try to do anything with it. Not all data loaders have a query attached; those that don’t will cause the code to crash out.

Here’s my take, which was abandoned, so not properly tested:

package com.willowpark.peer.web.mixins.parameterloader;

import com.haulmont.cuba.gui.model.*;
import com.haulmont.cuba.gui.screen.*;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public interface DeclarativeLoaderParameters {

    Pattern CONTAINER_REF_PATTERN = Pattern.compile(":(container\\$(\\w+))");

    @Subscribe
    default void onDeclarativeLoaderParametersBeforeShow(Screen.BeforeShowEvent event) {
        Screen screen = event.getSource();
        ScreenData screenData = UiControllerUtils.getScreenData(screen);
        
        for (String loaderId : screenData.getLoaderIds()) {
            DataLoader loader = screenData.getLoader(loaderId);
            String query = loader.getQuery();

            if (query != null) { // <---- DON'T FORGET THIS. 
                Matcher matcher = CONTAINER_REF_PATTERN.matcher(query);
                while (matcher.find()) {
                    String paramName = matcher.group(1);
                    String containerId = matcher.group(2);
                    InstanceContainer<?> container = screenData.getContainer(containerId);
                    container.addItemChangeListener(itemChangeEvent -> {
                        loader.setParameter(paramName, itemChangeEvent.getItem());

                    });

                }
            }

            loader.load();
        }

    }

}
3 Likes

Hi
Thanks.
That looks pretty complicated and lengthy!! I’m wondering if there is any alternatives that Cuba team may think of!!

We live in hope :neutral_face:

1 Like

We will definitely provide a mechanism of linking data components without the need to define event handlers in the screen, there is a ticket for this.

Please notice that we have made a lot of efforts to keep your entire codebase working without any changes on the new version of the framework. At the same time you can create new screens on the new API and invoke them from the old screens and vice versa. Migration to the new API is completely optional, it makes sense only if you use a lot of frames and need to share them between old and new screens.

Hi Konstantin
I fully endorse the amount of efforts that the whole CUBA team has been deploying to ease the upgrade in order to simplify it for the users. I do sometimes say the same to others. Though there are still many bugs existing and it has been a pain for some of us using this new version for active development, I believe the CUBA team will continuously fix and enhance them to bring it solid like how it was in legacy versions from the stability point of view. Thank you again for bring such a great tool.