1 Reply Latest reply on Jun 8, 2017 11:08 AM by m p

    Request for example how to use OR or AND for AuditCriterions

    m p Newbie

      I would like to apply a AuditCriterion as below but am struggling to find an example of how to do this.

       

      OR_criterion1 = (prop[0] like str[0] OR prop[1] like str[0])

      OR_criterion2 = (prop[0] like str[1] OR prop[1] like str[1])

       

      criterion to apply:   OR_criterion1 AND OR_criterion2

       

      Effectively I generate a number of OR criterions that I then AND together.

       

      Each OR criterion consists of  ORing a number of properties that are compared to a given string.

       

      The equivalent SQL where clause would look like:

       

      where

      ( HostDO.HOSTNAME like '%ai%' OR HostDO.OS_CLASS like '%ai%' )   <-- this is OR_criterion1

      AND

      (HostDO.HOSTNAME like '%ibm%' OR HostDO.OS_CLASS like '%ibm%' ) <-- this is OR_criterion2

       

      here prop[0] is HostDO.HOSTNAME, prop[1] is HostDO.OS_CLASS

      and str[0] is ai str[1] is ibm

       

       

      regards.

        • 1. Re: Request for example how to use OR or AND for AuditCriterions
          m p Newbie

          here we go:

           

          In case you were wondering why, this is for dataTables search field.  The text entered in the search field is searched for in every column.

           

          AuditQuery auditQuery = reader.createQuery() ...

           

          String properties[] = filters.split(",");  // get property names; COMMA delimited

          String searchWords[] = searchString.split(" ");  // get search strings from searchString; SPACE delimited

           

          AuditCriterion andCriterion = null;

          for(int i = 0; i < searchWords.length; i++){

              String searchWord = searchWords[i];

             

              // build up OR-criteria

              AuditCriterion orCriterion = null;

              for(int j = 0; j < properties.length; j++){

                  String property = properties[j];

              

                  if(j == 0) // first one, nothing to OR with so just add it

                      orCriterion = AuditEntity.property(property).like("%"+searchWord+"%");

                  else

                      orCriterion = AuditEntity.or(orCriterion, AuditEntity.property(property).like("%"+searchWord+"%"));

              }

              // AND together the OR-criteria

              if(i == 0)   // first one, nothing to AND with so just add it

                  andCriterion = orCriterion;

              else

                  andCriterion = AuditEntity.and(andCriterion, orCriterion);

          }

            

          // add AND-criteria to query

          auditQuery = auditQuery.add(andCriterion);

          1 of 1 people found this helpful