Changeset 4855


Ignore:
Timestamp:
Mar 27, 2009, 11:11:38 AM (13 years ago)
Author:
Nicklas Nordborg
Message:

Fixes #1272: Add support for using Formatters to SimpleExporter? (aka. table exporter)

Formatters should be stored in the current ItemContext? object as:

ItemContext.setObject("export.formatter.<propertyname>", formatter);

where <propertyname> is the name of the property that are exported. The formatter is cleared after the export and if the same context is used again a new formatter needs to be specified.

File:
1 edited

Legend:

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

    r4548 r4855  
    6767import net.sf.basedb.util.Enumeration;
    6868import net.sf.basedb.util.Values;
     69import net.sf.basedb.util.formatter.Formatter;
    6970import net.sf.basedb.util.formatter.MultiFormatter;
    7071import net.sf.basedb.util.formatter.ToStringFormatter;
     
    242243    List<String> columns = (List<String>)job.getValues(WHICH_PROPERTIES);
    243244    String columnPrefix = (String)job.getValue(COLUMN_PREFIX);
    244    
     245    ItemContext cc = sc.getCurrentContext(itemType, subContext);
     246
    245247    // Parse the WHICH_PROPERTIES parameter. It is a list of property:title entries
    246248    List<ExportedProperty> exportedProperties =
     
    249251    boolean annotatable = itemType.getItemClass() != null &&
    250252      Annotatable.class.isAssignableFrom(itemType.getItemClass());
    251     MultiFormatter formatter = new MultiFormatter(new ToStringFormatter<Object>(), true);
    252     formatter.registerFormatter(Date.class, FormatterFactory.getDateFormatter(sc));
     253    MultiFormatter defaultFormatter = new MultiFormatter(new ToStringFormatter<Object>(), true);
     254    defaultFormatter.registerFormatter(Date.class, FormatterFactory.getDateFormatter(sc));
    253255    for (String column : columns)
    254256    {
     257      Formatter<?> formatter = defaultFormatter;
     258      int splitAt = column.indexOf(":");
     259      if (splitAt >= 0)
     260      {
     261        String property = column.substring(0, splitAt);
     262        // Get a registered formatter and clear it to allow GC to reclaim it later
     263        Object possibleFormatter = cc.setObject("export.formatter." + property, null);
     264        if (possibleFormatter instanceof Formatter)
     265        {
     266          formatter = (Formatter<?>)possibleFormatter;
     267        }
     268      }
    255269      ExportedProperty ep = ExportedProperty.parse(dc, column, columnPrefix,
    256270        annotatable, formatter, sameUnits);
     
    260274   
    261275    // Get the query from the current context
    262     ItemContext cc = sc.getCurrentContext(itemType, subContext);
    263276    Query query = cc.getQuery();
    264277    Set<Integer>selectedItems = cc.getSelected();
Note: See TracChangeset for help on using the changeset viewer.