Changeset 7650


Ignore:
Timestamp:
Mar 14, 2019, 9:31:57 AM (3 years ago)
Author:
Nicklas Nordborg
Message:

References #2160: Table exporter should support exporting to Excel files

Implemented possibility for table listings to specify a formatter for Excel that is independent of the regular formatter. Just as a regular formatter can be specified by setting the context variable ItemContext.setObject("export.formatter.<property>", formatter) an Excel formatter can be specificed by setting ItemContext?.setObject("export.excelFormatter.<property>", excelFormatter) where <property>` is the name of the exported property.

Location:
trunk/src
Files:
1 added
4 edited

Legend:

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

    r7560 r7650  
    3434import net.sf.basedb.core.Metadata.PropertyPath;
    3535import net.sf.basedb.util.Values;
     36import net.sf.basedb.util.excel.ExcelFormatter;
     37import net.sf.basedb.util.excel.WrappedFormatter;
    3638import net.sf.basedb.util.formatter.FilePathFormatter;
    3739import net.sf.basedb.util.formatter.Formatter;
     
    5456  public final Formatter formatter;
    5557  @SuppressWarnings("rawtypes")
     58  public final ExcelFormatter excelFormatter;
     59  @SuppressWarnings("rawtypes")
    5660  public final DataLoader dataloader;
    5761  @SuppressWarnings("rawtypes")
     
    5963
    6064  @SuppressWarnings("rawtypes")
    61   public static ExportedProperty createExportedProperty(String path, String title, Metadata metadata, Formatter formatter)
     65  public static ExportedProperty createExportedProperty(String path, String title, Metadata metadata, Formatter formatter, ExcelFormatter excelFormatter)
    6266  {
    6367    PropertyPath propertyPath = metadata.getPropertyPath(path, false);
    64     return new ExportedProperty(path, title, propertyPath, null, false, null, formatter, null);
     68    return new ExportedProperty(path, title, propertyPath, null, false, null, formatter, excelFormatter, null);
    6569  }
    6670
     
    7175  @SuppressWarnings({ "unchecked", "rawtypes" })
    7276  public static ExportedProperty parse(DbControl dc, Metadata metadata, String column, String columnPrefix,
    73       boolean annotatable, Formatter formatter, boolean sameUnits, DataLoader dataloader)
     77      boolean annotatable, Formatter formatter, ExcelFormatter excelFormatter, boolean sameUnits, DataLoader dataloader)
    7478  {
    7579    String[] p = column.split(":");
     
    176180    if (columnPrefix != null) title = columnPrefix + title;
    177181    if (columnSuffix != null) title += columnSuffix;
    178     return new ExportedProperty(name, title, propertyPath, at, loadInherited, unit, formatter, dataloader);
     182    return new ExportedProperty(name, title, propertyPath, at, loadInherited, unit, formatter, excelFormatter, dataloader);
    179183  }
    180184
    181185  /**
    182     @since 2.15
     186    @since 3.15
    183187  */
    184   @SuppressWarnings("rawtypes")
     188  @SuppressWarnings({ "rawtypes", "unchecked" })
    185189  private ExportedProperty(String name, String title, PropertyPath propertyPath,
    186       AnnotationType annotationType, boolean loadInherited, Unit unit, Formatter formatter, DataLoader dataloader)
     190      AnnotationType annotationType, boolean loadInherited, Unit unit, Formatter formatter, ExcelFormatter excelFormatter, DataLoader dataloader)
    187191  {
    188192    this.name = name;
     
    191195    this.annotationType = annotationType;
    192196    this.loadInherited = loadInherited;
    193     this.formatter = formatter == null ? new ToStringFormatter() : formatter;
     197    if (formatter == null) formatter = new ToStringFormatter();
     198    this.formatter = formatter;
     199    if (excelFormatter == null)
     200    {
     201      if (formatter instanceof ExcelFormatter)
     202      {
     203        excelFormatter = (ExcelFormatter)formatter;
     204      }
     205      else
     206      {
     207        excelFormatter = new WrappedFormatter(formatter);
     208      }
     209    }
     210    this.excelFormatter = excelFormatter;
    194211    this.unit = unit;
    195212    this.dataloader = dataloader;
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/SimpleExport.java

    r7645 r7650  
    7070import net.sf.basedb.util.FileUtil;
    7171import net.sf.basedb.util.Values;
     72import net.sf.basedb.util.excel.ExcelFormatter;
    7273import net.sf.basedb.util.formatter.Formatter;
    7374import net.sf.basedb.util.units.UnitConverter;
     
    285286    for (String column : columns)
    286287    {
    287       Formatter<?> formatter = null;
     288      Formatter formatter = null;
     289      ExcelFormatter excelFormatter = null;
    288290      DataLoader dataloader = null;
    289291      int splitAt = column.indexOf(":");
     
    295297        if (possibleFormatter instanceof Formatter)
    296298        {
    297           formatter = (Formatter<?>)possibleFormatter;
     299          formatter = (Formatter)possibleFormatter;
     300        }
     301        // Repeat for excel formatter..
     302        possibleFormatter = cc.setObject("export.excelFormatter." + property, null);
     303        if (possibleFormatter instanceof ExcelFormatter)
     304        {
     305          excelFormatter = (ExcelFormatter)possibleFormatter;
    298306        }
    299307        // Get the registered dataloader for current column, if any, and clear it.
     
    305313      }
    306314      ExportedProperty ep = ExportedProperty.parse(dc, metadata, column, columnPrefix,
    307         annotatable, formatter, sameUnits, dataloader);
     315        annotatable, formatter, excelFormatter, sameUnits, dataloader);
    308316      if (ep != null)
    309317      {
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/XlsxTemplate.java

    r7649 r7650  
    176176    throws IOException
    177177  {
    178     Cell cell = currentRow.createCell(colNum++);
    179    
    180     ExcelValue ev = null;
    181     if (ep.formatter instanceof ExcelFormatter)
    182     {
    183       ExcelFormatter ef = (ExcelFormatter)ep.formatter;
    184       ev = ef.toExcelValue(data);
    185     }
    186     else
    187     {
    188       ev = ExcelValue.asString(ep.formatter.format(data));
    189     }
     178    Cell cell = currentRow.createCell(colNum++);   
     179    ExcelValue ev = ep.excelFormatter.toExcelValue(data);
    190180    if (ev == null) ev = ExcelValue.emptyCell();
    191181    ev.writeTo(cell, styleCreator);
  • trunk/src/core/net/sf/basedb/util/formatter/ToStringFormatter.java

    r6127 r7650  
    2222package net.sf.basedb.util.formatter;
    2323
     24import net.sf.basedb.util.excel.ExcelFormatter;
     25import net.sf.basedb.util.excel.ExcelValue;
    2426
    2527/**
     
    3234*/
    3335public class ToStringFormatter<T>
    34   implements Formatter<T>
     36  implements Formatter<T>, ExcelFormatter<T, String>
    3537{
    3638
     
    5759  // -------------------------------------------
    5860
     61  /**
     62    @since 3.15
     63  */
     64  @Override
     65  public ExcelValue<String> toExcelValue(T value)
     66  {
     67    return value == null ? ExcelValue.emptyCell() : ExcelValue.asString(format(value));
     68  }
     69
    5970}
Note: See TracChangeset for help on using the changeset viewer.