concept restriction in category seam

appears as: restrictions, restrictions, The restrictions, restriction, restriction
Seam in Action

This is an excerpt from Manning's book Seam in Action.

  • Page parameters, actions, and restrictions defined in the page descriptor are applied.
  • Restrictions are a way to make a JPQL/HQL query contextual. The context—or state, in this case—is the criteria the user has entered in the search form. To get the criteria values from the form to the query, they need to be bound to the properties of a component instance. The Query by Example (QBE) pattern lends itself nicely to this problem. In QBE, you pass a criteria object to the query engine and tell it to “find results like this.” The object that you pass to the query engine is a partial representation of the objects in the result set. Since the results in the listing page are instances of an entity class, in this case Round, then the example object must be an instance of Round.

    The restrictions comprise the where clause of the JPQL/HQL query. The entity properties in the restriction must be qualified to the entity alias to which they belong. For instance, in the first restriction, the g prefix in g.firstName is an alias to the Golfer entity. When defining a restriction, you have the full power of JPQL at your disposal. That means you can use built-in JPQL/HQL functions (not SQL functions) such as concat() and lower() to customize the condition, as shown earlier. Unfortunately, you can’t apply two different value expressions in the same restriction. In that case, you probably need to rethink the problem or consider if you’ve outgrown the intentionally focused restriction facility, perhaps graduating to Hibernate Search.

    The restrictions are one half of the equation. The other is the search form. The properties of the example object are bound to the inputs in the search form to capture the criteria values from the user:

      <h:form id="roundSearch">
        <rich:panel><f:facet name="header">Round search parameters</f:facet>
            <s:decorate id="firstNameField" template="layout/display.xhtml">
              <ui:define name="label">First name:</ui:define>
              <h:inputText id="firstName"
                value="#{roundExample.golfer.firstName}"/>
            </s:decorate>
            <s:decorate id="lastNameField" template="layout/display.xhtml">
              <ui:define name="label">Last name:</ui:define>
              <h:inputText id="lastName"
                value="#{roundExample.golfer.lastName}"/>
            </s:decorate>
            <s:decorate id="colorField" template="layout/display.xhtml">
              <ui:define name="label">Tee set color:</ui:define>
              <h:inputText id="color" value="#{roundExample.teeSet.color}"/>
            </s:decorate>
        </rich:panel>
        <div class="actionButtons">
          <h:commandButton id="search" value="Search"
            actionListener="#{roundList.first}"/>
        </div>
      </h:form>

    Notice the #{roundList.first} method expression used in the action listener of the UI command component that submits the form. This action listener ensures that the pagination offset is reset before the search is performed. Although the Query class clears the result set when it detects a change to the restrictions, it doesn’t reset the pagination offset. It’s important to rewind the offset back to the first page because it ensures that if the search criteria were to reduce the size of the result set, the pagination offset wouldn’t be left beyond the last result. If that happened, no results would be displayed, even though results might have been returned by the query. To avoid putting the user in this confusing situation, we introduce the minor inconvenience of resetting the pagination.