001package org.avaje.ebean.typequery; 002 003import com.avaje.ebean.ExpressionList; 004 005/** 006 * Base type for associated beans. 007 * 008 * @param <T> the entity bean type (normal entity bean type e.g. Customer) 009 * @param <R> the specific root query bean type (e.g. QCustomer) 010 */ 011public abstract class TQAssocBean<T, R> { 012 013 protected final String _name; 014 015 protected final R _root; 016 017 /** 018 * Construct with a property name and root instance. 019 * 020 * @param name the name of the property 021 * @param root the root query bean instance 022 */ 023 public TQAssocBean(String name, R root) { 024 this(name, root, null); 025 } 026 027 /** 028 * Construct with additional path prefix. 029 */ 030 public TQAssocBean(String name, R root, String prefix) { 031 this._root = root; 032 this._name = TQPath.add(prefix, name); 033 } 034 035 /** 036 * Eagerly fetch this association fetching all the properties. 037 */ 038 public R fetchAll() { 039 040 ((TQRootBean) _root).query().fetch(_name, "*"); 041 return _root; 042 } 043 044 /** 045 * Eagerly fetch this association fetching some of the properties. 046 */ 047 protected R fetchProperties(TQProperty<?>... props) { 048 StringBuilder selectProps = new StringBuilder(50); 049 for (int i = 0; i < props.length; i++) { 050 if (i > 0) { 051 selectProps.append(","); 052 } 053 selectProps.append(props[i].propertyName()); 054 } 055 ((TQRootBean) _root).query().fetch(_name, selectProps.toString()); 056 return _root; 057 } 058 059 /** 060 * Internal method to return the underlying expression list. 061 */ 062 protected ExpressionList<?> expr() { 063 return ((TQRootBean) _root).peekExprList(); 064 } 065 066 /** 067 * Is equal to by ID property. 068 */ 069 public R equalTo(T other) { 070 expr().eq(_name, other); 071 return _root; 072 } 073 074 /** 075 * Is not equal to by ID property. 076 */ 077 public R notEqualTo(T other) { 078 expr().ne(_name, other); 079 return _root; 080 } 081 082 /** 083 * Apply a filter when fetching these beans. 084 */ 085 public R filterMany(ExpressionList<T> filter) { 086 087 ExpressionList<T> expressionList = (ExpressionList<T>)expr().filterMany(_name); 088 089 expressionList.addAll(filter); 090 return _root; 091 } 092}