XMLWordPrintable

Details

    • New Feature
    • Status: Closed
    • Normal
    • Resolution: Fixed
    • None
    • 4.1.0
    • None
    • None
    • 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

              adouma Ate Douma (Inactive)
              wko Woonsan Ko (Inactive)
              Votes:
              3 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: