3.5.13.4. URL Routes Generator

Sometimes, it is necessary to get proper URL that can be sent via email or shown to the user. The simplest way to generate it is by using URL Routes Generator.

URL Routes Generator provides API for URL links generation:

  • Link to the editor of an entity instance.

  • Link to the screen by screen id or screen class.

  • Link to the screen with provided parameters.

The getRouteGenerator() method of UrlRouting bean allows you to get an instance of RouteGenerator. RouteGenerator has the following methods:

  • getRoute(String screenId) – returns a route for screen with given screenId, for example:

    String route = urlRouting.getRouteGenerator().getRoute("demo_Customer.browse");

    The result:

    route = "http://host:port/context/#main/customers"

  • getRoute(Class<? extends Screen> screenClass) – generates a route for screen with the given screenClass, for example:

    String route = urlRouting.getRouteGenerator().getRoute(CustomerBrowse.class);

    The result:

    route = "http://host:port/context/#main/customers"

  • getEditorRoute(Entity entity) – generates a route for default editor with the given entity, for example:

    Customer сustomer = customersTable.getSingleSelected();
    
    String route = urlRouting.getRouteGenerator().getEditorRoute(сustomer);

    The result:

    route == "http://localhost:8080/app/#main/customers/edit?id=5jqtc3pwzx6g6mq1vv5gkyjn0s"

  • getEditorRoute(Entity entity, Class<? extends Screen> screenClass) – generates a route for editor with the given screenClass and entity.

  • getRoute(Class<? extends Screen> screenClass, Map<String, String> urlParams) – generates a route for screen with the given screenClass and urlParams.

URL Routes Generator Example

Suppose that we have a Customer entity with standard screens that have registered routes. Let us add a button to the CustomerBrowse that generates a link to the selected entity with some parameters. This button invokes the generateRoute method:

@Inject
private UrlRouting urlRouting;

@Inject
private GroupTable<Customer> customersTable;

@Inject
private Dialogs dialogs;

public void generateRoute() {
   Customer selectedCustomer = customersTable.getSingleSelected();
   if (selectedCustomer != null) {
      String routeToSelectedRole = urlRouting.getRouteGenerator()
         .getEditorRoute(selectedCustomer, ImmutableMap.of("someParam", "someValue"));

         dialogs.createMessageDialog()
                    .withCaption("Generated route")
                    .withMessage(routeToSelectedRole)
                    .withWidth("710")
                    .show();
      }
}

The resulting route looks like this:

url generate route