Ignore:
Timestamp:
Apr 21, 2009, 11:29:09 AM (14 years ago)
Author:
Martin Svensson
Message:

References #1297 Added support for custom data loaders and collections

Location:
trunk/src/clients/web/net/sf/basedb/clients/web/plugins
Files:
1 added
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/ExportTemplate.java

    r4548 r4898  
    2323
    2424import java.io.IOException;
     25import java.util.Collection;
    2526import java.util.List;
    2627
     
    8485 
    8586  /**
     87    Write the values in a collection
     88    @param ep Exported property the values belong to
     89    @param values The values to be written
     90    @since 2.12
     91   */
     92  public void writeCollection(ExportedProperty ep, Collection<?> values)
     93    throws IOException;
     94 
     95  /**
    8696    End writing an item
    8797    @throws IOException If there is an error
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/ExportedProperty.java

    r4896 r4898  
    4141  public final Unit unit;
    4242  public final Formatter formatter;
     43  public final DataLoader dataloader;
    4344 
    4445  public static ExportedProperty parse(DbControl dc, String column, boolean annotatable, Formatter formatter)
     
    6061  public static ExportedProperty parse(DbControl dc, String column, String columnPrefix,
    6162    boolean annotatable, Formatter formatter, boolean sameUnits)
     63  {
     64    return parse(dc, column, columnPrefix, annotatable, formatter, sameUnits, null);
     65  }
     66 
     67  /**
     68    @since 2.12
     69  */
     70  @SuppressWarnings("unchecked")
     71  public static ExportedProperty parse(DbControl dc, String column, String columnPrefix,
     72      boolean annotatable, Formatter formatter, boolean sameUnits, DataLoader dataloader)
    6273  {
    6374    String[] p = column.split(":");
     
    8192    if (columnPrefix != null) title = columnPrefix + title;
    8293    if (columnSuffix != null) title += columnSuffix;
    83     return new ExportedProperty(name, title, at, unit, formatter);
     94    return new ExportedProperty(name, title, at, unit, formatter, dataloader);
    8495  }
    8596
     
    95106    AnnotationType annotationType, Unit unit, Formatter formatter)
    96107  {
     108    this(name, title, annotationType, unit, formatter, null);   
     109  }
     110 
     111  public ExportedProperty(String name, String title,
     112      AnnotationType annotationType, Unit unit, Formatter formatter, DataLoader dataloader)
     113  {
    97114    this.name = name;
    98115    this.title = title;
     
    100117    this.formatter = formatter;
    101118    this.unit = unit;
     119    this.dataloader = dataloader;
    102120  }
    103  
    104121}
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/PlainTextTemplate.java

    r4548 r4898  
    2424import java.io.IOException;
    2525import java.io.Writer;
     26import java.util.Collection;
    2627import java.util.List;
    2728import java.util.regex.Pattern;
     
    122123    exportStream.write(escape(ep.formatter.format(data)));
    123124  }
     125 
     126  /**
     127    Writes a collection of values.
     128    @since 2.12
     129   */
     130  @Override
     131  @SuppressWarnings("unchecked")
     132  public void writeCollection(ExportedProperty ep, Collection<?> values)
     133      throws IOException
     134  {
     135    if (colNum > 0) exportStream.write("\t");
     136    colNum++;   
     137    exportStream.write(escape(Values.getString(values, ",", true, ep.formatter)));
     138  }
    124139
    125140  /**
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/QueryWrapper.java

    r4512 r4898  
    3232  @base.modified $Date$
    3333*/
    34 interface QueryWrapper<T>
     34interface QueryWrapper<T> extends DataLoader<T>
    3535{
    3636  /**
     
    4747  public int getId(T item);
    4848 
    49   /**
    50     Get the value for the specified property.
    51     @param exportedProperty The property to get the value for
    52     @param item The item that holds the value
    53    * @throws Exception If failing to get the data.
    54   */
    55   public Object getData(ExportedProperty exportedProperty, T item)
    56     throws Exception;
    57  
    5849}
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/SimpleExport.java

    r4896 r4898  
    7676import java.util.ArrayList;
    7777import java.util.Arrays;
     78import java.util.Collection;
    7879import java.util.Date;
    7980import java.util.List;
     
    252253    MultiFormatter defaultFormatter = new MultiFormatter(new ToStringFormatter<Object>(), true);
    253254    defaultFormatter.registerFormatter(Date.class, FormatterFactory.getDateFormatter(sc));
     255    DataLoader dataloader = null;
    254256    for (String column : columns)
    255257    {
     
    265267          formatter = (Formatter<?>)possibleFormatter;
    266268        }
     269        // Get the registered dataloader for current column, if any, and clear it.
     270        Object possibleDataloader = cc.setObject("export.dataloader." + property, null);       
     271        if (possibleDataloader instanceof DataLoader)
     272        {
     273          dataloader = (DataLoader<?>)possibleDataloader;
     274        }
    267275      }
    268276      ExportedProperty ep = ExportedProperty.parse(dc, column, columnPrefix,
    269         annotatable, formatter, sameUnits);
     277        annotatable, formatter, sameUnits, dataloader);
    270278      exportedProperties.add(ep);
    271279      properties.add(ep.name);
     
    569577            else
    570578            {
    571               Object data = queryWrapper.getData(ep, item);
    572               template.writeProperty(ep, data);
     579              Object data = ep.dataloader == null ?
     580                  queryWrapper.getData(ep, item) : ep.dataloader.getData(ep, item);
     581             
     582              if (data instanceof Collection)
     583              {
     584                template.writeCollection(ep, (Collection)data);
     585              }
     586              else
     587              {
     588                template.writeProperty(ep, data);
     589              }
    573590            }
    574591          }
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/XMLTemplate.java

    r4548 r4898  
    2424import java.io.IOException;
    2525import java.io.Writer;
     26import java.util.Collection;
    2627import java.util.List;
    2728import java.util.regex.Pattern;
     
    138139  /**
    139140    Add the property value to the current item element.
     141    @since 2.12
    140142  */
    141143  @SuppressWarnings("unchecked")
     
    149151 
    150152  /**
     153    Add collection values to the current item element.
     154   */
     155  @Override
     156  @SuppressWarnings("unchecked")
     157  public void writeCollection(ExportedProperty ep, Collection<?> values)
     158    throws IOException
     159  {
     160   
     161    Element property = new Element("property-value");
     162    property.setAttribute("ref", ep.name);
     163    if (values != null)
     164    {
     165      for (Object value : values)
     166      {
     167        Element valueElement = new Element("value");
     168        valueElement.setText(escape(ep.formatter.format(value)));
     169        property.addContent(valueElement);
     170      }
     171    }
     172    itemElement.addContent(property);
     173  }
     174 
     175  /**
    151176    Write the current item element to the export stream.
    152177  */
Note: See TracChangeset for help on using the changeset viewer.