Changeset 3991


Ignore:
Timestamp:
Nov 22, 2007, 2:06:17 PM (14 years ago)
Author:
Martin Svensson
Message:

Fixes #827 Reporters can now be exported through web services. Two new methods in ReporterService? are added, one that exports reporters in a reporters list and one that exports all reports. The reporters are exported to a tab-separated text file.

Location:
trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/build.xml

    r3978 r3991  
    966966      <pathelement location="${core.build}"/>
    967967      <pathelement location="${coreplugins.build}"/>
     968      <pathelement location="${web.build}"/>
    968969    </path>
    969970    <path id="webservices.client.classpath"
  • trunk/src/clients/web/net/sf/basedb/clients/web/plugins/SimpleExport.java

    r3985 r3991  
    3232import net.sf.basedb.core.DbControl;
    3333import net.sf.basedb.core.DynamicQuery;
     34import net.sf.basedb.core.InvalidUseOfNullException;
    3435import net.sf.basedb.core.Item;
    3536import net.sf.basedb.core.ItemContext;
     
    211212  */
    212213  private DbControl dc;
    213   private int numExported;
     214  private String successMessage;
    214215  protected void begin(DbControl dc)
    215216  {
    216217    this.dc = dc;
    217     this.numExported = 0;
     218    this.successMessage = "";
    218219  }
    219220 
     
    233234      new ArrayList<ExportedProperty>(columns.size());
    234235    List<String> properties = new ArrayList<String>(columns.size());
    235     boolean hasAnnotations = false;
    236236    boolean annotatable = itemType.getItemClass() != null &&
    237237      Annotatable.class.isAssignableFrom(itemType.getItemClass());
     
    239239    formatter.registerFormatter(Date.class, FormatterFactory.getDateFormatter(sc));
    240240    for (String column : columns)
    241     {
    242       ExportedProperty ep = ExportedProperty.parse(dc, column, annotatable, formatter);
    243       hasAnnotations |= ep.annotationType != null;
     241    {     
     242      ExportedProperty ep = ExportedProperty.parse(dc, column, annotatable, formatter);     
    244243      exportedProperties.add(ep);
    245       properties.add(ep.name);
    246     }
    247 
    248     // Open stream for output and set file name and MIME type
    249     Writer exportStream = new OutputStreamWriter(out, "UTF-8");
    250     // Create export template for outputting the data
    251     ExportTemplate template = "xml".equals(format) ?
    252       new XMLTemplate(exportStream, exportedProperties, itemType) :
    253       new PlainTextTemplate(exportStream, exportedProperties, itemType);
    254     out.setFilename(itemType.name().toLowerCase()+".export.txt");
    255     out.setMimeType(template.getMimeType());
    256    
    257     // Write start of document and headers
    258     template.beginDocument();
    259     template.writeHeaders();
     244      properties.add(ep.name);     
     245    }   
    260246   
    261247    // Get the query from the current context
    262248    ItemContext cc = sc.getCurrentContext(itemType, subContext);
    263249    Query query = cc.getQuery();
    264     QueryWrapper queryWrapper = null;
    265     ResultIterator result = null;
    266250    Set<Integer>selectedItems = cc.getSelected();
    267    
    268     // The query wrapper allows us to get results from the query in a simplier way
    269     if (query instanceof ItemQuery)
    270     {
    271       queryWrapper = new ItemQueryWrapper(dc, (ItemQuery<BasicItem>)query, itemType);
    272     }
    273     else if (query instanceof DataQuery)
    274     {
    275       String entityName = null;
    276       if (itemType == Item.RAWDATA)
    277       {
    278         RawBioAssay rba = RawBioAssay.getById(dc, sc.getCurrentContext(Item.RAWBIOASSAY).getId());
    279         entityName = rba.getRawDataType().getEntityName();
    280       }
    281       queryWrapper = new DataQueryWrapper(dc, (DataQuery<BasicData>)query, itemType, entityName);
    282     }
    283     else if (query instanceof ReporterScoreQuery)
    284     {
    285       queryWrapper = new ReporterScoreQueryWrapper(dc, (ReporterScoreQuery)query);
    286     }
    287     else if (query instanceof DynamicQuery)
    288     {
    289       DynamicQuery dynamicQuery = (DynamicQuery)query;
    290       cc.configureQuery(dc, dynamicQuery, properties);
    291       queryWrapper = new DynamicQueryWrapper(dc, dynamicQuery);
     251    if (query instanceof DynamicQuery)
     252    {     
     253      cc.configureQuery(dc, (DynamicQuery)query, properties);
    292254    }
    293255   
    294256    // Configure the query with additional filters depending on which items should be exported
    295257    query.setReturnTotalCount(false);
    296     boolean needToStep = false;
     258    //boolean needToStep = false;
    297259    int itemsPerStep = Config.getInt("export.max.items", 0);
    298260    // Which of "all", "current page", or "selected" is selected?
    299261    long totalItems = 0;
    300262    if ("all".equals(whichItems))
    301     {
    302       needToStep = itemsPerStep > 0;
     263    {
    303264      query.setFirstResult(0);
    304265      query.setMaxResults(itemsPerStep);
     
    337298      query.setParameter("_excludes_", Arrays.asList(Values.getInt(exclude.split(","))), Type.INT);
    338299    }
     300   
     301    exportProperties(dc, out, format, exportedProperties, itemType, query, progress);
     302  }
     303 
     304  protected void end(boolean success)
     305  {
     306    this.dc = null;
     307  }
     308  protected String getSuccessMessage()
     309  {
     310    return successMessage;
     311  }
     312  // -------------------------------------------
     313 
     314  private void initGuiContexts()
     315  {
     316    for (Item i : Item.values())
     317    {
     318      if (i.getDataClass() != null)
     319      {
     320        guiContexts.add(new GuiContext(i, GuiContext.Type.LIST));
     321      }
     322    }
     323    guiContexts.add(new GuiContext(Item.REPORTERSCORE, GuiContext.Type.LIST));
     324    guiContexts.add(new GuiContext(Item.SPOTDATA, GuiContext.Type.LIST));
     325  }
     326 
     327  private RequestInformation getConfigureJobParameters(GuiContext context)
     328  {
     329    if (configureJob == null)
     330    {
     331      List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
     332
     333      parameters.add(getFormatParameter());
     334      parameters.add(getWhichItemsParameter());
     335      parameters.add(getWhichPropertiesParameter());   
     336      parameters.add(getSaveAsParameter(null, null, null, false));
     337      parameters.add(getOverwriteParameter(null, null));
     338     
     339      configureJob = new RequestInformation
     340      (
     341        Request.COMMAND_CONFIGURE_JOB,
     342        "Export " + context.getItem(),
     343        "Set options for which items and which columns that should be exported.",
     344        parameters, "simple_export.jsp"
     345      );
     346    }
     347    return configureJob;
     348  }
     349 
     350  private static final String FORMAT = "format";
     351  private PluginParameter<String> getFormatParameter()
     352  {
     353    String label = "Format";
     354    String description = "You may select to export in text or XML format.";
     355    Enumeration<String, String> options = new Enumeration<String, String>();
     356    options.add("text", "Tab-separated text file");
     357    options.add("xml", "XML");
     358    PluginParameter<String> formatParameter = new PluginParameter<String>
     359      (
     360        FORMAT, label, description, "text",
     361        new StringParameterType(255, "text", true, 1, 0, 0, options)
     362      );
     363    return formatParameter;
     364  }
     365
     366  private static final String WHICH_ITEMS = "whichItems";
     367  private PluginParameter<String> getWhichItemsParameter()
     368  {
     369    String label = "Which items?";
     370    String description = "Select which items you want to export";
     371    Enumeration<String, String> options = new Enumeration<String, String>();
     372    options.add("selected", "Selected items");
     373    options.add("page", "Current page");
     374    options.add("all", "All pages");
     375    PluginParameter<String> whichItemsParameter = new PluginParameter<String>
     376      (
     377        WHICH_ITEMS, label, description, "all",
     378        new StringParameterType(255, "all", true, 1, 0, 0, options)
     379      );
     380    return whichItemsParameter;
     381  }
     382
     383  private static final String WHICH_PROPERTIES = "whichProperties";
     384  private PluginParameter<String> getWhichPropertiesParameter()
     385  {
     386    String label = "Which properties?";
     387    String description = "Enter the names of the properties you want to export. " +
     388      "Use a colon to separate an optional title from the property name. For example: " +
     389      "name:Name of sample";
     390    PluginParameter<String> whichPropertiesParameter = new PluginParameter<String>
     391      (
     392        WHICH_PROPERTIES, label, description, null,
     393        new StringParameterType(255, null, true, 0, 0, 0)
     394      );
     395    return whichPropertiesParameter;
     396  }
     397 
     398  /**
     399    Exports properties from a query to an output stream. 
     400    The export can be either tab-separated text or XML.
     401      @param dc The DbControl to use when accessing the database
     402      @param out Output stream to export to.
     403      @param format Use 'xml' to export in XML-format. All other inputs will give a tab-separated format.
     404      @param exportedProperties A list of which properties to include in the export.
     405      @param itemType Kind of item the export is about
     406      @param query This query is used to get the items to export.
     407      @param progress Optional progress reporter to get overview of how the export is proceeding.
     408      @throws IOException If anything goes wrong with export to the output stream.
     409   */
     410  @SuppressWarnings("unchecked")
     411  public void exportProperties(DbControl dc, ExportOutputStream out, String format, List<ExportedProperty> exportedProperties,
     412      Item itemType, Query query, ProgressReporter progress)
     413    throws IOException
     414  {
     415    if (dc == null) throw new InvalidUseOfNullException("dc");
     416    if (out == null) throw new InvalidUseOfNullException("out");
     417    if (exportedProperties == null) throw new InvalidUseOfNullException("exportedProperties");
     418    if (itemType == null) throw new InvalidUseOfNullException("itemType");
     419    if (query == null) throw new InvalidUseOfNullException("query");
     420   
     421    this.dc = dc;
     422    boolean hasAnnotations = false;
     423    int itemsPerStep = Config.getInt("export.max.items", 0);
     424    boolean needToStep = query.getMaxResults() > 0 ? (itemsPerStep > 0) : false;
     425    long totalItems = query.getMaxResults() == 0 ? query.count(dc) : query.getMaxResults();
     426   
     427    for (ExportedProperty ep : exportedProperties)
     428    {
     429      hasAnnotations |= ep.annotationType != null;
     430    }
     431    QueryWrapper queryWrapper = null;
     432    // The query wrapper allows us to get results from the query in a simplier way
     433    if (query instanceof ItemQuery)
     434    {
     435      queryWrapper = new ItemQueryWrapper(dc, (ItemQuery<BasicItem>)query, itemType);
     436    }
     437    else if (query instanceof DataQuery)
     438    {
     439      String entityName = null;
     440      if (itemType == Item.RAWDATA)
     441      {
     442        RawBioAssay rba = RawBioAssay.getById(dc, sc.getCurrentContext(Item.RAWBIOASSAY).getId());
     443        entityName = rba.getRawDataType().getEntityName();
     444      }
     445      queryWrapper = new DataQueryWrapper(dc, (DataQuery<BasicData>)query, itemType, entityName);
     446    }
     447    else if (query instanceof ReporterScoreQuery)
     448    {
     449      queryWrapper = new ReporterScoreQueryWrapper(dc, (ReporterScoreQuery)query);
     450    }
     451    else if (query instanceof DynamicQuery)
     452    {
     453      DynamicQuery dynamicQuery = (DynamicQuery)query;     
     454      queryWrapper = new DynamicQueryWrapper(dc, dynamicQuery);
     455    }
     456   
     457    // Open stream for output and set file name and MIME type
     458    Writer exportStream = new OutputStreamWriter(out, "UTF-8");
     459    // Create export template for outputting the data
     460    ExportTemplate template = "xml".equals(format) ?
     461      new XMLTemplate(exportStream, exportedProperties, itemType) :
     462      new PlainTextTemplate(exportStream, exportedProperties, itemType);
     463    out.setFilename(itemType.name().toLowerCase()+".export.txt");
     464    out.setMimeType(template.getMimeType());
     465   
     466    // Write start of document and headers
     467    template.beginDocument();
     468    template.writeHeaders();
     469   
     470    int numExported = 0;
     471    ResultIterator result = null;
    339472    try
    340473    {
     
    384517      while (needToStep && (numExported % itemsPerStep == 0));
    385518      template.endDocument();
     519      successMessage = numExported + " items were exported successfully";
    386520    }
    387521    catch (Exception ex)
     
    396530    }
    397531  }
    398  
    399   protected void end(boolean success)
    400   {
    401     this.dc = null;
    402   }
    403   protected String getSuccessMessage()
    404   {
    405     return numExported + " items were exported successfully";
    406   }
    407   // -------------------------------------------
    408  
    409   private void initGuiContexts()
    410   {
    411     for (Item i : Item.values())
    412     {
    413       if (i.getDataClass() != null)
    414       {
    415         guiContexts.add(new GuiContext(i, GuiContext.Type.LIST));
    416       }
    417     }
    418     guiContexts.add(new GuiContext(Item.REPORTERSCORE, GuiContext.Type.LIST));
    419     guiContexts.add(new GuiContext(Item.SPOTDATA, GuiContext.Type.LIST));
    420   }
    421  
    422   private RequestInformation getConfigureJobParameters(GuiContext context)
    423   {
    424     if (configureJob == null)
    425     {
    426       List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
    427 
    428       parameters.add(getFormatParameter());
    429       parameters.add(getWhichItemsParameter());
    430       parameters.add(getWhichPropertiesParameter());   
    431       parameters.add(getSaveAsParameter(null, null, null, false));
    432       parameters.add(getOverwriteParameter(null, null));
    433      
    434       configureJob = new RequestInformation
    435       (
    436         Request.COMMAND_CONFIGURE_JOB,
    437         "Export " + context.getItem(),
    438         "Set options for which items and which columns that should be exported.",
    439         parameters, "simple_export.jsp"
    440       );
    441     }
    442     return configureJob;
    443   }
    444  
    445   private static final String FORMAT = "format";
    446   private PluginParameter<String> getFormatParameter()
    447   {
    448     String label = "Format";
    449     String description = "You may select to export in text or XML format.";
    450     Enumeration<String, String> options = new Enumeration<String, String>();
    451     options.add("text", "Tab-separated text file");
    452     options.add("xml", "XML");
    453     PluginParameter<String> formatParameter = new PluginParameter<String>
    454       (
    455         FORMAT, label, description, "text",
    456         new StringParameterType(255, "text", true, 1, 0, 0, options)
    457       );
    458     return formatParameter;
    459   }
    460 
    461   private static final String WHICH_ITEMS = "whichItems";
    462   private PluginParameter<String> getWhichItemsParameter()
    463   {
    464     String label = "Which items?";
    465     String description = "Select which items you want to export";
    466     Enumeration<String, String> options = new Enumeration<String, String>();
    467     options.add("selected", "Selected items");
    468     options.add("page", "Current page");
    469     options.add("all", "All pages");
    470     PluginParameter<String> whichItemsParameter = new PluginParameter<String>
    471       (
    472         WHICH_ITEMS, label, description, "all",
    473         new StringParameterType(255, "all", true, 1, 0, 0, options)
    474       );
    475     return whichItemsParameter;
    476   }
    477 
    478   private static final String WHICH_PROPERTIES = "whichProperties";
    479   private PluginParameter<String> getWhichPropertiesParameter()
    480   {
    481     String label = "Which properties?";
    482     String description = "Enter the names of the properties you want to export. " +
    483       "Use a colon to separate an optional title from the property name. For example: " +
    484       "name:Name of sample";
    485     PluginParameter<String> whichPropertiesParameter = new PluginParameter<String>
    486       (
    487         WHICH_PROPERTIES, label, description, null,
    488         new StringParameterType(255, null, true, 0, 0, 0)
    489       );
    490     return whichPropertiesParameter;
    491   }
    492  
    493532}
  • trunk/src/test/TestWebservices.java

    r3978 r3991  
    613613          "--No extended properties were received");
    614614     
    615       write("--Test of getReporterLists OK");
     615      if (listInfo.length > 0 && extPropertyInfos.length > 0)
     616      {       
     617        System.out.print("--Download of reporters in reporterlist '" + listInfo[0].getName() +"'...." );
     618        File receivedFile = new File(listInfo[0].getName() + ".txt");
     619        OutputStream out = new FileOutputStream(receivedFile);
     620         
     621        InputStream in = reporterClient.downloadReportersInList(listInfo[0].getId(), extPropertyInfos, new QueryOptions());
     622       
     623        if (in == null)
     624        {
     625          write ("FAILED InputStream==null");
     626          ok = false;
     627        }
     628        else
     629        {
     630          FileUtil.copy(in, out);
     631          in.close();
     632          out.close();
     633          write("OK; size=" + receivedFile.length() + " bytes");
     634          receivedFile.delete();
     635        }
     636       
     637        receivedFile = null;
     638        out = null;
     639       
     640        //Test to receive reporters from service, at most 50 reporters       
     641        System.out.print("--Download of reporters....");       
     642        receivedFile = new File("ReporterFile.txt");
     643        out = new FileOutputStream(receivedFile);
     644        in = null;
     645        QueryOptions qOpt = new QueryOptions();
     646        in = reporterClient.downloadReporters(extPropertyInfos, qOpt);
     647       
     648        if (in == null)
     649        {
     650          write ("FAILED InputStream==null");
     651          ok = false;
     652        }
     653        else
     654        {
     655          FileUtil.copy(in, out);
     656          in.close();
     657          out.close();
     658          write("OK; size=" + receivedFile.length() + " bytes");
     659          receivedFile.delete();
     660        }
     661      }
     662       
     663      write("--Test of ReporterService OK");
    616664    }
    617665    catch (Throwable t)
  • trunk/src/webservices/client/java/net/sf/basedb/ws/client/ReporterClient.java

    r3978 r3991  
    2525
    2626import net.sf.basedb.info.ExtendedPropertyInfo;
     27import net.sf.basedb.info.QueryOptions;
     28
     29import java.io.IOException;
     30import java.io.InputStream;
    2731
    2832import org.apache.axis2.AxisFault;
     33import org.apache.axis2.Constants;
     34import org.apache.axis2.client.Options;
     35import org.apache.axis2.rpc.client.RPCServiceClient;
    2936
    3037/**
     
    6168  }
    6269
     70  /**
     71    Gets an input stream for a file with info about
     72    reporters in a reporter list.
     73      @param reporterListId Id of the reporter list to export and download.
     74      @param extPropInfo An array of ExtendedPropertyInfo objects to include in the export/download.
     75      @return InputStream object to the exported data.
     76      @throws IOException If something goes wrong with the input stream
     77   */
     78  public InputStream downloadReportersInList(int reporterListId, ExtendedPropertyInfo[] extPropInfo, QueryOptions qOpt)
     79    throws IOException
     80  {
     81    RPCServiceClient service = getService();
     82    Options options = service.getOptions();   
     83    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
     84    options.setTimeOutInMilliSeconds(10000);
     85    service.setOptions(options);
     86    return invokeFileBlocking
     87      (
     88        "downloadReportersInList",
     89        session.getId(),
     90        reporterListId,
     91        extPropInfo,
     92        qOpt
     93      );
     94  }
     95 
     96  public InputStream downloadReporters(ExtendedPropertyInfo[] extPropInfo, QueryOptions qOpt)
     97    throws IOException
     98  {
     99    RPCServiceClient service = getService();
     100    Options options = service.getOptions();   
     101    options.setProperty(Constants.Configuration.ENABLE_MTOM, Constants.VALUE_TRUE);
     102    options.setTimeOutInMilliSeconds(10000);
     103    service.setOptions(options);
     104    return invokeFileBlocking
     105      (
     106        "downloadReporters",
     107        session.getId(),
     108        extPropInfo,
     109        qOpt
     110      );
     111  }
     112
    63113}
  • trunk/src/webservices/server/net/sf/basedb/ws/server/ReporterService.java

    r3978 r3991  
    2424package net.sf.basedb.ws.server;
    2525
     26import net.sf.basedb.clients.web.formatter.FormatterFactory;
     27import net.sf.basedb.clients.web.plugins.ExportedProperty;
     28import net.sf.basedb.clients.web.plugins.SimpleExport;
     29import net.sf.basedb.core.DbControl;
    2630import net.sf.basedb.core.ExtendedProperties;
    2731import net.sf.basedb.core.ExtendedProperty;
     32import net.sf.basedb.core.Item;
     33import net.sf.basedb.core.Reporter;
     34import net.sf.basedb.core.ReporterList;
     35import net.sf.basedb.core.ReporterScoreQuery;
     36import net.sf.basedb.core.SessionControl;
     37import net.sf.basedb.core.plugin.ExportOutputStream;
     38import net.sf.basedb.core.query.Hql;
     39import net.sf.basedb.core.query.Query;
    2840import net.sf.basedb.info.ExtendedPropertyInfo;
     41import net.sf.basedb.info.QueryOptions;
     42import net.sf.basedb.util.formatter.MultiFormatter;
     43import net.sf.basedb.util.formatter.ToStringFormatter;
    2944
     45import java.io.File;
     46import java.io.FileOutputStream;
     47import java.util.ArrayList;
     48import java.util.Date;
    3049import java.util.List;
     50
     51import javax.activation.DataSource;
     52
     53import org.apache.axiom.om.OMElement;
    3154
    3255/**
     
    5780    }
    5881  }
     82 
     83  /**
     84    Gets an input stream to exported data from a reporter list.
     85      @param ID Id of an active session.
     86      @param reporterListId Id of the reporter list to download reporters information from.
     87      @param extPropInfos The extended property to include in the download.
     88      @return java.io.InputStream object to the exported data.
     89      @throws Exception If something goes wrong with the export.
     90   */
     91  public OMElement downloadReportersInList(String ID, int reporterListId, ExtendedPropertyInfo[] extPropInfos, QueryOptions qOpt)
     92    throws Exception
     93  {   
     94    SessionControl sc = getSessionControl(ID);
     95    DbControl dc = sc.newDbControl();
     96   
     97    ExportOutputStream eos = null;
     98    File exportFile = null;   
     99    OMElement elementWithAttachment = null;
     100    String elementName = "exportFile";
     101    String format = "text";
     102    Item itemType = Item.REPORTERSCORE;
     103   
     104    boolean annotatable = false;
     105    MultiFormatter formatter = new MultiFormatter(new ToStringFormatter(), true);
     106    formatter.registerFormatter(Date.class, FormatterFactory.getDateFormatter(sc));
     107    List<ExportedProperty> exportedProperties = new ArrayList<ExportedProperty>(extPropInfos.length);   
     108    //Fill the list with properties to export, name, external id and reporter type are always included.
     109    exportedProperties.add(ExportedProperty.parse(dc, "reporter.name:Name", annotatable, formatter));
     110    exportedProperties.add(ExportedProperty.parse(dc, "reporter.externalId:External ID", annotatable, formatter));
     111    exportedProperties.add(ExportedProperty.parse(dc, "reporter.reporterType.name:Reporter type", annotatable, formatter));
     112    exportedProperties.add(ExportedProperty.parse(dc, "score:Score", annotatable, formatter));
     113    for (int i=0; i<extPropInfos.length; i++)
     114    {
     115      String propertyString = "reporter."+extPropInfos[i].getName() + ":" + extPropInfos[i].getTitle();
     116      ExportedProperty ep = ExportedProperty.parse(dc, propertyString, annotatable, formatter);
     117      exportedProperties.add(ep);
     118    }
     119   
     120    //Get and configure the query that select the reporters in the reporter list.
     121    ReporterList reporterList = ReporterList.getById(dc, reporterListId);
     122    ReporterScoreQuery query = reporterList.getReporterScores();
     123    if (qOpt != null && qOpt.getRestriction() != null)
     124    {
     125      query.restrict(Hql.restriction(qOpt.getRestriction(), qOpt.getRestrictionPrefix()));
     126    }
     127   
     128    //Do the export to a temporary file and attach this file to the message that will be returned to the client.
     129    try
     130    {
     131      SimpleExport exporter = new SimpleExport();
     132      exportFile = File.createTempFile("ReporterListFile", null);
     133      eos = new ExportOutputStream(new FileOutputStream(exportFile));     
     134      exporter.exportProperties(dc, eos, format, exportedProperties, itemType, query, null);
     135      eos.close();
     136     
     137      DataSource source = new TempFileDataSource(exportFile, "text/plain");
     138      elementWithAttachment = attachFile(source, elementName);
     139    }
     140    finally
     141    {
     142      if (dc != null) dc.close();
     143    }
     144    return elementWithAttachment;
     145  }
     146 
     147  public OMElement downloadReporters(String ID, ExtendedPropertyInfo[] extPropInfos, QueryOptions qOpt)
     148    throws Exception
     149  {
     150    SessionControl sc = getSessionControl(ID);
     151    DbControl dc = sc.newDbControl();
     152   
     153    ExportOutputStream eos = null;
     154    File exportFile = null;   
     155    OMElement elementWithAttachment = null;
     156    String elementName = "exportFile";
     157    String format = "text";
     158    Item itemType = Item.REPORTER;
     159   
     160    boolean annotatable = false;
     161    MultiFormatter formatter = new MultiFormatter(new ToStringFormatter(), true);
     162    formatter.registerFormatter(Date.class, FormatterFactory.getDateFormatter(sc));
     163    List<ExportedProperty> exportedProperties = new ArrayList<ExportedProperty>(extPropInfos.length);   
     164    //Fill the list with properties to export, name, external id and reporter type are always included.
     165    exportedProperties.add(ExportedProperty.parse(dc, "name:Name", annotatable, formatter));
     166    exportedProperties.add(ExportedProperty.parse(dc, "externalId:External ID", annotatable, formatter));
     167    exportedProperties.add(ExportedProperty.parse(dc, "reporterType.name:Reporter type", annotatable, formatter));
     168    for (int i=0; i<extPropInfos.length; i++)
     169    {
     170      String propertyString = extPropInfos[i].getName() + ":" + extPropInfos[i].getTitle();
     171      ExportedProperty ep = ExportedProperty.parse(dc, propertyString, annotatable, formatter);
     172      exportedProperties.add(ep);
     173    }
     174   
     175    Query query = Reporter.getQuery();   
     176    if (qOpt != null && qOpt.getRestriction() != null)
     177    {
     178      query.restrict(Hql.restriction(qOpt.getRestriction(), qOpt.getRestrictionPrefix()));
     179    }
     180   
     181    //Do the export to a temporary file and attach this file to the message that will be returned to the client.
     182    try
     183    {
     184      SimpleExport exporter = new SimpleExport();
     185      exportFile = File.createTempFile("ReporterFile", null);
     186      eos = new ExportOutputStream(new FileOutputStream(exportFile));     
     187      exporter.exportProperties(dc, eos, format, exportedProperties, itemType, query, null);
     188      eos.close();     
     189      DataSource source = new TempFileDataSource(exportFile, "text/plain");
     190      elementWithAttachment = attachFile(source, elementName);
     191    }
     192    finally
     193    {
     194      if (dc != null) dc.close();
     195    }
     196    return elementWithAttachment;
     197  }
    59198}
Note: See TracChangeset for help on using the changeset viewer.