Generate internal link to entity

Hi,
I have table with list of some events. And I would like to create link to another entity in each row. There is a column with link/url in a table. How to generate internal link based on UUID or unique entity name? I’m trying to create link when event(row) is adding to table. So it is a service layer and I can’t use screen links or routing API.
What is the best practice to do it?

Hello @uladzislau_chabatkou,

I can suggest you to use the RouteGenerator and the generated column in the table.

Example:

The Car entity has a driver field with Many-to-One association with the Driver entity.

  1. Add @Route annotation to DriverEdit
@Route("driver")
@UiController("sample_Driver.edit")
@UiDescriptor("driver-edit.xml")
@EditedEntityContainer("driverDc")
@LoadDataBeforeShow
public class DriverEdit extends StandardEditor<Driver> {
}
  1. Add generated column to the Table in CarBrowse screen
@UiController("sample_Car.browse")
@UiDescriptor("car-browse.xml")
@LookupComponent("carsTable")
@LoadDataBeforeShow
public class CarBrowse extends StandardLookup<Car> {

    @Inject
    protected UiComponents uiComponents;
    @Inject
    protected UrlRouting urlRouting;
    @Inject
    protected MetadataTools metadataTools;

    @Install(to = "carsTable.driver", subject = "columnGenerator")
    protected Component carsTableDriverColumnGenerator(Car car) {
        Driver driver = car.getDriver();
        if (driver != null) {
            Link link = uiComponents.create(Link.NAME);
            link.setCaption(metadataTools.getInstanceName(driver));
            link.setUrl(urlRouting.getRouteGenerator().getEditorRoute(driver));
            return link;
        } else {
            return new Table.PlainTextCell("");
        }
    }
}

Result


Generated route for a Driver entity named Jake - http://localhost:8080/app/#main/0/driver?id=4w0fqas3ghzeckrv82pptnzjt3.

Regards,
Gleb

1 Like