Changeset 3498


Ignore:
Timestamp:
Sep 18, 2015, 2:22:06 PM (8 years ago)
Author:
Nicklas Nordborg
Message:

References #810: Gene report combiner plug-in should be able to combine to a zip file

The plug-in now has support for combining to a single PDF or to a ZIP file. Other formats should be relatively easy to implement.

Preparations for allowing the combiner to combine any report has been made. Actually, by manually changing the file name to something else (eg. pilotreport.pdf) it is already possible to do so. But this should probably be implemented as different configurations to make it easier to use.

Location:
extensions/net.sf.basedb.reggie/trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • extensions/net.sf.basedb.reggie/trunk/META-INF/extensions.xml

    r3492 r3498  
    125125    </settings>
    126126  </plugin-definition>
    127   <plugin-definition id="GeneReportsCombinerPlugin">
    128     <about>
    129       <name>Gene reports combiner</name>
     127  <plugin-definition id="ReportsCombinerPlugin">
     128    <about>
     129      <name>Reports combiner</name>
    130130      <description>
    131131        Plug-in for combining multiple existing
    132         Gene reports to a single PDF and adding
     132        reports to a single PDF/ZIP file and adding
    133133        personal information about the patients.
    134134      </description>
  • extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/plugins/GeneReportsCombinerPlugin.java

    r3129 r3498  
    11package net.sf.basedb.reggie.plugins;
    22
     3import java.io.Closeable;
    34import java.io.IOException;
    45import java.io.InputStream;
     
    89import java.util.List;
    910import java.util.Set;
     11import java.util.zip.ZipEntry;
     12import java.util.zip.ZipOutputStream;
    1013
    1114import net.sf.basedb.core.AnyToAny;
     
    4144import net.sf.basedb.reggie.r.GeneReport;
    4245import net.sf.basedb.util.Enumeration;
     46import net.sf.basedb.util.FileUtil;
    4347import net.sf.basedb.util.NameableComparator;
    4448
     
    123127        storeValue(job, request, ri.getParameter("whichItems"));
    124128        storeValue(job, request, ri.getParameter("filename"));
     129        storeValue(job, request, ri.getParameter("format"));
    125130
    126131        String whichItems = (String)request.getParameterValue("whichItems");
     
    157162          job.setValues("rawBioAssays", new ItemParameterType<RawBioAssay>(RawBioAssay.class, null, true, 0, null), rawBioAssays);
    158163         
    159           response.setSuggestedJobName("Combine gene report for " + rawBioAssays.size() + " raw bioassays");
     164          response.setSuggestedJobName("Combine reports for " + rawBioAssays.size() + " raw bioassays");
    160165          response.setDownloadImmediately("The job configuration is complete", ExecutionTime.SHORTEST, true);
    161166        }
     
    196201      parameters.add(new PluginParameter<String>
    197202        (
    198           "whichItems", "Which raw bioassays", "The raw bioassays to create a combined Gene report for.", defaultWhich,
     203          "whichItems", "Which raw bioassays", "The raw bioassays to create a combined report for.", defaultWhich,
    199204          new StringParameterType(255, defaultWhich, true, 1, 0, 0, options)
    200205        ));
    201206      parameters.add(new PluginParameter<String>
    202207        (
    203           "filename", "Filename", "The filename to look for when searching for existing gene reporter.", GeneReport.DEFAULT_PDF_NAME,
     208          "filename", "Report filename", "The filename to look for when searching for existing reports.", GeneReport.DEFAULT_PDF_NAME,
    204209          new StringParameterType(255, GeneReport.DEFAULT_PDF_NAME, true)
    205210        ));
     211     
     212      String defaultCombine = "PDF";
     213      Enumeration<String, String> combineOptions = new Enumeration<String, String>();
     214      combineOptions.add("PDF", "PDF");
     215      combineOptions.add("ZIP", "ZIP");
     216      parameters.add(new PluginParameter<String>
     217      (
     218        "format", "Format", "Select the output format for the combined reports\n\n"
     219            + "PDF = Creates a single PDF with one page for each report\n"
     220            + "ZIP = Creates a ZIP file with one file for each report", defaultCombine,
     221        new StringParameterType(255, defaultCombine, true, 1, 0, 0, combineOptions)
     222      ));     
    206223     
    207224      configureJob = new RequestInformation
    208225      (
    209226        Request.COMMAND_CONFIGURE_JOB,
    210         "Gene report options",
    211         "Set options for combining the Gene report PDF:s",
     227        "Report options",
     228        "Set options for combining the report PDF:s",
    212229        parameters
    213230      );
     
    248265      filename = Reggie.getConfig().getConfig("rscript/gene-report/pdf-name", null, GeneReport.DEFAULT_PDF_NAME);
    249266    }
    250     out.setFilename(filename);
    251     out.setMimeType("application/pdf");
    252267   
    253268    List<RawBioAssay> rawBioAssays = (List<RawBioAssay>)job.getValues("rawBioAssays");
     
    256271    ItemSubtype specimenType = Subtype.SPECIMEN.get(dc);
    257272    ItemSubtype patientType = Subtype.PATIENT.get(dc);
    258     PdfUtil pdf = new PdfUtil("Combined gene report: " + rawBioAssays.size() + " pages", null);
     273   
     274    String format = (String)job.getValue("format");
     275    PdfCombiner combiner = null;
     276   
    259277    try
    260278    {
    261       pdf.open(out);
     279      if ("ZIP".equals(format))
     280      {
     281        combiner = new PdfToZipCombiner(filename, out);
     282      }
     283      else
     284      {
     285        combiner = new PdfToPdfCombiner(filename, out);
     286      }
    262287     
    263288      int total = rawBioAssays.size();
     
    276301        // Get existing PDF
    277302        InputStream existingPdf = null;
     303        File file = null;
    278304        try
    279305        {
     
    281307          if (link.getToType() == Item.FILE)
    282308          {
    283             File f = (File)link.getTo();
    284             existingPdf = f.getDownloadStream(0);
     309            file = (File)link.getTo();
     310            existingPdf = file.getDownloadStream(0);
    285311          }
    286312        }
     
    288314        {}
    289315       
    290         if (existingPdf == null)
    291         {
     316        try
     317        {
     318          if (existingPdf != null && combiner.addFile(rba, file, existingPdf))
     319          {
     320            combined++;
     321          }
     322          else
     323          {
     324            skipped++;
     325          }
     326        }
     327        catch (IOException ex)
     328        {
     329          ex.printStackTrace(System.out);
    292330          skipped++;
    293331        }
    294         else
    295         {
    296           // Load existing PDF
    297           pdf.importPdf(existingPdf, 0, 0, 1, 1);
    298           pdf.getDocument().newPage();
    299           combined++;
    300         }
    301332      }
    302333     
    303334      if (progress != null)
    304335      {
    305         String msg = "Combined gene reports for " + combined + " raw bioassays.";
     336        String msg = "Combined reports for " + combined + " raw bioassays.";
    306337        if (skipped > 0) msg += " Skipped " + skipped + " that was not found.";
    307338        progress.display(100, msg);
     
    310341    finally
    311342    {
     343      FileUtil.close(combiner);
    312344      out.flush();
    313       if (pdf != null) pdf.close();
    314345    }
    315346  }
     
    324355  protected String getSuccessMessage()
    325356  {
    326     String msg = "Combined gene reports for " + combined + " raw bioassays.";
     357    String msg = "Combined reports for " + combined + " raw bioassays.";
    327358    if (skipped > 0) msg += " Skipped " + skipped + " that was not found.";
    328359    return msg;
    329360  }
    330361
    331  
    332  
     362  /**
     363    Abstract base class for combining multiple PDF:s into a single file.
     364  */
     365  static abstract class PdfCombiner
     366    implements Closeable
     367  {
     368   
     369    /**
     370      Add a PDF file to the combined file.
     371      @param raw The raw bioassay that the pdf file is related to
     372      @param file The file item representing the pdf file
     373      @param pdf The input stream reading the pdf file.
     374      @return TRUE if the file was added, FALSE if not
     375      @throws IOException
     376    */
     377    abstract boolean addFile(RawBioAssay raw, File file, InputStream pdf)
     378      throws IOException;
     379   
     380  }
     381 
     382  /**
     383    Combine PDF files info a ZIP archive. The file names in the ZIP
     384    are set from the raw bioassay.
     385  */
     386  static class PdfToZipCombiner
     387    extends PdfCombiner
     388  {
     389
     390    private ZipOutputStream zip;
     391   
     392    PdfToZipCombiner(String filename, ExportOutputStream out)
     393    {
     394      out.setFilename(filename.substring(0, filename.lastIndexOf("."))+".zip");
     395      out.setMimeType("application/zip");
     396      zip = new ZipOutputStream(out);
     397      zip.setMethod(ZipOutputStream.DEFLATED);
     398    }
     399   
     400    @Override
     401    public void close()
     402      throws IOException
     403    {
     404      zip.flush();
     405      zip.close();
     406      zip = null;
     407    }
     408
     409    @Override
     410    boolean addFile(RawBioAssay raw, File file, InputStream pdf)
     411      throws IOException
     412    {
     413      ZipEntry entry = new ZipEntry(raw.getName()+".pdf");
     414      entry.setSize(file.getSize());
     415      if (file.getLastUpdate() != null)
     416      {
     417        entry.setTime(file.getLastUpdate().getTime());
     418      }
     419      zip.putNextEntry(entry);
     420     
     421      FileUtil.copy(pdf, zip);
     422      zip.flush();
     423      zip.closeEntry();
     424      return true;
     425    }
     426   
     427  }
     428 
     429  /**
     430    Combine PDF files into another PDF file.
     431  */
     432  static class PdfToPdfCombiner
     433    extends PdfCombiner
     434  {
     435    private PdfUtil pdfOut;
     436   
     437    PdfToPdfCombiner(String filename, ExportOutputStream out)
     438    {
     439      out.setFilename(filename);
     440      out.setMimeType("application/pdf");
     441      pdfOut = new PdfUtil("Combined report ", null);
     442      pdfOut.open(out);
     443    }
     444   
     445    @Override
     446    public void close()
     447      throws IOException
     448    {
     449      pdfOut.close();
     450      pdfOut = null;
     451    }
     452   
     453    @Override
     454    boolean addFile(RawBioAssay raw, File file, InputStream pdfIn)
     455      throws IOException
     456    {
     457      pdfOut.importPdf(pdfIn, 0, 0, 1, 1);
     458      pdfOut.getDocument().newPage();
     459      return true;
     460    }
     461   
     462  }
    333463}
Note: See TracChangeset for help on using the changeset viewer.