Changeset 6933


Ignore:
Timestamp:
Jun 22, 2015, 9:52:46 AM (7 years ago)
Author:
Nicklas Nordborg
Message:

References #1950: Add support for any-to-any link filters in table lists views

Filtering on properties on the target item is now supported using a subquery. A remaining issue is that the sharing options selected from the 'view/preset' list is applied also to the linked items. They should only be applied to the main query to make the behaviour consistent with how other filters work.

Location:
trunk/src/core/net/sf/basedb/core
Files:
2 edited

Legend:

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

    r6931 r6933  
    712712      Item linkType = parts.length > 2 ? Item.valueOf(parts[2]) : null;
    713713      String linkProperty = parts.length > 3 ? parts[3] : null;
     714      Object value = getValueAsObject();
    714715     
    715       if (linkType == null || linkProperty == null)
    716       {
    717         if (Boolean.TRUE.equals(getValueAsObject()))
    718         {
    719           restriction = AnyToAnyRestriction.exists(null, linkName, linkType);
     716      if (linkType == null || linkProperty == null || value == null)
     717      {
     718        if (Boolean.TRUE.equals(value) || (value == null && operator.isNegationOperator()))
     719        {
     720          restriction = AnyToAnyRestriction.exists(null, linkName, linkType, null);
    720721        }
    721722        else
    722723        {
    723           restriction = AnyToAnyRestriction.missing(null, linkName, linkType);
    724         }
     724          restriction = AnyToAnyRestriction.missing(null, linkName, linkType, null);
     725        }
     726      }
     727      else
     728      {
     729        EntityQuery subquery = new AbstractEntityQuery(linkType, linkType, null, "id", false, null)
     730        {};
     731
     732        PropertyFilter pp = new PropertyFilter(linkProperty, operator, getValue(), getValueType());
     733        subquery.restrict(pp.getRestriction(dc, subquery));
     734        restriction = AnyToAnyRestriction.exists(null, linkName, linkType, subquery);
    725735      }
    726736    }
  • trunk/src/core/net/sf/basedb/core/query/AnyToAnyRestriction.java

    r6931 r6933  
    22
    33import java.util.Collection;
     4import java.util.Collections;
    45
    56import net.sf.basedb.core.DbControl;
     
    3031    @param linkName The name of the link (required)
    3132    @param linkedTo The type of the linked item (optional)
     33    @param linkedQuery An optional query that the linked items must match
    3234  */
    33   public static AnyToAnyRestriction exists(String alias, String linkName, Item linkedTo)
     35  public static AnyToAnyRestriction exists(String alias, String linkName, Item linkedTo, EntityQuery linkedQuery)
    3436  {
    35     return new AnyToAnyRestriction(alias, linkName, linkedTo, true);
     37    return new AnyToAnyRestriction(alias, linkName, linkedTo, true, linkedQuery);
    3638  }
    3739 
     
    4446    @param linkName The name of the link (required)
    4547    @param linkedTo The type of the linked item (optional)
     48    @param linkedQuery An optional query that the linked items must not match
    4649  */
    47   public static AnyToAnyRestriction missing(String alias, String linkName, Item linkedTo)
     50  public static AnyToAnyRestriction missing(String alias, String linkName, Item linkedTo, EntityQuery linkedQuery)
    4851  {
    49     return new AnyToAnyRestriction(alias, linkName, linkedTo, false);
     52    return new AnyToAnyRestriction(alias, linkName, linkedTo, false, linkedQuery);
    5053  }
    5154 
     
    5457  private final Item linkedTo;
    5558  private final boolean hasLink;
     59  private final EntityQuery linkedQuery;
     60  private final Restriction linkedRestriction;
    5661  private String parameterName;
    5762 
    58   private AnyToAnyRestriction(String alias, String linkName, Item linkedTo, boolean hasLink)
     63  private AnyToAnyRestriction(String alias, String linkName, Item linkedTo, boolean hasLink, EntityQuery linkedQuery)
    5964  {
    6065    this.alias = alias;
     
    6267    this.linkedTo = linkedTo;
    6368    this.hasLink = hasLink;
     69    this.linkedQuery = linkedQuery;
     70    this.linkedRestriction = linkedQuery == null ? null : Restrictions.in(Hql.property("at", "toId"), linkedQuery);
    6471  }
    6572 
     
    8289    {
    8390      sb.append(" AND at.toType=").append(linkedTo.getValue());
     91      if (linkedRestriction != null)
     92      {
     93        sb.append(" AND ").append(linkedRestriction.toQl(query, dc));
     94      }
    8495    }
    8596    sb.append(")");
     
    91102  public Collection<? extends QueryElement> getChildren()
    92103  {
    93     return null;
     104    return linkedRestriction == null ? null : Collections.singleton(linkedRestriction);
    94105  }
    95106
     
    104115    sb.append("SELECT at.fromId FROM AnyToAnyData at WHERE at.name = '");
    105116    sb.append(linkName).append("'");
     117    if (linkedTo != null)
     118    {
     119      sb.append(" AND at.toType=").append(linkedTo.getValue());
     120      if (linkedRestriction != null)
     121      {
     122        sb.append(" AND ").append(linkedRestriction.toString());
     123      }
     124    }
    106125    sb.append(")");
    107126    return sb.toString();
     
    110129  /**
    111130    This expression is equal to another AnyToAnyRestriction if their
    112     named link and 'hasLink' parameter is the same.
     131    named link, 'hasLink' and subquery restriction are the same.
    113132  */
    114133  @Override
     
    118137    if ((other == null) || (super.getClass() != other.getClass())) return false;
    119138    AnyToAnyRestriction o = (AnyToAnyRestriction)other;
    120     return EqualsHelper.equals(linkName, o.linkName) && EqualsHelper.equals(linkedTo, o.linkedTo) && hasLink == o.hasLink;
     139    return EqualsHelper.equals(linkName, o.linkName) && EqualsHelper.equals(linkedTo, o.linkedTo)
     140      && hasLink == o.hasLink && EqualsHelper.equals(linkedRestriction, o.linkedRestriction);
    121141  }
    122142 
     
    124144  public int hashCode()
    125145  {
    126     return EqualsHelper.hashCode(linkName) + EqualsHelper.hashCode(linkedTo);
     146    return EqualsHelper.hashCode(linkName) + EqualsHelper.hashCode(linkedTo) + EqualsHelper.hashCode(linkedRestriction);
    127147  }
    128148
Note: See TracChangeset for help on using the changeset viewer.