Uploaded image for project: 'Hippo Site Toolkit 2'
  1. Hippo Site Toolkit 2
  2. HSTTWO-3780

HstQuery Builder Fluent API

    XMLWordPrintable

    Details

    • Type: New Feature
    • Status: Closed
    • Priority: Normal
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 4.1.0
    • Component/s: None
    • Labels:
      None
    • Similar issues:
    • Processed by team:
      Platform
    • Sprint:
      Platform Sprint 140

      Description

      It would be nice if we can build HstQuery through a fluent builder API.

      Rough idea in example code for comparison:

      AS-IS

          /*
           * The current way
           */
          private HstQuery createHstQueryInfluently() throws Exception {
              HstRequestContext requestContext = RequestContextProvider.get();
              HippoBean scopeBean = requestContext.getSiteContentBaseBean();
      
              HstQuery query = requestContext.getQueryManager().createQuery(scopeBean);
              Filter filter = query.createFilter();
      
              Filter nestedFilter1 = query.createFilter();
              nestedFilter1.addEqualTo("myhippoproject:customid", "123");
      
              Filter nestedFilter2 = query.createFilter();
              Filter nestedFilter21 = query.createFilter();
              nestedFilter21.addLike("myhippoproject:title", "Hello%");
              Filter nestedFilter22 = query.createFilter();
              nestedFilter22.addContains("myhippoproject:description", "foo");
              nestedFilter2.addOrFilter(nestedFilter21);
              nestedFilter2.addOrFilter(nestedFilter22);
      
              filter.addAndFilter(nestedFilter1);
              filter.addAndFilter(nestedFilter2);
      
              query.setFilter(filter);
      
              return query;
          }
      

      TO-BE

          /*
           * New fluent way
           */
          private HstQuery createHstQueryFluently() throws Exception {
              HstRequestContext requestContext = RequestContextProvider.get();
              HippoBean scopeBean = requestContext.getSiteContentBaseBean();
      
              // constructor-arg and scope can be optional with sensible defaults.
              // obviously .filter() is also optional.
              // .filter() is to set filter whereas filter() to create a new filter as static import.
              return new HstQueryBuilder(requestContext.getQueryManager())
                      .scope(scopeBean)
                      .filter(
                          and(filter("myhippoproject:customid").equalsTo("123"),
                              or(
                                  filter("myhippoproject:title").like("Hello%"),
                                  filter("myhippoproject:description").contains("foo")
                              )
                          )
                      );
          }
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                adouma Ate Douma
                Reporter:
                wko Woonsan Ko
              • Votes:
                3 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: