Skip to content

Configuring Scopes

ikostenko edited this page Apr 18, 2013 · 5 revisions

It is possible to categorize entity data using Scopes, pre-defined advanced filtering rules utilizing the power of Java and JPA.

Scopes can be used in combination with Filter criteria for more accurate searching within the List View. A Scope displays the total number of filtered items, that is dynamically re-calculated when applying or resetting Filter criteria or on item deletion.

Scopes are defined within ScopesConfigurationUnit section of an entity administration and can be any of the following types:

  • Specification, where filtering conditions are defined using SQL-like syntax of JPA queries. The filtering logic should be defined in a class implementing org.lightadmin.core.config.domain.scope.DomainTypeSpecification interface
  • Filter allows specifying more sophisticated filtering logic using Java, that can be defined in a class implementing org.lightadmin.core.config.domain.scope.DomainTypePredicate interface

If you would like a specific scope to be pre-selected each time you enter List View, you can set it as default by appending its definition with defaultScope() (see "Long-term bookings" scope in the example below). When none of the listed scopes is marked as default, the first of them will be treated as such.
When ScopesConfigurationUnit is missing in an entity administration, "All" scope will be displayed and pre-selected showing all entity items.

@Administration( Booking.class )
public class BookingAdministration {

	public static ScopesConfigurationUnit scopes( final ScopesConfigurationUnitBuilder scopeBuilder ) {
		return scopeBuilder
			.scope( "All", all() )
			.scope( "Smoking Apartments", specification( smokingApartmentsSpec( true ) ) )
			.scope( "Non Smoking Apartments", specification( smokingApartmentsSpec( false ) ) )
			.scope( "Long-term bookings", filter( longTermBookingPredicate() ) ).defaultScope().build();
	}

	public static FiltersConfigurationUnit filters( final FiltersConfigurationUnitBuilder filterBuilder ) {
		return filterBuilder
			.filter( "Customer", "user" )
			.filter( "Booked Hotel", "hotel" )
			.filter( "Check-In Date", "checkinDate" ).build();
	}

	public static FieldSetConfigurationUnit listView( final FieldSetConfigurationUnitBuilder fragmentBuilder ) {
		return fragmentBuilder
			.field( "user" ).caption( "Customer" )
			.field( "hotel" ).caption( "Hotel" )
			.field( "checkinDate" ).caption( "Check-In Date" )
			.field( "smoking" ).caption( "Smoking" )
			.field( "beds" ).caption( "Beds" )
			.build();
	}

	public static DomainTypePredicate<Booking> longTermBookingPredicate() {
		return new DomainTypePredicate<Booking>() {
			@Override
			public boolean apply( final Booking booking ) {
				return booking.getNights() > 20;
			}
		};
	}

	public static DomainTypeSpecification<Booking> smokingApartmentsSpec( final boolean isSmokingApartment ) {
		return new DomainTypeSpecification<Booking>() {
			@Override
			public Predicate toPredicate( final Root<Booking> root, final CriteriaQuery<?> query, final CriteriaBuilder cb ) {
				return cb.equal( root.get( "smoking" ), isSmokingApartment );
			}
		};
	}

}

Scopes