Changeset 7765


Ignore:
Timestamp:
Feb 5, 2020, 10:53:21 AM (22 months ago)
Author:
Nicklas Nordborg
Message:

References #2202: Include information from parent items in list pages

Started with re-factoring the implementation. An abstract base class ParentItemColumn takes care of finding the correct parent item(s). Added subclasses for taking an annototation value and a regular property (eg. name) from the parent item.

Introduced a helper class that should improve performance by caching already loaded items and annotations.

Location:
trunk/src/clients/web/net/sf/basedb/clients/web/extensions/list
Files:
4 added
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/extensions/list/ParentItemColumn.java

    r7764 r7765  
    44
    55import net.sf.basedb.core.Annotatable;
    6 import net.sf.basedb.core.Annotation;
    76import net.sf.basedb.core.AnnotationType;
    87import net.sf.basedb.core.DbControl;
    98import net.sf.basedb.core.Item;
    109import net.sf.basedb.core.ItemSubtype;
    11 import net.sf.basedb.core.Subtypable;
    12 import net.sf.basedb.core.Type;
     10import net.sf.basedb.core.Metadata;
     11import net.sf.basedb.core.Metadata.PropertyPath;
    1312import net.sf.basedb.util.AnnotationUtil;
    14 import net.sf.basedb.util.Enumeration;
    1513import net.sf.basedb.util.Values;
    1614import net.sf.basedb.util.filter.Filter;
    17 import net.sf.basedb.util.formatter.Formatter;
    1815
    1916/**
     
    2724  @since 3.16
    2825*/
    29 public class ParentItemColumn<I extends Annotatable, V>
    30   implements ListColumnAction<I, V>,
    31    Filter<Annotatable>
     26public abstract class ParentItemColumn
     27  extends AbstractListColumnBean<Annotatable, Object>
    3228{
    3329 
    3430  /**
    35     For now, we assume that 'expr' is /ITEMTYPE/subtype-id/#annotationtype-id
     31    For now, we assume that 'expr' is /ITEMTYPE/subtype-id/#annotationtype-id or
     32    /ITEMTYPE/subtype-id/property
    3633  */
    37   static <I extends Annotatable, V> ParentItemColumn<I, V> create(DbControl dc, String expr)
     34  public static ParentItemColumn create(DbControl dc, String expr, ParentItemHelper helper)
    3835  {
    3936    String[] tmp = expr.split("/");
    4037    Item parentType = Item.valueOf(tmp[1]);
    4138    ItemSubtype subtype = ItemSubtype.getById(dc, Values.getInt(tmp[2]));
    42     AnnotationType at = AnnotationType.getById(dc, Values.getInt(tmp[3].substring(1)));
    4339   
    44     return new ParentItemColumn<>(expr, subtype, at);
     40    ParentItemColumn col = null;
     41    if (tmp[3].startsWith("#"))
     42    {
     43      AnnotationType at = AnnotationType.getById(dc, Values.getInt(tmp[3].substring(1)));
     44      col = new ParentItemAnnotationColumn(expr, new SubtypeFilter(subtype), helper, at);
     45      col.setTitle(at.getName() + " [A]");
     46      col.setTooltip(subtype.getName() + " › "+at.getName());
     47      col.setValueType(at.getValueType());
     48    }
     49    else
     50    {
     51      Metadata m = Metadata.getInstance(parentType.getItemClass());
     52      PropertyPath property = m.getPropertyPath(tmp[3], false);
     53      col = new ParentItemPropertyColumn(expr, new SubtypeFilter(subtype), helper, property);
     54      col.setTitle(subtype.getName()+"."+tmp[3]);
     55    }
     56   
     57    return col;
    4558  }
    4659
    47   private String id;
     60  private final Filter<Annotatable> filter;
     61  protected final ParentItemHelper helper;
    4862 
    49   private ItemSubtype subtype;
    50   private AnnotationType at;
    51  
    52   public ParentItemColumn(String id, ItemSubtype subtype, AnnotationType at)
     63  ParentItemColumn(String id, Filter<Annotatable> filter, ParentItemHelper helper)
    5364  {
    54     this.id = id;
    55     this.subtype = subtype;
    56     this.at = at;
     65    this.filter = filter;
     66    this.helper = helper;
     67    setId(id);
     68    setProperty(id);
     69    setExportable(true);
     70  }
     71
     72  protected Set<Annotatable> getParentItems(DbControl dc, Annotatable item)
     73  {
     74    return AnnotationUtil.getAllAnnotatableParentItems(dc, item, filter, helper.cache);
    5775  }
    5876 
    59   @Override
    60   public String getId()
    61   {
    62     return id;
    63   }
    64 
    65   @Override
    66   public String getClazz()
    67   {
    68     return null;
    69   }
    70 
    71   @Override
    72   public String getStyle()
    73   {
    74     return null;
    75   }
    76 
    77   @Override
    78   public String getTitle()
    79   {
    80     return at.getName() + " [A]";
    81   }
    82 
    83   @Override
    84   public String getTooltip()
    85   {
    86     return subtype.getName() + ">"+at.getName();
    87   }
    88 
    89   @Override
    90   public String getProperty()
    91   {
    92     return id;
    93   }
    94 
    95   @Override
    96   public Type getValueType()
    97   {
    98     return at.getValueType();
    99   }
    100 
    101   @Override
    102   public boolean isSortable()
    103   {
    104     return false;
    105   }
    106 
    107   @Override
    108   public String getSortProperty()
    109   {
    110     return null;
    111   }
    112 
    113   @Override
    114   public boolean isFilterable()
    115   {
    116     return false;
    117   }
    118 
    119   @Override
    120   public String getFilterProperty()
    121   {
    122     return null;
    123   }
    124 
    125   @Override
    126   public boolean isExportable()
    127   {
    128     return true;
    129   }
    130 
    131   @Override
    132   public String getExportProperty()
    133   {
    134     return id;
    135   }
    136 
    137   @Override
    138   public Formatter getExportFormatter()
    139   {
    140     return null;
    141   }
    142 
    143   @Override
    144   public Object getExportValue(DbControl dc, I item)
    145   {
    146     return getValue(dc, item);
    147   }
    148 
    149   @Override
    150   public Enumeration<String, String> getEnumeration()
    151   {
    152     return null;
    153   }
    154 
    155   @Override
    156   public Formatter<? super V> getFormatter()
    157   {
    158     return null;
    159   }
    160 
    161   @Override
    162   public V getValue(DbControl dc, I item)
    163   {
    164    
    165     Set<Annotatable> parents = AnnotationUtil.getAllAnnotatableParentItems(dc, item, this);
    166     Annotatable p = parents.iterator().next();
    167    
    168     if (!p.isAnnotated()) return null;
    169     if (!p.getAnnotationSet().hasAnnotation(at, Annotation.Source.PRIMARY)) return null;
    170     return (V)p.getAnnotationSet().getAnnotation(at).getValues();
    171   }
    172 
    173   @Override
    174   public boolean evaluate(Annotatable item)
    175   {
    176     if (item.getType() != subtype.getMainItemType()) return false;
    177     return subtype.equals(((Subtypable)item).getItemSubtype());
    178 
    179   }
    18077 
    18178}
  • trunk/src/clients/web/net/sf/basedb/clients/web/extensions/list/ParentItemColumnActionFactory.java

    r7764 r7765  
    2828  @since 3.16
    2929*/
    30 public class ParentItemColumnActionFactory<I extends Annotatable, V>
    31   extends AbstractJspActionFactory<ListColumnAction<I, V>>
     30public class ParentItemColumnActionFactory
     31  extends AbstractJspActionFactory<ListColumnAction<Annotatable, Object>>
    3232{
    3333 
     
    4040  */
    4141  @Override
    42   public boolean prepareContext(InvokationContext<? super ListColumnAction<I, V>> context)
     42  public boolean prepareContext(InvokationContext<? super ListColumnAction<Annotatable, Object>> context)
    4343  {
    4444    JspContext jspContext = (JspContext)context.getClientContext();
     
    6060  @SuppressWarnings("unchecked")
    6161  @Override
    62   public ListColumnAction<I, V>[] getActions(InvokationContext<? super ListColumnAction<I, V>> context)
     62  public ListColumnAction<Annotatable, Object>[] getActions(InvokationContext<? super ListColumnAction<Annotatable, Object>> context)
    6363  {
    6464    JspContext jspContext = (JspContext)context.getClientContext();
     
    7474    if (allColumns == null || !allColumns.contains("/")) return null;
    7575   
    76     List<ListColumnAction<I, V>> actions = new ArrayList<>();
     76    ParentItemHelper helper = new ParentItemHelper();
     77    List<ListColumnAction<Annotatable, Object>> actions = new ArrayList<>();
    7778    for (String col : allColumns.split(","))
    7879    {
    7980      if (col.startsWith("/"))
    8081      {
    81         ListColumnAction<I, V> action = ParentItemColumn.create(jspContext.getDbControl(), col);
     82        ListColumnAction<Annotatable, Object> action = ParentItemColumn.create(jspContext.getDbControl(), col, helper);
    8283        if (action != null) actions.add(action);
    8384      }
Note: See TracChangeset for help on using the changeset viewer.