10 Replies Latest reply on Jul 20, 2009 11:19 AM by Ronald van Kuijk

    implementing ModifiableDatamodel

    Carsten Hoehne Master

      Hello,
      we tried to implement a datamodel

      public class FongDataModel extends ExtendedDataModel implements Modifiable{

      The HibernateDataModel from the demo pages was choosen as a template.

      The paging is working. Only the part which sorts and filters the table do not do what they should.
      Now my question:
      The private method getPropertyName(copied from the demo) does not resolve any property name. We get "" or even null.
      What precondition must be nmet to successfully call getPropertyName?

      Our table definition looks like this:

      <rich:dataTable id="searchPersonResults" rows="10" value="#{bean.personList}" var="personTO"
       <f:facet name="header">
       <a4j:outputPanel ajaxRendered="true">
       <rich:datascroller for="searchPersonResults" stepControls="false" renderIfSinglePage="false" maxPages="10" />
       </a4j:outputPanel>
       </f:facet>
      
       <rich:column filterEvent="onkeyup" filterBy="#{personTO.contractNumber}" sortOrder="ASCENDING" sortBy="#{personTO.contractNumber}">
       <f:facet name="header">
       <h:outputText value="#{messages.global_contract}" />
       </f:facet>
       <h:outputText value="#{personTO.contractNumber}" />
       </rich:column>
      
       [.....]
       <rich:column filterEvent="onkeyup" filterBy="#{personTO.person.firstname} #{personTO.person.lastname}" sortOrder="ASCENDING" sortBy="#{personTO.person.firstname} #{personTO.person.lastname}">
       <f:facet name="header">
       <h:outputText value="#{messages.page_person_username}" />
       </f:facet>
       <h:outputText value="#{personTO.person.firstname} #{personTO.person.lastname}" />
       </rich:column>
      [...]
       </rich:dataTable>
      
      

      Any suggestions or pointers to more information will be welcomed.
      Thanks in nadvance

        • 1. Re: implementing ModifiableDatamodel
          Nick Belaevski Master

          Hi,

          You have to define property name by which to filter/sort at column level. Take a look at livedemo page source.

          • 2. Re: implementing ModifiableDatamodel
            Carsten Hoehne Master

            Hello,
            thanks for the quick answer.

            You have to define property name by which to filter/sort at column level

            I suggest that the sortBy attribute should be used for that.

            Our datatable sorts correct when it is backed by a normal list. And sorting is also available for the name column
            sortBy="#{personTO.person.firstname} #{personTO.person.lastname}"


            What exactly has to change in the datatable definition that the sort capability is available when our table is backed with a "Datamodel"?

            In the reference doku and the example i can not find any attribute for propertyname:-(

            Ciao,


            • 3. Re: implementing ModifiableDatamodel
              Carsten Hoehne Master

              Hello,
              this is what i have found out:

              the sortBy Attribute of a rich:column serves two purposes.
              1. If the datatable is backed by a normal list, the el-expression is used to access the object of the current row. With this information the table is sorted in memory

              sortBy="#{personTO.person.firstname}"


              2. If a datamodel is supplied the sortBy attribute must be used to specify database colums for orderBy clause.
              sortBy="#{'person.firstname'}"


              so, two new problems aroses:
              1. How can i use the same facelet(snippet) which is used from pages where the table is backed by a datamodel and from other pages where the table is backed by a a list.

              2. How can i order (in the db) with multiple columns (This is functioning when sorting in memory:
              sortBy="#{personTO.person.firstname} #{personTO.person.lastname}"


              Ciao,

              • 4. Re: implementing ModifiableDatamodel
                Ronald van Kuijk Master

                2: By implementing that in your model e.g. with the hibernate queryapi.

                I think you can even achieve what you mention by 1 as well

                • 5. Re: implementing ModifiableDatamodel
                  Carsten Hoehne Master

                  Thanks for your answer

                  2: By implementing that in your model e.g. with the hibernate queryapi.

                  It is possible also with JPA. You have to denote the columns separated by colums and parse this string in your code. In this way you are able to generate the correct orderBy Clause

                  I think you can even achieve what you mention by 1 as well

                  No. :-(
                  Because from the el (for inmemory sorting and filtering) i can not deduce the correct column names

                  I would suggest two propertys. One for inmemory sorting and the other for db backed ordering

                  • 6. Re: implementing ModifiableDatamodel
                    Ronald van Kuijk Master

                    1: Yes you can with something like:

                    public void modify(List filterFields, List<SortField2> sortFields) {

                    this.filterFields = new ArrayList();

                    for (int i = 0; i < filterFields.size(); i++) {
                    Expression expression = filterFields.get(i).getExpression();
                    String expressionStr = expression.getExpressionString();
                    if (!expression.isLiteralText()) {
                    expressionStr = expressionStr.replaceAll("[#|$]{1}\\{.*?\\.", "").replaceAll("\\}", "");
                    }
                    this.filterFields.add(expressionStr);
                    }

                    if (sortFields != null && !sortFields.isEmpty()) {
                    SortField2 sortField2 = sortFields.get(0);
                    log.debug("sortField expression: #0, order #1", sortField2.getExpression(), sortField2.getOrdering());
                    Expression expression = sortField2.getExpression();
                    String expressionStr = expression.getExpressionString();
                    if (!expression.isLiteralText()) {
                    expressionStr = expressionStr.replaceAll("[#|$]{1}\\{.*?\\.", "").replaceAll("\\}", "");
                    }
                    this.sortField = expressionStr;

                    Ordering ordering = sortField2.getOrdering();
                    if (ordering == Ordering.DESCENDING) {
                    this.descending = true;
                    }
                    else {
                    this.descending = false;
                    }
                    }
                    }

                    in your model

                    • 7. Re: implementing ModifiableDatamodel
                      Ronald van Kuijk Master

                      sorry, should have put [ c o d e ] tags around that

                      • 8. Re: implementing ModifiableDatamodel
                        Carsten Hoehne Master

                        Thanks for the code. But this does not help.
                        How would you do this:
                        When sorting in memory you have to denote this: #{personTO.contractNumber}
                        But when ordering in db you have to write this: #{bpca.person.contractNumber}

                        Ciao,

                        • 9. Re: implementing ModifiableDatamodel
                          Ronald van Kuijk Master

                          Write the modify method in such a way that it knows how to expand 'personTo' to 'bpca.person' be creative... worked for me perfectly.

                          • 10. Re: implementing ModifiableDatamodel
                            Ronald van Kuijk Master

                            keep in mind that within the modify method, the EL is not realy used as an el, but just as a string.