Changeset 3498
- Timestamp:
- Sep 18, 2015, 2:22:06 PM (8 years ago)
- 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 125 125 </settings> 126 126 </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> 130 130 <description> 131 131 Plug-in for combining multiple existing 132 Gene reports to a single PDFand adding132 reports to a single PDF/ZIP file and adding 133 133 personal information about the patients. 134 134 </description> -
extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/plugins/GeneReportsCombinerPlugin.java
r3129 r3498 1 1 package net.sf.basedb.reggie.plugins; 2 2 3 import java.io.Closeable; 3 4 import java.io.IOException; 4 5 import java.io.InputStream; … … 8 9 import java.util.List; 9 10 import java.util.Set; 11 import java.util.zip.ZipEntry; 12 import java.util.zip.ZipOutputStream; 10 13 11 14 import net.sf.basedb.core.AnyToAny; … … 41 44 import net.sf.basedb.reggie.r.GeneReport; 42 45 import net.sf.basedb.util.Enumeration; 46 import net.sf.basedb.util.FileUtil; 43 47 import net.sf.basedb.util.NameableComparator; 44 48 … … 123 127 storeValue(job, request, ri.getParameter("whichItems")); 124 128 storeValue(job, request, ri.getParameter("filename")); 129 storeValue(job, request, ri.getParameter("format")); 125 130 126 131 String whichItems = (String)request.getParameterValue("whichItems"); … … 157 162 job.setValues("rawBioAssays", new ItemParameterType<RawBioAssay>(RawBioAssay.class, null, true, 0, null), rawBioAssays); 158 163 159 response.setSuggestedJobName("Combine gene reportfor " + rawBioAssays.size() + " raw bioassays");164 response.setSuggestedJobName("Combine reports for " + rawBioAssays.size() + " raw bioassays"); 160 165 response.setDownloadImmediately("The job configuration is complete", ExecutionTime.SHORTEST, true); 161 166 } … … 196 201 parameters.add(new PluginParameter<String> 197 202 ( 198 "whichItems", "Which raw bioassays", "The raw bioassays to create a combined Genereport for.", defaultWhich,203 "whichItems", "Which raw bioassays", "The raw bioassays to create a combined report for.", defaultWhich, 199 204 new StringParameterType(255, defaultWhich, true, 1, 0, 0, options) 200 205 )); 201 206 parameters.add(new PluginParameter<String> 202 207 ( 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, 204 209 new StringParameterType(255, GeneReport.DEFAULT_PDF_NAME, true) 205 210 )); 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 )); 206 223 207 224 configureJob = new RequestInformation 208 225 ( 209 226 Request.COMMAND_CONFIGURE_JOB, 210 " Gene report options",211 "Set options for combining the Genereport PDF:s",227 "Report options", 228 "Set options for combining the report PDF:s", 212 229 parameters 213 230 ); … … 248 265 filename = Reggie.getConfig().getConfig("rscript/gene-report/pdf-name", null, GeneReport.DEFAULT_PDF_NAME); 249 266 } 250 out.setFilename(filename);251 out.setMimeType("application/pdf");252 267 253 268 List<RawBioAssay> rawBioAssays = (List<RawBioAssay>)job.getValues("rawBioAssays"); … … 256 271 ItemSubtype specimenType = Subtype.SPECIMEN.get(dc); 257 272 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 259 277 try 260 278 { 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 } 262 287 263 288 int total = rawBioAssays.size(); … … 276 301 // Get existing PDF 277 302 InputStream existingPdf = null; 303 File file = null; 278 304 try 279 305 { … … 281 307 if (link.getToType() == Item.FILE) 282 308 { 283 File f= (File)link.getTo();284 existingPdf = f .getDownloadStream(0);309 file = (File)link.getTo(); 310 existingPdf = file.getDownloadStream(0); 285 311 } 286 312 } … … 288 314 {} 289 315 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); 292 330 skipped++; 293 331 } 294 else295 {296 // Load existing PDF297 pdf.importPdf(existingPdf, 0, 0, 1, 1);298 pdf.getDocument().newPage();299 combined++;300 }301 332 } 302 333 303 334 if (progress != null) 304 335 { 305 String msg = "Combined genereports for " + combined + " raw bioassays.";336 String msg = "Combined reports for " + combined + " raw bioassays."; 306 337 if (skipped > 0) msg += " Skipped " + skipped + " that was not found."; 307 338 progress.display(100, msg); … … 310 341 finally 311 342 { 343 FileUtil.close(combiner); 312 344 out.flush(); 313 if (pdf != null) pdf.close();314 345 } 315 346 } … … 324 355 protected String getSuccessMessage() 325 356 { 326 String msg = "Combined genereports for " + combined + " raw bioassays.";357 String msg = "Combined reports for " + combined + " raw bioassays."; 327 358 if (skipped > 0) msg += " Skipped " + skipped + " that was not found."; 328 359 return msg; 329 360 } 330 361 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 } 333 463 }
Note: See TracChangeset
for help on using the changeset viewer.