Load screen for a single item based on tag

Hi. I want to load a screen which shows a page entity in a certain layout, mainly with some HTML layouting and no editing. I have created a screen with an HTML layout and some labels (with HTML support) that would show the page entity parameters.

I am now having trouble loading the screen and having a specific page entity loaded based on a tag (that is a field of the page entity). I am passing the tag as a windows parameter but on the init routine I am not able to load the appropriate page entity.

This is the definition of the page entity:


package com.axxemble.base27.entity;

import javax.persistence.Entity;
import javax.persistence.Table;
import com.axxemble.base27.page.PageStatus;
import javax.persistence.Column;
import javax.persistence.Lob;
import com.haulmont.cuba.core.entity.StandardEntity;

@Table(name = "BASE_PAGE")
@Entity(name = "base$Page")
public class Page extends StandardEntity {
    private static final long serialVersionUID = -1437968217624753094L;

    @Column(name = "PAGE_TITLE", nullable = false)
    protected String pageTitle;

    @Lob
    @Column(name = "PAGE_CONTENT")
    protected String pageContent;

    @Column(name = "PAGE_STATUS", nullable = false)
    protected Integer pageStatus;

    @Column(name = "PAGE_TAG", nullable = false, length = 32)
    protected String pageTag;

    public void setPageTag(String pageTag) {
        this.pageTag = pageTag;
    }

    public String getPageTag() {
        return pageTag;
    }


    public void setPageTitle(String pageTitle) {
        this.pageTitle = pageTitle;
    }

    public String getPageTitle() {
        return pageTitle;
    }

    public void setPageContent(String pageContent) {
        this.pageContent = pageContent;
    }

    public String getPageContent() {
        return pageContent;
    }

    public void setPageStatus(PageStatus pageStatus) {
        this.pageStatus = pageStatus == null ? null : pageStatus.getId();
    }

    public PageStatus getPageStatus() {
        return pageStatus == null ? null : PageStatus.fromId(pageStatus);
    }


}

This is the screen XML definition:


<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<window xmlns="http://schemas.haulmont.com/cuba/window.xsd"
        caption="msg://Title"
        class="com.axxemble.base27.web.page.PageShow"
        datasource="pageDs"
        messagesPack="com.axxemble.base27.web.page">
    <dsContext>
        <datasource id="pageDs"
                    class="com.axxemble.base27.entity.Page"
                    view="_local"/>
    </dsContext>
    <dialogMode height="600"
                width="800"/>
    <layout spacing="true"
            stylename="showPageLayout">
        <vbox>
            <hbox expand="spacer"
                  height="46px"
                  spacing="true"
                  width="100%">
                <label id="spacer"/>
                <buttonsPanel id="buttonRow"
                              align="TOP_RIGHT">
                    <button id="btnEdit"
                            caption="msg://Edit"
                            invoke="onBtnSaveClick"/>
                    <button id="btnPrint"
                            caption="msg://Print"
                            invoke="onBtnStartReviewClick"/>
                </buttonsPanel>
            </hbox>
            <htmlBox id="htmlContent"
                     responsive="true"
                     template="pageContent">
                <label id="pageTitle"
                       datasource="pageDs"
                       htmlEnabled="true"
                       property="pageTitle"/>
                <label id="pageContent"
                       datasource="pageDs"
                       htmlEnabled="true"
                       property="pageContent"
                       width="100%"/>
            </htmlBox>
        </vbox>
    </layout>
</window>

This is the controller of this screen:


package com.axxemble.base27.web.page;

import com.haulmont.chile.core.datatypes.impl.EnumClass;
import com.haulmont.cuba.gui.components.AbstractEditor;
import com.axxemble.base27.entity.Page;
import com.axxemble.base27.page.PageStatus;
import com.haulmont.cuba.gui.components.AbstractLookup;
import com.haulmont.cuba.gui.components.Component;
import com.haulmont.cuba.gui.components.Frame;
import com.haulmont.cuba.gui.data.Datasource;
import com.haulmont.cuba.gui.components.RichTextArea;

import com.haulmont.cuba.web.WebWindowManager;

import com.haulmont.cuba.core.entity.Entity;
import com.haulmont.cuba.core.global.*;
import com.haulmont.cuba.gui.WindowManager;
import com.haulmont.cuba.gui.components.*;

import com.haulmont.cuba.gui.WindowParam;
import com.haulmont.cuba.gui.data.CollectionDatasource;

import javax.inject.Inject;
import java.util.Map;
import java.util.UUID;

public class PageShow extends AbstractEditor<Page> {

    @WindowParam
    private String Tag;

    @Inject
    private Datasource<Page> pageDs;

    @Override
    public void init(Map<String, Object> params) {
        if (Tag != "") {
            // Get page for tag
            //pageDs.setQuery( "select * from base$Page where pageTage = :param$Tag" );
        }
    }
    
    public void onBtnPrintClick() {
        // Do some printing routine
    }

    public void onBtnEditClick() {
        // Open window for page editing
    }
}

This is the call that is being made to load the screen (assuming a page entity with the mentioned tag is available:


openWindow("base$Page.show", WindowManager.OpenType.THIS_TAB,
                ParamsMap.of("Tag", "POLICY_SCOPE"));

And finally (although not essential), this is the HTML layout used:


<div class="pagePaper">
<div location="pageTitle" class="pageTitle"></div>
<hr>
<div location="pageContent" class="pageContent"></div>
</div>

Hope you are able to give some hints in how to proceed on this.

Hi Berend,

Single-entity datasources cannot load data by themselves - you should provide an entity instance somehow. In your case, the code might be like this:


public class PageShow extends AbstractEditor<Page> {

    @WindowParam
    private String Tag;

    @Inject
    private Datasource<Page> pageDs;

    @Inject
    private DataManager dm;

    @Override
    public void init(Map<String, Object> params) {
        if (Tag != "") {
            Page page = dm.load(LoadContext.create(Page.class).setQuery(
                LoadContext.createQuery("select p from base$Page p where p.pageTage = :Tag")
                    .setParameter("Tag", Tag)));
            pageDs.setItem(page);
        }
    }

1 Like

Hi Konstantin, this worked 100% - thanx!

Seems to me that this solution doesn’t work in version 6.9.1
Solution is to put the same logic in the postInit method