Changeset 5168


Ignore:
Timestamp:
Oct 29, 2009, 11:02:49 AM (12 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1421: Filter on the 'name', 'description' and other columns in "Project items" and "All items" lists

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/core/net/sf/basedb/core/ItemContext.java

    r5164 r5168  
    608608      propertyFilters.clear();
    609609    }
     610  }
     611 
     612  /**
     613    Create a restriction from a property filter.
     614    @param property The property
     615    @param dc A DbControl that should be used if database access is needed
     616    @param query The query that the filter is going to be used in (can be
     617      null for most properties)
     618    @return A restriction, or null if no filter is active for the given
     619      property
     620    @since 2.15
     621  */
     622  public Restriction getFilterRestriction(String property, DbControl dc, EntityQuery query)
     623  {
     624    Restriction r = null;
     625    PropertyFilter filter = getPropertyFilter(property);
     626    if (filter != null)
     627    {
     628      r = filter.getRestriction(dc, query);
     629    }
     630    return r;
    610631  }
    611632 
  • trunk/src/core/net/sf/basedb/core/query/Restrictions.java

    r4889 r5168  
    6161
    6262  /**
     63    Combine one or more restrictions with AND ignoring null elements.
     64    @param r The restrictions to combine
     65    @return The new restriction, or null if the array was null
     66      or didn't contain any not-null elements
     67    @since 2.15
     68  */
     69  public static Restriction nullSafeAnd(Restriction... r)
     70    throws InvalidDataException
     71  {
     72    int notNulls = countNotNulls(r);
     73    Restriction result = null;
     74    if (notNulls > 0)
     75    {
     76      result = new AndRestriction(copyNotNulls(r, new Restriction[notNulls]));
     77    }
     78    return result;
     79  }
     80 
     81  /**
    6382    Combine one or more restrictions with OR: new restriction = r[0] OR r[1] ...
    6483    @param r The restrictions to combine
     
    7998  }
    8099
     100  /**
     101    Combine one or more restrictions with OR ignoring null elements.
     102    @param r The restrictions to combine
     103    @return The new restriction, or null if the array was null
     104      or didn't contain any not-null elements
     105    @since 2.15
     106  */
     107  public static Restriction nullSafeOr(Restriction... r)
     108    throws InvalidDataException
     109  {
     110    int notNulls = countNotNulls(r);
     111    Restriction result = null;
     112    if (notNulls > 0)
     113    {
     114      result = new OrRestriction(copyNotNulls(r, new Restriction[notNulls]));
     115    }
     116    return result;
     117  }
     118
     119 
    81120  /**
    82121    Negate a restriction: new restriction = NOT r
     
    341380    return -1;
    342381  }
     382 
     383  private static int countNotNulls(Object[] array)
     384  {
     385    if (array == null) return 0;
     386    int sum = 0;
     387    for (Object a : array)
     388    {
     389      if (a != null) ++sum;
     390    }
     391    return sum;
     392  }
     393 
     394  private static <T> T[] copyNotNulls(T[] source, T[] dest)
     395  {
     396    int index = 0;
     397    for (T s : source)
     398    {
     399      if (s != null)
     400      {
     401        dest[index] = s;
     402        ++index;
     403      }
     404    }
     405    return dest;
     406  }
    343407}
  • trunk/www/my_base/projects/items/list_items.jsp

    r5167 r5168  
    4343  import="net.sf.basedb.core.query.ResultList"
    4444  import="net.sf.basedb.core.query.Restriction"
     45  import="net.sf.basedb.core.query.Restrictions"
    4546  import="net.sf.basedb.util.Enumeration"
    4647  import="net.sf.basedb.util.ShareableUtil"
     
    101102      }
    102103    }
    103     PropertyFilter sharedToFilter = cc.getPropertyFilter("!sharedTo.name");
    104     Restriction sharedTo = null;
    105     if (sharedToFilter != null) sharedTo = sharedToFilter.getRestriction(dc, null);
     104    Restriction sharedTo = cc.getFilterRestriction("!sharedTo.name", dc, null);
     105    Restriction owner = cc.getFilterRestriction("owner.name", dc, null);
     106    Restriction name = cc.getFilterRestriction("name", dc, null);
     107    Restriction description = cc.getFilterRestriction("description", dc, null);
     108    Restriction id = cc.getFilterRestriction("id", dc, null);
     109    Restriction restriction = Restrictions.nullSafeAnd(sharedTo, owner, name, description, id);
    106110    items = project.getItems(itemTypes,
    107111      cc.getPage() * cc.getRowsPerPage(), cc.getRowsPerPage(), cc.getItemPermission(),
    108       cc.getInclude(), sharedTo);
     112      cc.getInclude(), restriction);
    109113  }
    110114  catch (Throwable t)
     
    239243        title="Name"
    240244        show="always"
     245        filterable="true"
     246        filterproperty="name"
     247        datatype="string"
    241248      />
    242249      <tbl:columndef
     
    245252        datatype="int"
    246253        title="ID"
    247         sortable="true"
    248         filterable="true"
    249         exportable="true"
     254        filterable="true"
    250255      />
    251256      <tbl:columndef
    252257        id="owner"
    253258        title="Owner"
     259        property="owner.name"
     260        datatype="string"
     261        filterable="true"
    254262      />
    255263      <tbl:columndef
    256264        id="description"
    257265        title="Description"
     266        property="description"
     267        filterable="true"
     268        datatype="string"
    258269      />
    259270      <tbl:columndef
  • trunk/www/views/items/list_items.jsp

    r5167 r5168  
    4343  import="net.sf.basedb.core.query.ResultList"
    4444  import="net.sf.basedb.core.query.Restriction"
     45  import="net.sf.basedb.core.query.Restrictions"
    4546  import="net.sf.basedb.util.Enumeration"
    4647  import="net.sf.basedb.util.ShareableUtil"
     
    9697      }
    9798    }
    98     PropertyFilter sharedToFilter = cc.getPropertyFilter("!sharedTo.name");
    99     Restriction sharedTo = null;
    100     if (sharedToFilter != null)
    101     {
    102       sharedTo = sharedToFilter.getRestriction(dc, null);
     99    Restriction sharedTo = cc.getFilterRestriction("!sharedTo.name", dc, null);
     100    Restriction name = cc.getFilterRestriction("name", dc, null);
     101    Restriction description = cc.getFilterRestriction("description", dc, null);
     102    Restriction id = cc.getFilterRestriction("id", dc, null);
     103    Restriction restriction = Restrictions.nullSafeAnd(sharedTo, name, description, id);
     104    if (sharedTo != null)
     105    {
    103106      // If we filter on "Shared to" we must make sure to only
    104107      // include Shareable items in the query
     
    116119    {
    117120      allItems = loggedInUser.getItems(itemTypes, cc.getPage() * cc.getRowsPerPage(),
    118           cc.getRowsPerPage(), null, sharedTo);
     121          cc.getRowsPerPage(), null, restriction);
    119122    }
    120123    totalCount = allItems.getTotalCount();
     
    235238        id="name"
    236239        title="Name"
     240        property="name"
     241        filterable="true"
     242        datatype="string"
    237243        show="always"
    238244      />
     
    242248        datatype="int"
    243249        title="ID"
    244         sortable="true"
    245250        filterable="true"
    246         exportable="true"
    247251      />
    248252      <tbl:columndef
    249253        id="description"
    250254        title="Description"
     255        property="description"
     256        filterable="true"
     257        datatype="string"
    251258      />
    252259      <tbl:columndef
Note: See TracChangeset for help on using the changeset viewer.