extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/plugins/GeneReportPlugin.java

Code
Comments
Other
Rev Date Author Line
2871 28 Oct 14 nicklas 1 package net.sf.basedb.reggie.plugins;
2871 28 Oct 14 nicklas 2
2871 28 Oct 14 nicklas 3 import java.io.IOException;
2871 28 Oct 14 nicklas 4 import java.util.ArrayList;
2871 28 Oct 14 nicklas 5 import java.util.Arrays;
2871 28 Oct 14 nicklas 6 import java.util.Collections;
2876 30 Oct 14 nicklas 7 import java.util.HashSet;
2871 28 Oct 14 nicklas 8 import java.util.List;
3618 23 Nov 15 nicklas 9 import java.util.Map;
2871 28 Oct 14 nicklas 10 import java.util.Set;
2871 28 Oct 14 nicklas 11
2876 30 Oct 14 nicklas 12 import net.sf.basedb.core.AnyToAny;
2871 28 Oct 14 nicklas 13 import net.sf.basedb.core.BaseException;
3055 19 Dec 14 nicklas 14 import net.sf.basedb.core.BooleanParameterType;
2871 28 Oct 14 nicklas 15 import net.sf.basedb.core.DbControl;
2876 30 Oct 14 nicklas 16 import net.sf.basedb.core.Directory;
2876 30 Oct 14 nicklas 17 import net.sf.basedb.core.File;
2871 28 Oct 14 nicklas 18 import net.sf.basedb.core.Item;
2876 30 Oct 14 nicklas 19 import net.sf.basedb.core.ItemContext;
6023 26 Oct 20 nicklas 20 import net.sf.basedb.core.ItemList;
2871 28 Oct 14 nicklas 21 import net.sf.basedb.core.ItemParameterType;
2876 30 Oct 14 nicklas 22 import net.sf.basedb.core.ItemQuery;
3055 19 Dec 14 nicklas 23 import net.sf.basedb.core.Job;
2871 28 Oct 14 nicklas 24 import net.sf.basedb.core.Path;
3618 23 Nov 15 nicklas 25 import net.sf.basedb.core.Permission;
2871 28 Oct 14 nicklas 26 import net.sf.basedb.core.PluginDefinition;
2871 28 Oct 14 nicklas 27 import net.sf.basedb.core.PluginParameter;
2871 28 Oct 14 nicklas 28 import net.sf.basedb.core.ProgressReporter;
2871 28 Oct 14 nicklas 29 import net.sf.basedb.core.RawBioAssay;
2871 28 Oct 14 nicklas 30 import net.sf.basedb.core.RequestInformation;
6033 29 Oct 20 nicklas 31 import net.sf.basedb.core.Sample;
3055 19 Dec 14 nicklas 32 import net.sf.basedb.core.SessionControl;
3618 23 Nov 15 nicklas 33 import net.sf.basedb.core.Software;
2876 30 Oct 14 nicklas 34 import net.sf.basedb.core.Type;
2871 28 Oct 14 nicklas 35 import net.sf.basedb.core.Job.ExecutionTime;
2876 30 Oct 14 nicklas 36 import net.sf.basedb.core.StringParameterType;
2871 28 Oct 14 nicklas 37 import net.sf.basedb.core.plugin.AbstractExporterPlugin;
2871 28 Oct 14 nicklas 38 import net.sf.basedb.core.plugin.ExportOutputStream;
2871 28 Oct 14 nicklas 39 import net.sf.basedb.core.plugin.GuiContext;
2871 28 Oct 14 nicklas 40 import net.sf.basedb.core.plugin.InteractivePlugin;
2871 28 Oct 14 nicklas 41 import net.sf.basedb.core.plugin.Request;
2871 28 Oct 14 nicklas 42 import net.sf.basedb.core.plugin.Response;
3618 23 Nov 15 nicklas 43 import net.sf.basedb.core.query.Annotations;
2876 30 Oct 14 nicklas 44 import net.sf.basedb.core.query.Expressions;
2876 30 Oct 14 nicklas 45 import net.sf.basedb.core.query.Hql;
3618 23 Nov 15 nicklas 46 import net.sf.basedb.core.query.Orders;
2876 30 Oct 14 nicklas 47 import net.sf.basedb.core.query.Restrictions;
3048 17 Dec 14 nicklas 48 import net.sf.basedb.core.signal.SignalHandler;
3048 17 Dec 14 nicklas 49 import net.sf.basedb.core.signal.SignalTarget;
3048 17 Dec 14 nicklas 50 import net.sf.basedb.core.signal.ThreadSignalHandler;
2871 28 Oct 14 nicklas 51 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 52 import net.sf.basedb.reggie.activity.ActivityDef;
5615 19 Sep 19 nicklas 53 import net.sf.basedb.reggie.activity.CountedActivity;
6186 26 Mar 21 nicklas 54 import net.sf.basedb.reggie.autoconfirm.AutoConfirmService;
3059 19 Dec 14 nicklas 55 import net.sf.basedb.reggie.counter.CounterService;
2871 28 Oct 14 nicklas 56 import net.sf.basedb.reggie.dao.Annotationtype;
2871 28 Oct 14 nicklas 57 import net.sf.basedb.reggie.dao.Rawbioassay;
4665 31 Jan 18 nicklas 58 import net.sf.basedb.reggie.dao.Rawdatatype;
3618 23 Nov 15 nicklas 59 import net.sf.basedb.reggie.dao.Subtype;
3506 23 Sep 15 nicklas 60 import net.sf.basedb.reggie.pdf.PdfReportWorker;
4537 21 Jun 17 nicklas 61 import net.sf.basedb.reggie.pdf.ReportOptions;
6036 02 Nov 20 nicklas 62 import net.sf.basedb.reggie.script.ScriptResult;
3506 23 Sep 15 nicklas 63 import net.sf.basedb.reggie.pdf.PdfReportTemplate;
2876 30 Oct 14 nicklas 64 import net.sf.basedb.util.Enumeration;
2876 30 Oct 14 nicklas 65 import net.sf.basedb.util.FileExportOutputStream;
3055 19 Dec 14 nicklas 66 import net.sf.basedb.util.Values;
2871 28 Oct 14 nicklas 67
2871 28 Oct 14 nicklas 68 public class GeneReportPlugin 
2871 28 Oct 14 nicklas 69   extends AbstractExporterPlugin 
3048 17 Dec 14 nicklas 70   implements InteractivePlugin, SignalTarget  
2871 28 Oct 14 nicklas 71 {
2871 28 Oct 14 nicklas 72
3055 19 Dec 14 nicklas 73   /**
3055 19 Dec 14 nicklas 74     Utility function for creating a BASE job that runs this plugin.
3055 19 Dec 14 nicklas 75     for a given list of raw bioassays.
3055 19 Dec 14 nicklas 76     
3055 19 Dec 14 nicklas 77     Other pararameter are using default values from configuration file.
3055 19 Dec 14 nicklas 78   */
5615 19 Sep 19 nicklas 79   public static Job createReportJob(DbControl dc, List<RawBioAssay> rawBioAssays, Map<String, Software> reportTypes, boolean autoConfirm)
3055 19 Dec 14 nicklas 80   {
3055 19 Dec 14 nicklas 81     SessionControl sc = dc.getSessionControl();
3055 19 Dec 14 nicklas 82     
3499 21 Sep 15 nicklas 83     // Create the job
3055 19 Dec 14 nicklas 84     PluginDefinition geneReportPlugin = PluginDefinition.getByClassName(dc, GeneReportPlugin.class.getName());
3055 19 Dec 14 nicklas 85     Job geneReportJob = Job.getNew(dc, geneReportPlugin, null, null);
3055 19 Dec 14 nicklas 86     dc.saveItem(geneReportJob);
3499 21 Sep 15 nicklas 87
4325 30 Jan 17 nicklas 88     geneReportJob.setItemSubtype(Subtype.REPORT_JOB.get(dc));
3055 19 Dec 14 nicklas 89     geneReportJob.setSendMessage(Values.getBoolean(sc.getUserClientSetting("plugins.sendmessage"), false));
3499 21 Sep 15 nicklas 90     geneReportJob.setName("Report for " + rawBioAssays.size() + " raw bioassays");
3618 23 Nov 15 nicklas 91     geneReportJob.setParameterValues("reports", new StringParameterType(255, null, true, 0, 0, 0), new ArrayList<String>(reportTypes.keySet()));
3055 19 Dec 14 nicklas 92     geneReportJob.setParameterValue("overwrite", new BooleanParameterType(), true);
3055 19 Dec 14 nicklas 93     geneReportJob.setParameterValues("rawBioAssays", 
3055 19 Dec 14 nicklas 94       new ItemParameterType<RawBioAssay>(RawBioAssay.class, null, true, 0, null), rawBioAssays);
5615 19 Sep 19 nicklas 95     if (autoConfirm)
5615 19 Sep 19 nicklas 96     {
6023 26 Oct 20 nicklas 97       geneReportJob.setParameterValue("AutoConfirmHandler", new StringParameterType(), "GeneReportAutoConfirmer");
5615 19 Sep 19 nicklas 98     }
4537 21 Jun 17 nicklas 99     
5669 15 Oct 19 nicklas 100     // Update annotations if supported by the reports
5669 15 Oct 19 nicklas 101     geneReportJob.setParameterValue("updateAnnotations", new BooleanParameterType(), true);
3055 19 Dec 14 nicklas 102     geneReportJob.setScheduled(null, null);
3499 21 Sep 15 nicklas 103   
3499 21 Sep 15 nicklas 104     // Link job with AnyToAny items
3618 23 Nov 15 nicklas 105     for (Map.Entry<String, Software> entry : reportTypes.entrySet())
3055 19 Dec 14 nicklas 106     {
3618 23 Nov 15 nicklas 107       String reportType = entry.getKey();
3618 23 Nov 15 nicklas 108       Software software = entry.getValue();
3618 23 Nov 15 nicklas 109       
3618 23 Nov 15 nicklas 110       if (software != null) 
3618 23 Nov 15 nicklas 111       {
3618 23 Nov 15 nicklas 112         geneReportJob.setParameterValue("software."+reportType, new ItemParameterType<Software>(Software.class, null), software);
3618 23 Nov 15 nicklas 113       }
3618 23 Nov 15 nicklas 114       
3506 23 Sep 15 nicklas 115       PdfReportTemplate report = PdfReportTemplate.getByCName(reportType, PdfReportTemplate.GENE_REPORT);
3499 21 Sep 15 nicklas 116       String reportFilename = report.getDefaultFilename();
3055 19 Dec 14 nicklas 117       
6023 26 Oct 20 nicklas 118       ItemList reportList = report.getItemList() != null ? report.getItemList().get(dc) : null;
6023 26 Oct 20 nicklas 119       
3499 21 Sep 15 nicklas 120       for (RawBioAssay raw : rawBioAssays)
3499 21 Sep 15 nicklas 121       {
3499 21 Sep 15 nicklas 122         // Reset AutoProcessing annotation
3499 21 Sep 15 nicklas 123         Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, raw, null);
6023 26 Oct 20 nicklas 124         if (reportList != null) reportList.removeItem(raw);
3499 21 Sep 15 nicklas 125       }
3055 19 Dec 14 nicklas 126     }
3055 19 Dec 14 nicklas 127   
3055 19 Dec 14 nicklas 128     return geneReportJob;
3055 19 Dec 14 nicklas 129   }
3055 19 Dec 14 nicklas 130
3055 19 Dec 14 nicklas 131   
3048 17 Dec 14 nicklas 132   private ThreadSignalHandler signalHandler;
3048 17 Dec 14 nicklas 133   
2871 28 Oct 14 nicklas 134   public GeneReportPlugin()
2871 28 Oct 14 nicklas 135   {}
2871 28 Oct 14 nicklas 136   
2871 28 Oct 14 nicklas 137   /*
2871 28 Oct 14 nicklas 138      From the Plugin interface
2871 28 Oct 14 nicklas 139      --------------------------------
2871 28 Oct 14 nicklas 140   */
2871 28 Oct 14 nicklas 141   @Override
2871 28 Oct 14 nicklas 142   public boolean supportsConfigurations()
2871 28 Oct 14 nicklas 143   {
3499 21 Sep 15 nicklas 144     return true;
2871 28 Oct 14 nicklas 145   }
2871 28 Oct 14 nicklas 146   @Override
2871 28 Oct 14 nicklas 147   public boolean requiresConfiguration()
2871 28 Oct 14 nicklas 148   {
2871 28 Oct 14 nicklas 149     return false;
2871 28 Oct 14 nicklas 150   }
2876 30 Oct 14 nicklas 151   
2876 30 Oct 14 nicklas 152   @Override
2876 30 Oct 14 nicklas 153   public void run(Request request, Response response, ProgressReporter progress)
2876 30 Oct 14 nicklas 154   {
3048 17 Dec 14 nicklas 155     if (signalHandler != null) signalHandler.setWorkerThread(null);
2876 30 Oct 14 nicklas 156     
2876 30 Oct 14 nicklas 157     if (job.getValue("rawBioAssay") != null)
2876 30 Oct 14 nicklas 158     {
2876 30 Oct 14 nicklas 159       // Single item context
2876 30 Oct 14 nicklas 160       super.run(request, response, progress);
2876 30 Oct 14 nicklas 161     }
2876 30 Oct 14 nicklas 162     else
2876 30 Oct 14 nicklas 163     {
3809 22 Mar 16 nicklas 164       DbControl dc = null;
3809 22 Mar 16 nicklas 165       try
3499 21 Sep 15 nicklas 166       {
3809 22 Mar 16 nicklas 167         dc = sc.newDbControl();
3809 22 Mar 16 nicklas 168         // List context or programmatic from createReportJob
5363 16 Apr 19 nicklas 169         List<RawBioAssay> rawBioAssays = job.getValues("rawBioAssays");      
5363 16 Apr 19 nicklas 170         List<String> reportTypes = job.getValues("reports");
3809 22 Mar 16 nicklas 171         
3809 22 Mar 16 nicklas 172         String filename = null;
3809 22 Mar 16 nicklas 173         boolean overwrite = getOverwrite();
6023 26 Oct 20 nicklas 174         boolean autoConfirm = job.getValue("AutoConfirmHandler") != null;
3809 22 Mar 16 nicklas 175         
3809 22 Mar 16 nicklas 176         if (reportTypes == null || reportTypes.size() == 0)
3618 23 Nov 15 nicklas 177         {
3809 22 Mar 16 nicklas 178           // Single report
3809 22 Mar 16 nicklas 179           reportTypes = Arrays.asList((String)getJobOrConfigurationValue("report"));
3809 22 Mar 16 nicklas 180           filename = (String)job.getValue("filename");
3618 23 Nov 15 nicklas 181         }
6032 29 Oct 20 nicklas 182                 
3809 22 Mar 16 nicklas 183         PdfReportWorker[] reports = new PdfReportWorker[reportTypes.size()];
3809 22 Mar 16 nicklas 184         Software[] software = new Software[reports.length];
5384 26 Apr 19 nicklas 185         int[] numReports = new int[reports.length];
3809 22 Mar 16 nicklas 186         for (int reportNo = 0; reportNo < reportTypes.size(); reportNo++)
3809 22 Mar 16 nicklas 187         {
3809 22 Mar 16 nicklas 188           String reportType = reportTypes.get(reportNo);
3809 22 Mar 16 nicklas 189           // Get specific or generic software parameter
3809 22 Mar 16 nicklas 190           software[reportNo] = (Software)job.getValue("software."+reportType);
3809 22 Mar 16 nicklas 191           if (software[reportNo] == null)
3809 22 Mar 16 nicklas 192           {
3809 22 Mar 16 nicklas 193             software[reportNo] = (Software)job.getValue("software");
3809 22 Mar 16 nicklas 194           }
3809 22 Mar 16 nicklas 195           if (software[reportNo] != null) software[reportNo] = Software.getById(dc, software[reportNo].getId());
3809 22 Mar 16 nicklas 196           String parameterSet = (String)Annotationtype.PARAMETER_SET.getAnnotationValue(dc, software[reportNo]);
3809 22 Mar 16 nicklas 197           reports[reportNo] = PdfReportTemplate.getByCName(reportType, PdfReportTemplate.GENE_REPORT).getWorker(parameterSet);
3809 22 Mar 16 nicklas 198         }
3506 23 Sep 15 nicklas 199       
2876 30 Oct 14 nicklas 200         begin(dc);
2876 30 Oct 14 nicklas 201         
2876 30 Oct 14 nicklas 202         int total = rawBioAssays.size();
2876 30 Oct 14 nicklas 203         int current = 0;
2876 30 Oct 14 nicklas 204         int skipped = 0;
2876 30 Oct 14 nicklas 205         int created = 0;
2876 30 Oct 14 nicklas 206         for (RawBioAssay rba : rawBioAssays)
2876 30 Oct 14 nicklas 207         {
3048 17 Dec 14 nicklas 208           ThreadSignalHandler.checkInterrupted();
3051 17 Dec 14 nicklas 209
2876 30 Oct 14 nicklas 210           Rawbioassay raw = Rawbioassay.getById(dc, rba.getId());
2876 30 Oct 14 nicklas 211           rba = raw.getItem();
3143 24 Feb 15 nicklas 212           
3143 24 Feb 15 nicklas 213           // Reset AutoProcessing annotation
3143 24 Feb 15 nicklas 214           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rba, null);
2876 30 Oct 14 nicklas 215
2876 30 Oct 14 nicklas 216           if (progress != null)
2876 30 Oct 14 nicklas 217           {
3499 21 Sep 15 nicklas 218             progress.display((100 * current) / total, "Creating report for " + rba.getName() + "...");
2876 30 Oct 14 nicklas 219           }
2876 30 Oct 14 nicklas 220           current++;
2876 30 Oct 14 nicklas 221           
5553 12 Aug 19 nicklas 222           boolean useExternalProjectArchive = Reggie.isExternalItem(rba.getName());
2876 30 Oct 14 nicklas 223           String dataFolder = (String)Annotationtype.DATA_FILES_FOLDER.getAnnotationValue(dc, rba);
3607 17 Nov 15 nicklas 224           String baseFolder = (useExternalProjectArchive ? Reggie.EXTERNAL_ANALYSIS_DIR : Reggie.SECONDARY_ANALYSIS_DIR)
3607 17 Nov 15 nicklas 225               + Reggie.convertDataFilesFolderToBaseFolder(dataFolder);
2876 30 Oct 14 nicklas 226           Directory localDataDir = Directory.getNew(dc, new Path(baseFolder, Path.Type.DIRECTORY));
2876 30 Oct 14 nicklas 227
3618 23 Nov 15 nicklas 228           for (int reportNo = 0; reportNo < reportTypes.size(); reportNo++)
2876 30 Oct 14 nicklas 229           {
3618 23 Nov 15 nicklas 230             PdfReportWorker report = reports[reportNo];
3506 23 Sep 15 nicklas 231             if (filename == null || reports.length > 1)
3499 21 Sep 15 nicklas 232             {
3506 23 Sep 15 nicklas 233               filename = report.getTemplate().getDefaultFilename();
3499 21 Sep 15 nicklas 234             }
3499 21 Sep 15 nicklas 235
3499 21 Sep 15 nicklas 236             File saveTo = File.getFile(dc, localDataDir, filename, true);
3499 21 Sep 15 nicklas 237             if (saveTo.isInDatabase() && !saveTo.isRemoved() && !overwrite)
3499 21 Sep 15 nicklas 238             {
3499 21 Sep 15 nicklas 239               skipped++;
3499 21 Sep 15 nicklas 240               continue;
3499 21 Sep 15 nicklas 241             }
3499 21 Sep 15 nicklas 242             
3499 21 Sep 15 nicklas 243             created++;
5384 26 Apr 19 nicklas 244             numReports[reportNo]++;
3499 21 Sep 15 nicklas 245             if (!saveTo.isInDatabase()) dc.saveItem(saveTo);
3499 21 Sep 15 nicklas 246             saveTo.setWriteProtected(false);
3618 23 Nov 15 nicklas 247             linkFileAndSoftware(dc, rba, saveTo, software[reportNo]);
3618 23 Nov 15 nicklas 248
3499 21 Sep 15 nicklas 249             ExportOutputStream out = new FileExportOutputStream(saveTo, true);
4537 21 Jun 17 nicklas 250             performExport(raw, report, out, options);
3499 21 Sep 15 nicklas 251             saveTo.setWriteProtected(true);
2876 30 Oct 14 nicklas 252           }
2876 30 Oct 14 nicklas 253         }
5384 26 Apr 19 nicklas 254         for (int reportNo = 0; reportNo < reportTypes.size(); reportNo++)
5384 26 Apr 19 nicklas 255         {
5384 26 Apr 19 nicklas 256           ActivityDef activity = reports[reportNo].getTemplate().getActivityDef();
5615 19 Sep 19 nicklas 257           if (activity != null) 
5615 19 Sep 19 nicklas 258           {
5615 19 Sep 19 nicklas 259             CountedActivity a = activity.merge(dc, numReports[reportNo]);
5615 19 Sep 19 nicklas 260             if (autoConfirm) a.setUser("Auto-confirm");
5615 19 Sep 19 nicklas 261           }
5384 26 Apr 19 nicklas 262         }
3499 21 Sep 15 nicklas 263         if (progress != null) progress.display(95, "Created " + created + " reports for " + total + " raw bioassays. Saving to database...");
2876 30 Oct 14 nicklas 264         dc.commit();
2876 30 Oct 14 nicklas 265         end(true);
2876 30 Oct 14 nicklas 266         
3499 21 Sep 15 nicklas 267         String msg = "Created " + created + " reports for " + total + " raw bioassays.";
2876 30 Oct 14 nicklas 268         if (skipped > 0) msg += " Skipped " + skipped + " that already existed.";
6032 29 Oct 20 nicklas 269         if (options.debugMode())
6032 29 Oct 20 nicklas 270         {
6032 29 Oct 20 nicklas 271           // DEBUG!! will make it easier to re-run a job with same settings
6032 29 Oct 20 nicklas 272           response.setError("[DEBUG] "+msg, null);
6032 29 Oct 20 nicklas 273         }
6032 29 Oct 20 nicklas 274         else
6032 29 Oct 20 nicklas 275         {
6032 29 Oct 20 nicklas 276           response.setDone(msg);
6032 29 Oct 20 nicklas 277         }
3059 19 Dec 14 nicklas 278         CounterService.getInstance().setForceCount();
6186 26 Mar 21 nicklas 279         if (autoConfirm) AutoConfirmService.getInstance().setForceCheck(15);
2876 30 Oct 14 nicklas 280       }
2876 30 Oct 14 nicklas 281       catch (Throwable t)
2876 30 Oct 14 nicklas 282       {
2876 30 Oct 14 nicklas 283         end(false);
2876 30 Oct 14 nicklas 284         response.setError(t.getMessage(), Arrays.asList(new Throwable[] { t }));
2876 30 Oct 14 nicklas 285       }
2876 30 Oct 14 nicklas 286       finally
2876 30 Oct 14 nicklas 287       {
2876 30 Oct 14 nicklas 288         if (dc != null) dc.close();
2876 30 Oct 14 nicklas 289       }
2876 30 Oct 14 nicklas 290       
2876 30 Oct 14 nicklas 291     }
2876 30 Oct 14 nicklas 292     
2876 30 Oct 14 nicklas 293   }
2876 30 Oct 14 nicklas 294
2871 28 Oct 14 nicklas 295   // -------------------------------------
2871 28 Oct 14 nicklas 296   /*
2871 28 Oct 14 nicklas 297      From the InteractivePlugin interface
2871 28 Oct 14 nicklas 298      -------------------------------------------
2871 28 Oct 14 nicklas 299   */
2871 28 Oct 14 nicklas 300   /**
2871 28 Oct 14 nicklas 301     The plug-in will appear on the single-item and list page for raw bioassays.
2871 28 Oct 14 nicklas 302   */
2871 28 Oct 14 nicklas 303   @Override
2871 28 Oct 14 nicklas 304   public Set<GuiContext> getGuiContexts()
2871 28 Oct 14 nicklas 305   {
2876 30 Oct 14 nicklas 306     Set<GuiContext> ctx = new HashSet<GuiContext>();
2876 30 Oct 14 nicklas 307     ctx.add(GuiContext.item(Item.RAWBIOASSAY));
2876 30 Oct 14 nicklas 308     ctx.add(GuiContext.list(Item.RAWBIOASSAY));
2876 30 Oct 14 nicklas 309     return Collections.unmodifiableSet(ctx);
2871 28 Oct 14 nicklas 310   }
2871 28 Oct 14 nicklas 311   /**
2876 30 Oct 14 nicklas 312     We accept all cufflinks raw bioassays.
2871 28 Oct 14 nicklas 313   */
2871 28 Oct 14 nicklas 314   @Override
2871 28 Oct 14 nicklas 315   public String isInContext(GuiContext context, Object item)
2871 28 Oct 14 nicklas 316   {
2871 28 Oct 14 nicklas 317     String message = null;
2876 30 Oct 14 nicklas 318     if (context.getType() == GuiContext.Type.ITEM)
2871 28 Oct 14 nicklas 319     {
2876 30 Oct 14 nicklas 320       if (item == null)
2871 28 Oct 14 nicklas 321       {
2876 30 Oct 14 nicklas 322         message = "The object is null";
2871 28 Oct 14 nicklas 323       }
2876 30 Oct 14 nicklas 324       else if (!(item instanceof RawBioAssay))
2876 30 Oct 14 nicklas 325       {
2876 30 Oct 14 nicklas 326         message = "The object is not a raw bioassay: " + item;
2876 30 Oct 14 nicklas 327       }
2876 30 Oct 14 nicklas 328       else
2876 30 Oct 14 nicklas 329       {
2876 30 Oct 14 nicklas 330         RawBioAssay pba = (RawBioAssay)item;
5650 09 Oct 19 nicklas 331         PdfReportTemplate reportType = PdfReportTemplate.getByCName((String)getJobOrConfigurationValue("report"), null);
5650 09 Oct 19 nicklas 332         Rawdatatype rdt = reportType == null ? null : reportType.getRawDataType();
5650 09 Oct 19 nicklas 333         if (rdt != null && !rdt.getRawDataType().equals(pba.getRawDataType()))
2876 30 Oct 14 nicklas 334         {
5650 09 Oct 19 nicklas 335           message = "The rawbioassay doesn't contain " + rdt.getName() + " data.";
2876 30 Oct 14 nicklas 336         }
2876 30 Oct 14 nicklas 337       }
2871 28 Oct 14 nicklas 338     }
2871 28 Oct 14 nicklas 339     return message;
2871 28 Oct 14 nicklas 340   }
2871 28 Oct 14 nicklas 341   
2871 28 Oct 14 nicklas 342   @Override
2871 28 Oct 14 nicklas 343   public RequestInformation getRequestInformation(GuiContext context, String command) 
2871 28 Oct 14 nicklas 344     throws BaseException
2871 28 Oct 14 nicklas 345   {
2871 28 Oct 14 nicklas 346     RequestInformation requestInformation = null;
3499 21 Sep 15 nicklas 347     if (Request.COMMAND_CONFIGURE_PLUGIN.equals(command))
2871 28 Oct 14 nicklas 348     {
3499 21 Sep 15 nicklas 349       requestInformation = getConfigurePluginParameters();
3499 21 Sep 15 nicklas 350     }
3499 21 Sep 15 nicklas 351     else if (Request.COMMAND_CONFIGURE_JOB.equals(command))
3499 21 Sep 15 nicklas 352     {
2876 30 Oct 14 nicklas 353       requestInformation = getConfigureJobParameters(context, null);
2871 28 Oct 14 nicklas 354     }
2871 28 Oct 14 nicklas 355     return requestInformation;
2871 28 Oct 14 nicklas 356   }
2871 28 Oct 14 nicklas 357   
2876 30 Oct 14 nicklas 358   @SuppressWarnings("unchecked")
2871 28 Oct 14 nicklas 359   @Override
2871 28 Oct 14 nicklas 360   public void configure(GuiContext context, Request request, Response response)
2871 28 Oct 14 nicklas 361   {
2876 30 Oct 14 nicklas 362     String command = request.getCommand();
2876 30 Oct 14 nicklas 363     DbControl dc = null;
2871 28 Oct 14 nicklas 364     try
2871 28 Oct 14 nicklas 365     {
3499 21 Sep 15 nicklas 366       if (command.equals(Request.COMMAND_CONFIGURE_PLUGIN))
2871 28 Oct 14 nicklas 367       {
3499 21 Sep 15 nicklas 368         RequestInformation ri = getConfigurePluginParameters();
3499 21 Sep 15 nicklas 369         List<Throwable> errors = validateRequestParameters(ri.getParameters(), request);
3499 21 Sep 15 nicklas 370         if (errors != null)
3499 21 Sep 15 nicklas 371         {
3499 21 Sep 15 nicklas 372           response.setError(errors.size() + " invalid parameters were found in the request",errors);
3499 21 Sep 15 nicklas 373           return;
3499 21 Sep 15 nicklas 374         }
3499 21 Sep 15 nicklas 375         storeValue(configuration, request, ri.getParameter("report"));
3499 21 Sep 15 nicklas 376         response.setDone("Configuration complete");
3499 21 Sep 15 nicklas 377       }
3499 21 Sep 15 nicklas 378       else if (command.equals(Request.COMMAND_CONFIGURE_JOB))
3499 21 Sep 15 nicklas 379       {
2876 30 Oct 14 nicklas 380         RequestInformation ri = getConfigureJobParameters(context, !request.isAllowedImmediateExecution());
2871 28 Oct 14 nicklas 381         List<Throwable> errors = validateRequestParameters(ri.getParameters(), request);
2871 28 Oct 14 nicklas 382         if (errors != null)
2871 28 Oct 14 nicklas 383         {
2871 28 Oct 14 nicklas 384           response.setError(errors.size() + " invalid parameters were found in the request",errors);
2871 28 Oct 14 nicklas 385           return;
2871 28 Oct 14 nicklas 386         }
2871 28 Oct 14 nicklas 387   
3618 23 Nov 15 nicklas 388         storeValue(job, request, ri.getParameter("software"));
4537 21 Jun 17 nicklas 389         storeValue(job, request, ri.getParameter("updateAnnotations"));
6028 28 Oct 20 nicklas 390         storeValue(job, request, ri.getParameter("debugMode"));
2876 30 Oct 14 nicklas 391         if (context.getType() == GuiContext.Type.ITEM)
2871 28 Oct 14 nicklas 392         {
2876 30 Oct 14 nicklas 393           storeValue(job, request, ri.getParameter("rawBioAssay"));
2876 30 Oct 14 nicklas 394           RawBioAssay rba = (RawBioAssay)job.getValue("rawBioAssay");
2876 30 Oct 14 nicklas 395           if (request.getParameterValue(SAVE_AS) == null)
2871 28 Oct 14 nicklas 396           {
2876 30 Oct 14 nicklas 397             if (!request.isAllowedImmediateExecution())
2876 30 Oct 14 nicklas 398             {
2876 30 Oct 14 nicklas 399               response.setError("Immediate download is not allowed. Please specify a filename.", null);
2876 30 Oct 14 nicklas 400               return;
2876 30 Oct 14 nicklas 401             }
3506 23 Sep 15 nicklas 402             response.setDownloadImmediately("Creating report: " + 
2876 30 Oct 14 nicklas 403                 rba.getName(), ExecutionTime.SHORTEST, true);
2871 28 Oct 14 nicklas 404           }
2876 30 Oct 14 nicklas 405           else
2876 30 Oct 14 nicklas 406           {
2876 30 Oct 14 nicklas 407             Object parameterValue = request.getParameterValue(OVERWRITE); 
2876 30 Oct 14 nicklas 408             boolean overwrite = parameterValue != null ? (Boolean)parameterValue : false;
2876 30 Oct 14 nicklas 409             if (!pathCanBeUsed((String)request.getParameterValue(ri.getParameter(SAVE_AS).getName()), overwrite))
2876 30 Oct 14 nicklas 410             {
2876 30 Oct 14 nicklas 411               response.setError("File exists: " + (String)request.getParameterValue(ri.getParameter(SAVE_AS).getName()), null);
2876 30 Oct 14 nicklas 412               return;
2876 30 Oct 14 nicklas 413             }
2876 30 Oct 14 nicklas 414             storeValue(job, request, ri.getParameter(SAVE_AS));
2876 30 Oct 14 nicklas 415             storeValue(job, request, ri.getParameter(OVERWRITE));
3506 23 Sep 15 nicklas 416             response.setSuggestedJobName("Create report: " + rba.getName());
2876 30 Oct 14 nicklas 417             response.setDone("The job configuration is complete", ExecutionTime.SHORTEST);
2876 30 Oct 14 nicklas 418           }
2871 28 Oct 14 nicklas 419         }
2871 28 Oct 14 nicklas 420         else
2871 28 Oct 14 nicklas 421         {
5650 09 Oct 19 nicklas 422           PdfReportTemplate reportType = PdfReportTemplate.getByCName((String)getJobOrConfigurationValue("report"), null);
5650 09 Oct 19 nicklas 423           Rawdatatype requiredRawDataType = reportType == null ? null : reportType.getRawDataType();
6032 29 Oct 20 nicklas 424           List<RawBioAssay> rawBioAssays = job.getValues("rawBioAssays");
6032 29 Oct 20 nicklas 425           if (rawBioAssays == null)
5647 08 Oct 19 nicklas 426           {
6032 29 Oct 20 nicklas 427             String whichItems = (String)request.getParameterValue("whichItems");
6032 29 Oct 20 nicklas 428             ItemContext cc = sc.getCurrentContext(context.getItem(), context.getSubContext());
6032 29 Oct 20 nicklas 429             
6032 29 Oct 20 nicklas 430             ItemQuery<RawBioAssay> query = (ItemQuery<RawBioAssay>)cc.getQuery();
6032 29 Oct 20 nicklas 431             if (requiredRawDataType != null) 
6032 29 Oct 20 nicklas 432             {
6032 29 Oct 20 nicklas 433               requiredRawDataType.addFilter(dc, query);
6032 29 Oct 20 nicklas 434             }
6032 29 Oct 20 nicklas 435             if ("all".equals(whichItems))
6032 29 Oct 20 nicklas 436             {  
6032 29 Oct 20 nicklas 437               query.setFirstResult(0);
6032 29 Oct 20 nicklas 438               query.setMaxResults(0);
6032 29 Oct 20 nicklas 439             }
6032 29 Oct 20 nicklas 440             else if ("selected".equals(whichItems))
6032 29 Oct 20 nicklas 441             {
6032 29 Oct 20 nicklas 442               query.setFirstResult(0);
6032 29 Oct 20 nicklas 443               query.setMaxResults(0);
6032 29 Oct 20 nicklas 444               query.restrict(
6032 29 Oct 20 nicklas 445                 Restrictions.in(
6032 29 Oct 20 nicklas 446                   Hql.property("id"),
6032 29 Oct 20 nicklas 447                   Expressions.parameter("_selected_", cc.getSelected(), Type.INT)
6032 29 Oct 20 nicklas 448                 )
6032 29 Oct 20 nicklas 449               );
6032 29 Oct 20 nicklas 450             }
6032 29 Oct 20 nicklas 451             
6032 29 Oct 20 nicklas 452             dc = sc.newDbControl();
6032 29 Oct 20 nicklas 453             rawBioAssays = query.list(dc);
6032 29 Oct 20 nicklas 454             if (rawBioAssays.size() > 0)
6032 29 Oct 20 nicklas 455             {
6032 29 Oct 20 nicklas 456               job.setValues("rawBioAssays", new ItemParameterType<RawBioAssay>(RawBioAssay.class, null, true, 0, null), rawBioAssays);
6032 29 Oct 20 nicklas 457             }
6032 29 Oct 20 nicklas 458             
5647 08 Oct 19 nicklas 459           }
6032 29 Oct 20 nicklas 460           storeValue(job, request, ri.getParameter("filename"));
6032 29 Oct 20 nicklas 461           storeValue(job, request, ri.getParameter(OVERWRITE));
2876 30 Oct 14 nicklas 462           
2876 30 Oct 14 nicklas 463           if (rawBioAssays.size() == 0)
2876 30 Oct 14 nicklas 464           {
5650 09 Oct 19 nicklas 465             if (requiredRawDataType != null)
5650 09 Oct 19 nicklas 466             {
5650 09 Oct 19 nicklas 467               response.setError("None of the specified raw bioassays have " + requiredRawDataType.getName() + " data", null);
5650 09 Oct 19 nicklas 468             }
5650 09 Oct 19 nicklas 469             else
5650 09 Oct 19 nicklas 470             {
5650 09 Oct 19 nicklas 471               response.setError("No raw bioassays have been selected", null);
5650 09 Oct 19 nicklas 472             }
2871 28 Oct 14 nicklas 473             return;
2871 28 Oct 14 nicklas 474           }
2876 30 Oct 14 nicklas 475           else
2876 30 Oct 14 nicklas 476           {
3506 23 Sep 15 nicklas 477             response.setSuggestedJobName("Create report for " + rawBioAssays.size() + " raw bioassays");
2876 30 Oct 14 nicklas 478           }
6032 29 Oct 20 nicklas 479           response.setDone("The job configuration is complete", ExecutionTime.SHORTEST);
2871 28 Oct 14 nicklas 480         }
2871 28 Oct 14 nicklas 481       }
2871 28 Oct 14 nicklas 482     }
2871 28 Oct 14 nicklas 483     catch(Throwable ex)
2871 28 Oct 14 nicklas 484     {
2871 28 Oct 14 nicklas 485       response.setError(ex.getMessage(), Arrays.asList(ex));
2876 30 Oct 14 nicklas 486     }
2876 30 Oct 14 nicklas 487     finally
2876 30 Oct 14 nicklas 488     {
2876 30 Oct 14 nicklas 489       if (dc != null) dc.close();
2876 30 Oct 14 nicklas 490     }
2871 28 Oct 14 nicklas 491   }
2871 28 Oct 14 nicklas 492   // ------------------------------------------------
2871 28 Oct 14 nicklas 493
3499 21 Sep 15 nicklas 494   private RequestInformation getConfigurePluginParameters()
3499 21 Sep 15 nicklas 495   {    
3499 21 Sep 15 nicklas 496     List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
3499 21 Sep 15 nicklas 497     
3499 21 Sep 15 nicklas 498     Enumeration<String, String> reportOptions = new Enumeration<String, String>();
3499 21 Sep 15 nicklas 499     reportOptions.add("GENE_REPORT", "Gene report");
3499 21 Sep 15 nicklas 500     reportOptions.add("PILOT_REPORT", "Pilot report");
6028 28 Oct 20 nicklas 501     reportOptions.add("SCANB_REPORT", "SCAN-B report");
3499 21 Sep 15 nicklas 502     parameters.add(new PluginParameter<String>
3499 21 Sep 15 nicklas 503     (
3499 21 Sep 15 nicklas 504       "report", "Report", "Select the report to generate by this configuration", null,
3499 21 Sep 15 nicklas 505       new StringParameterType(255, null, true, 1, 0, 0, reportOptions)
3499 21 Sep 15 nicklas 506     ));
3499 21 Sep 15 nicklas 507     
3499 21 Sep 15 nicklas 508     RequestInformation configurePlugin = new RequestInformation
3499 21 Sep 15 nicklas 509     (
3499 21 Sep 15 nicklas 510       Request.COMMAND_CONFIGURE_PLUGIN, 
3499 21 Sep 15 nicklas 511       "Report options", 
3499 21 Sep 15 nicklas 512       "Set options for the report.", 
3499 21 Sep 15 nicklas 513       parameters
3499 21 Sep 15 nicklas 514     );
3499 21 Sep 15 nicklas 515     return configurePlugin;
3499 21 Sep 15 nicklas 516   }
2871 28 Oct 14 nicklas 517
2876 30 Oct 14 nicklas 518   private RequestInformation getConfigureJobParameters(GuiContext context, Boolean requireFile)
2871 28 Oct 14 nicklas 519   {
3499 21 Sep 15 nicklas 520     String reportType = (String)getJobOrConfigurationValue("report");
3506 23 Sep 15 nicklas 521     PdfReportTemplate report = PdfReportTemplate.getByCName(reportType, PdfReportTemplate.GENE_REPORT);
3499 21 Sep 15 nicklas 522
2876 30 Oct 14 nicklas 523     RequestInformation configureJob = null;
2876 30 Oct 14 nicklas 524     
2876 30 Oct 14 nicklas 525     List<PluginParameter<?>> parameters = new ArrayList<PluginParameter<?>>();
2876 30 Oct 14 nicklas 526     DbControl dc = null;
2876 30 Oct 14 nicklas 527     try
2871 28 Oct 14 nicklas 528     {
2876 30 Oct 14 nicklas 529       ItemContext cc = sc.getCurrentContext(context.getItem(), context.getSubContext());
2876 30 Oct 14 nicklas 530       dc = sc.newDbControl();
2876 30 Oct 14 nicklas 531       if (context.getType() == GuiContext.Type.ITEM)
2871 28 Oct 14 nicklas 532       {
2871 28 Oct 14 nicklas 533         RawBioAssay rawBioAssay = null;
2871 28 Oct 14 nicklas 534         String baseFolder = null;
2876 30 Oct 14 nicklas 535         int currentBioAssayId = cc.getId();
2871 28 Oct 14 nicklas 536         if (currentBioAssayId != 0)
2871 28 Oct 14 nicklas 537         {
2871 28 Oct 14 nicklas 538           rawBioAssay = RawBioAssay.getById(dc, currentBioAssayId);
5553 12 Aug 19 nicklas 539           boolean useExternalProjectArchive = Reggie.isExternalItem(rawBioAssay.getName());
2871 28 Oct 14 nicklas 540           String dataFolder = (String)Annotationtype.DATA_FILES_FOLDER.getAnnotationValue(dc, rawBioAssay);
3607 17 Nov 15 nicklas 541           
3607 17 Nov 15 nicklas 542           baseFolder = (useExternalProjectArchive ? Reggie.EXTERNAL_ANALYSIS_DIR : Reggie.SECONDARY_ANALYSIS_DIR)
3607 17 Nov 15 nicklas 543               + Reggie.convertDataFilesFolderToBaseFolder(dataFolder);
2871 28 Oct 14 nicklas 544         }
2871 28 Oct 14 nicklas 545         
2871 28 Oct 14 nicklas 546         parameters.add(new PluginParameter<RawBioAssay>(
2871 28 Oct 14 nicklas 547             "rawBioAssay", "Raw bioassay", "Select the rawbioassay", 
2871 28 Oct 14 nicklas 548             new ItemParameterType<RawBioAssay>(RawBioAssay.class, rawBioAssay, true, 1, null)
2871 28 Oct 14 nicklas 549           ));
2871 28 Oct 14 nicklas 550           
2871 28 Oct 14 nicklas 551         if (requireFile == null)
2871 28 Oct 14 nicklas 552         {
2871 28 Oct 14 nicklas 553           PluginDefinition pd = job.getPluginDefinition();
2871 28 Oct 14 nicklas 554           requireFile = pd == null ? false : !pd.getAllowImmediateExecution();
2871 28 Oct 14 nicklas 555         }
2871 28 Oct 14 nicklas 556         String defaultPath = null;
2871 28 Oct 14 nicklas 557         if (requireFile && baseFolder != null)
2871 28 Oct 14 nicklas 558         {
2871 28 Oct 14 nicklas 559           defaultPath = baseFolder + Path.makeSafeFilename(rawBioAssay.getName(), "") + ".pdf";
2871 28 Oct 14 nicklas 560         }
2871 28 Oct 14 nicklas 561         parameters.add(getSaveAsParameter(null, null, defaultPath, requireFile));
2871 28 Oct 14 nicklas 562         parameters.add(getOverwriteParameter(null, null));
3618 23 Nov 15 nicklas 563         parameters.add(getSoftwareParameter(dc, reportType));
5669 15 Oct 19 nicklas 564         if (report.canUpdateAnnotations())
4537 21 Jun 17 nicklas 565         {
4537 21 Jun 17 nicklas 566           parameters.add(getUpdateAnnotationsParameter());
4537 21 Jun 17 nicklas 567         }
2871 28 Oct 14 nicklas 568         
6028 28 Oct 20 nicklas 569         // If <developer-mode>1</developer-mode> is in reggie-config.xml we allow debug via parameter
6028 28 Oct 20 nicklas 570         if (Values.getBoolean(Reggie.getConfig().getConfig("developer-mode")))
6028 28 Oct 20 nicklas 571         {
6028 28 Oct 20 nicklas 572           parameters.add(new PluginParameter<Boolean>(
6028 28 Oct 20 nicklas 573             "debugMode", "Debug mode", "Runs the plug-in in debug mode. Major difference is "
6028 28 Oct 20 nicklas 574                 + "that it will always report an error making it easier to re-start the "
6028 28 Oct 20 nicklas 575                 + "plug-in with the same parameters. It is recommended that <b>Overwrite</b> "
6028 28 Oct 20 nicklas 576                 + "is also enabled.",
6028 28 Oct 20 nicklas 577             new BooleanParameterType(null, false)
6028 28 Oct 20 nicklas 578             ));
6028 28 Oct 20 nicklas 579         }
6028 28 Oct 20 nicklas 580         
2876 30 Oct 14 nicklas 581         configureJob = new RequestInformation
2876 30 Oct 14 nicklas 582         (
2876 30 Oct 14 nicklas 583           Request.COMMAND_CONFIGURE_JOB,
3499 21 Sep 15 nicklas 584           "Report options",
3499 21 Sep 15 nicklas 585           "Select raw bioassay and the file path where the report PDF should be saved.",
2876 30 Oct 14 nicklas 586           parameters
2876 30 Oct 14 nicklas 587         );
2871 28 Oct 14 nicklas 588       }
2876 30 Oct 14 nicklas 589       else
2871 28 Oct 14 nicklas 590       {
6032 29 Oct 20 nicklas 591         if (job.getValues("rawBioAssays") == null)
2891 04 Nov 14 nicklas 592         {
6032 29 Oct 20 nicklas 593           String defaultWhich = "all";
6032 29 Oct 20 nicklas 594           Enumeration<String, String> options = new Enumeration<String, String>();
6032 29 Oct 20 nicklas 595           if (cc.getSelected().size() > 0)
6032 29 Oct 20 nicklas 596           {
6032 29 Oct 20 nicklas 597             options.add("selected", "Selected items");
6032 29 Oct 20 nicklas 598             defaultWhich = "selected";
6032 29 Oct 20 nicklas 599           }
6032 29 Oct 20 nicklas 600           options.add("all", "All items");
6032 29 Oct 20 nicklas 601           
6032 29 Oct 20 nicklas 602           parameters.add(new PluginParameter<String>
6032 29 Oct 20 nicklas 603             (
6032 29 Oct 20 nicklas 604               "whichItems", "Which raw bioassays", "The raw bioassays to create a report for.", defaultWhich,
6032 29 Oct 20 nicklas 605               new StringParameterType(255, defaultWhich, true, 1, 0, 0, options)
6032 29 Oct 20 nicklas 606             ));
2891 04 Nov 14 nicklas 607         }
2876 30 Oct 14 nicklas 608         parameters.add(new PluginParameter<String>
2876 30 Oct 14 nicklas 609           (
3499 21 Sep 15 nicklas 610             "filename", "Filename", "Set filename for generated PDF.", report.getDefaultFilename(),
3499 21 Sep 15 nicklas 611             new StringParameterType(255, report.getDefaultFilename(), true)
2883 03 Nov 14 nicklas 612           ));
2876 30 Oct 14 nicklas 613         parameters.add(getOverwriteParameter(null, null));
3618 23 Nov 15 nicklas 614         parameters.add(getSoftwareParameter(dc, reportType));
5669 15 Oct 19 nicklas 615         if (report.canUpdateAnnotations())
4537 21 Jun 17 nicklas 616         {
4537 21 Jun 17 nicklas 617           parameters.add(getUpdateAnnotationsParameter());
4537 21 Jun 17 nicklas 618         }
2876 30 Oct 14 nicklas 619
6032 29 Oct 20 nicklas 620         // If <developer-mode>1</developer-mode> is in reggie-config.xml we allow debug via parameter
6032 29 Oct 20 nicklas 621         if (Values.getBoolean(Reggie.getConfig().getConfig("developer-mode")))
6032 29 Oct 20 nicklas 622         {
6032 29 Oct 20 nicklas 623           parameters.add(new PluginParameter<Boolean>(
6032 29 Oct 20 nicklas 624             "debugMode", "Debug mode", "Runs the plug-in in debug mode. Major difference is "
6032 29 Oct 20 nicklas 625                 + "that it will always report an error making it easier to re-start the "
6032 29 Oct 20 nicklas 626                 + "plug-in with the same parameters. It is recommended that <b>Overwrite</b> "
6032 29 Oct 20 nicklas 627                 + "is also enabled.",
6032 29 Oct 20 nicklas 628             new BooleanParameterType(null, false)
6032 29 Oct 20 nicklas 629             ));
6032 29 Oct 20 nicklas 630         }
6032 29 Oct 20 nicklas 631
6032 29 Oct 20 nicklas 632         
2876 30 Oct 14 nicklas 633         configureJob = new RequestInformation
2876 30 Oct 14 nicklas 634         (
2876 30 Oct 14 nicklas 635           Request.COMMAND_CONFIGURE_JOB, 
3499 21 Sep 15 nicklas 636           "Report options", 
3499 21 Sep 15 nicklas 637           "Set options for generating the report PDF.", 
2876 30 Oct 14 nicklas 638           parameters
2876 30 Oct 14 nicklas 639         );
2876 30 Oct 14 nicklas 640         dc.close();
2871 28 Oct 14 nicklas 641       }
2876 30 Oct 14 nicklas 642     }
2876 30 Oct 14 nicklas 643     finally
2876 30 Oct 14 nicklas 644     {
2876 30 Oct 14 nicklas 645       if (dc != null) dc.close();
2876 30 Oct 14 nicklas 646     }
2871 28 Oct 14 nicklas 647       
2871 28 Oct 14 nicklas 648     return configureJob;
2871 28 Oct 14 nicklas 649   }
3048 17 Dec 14 nicklas 650   /*
3048 17 Dec 14 nicklas 651     From the SignalTarget interface
3048 17 Dec 14 nicklas 652     -------------------------------------------
3048 17 Dec 14 nicklas 653   */
3048 17 Dec 14 nicklas 654   @Override
3048 17 Dec 14 nicklas 655   public SignalHandler getSignalHandler()
3048 17 Dec 14 nicklas 656   {
3048 17 Dec 14 nicklas 657     signalHandler = new ThreadSignalHandler();
3048 17 Dec 14 nicklas 658     return signalHandler;
3048 17 Dec 14 nicklas 659   }
3048 17 Dec 14 nicklas 660   // -------------------------------------------
2871 28 Oct 14 nicklas 661
2871 28 Oct 14 nicklas 662   /*
2871 28 Oct 14 nicklas 663     From the AbstractExporterPlugin interface
2871 28 Oct 14 nicklas 664     -----------------------------------------
2871 28 Oct 14 nicklas 665   */
2871 28 Oct 14 nicklas 666   private DbControl dc;
6028 28 Oct 20 nicklas 667   private ReportOptions options;
2871 28 Oct 14 nicklas 668   
2871 28 Oct 14 nicklas 669   @Override
2871 28 Oct 14 nicklas 670   protected void begin(DbControl dc)
2871 28 Oct 14 nicklas 671   {
2871 28 Oct 14 nicklas 672     this.dc = dc;
6028 28 Oct 20 nicklas 673     options = new ReportOptions();
6028 28 Oct 20 nicklas 674     options.setUpdateAnnotations(Boolean.TRUE.equals(job.getValue("updateAnnotations")));
6028 28 Oct 20 nicklas 675     options.setDebugMode(Boolean.TRUE.equals(job.getValue("debugMode")));
2871 28 Oct 14 nicklas 676   }
2871 28 Oct 14 nicklas 677   
2871 28 Oct 14 nicklas 678   @Override
2871 28 Oct 14 nicklas 679   protected void performExport(ExportOutputStream out, ProgressReporter progress)
2871 28 Oct 14 nicklas 680     throws IOException
2871 28 Oct 14 nicklas 681   {
2876 30 Oct 14 nicklas 682     try
2876 30 Oct 14 nicklas 683     {
2876 30 Oct 14 nicklas 684       Rawbioassay raw = Rawbioassay.getById(dc, ((RawBioAssay)job.getValue("rawBioAssay")).getId());
3499 21 Sep 15 nicklas 685       String reportType = (String)getJobOrConfigurationValue("report");
3618 23 Nov 15 nicklas 686       Software software = (Software)job.getValue("software");
3620 24 Nov 15 nicklas 687       if (software != null) software = Software.getById(dc, software.getId());
3620 24 Nov 15 nicklas 688
3620 24 Nov 15 nicklas 689       String parameterSet = (String)Annotationtype.PARAMETER_SET.getAnnotationValue(dc, software);
3506 23 Sep 15 nicklas 690       PdfReportTemplate report = PdfReportTemplate.getByCName(reportType, PdfReportTemplate.GENE_REPORT);
4537 21 Jun 17 nicklas 691       performExport(raw, report.getWorker(parameterSet), out, options);
3618 23 Nov 15 nicklas 692       if (getSaveTo() != null)
3618 23 Nov 15 nicklas 693       {
3618 23 Nov 15 nicklas 694         linkFileAndSoftware(dc, raw.getItem(), getSaveTo(), software);
6028 28 Oct 20 nicklas 695         ActivityDef activity = report.getActivityDef();
6028 28 Oct 20 nicklas 696         if (activity != null) activity.merge(dc, 1);
3618 23 Nov 15 nicklas 697       }
3506 23 Sep 15 nicklas 698       if (progress != null) progress.display(100, "\"" + report.getReportName() + "\" generated succesfully");
2876 30 Oct 14 nicklas 699     }
2876 30 Oct 14 nicklas 700     catch (RuntimeException | IOException ex)
2876 30 Oct 14 nicklas 701     {
2876 30 Oct 14 nicklas 702       if (progress != null) progress.display(100, ex.getClass().getName() + ": " + ex.getMessage());
2876 30 Oct 14 nicklas 703       throw ex;
2876 30 Oct 14 nicklas 704     }
2876 30 Oct 14 nicklas 705   }
2876 30 Oct 14 nicklas 706   
2876 30 Oct 14 nicklas 707   @Override
2876 30 Oct 14 nicklas 708   protected void end(boolean success)
2876 30 Oct 14 nicklas 709   {
2876 30 Oct 14 nicklas 710     this.dc = null;
6032 29 Oct 20 nicklas 711     if (success && options.debugMode() && job.getJob().getLastGuiContext().getType() == GuiContext.Type.ITEM)
6028 28 Oct 20 nicklas 712     {
6032 29 Oct 20 nicklas 713       throw new RuntimeException("[DEBUG] " + getSuccessMessage());
6028 28 Oct 20 nicklas 714     }
2876 30 Oct 14 nicklas 715   }
2876 30 Oct 14 nicklas 716   
2876 30 Oct 14 nicklas 717   @Override
2876 30 Oct 14 nicklas 718   protected String getSuccessMessage()
2876 30 Oct 14 nicklas 719   {
3506 23 Sep 15 nicklas 720     return "PDF report generated successfully";
2876 30 Oct 14 nicklas 721   }
2871 28 Oct 14 nicklas 722
2876 30 Oct 14 nicklas 723   
4537 21 Jun 17 nicklas 724   private void performExport(Rawbioassay raw, PdfReportWorker report, ExportOutputStream out, ReportOptions options)
2876 30 Oct 14 nicklas 725     throws IOException
2876 30 Oct 14 nicklas 726   {
2871 28 Oct 14 nicklas 727     // Save to pdf
2871 28 Oct 14 nicklas 728     out.setMimeType("application/pdf");
2871 28 Oct 14 nicklas 729     String filename = Path.makeSafeFilename(raw.getName(), "")+".pdf";
6033 29 Oct 20 nicklas 730     if (report.getTemplate().useExternalIds())
6033 29 Oct 20 nicklas 731     {
6033 29 Oct 20 nicklas 732       Sample specimen = (Sample)raw.findSingleParent(dc, Subtype.SPECIMEN);
6068 19 Nov 20 nicklas 733       if (specimen != null && specimen.getExternalId() != null)
6033 29 Oct 20 nicklas 734       {
6033 29 Oct 20 nicklas 735         filename = filename.replace(specimen.getName(), specimen.getExternalId());
6033 29 Oct 20 nicklas 736       }
6033 29 Oct 20 nicklas 737     }
2871 28 Oct 14 nicklas 738     out.setFilename(filename);
2871 28 Oct 14 nicklas 739     
6036 02 Nov 20 nicklas 740     ScriptResult result = null;
2871 28 Oct 14 nicklas 741     try
2871 28 Oct 14 nicklas 742     {
4537 21 Jun 17 nicklas 743       result = report.runAndCreatePdf(dc, raw, out, options);
2871 28 Oct 14 nicklas 744       if (result.getExitStatus() != 0)
2871 28 Oct 14 nicklas 745       {
2871 28 Oct 14 nicklas 746         throw new RuntimeException(result.getStderr(), result.getException());
2871 28 Oct 14 nicklas 747       }
2871 28 Oct 14 nicklas 748     }
2871 28 Oct 14 nicklas 749     finally
2871 28 Oct 14 nicklas 750     {
2871 28 Oct 14 nicklas 751       if (result != null) result.removeWorkDir();
2871 28 Oct 14 nicklas 752     }
2871 28 Oct 14 nicklas 753   }
2871 28 Oct 14 nicklas 754   
3618 23 Nov 15 nicklas 755   private void linkFileAndSoftware(DbControl dc, RawBioAssay raw, File file, Software software)
3618 23 Nov 15 nicklas 756   {
3618 23 Nov 15 nicklas 757     AnyToAny link = AnyToAny.getNewOrExisting(dc, raw, file.getName(), file, true);
3618 23 Nov 15 nicklas 758     if (!link.isInDatabase()) dc.saveItem(link);
3618 23 Nov 15 nicklas 759
3618 23 Nov 15 nicklas 760     if (software != null)
3618 23 Nov 15 nicklas 761     {
3618 23 Nov 15 nicklas 762       link.setDescription("Created with " + software.getName());
3618 23 Nov 15 nicklas 763       AnyToAny swLink = AnyToAny.getNewOrExisting(dc, file, "software", software, true);
3618 23 Nov 15 nicklas 764       if (!swLink.isInDatabase()) dc.saveItem(swLink);
3618 23 Nov 15 nicklas 765     }
3618 23 Nov 15 nicklas 766   }
3618 23 Nov 15 nicklas 767   
3618 23 Nov 15 nicklas 768   private PluginParameter<Software> getSoftwareParameter(DbControl dc, String reportTemplate)
3618 23 Nov 15 nicklas 769   {
3618 23 Nov 15 nicklas 770     ItemQuery<Software> query = Software.getQuery();
3618 23 Nov 15 nicklas 771     query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
3618 23 Nov 15 nicklas 772     query.setItemPermission(Permission.USE);
3618 23 Nov 15 nicklas 773     Subtype.REPORT_SOFTWARE.addFilter(dc, query);
3618 23 Nov 15 nicklas 774     if (reportTemplate != null)
3618 23 Nov 15 nicklas 775     {
3618 23 Nov 15 nicklas 776       query.join(Annotations.leftJoin(Annotationtype.REPORT_TEMPLATE.get(dc), "rt"));
3618 23 Nov 15 nicklas 777       query.restrict(Restrictions.eq(Hql.alias("rt"), Expressions.string(reportTemplate)));
3618 23 Nov 15 nicklas 778     }
3618 23 Nov 15 nicklas 779     
3618 23 Nov 15 nicklas 780     query.order(Orders.asc(Hql.property("name")));
3618 23 Nov 15 nicklas 781     query.order(Orders.asc(Hql.property("id")));
3618 23 Nov 15 nicklas 782
3618 23 Nov 15 nicklas 783     List<Software> software = query.list(dc);
3618 23 Nov 15 nicklas 784     Software defaultSoftware = Subtype.REPORT_SOFTWARE.getLatestProjectDefault(dc, Annotationtype.REPORT_TEMPLATE.createFilter(reportTemplate));
5664 14 Oct 19 nicklas 785     String description = "Select the software item that represents this report.";
5664 14 Oct 19 nicklas 786     if (software.isEmpty()) 
5664 14 Oct 19 nicklas 787     {
5664 14 Oct 19 nicklas 788       description += " Note! If the list is empty you should register a new software item with type='Report'";
5664 14 Oct 19 nicklas 789       if (reportTemplate != null)
5664 14 Oct 19 nicklas 790       {
5664 14 Oct 19 nicklas 791         description += " and set the 'ReportTemplate' annotation to '" + reportTemplate + "'.";
5664 14 Oct 19 nicklas 792       }
5664 14 Oct 19 nicklas 793     }
3618 23 Nov 15 nicklas 794     return new PluginParameter<Software>("software", "Software", 
5664 14 Oct 19 nicklas 795       description, 
3618 23 Nov 15 nicklas 796       new ItemParameterType<Software>(Software.class, defaultSoftware, false, 1, software));
3618 23 Nov 15 nicklas 797   }
4537 21 Jun 17 nicklas 798   
4537 21 Jun 17 nicklas 799   protected PluginParameter<Boolean> getUpdateAnnotationsParameter()
4537 21 Jun 17 nicklas 800   {
4537 21 Jun 17 nicklas 801     return new PluginParameter<Boolean>("updateAnnotations", "Update annotations", 
5669 15 Oct 19 nicklas 802       "Save or update annotations created by the report on the raw bioassay.", 
4537 21 Jun 17 nicklas 803       new BooleanParameterType());
4537 21 Jun 17 nicklas 804   }
2871 28 Oct 14 nicklas 805 }