extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/servlet/ReportServlet.java

Code
Comments
Other
Rev Date Author Line
3492 18 Sep 15 nicklas 1 package net.sf.basedb.reggie.servlet;
3492 18 Sep 15 nicklas 2
3492 18 Sep 15 nicklas 3 import java.io.IOException;
3492 18 Sep 15 nicklas 4 import java.util.ArrayList;
3618 23 Nov 15 nicklas 5 import java.util.HashMap;
3492 18 Sep 15 nicklas 6 import java.util.List;
3618 23 Nov 15 nicklas 7 import java.util.Map;
3492 18 Sep 15 nicklas 8
3492 18 Sep 15 nicklas 9 import javax.servlet.ServletException;
3492 18 Sep 15 nicklas 10 import javax.servlet.http.HttpServlet;
3492 18 Sep 15 nicklas 11 import javax.servlet.http.HttpServletRequest;
3492 18 Sep 15 nicklas 12 import javax.servlet.http.HttpServletResponse;
3492 18 Sep 15 nicklas 13
3492 18 Sep 15 nicklas 14 import org.json.simple.JSONArray;
3492 18 Sep 15 nicklas 15 import org.json.simple.JSONObject;
3492 18 Sep 15 nicklas 16
3492 18 Sep 15 nicklas 17 import net.sf.basedb.core.DbControl;
6023 26 Oct 20 nicklas 18 import net.sf.basedb.core.ItemList;
3492 18 Sep 15 nicklas 19 import net.sf.basedb.core.ItemQuery;
3825 05 Apr 16 nicklas 20 import net.sf.basedb.core.Job;
3492 18 Sep 15 nicklas 21 import net.sf.basedb.core.RawBioAssay;
3492 18 Sep 15 nicklas 22 import net.sf.basedb.core.SessionControl;
3616 23 Nov 15 nicklas 23 import net.sf.basedb.core.Software;
3492 18 Sep 15 nicklas 24 import net.sf.basedb.core.query.Hql;
3492 18 Sep 15 nicklas 25 import net.sf.basedb.core.query.Orders;
3768 24 Feb 16 nicklas 26 import net.sf.basedb.core.snapshot.SnapshotManager;
3492 18 Sep 15 nicklas 27 import net.sf.basedb.reggie.JsonUtil;
3492 18 Sep 15 nicklas 28 import net.sf.basedb.reggie.Reggie;
3492 18 Sep 15 nicklas 29 import net.sf.basedb.reggie.counter.CounterService;
3492 18 Sep 15 nicklas 30 import net.sf.basedb.reggie.dao.Annotationtype;
3492 18 Sep 15 nicklas 31 import net.sf.basedb.reggie.dao.Library;
5543 06 Aug 19 nicklas 32 import net.sf.basedb.reggie.dao.Pipeline;
3492 18 Sep 15 nicklas 33 import net.sf.basedb.reggie.dao.Rawbioassay;
4665 31 Jan 18 nicklas 34 import net.sf.basedb.reggie.dao.Rawdatatype;
3492 18 Sep 15 nicklas 35 import net.sf.basedb.reggie.dao.ReggieRole;
3768 24 Feb 16 nicklas 36 import net.sf.basedb.reggie.dao.Rna;
3825 05 Apr 16 nicklas 37 import net.sf.basedb.reggie.dao.Subtype;
3506 23 Sep 15 nicklas 38 import net.sf.basedb.reggie.pdf.PdfReportTemplate;
3492 18 Sep 15 nicklas 39 import net.sf.basedb.reggie.plugins.GeneReportPlugin;
4991 01 Oct 18 nicklas 40 import net.sf.basedb.util.Values;
3492 18 Sep 15 nicklas 41 import net.sf.basedb.util.error.ThrowableUtil;
3492 18 Sep 15 nicklas 42
3492 18 Sep 15 nicklas 43
3492 18 Sep 15 nicklas 44 public class ReportServlet 
3492 18 Sep 15 nicklas 45   extends HttpServlet 
3492 18 Sep 15 nicklas 46 {
3492 18 Sep 15 nicklas 47
3492 18 Sep 15 nicklas 48   private static final long serialVersionUID = -6240967935571591048L;
3492 18 Sep 15 nicklas 49
3492 18 Sep 15 nicklas 50   public ReportServlet()
3492 18 Sep 15 nicklas 51   {}
3492 18 Sep 15 nicklas 52
6023 26 Oct 20 nicklas 53   @SuppressWarnings("unchecked")
3492 18 Sep 15 nicklas 54   @Override
3492 18 Sep 15 nicklas 55   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
3492 18 Sep 15 nicklas 56     throws ServletException, IOException 
3492 18 Sep 15 nicklas 57   {
3492 18 Sep 15 nicklas 58     String cmd = req.getParameter("cmd");
3492 18 Sep 15 nicklas 59     JsonUtil.setJsonResponseHeaders(resp);
3492 18 Sep 15 nicklas 60     
3492 18 Sep 15 nicklas 61     JSONObject json = new JSONObject();
3492 18 Sep 15 nicklas 62     json.put("status", "ok");
3492 18 Sep 15 nicklas 63     
3975 26 May 16 nicklas 64     final SessionControl sc = Reggie.getSessionControl(req);
3492 18 Sep 15 nicklas 65     DbControl dc = null;
3492 18 Sep 15 nicklas 66     try
3492 18 Sep 15 nicklas 67     {
3492 18 Sep 15 nicklas 68       if ("GetRawBioAssayWithoutReport".equals(cmd))
3492 18 Sep 15 nicklas 69       {
3499 21 Sep 15 nicklas 70         String reportType = req.getParameter("report");
3492 18 Sep 15 nicklas 71         
3506 23 Sep 15 nicklas 72         PdfReportTemplate report = PdfReportTemplate.getByCName(reportType, PdfReportTemplate.GENE_REPORT);
3499 21 Sep 15 nicklas 73         String pdfName = report.getDefaultFilename();
3513 24 Sep 15 nicklas 74         String reportName = report.getReportName();
5666 14 Oct 19 nicklas 75         Rawdatatype rawDataType = report.getRawDataType();
5666 14 Oct 19 nicklas 76         Pipeline pipeline = report.getPipeline();
3492 18 Sep 15 nicklas 77
6335 15 Jun 21 nicklas 78         dc = sc.newDbControl("Report wizard ("+report+")");
4991 01 Oct 18 nicklas 79         List<Rawbioassay> list = null;
4991 01 Oct 18 nicklas 80         String items = Values.getStringOrNull(req.getParameter("items"));
4991 01 Oct 18 nicklas 81         if (items == null)
4991 01 Oct 18 nicklas 82         {
6023 26 Oct 20 nicklas 83           ItemList reportList = report.getItemList().get(dc);
6183 26 Mar 21 nicklas 84           ItemQuery<RawBioAssay> query = reportList.getMembers();
4991 01 Oct 18 nicklas 85           query.setIncludes(Reggie.INCLUDE_IN_CURRENT_PROJECT);
5666 14 Oct 19 nicklas 86           if (rawDataType != null) rawDataType.addFilter(dc, query);
5666 14 Oct 19 nicklas 87           if (pipeline != null) pipeline.addFilter(dc, query);
4991 01 Oct 18 nicklas 88   
4991 01 Oct 18 nicklas 89           // Join LibPlate for sorting
4991 01 Oct 18 nicklas 90           query.join(Hql.innerJoin(null, "parentExtract", "lib", true));
4991 01 Oct 18 nicklas 91           query.join(Hql.innerJoin("lib", "bioWell", "bw", true));
4991 01 Oct 18 nicklas 92           query.join(Hql.innerJoin("bw", "bioPlate", "libPlate", true));
4991 01 Oct 18 nicklas 93           query.order(Orders.asc(Hql.property("libPlate", "id")));
4991 01 Oct 18 nicklas 94           query.order(Orders.asc(Hql.property("name")));
4991 01 Oct 18 nicklas 95           query.setMaxResults(250);
4991 01 Oct 18 nicklas 96           list = Rawbioassay.toList(query.list(dc));
4991 01 Oct 18 nicklas 97         }
4991 01 Oct 18 nicklas 98         else
4991 01 Oct 18 nicklas 99         {
4991 01 Oct 18 nicklas 100           Integer[] ids = Values.getInt(items.split(","));
4991 01 Oct 18 nicklas 101           list = new ArrayList<Rawbioassay>();
4991 01 Oct 18 nicklas 102           for (Integer id : ids)
4991 01 Oct 18 nicklas 103           {
4991 01 Oct 18 nicklas 104             list.add(Rawbioassay.getById(dc, id));
4991 01 Oct 18 nicklas 105           }
4991 01 Oct 18 nicklas 106         }
3492 18 Sep 15 nicklas 107         
3768 24 Feb 16 nicklas 108         SnapshotManager manager = new SnapshotManager();
3492 18 Sep 15 nicklas 109         JSONArray jsonRawBioAssays = new JSONArray();
3492 18 Sep 15 nicklas 110         for (Rawbioassay raw : list)
3492 18 Sep 15 nicklas 111         {
3492 18 Sep 15 nicklas 112           RawBioAssay rba = raw.getItem();
3768 24 Feb 16 nicklas 113           raw.loadAnnotations(dc, manager, "AutoProcess", Annotationtype.AUTO_PROCESSING, null);
5666 14 Oct 19 nicklas 114           raw.loadAnnotations(dc, manager, "pipeline", Annotationtype.PIPELINE, null);
3492 18 Sep 15 nicklas 115           
3492 18 Sep 15 nicklas 116           // Load library information
3492 18 Sep 15 nicklas 117           Library lib = raw.getLibrary(dc);
3768 24 Feb 16 nicklas 118           Rna r = lib.getRna(dc, true);
4541 26 Jun 17 nicklas 119           lib.loadBioPlateLocation();
3768 24 Feb 16 nicklas 120           lib.setAnnotation("specimen", r.loadYellowLabelInfo(dc, manager));
3492 18 Sep 15 nicklas 121           raw.setAnnotation("lib", lib.asJSONObject());
4991 01 Oct 18 nicklas 122           raw.loadDoNotUseAnnotations(dc, manager);
3492 18 Sep 15 nicklas 123           jsonRawBioAssays.add(raw.asJSONObject());
3492 18 Sep 15 nicklas 124         }
3492 18 Sep 15 nicklas 125         
3492 18 Sep 15 nicklas 126         json.put("rawBioAssays", jsonRawBioAssays);
3513 24 Sep 15 nicklas 127         json.put("pdfName", pdfName);
3513 24 Sep 15 nicklas 128         json.put("reportName", reportName);
5666 14 Oct 19 nicklas 129         json.put("rawDataType", rawDataType != null ? rawDataType.getId() : null);
5666 14 Oct 19 nicklas 130         json.put("pipeline", pipeline != null ? pipeline.getName() : "");
3616 23 Nov 15 nicklas 131         json.put("configSection", "<"+report.getConfigSection().replace("/", "><")+">");
3492 18 Sep 15 nicklas 132       }
3492 18 Sep 15 nicklas 133
3492 18 Sep 15 nicklas 134     }
3492 18 Sep 15 nicklas 135     catch (Throwable t)
3492 18 Sep 15 nicklas 136     {
3492 18 Sep 15 nicklas 137       t.printStackTrace();
3492 18 Sep 15 nicklas 138       json.clear();
3492 18 Sep 15 nicklas 139       json.put("status", "error");
3492 18 Sep 15 nicklas 140       json.put("message", t.getMessage());
3492 18 Sep 15 nicklas 141       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3492 18 Sep 15 nicklas 142     }
3492 18 Sep 15 nicklas 143     finally
3492 18 Sep 15 nicklas 144     {
3492 18 Sep 15 nicklas 145       if (dc != null) dc.close();
3492 18 Sep 15 nicklas 146       json.writeJSONString(resp.getWriter());
3492 18 Sep 15 nicklas 147     }
3492 18 Sep 15 nicklas 148     
3492 18 Sep 15 nicklas 149   }
3492 18 Sep 15 nicklas 150
3492 18 Sep 15 nicklas 151   @Override
3492 18 Sep 15 nicklas 152   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
3492 18 Sep 15 nicklas 153     throws ServletException, IOException 
3492 18 Sep 15 nicklas 154   {
3492 18 Sep 15 nicklas 155     String cmd = req.getParameter("cmd");
3492 18 Sep 15 nicklas 156     JsonUtil.setJsonResponseHeaders(resp);
3492 18 Sep 15 nicklas 157     
3492 18 Sep 15 nicklas 158     JSONObject json = new JSONObject();
3492 18 Sep 15 nicklas 159     json.put("status", "ok");
3492 18 Sep 15 nicklas 160     JSONArray jsonMessages = new JSONArray();
3492 18 Sep 15 nicklas 161   
3975 26 May 16 nicklas 162     final SessionControl sc = Reggie.getSessionControl(req);
3492 18 Sep 15 nicklas 163     DbControl dc = null;
3492 18 Sep 15 nicklas 164     try
3492 18 Sep 15 nicklas 165     {
3492 18 Sep 15 nicklas 166
3492 18 Sep 15 nicklas 167       if ("CreateReport".equals(cmd))
3492 18 Sep 15 nicklas 168       {
3752 17 Feb 16 nicklas 169         JSONObject jsonReq = JsonUtil.parseRequest(req);
3618 23 Nov 15 nicklas 170         Number softwareId = (Number)jsonReq.get("software");
3492 18 Sep 15 nicklas 171         JSONArray jsonRawBioAssays = (JSONArray)jsonReq.get("rawBioAssays");
3492 18 Sep 15 nicklas 172         String report = (String)jsonReq.get("report");
3616 23 Nov 15 nicklas 173         
6335 15 Jun 21 nicklas 174         dc = sc.newDbControl("Report wizard ("+report+")");
6335 15 Jun 21 nicklas 175         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SECONDARY_ANALYSIS, ReggieRole.PATIENT_CURATOR, ReggieRole.ADMINISTRATOR);
6335 15 Jun 21 nicklas 176         
3825 05 Apr 16 nicklas 177         if (softwareId == null && jsonReq.get("useDefaultSoftware") != null)
3825 05 Apr 16 nicklas 178         {
3825 05 Apr 16 nicklas 179           Software defaultSoftware = Subtype.REPORT_SOFTWARE.getLatestProjectDefault(dc, Annotationtype.REPORT_TEMPLATE.createFilter(report));
3825 05 Apr 16 nicklas 180           if (defaultSoftware != null) softwareId = defaultSoftware.getId();
3825 05 Apr 16 nicklas 181         }
3825 05 Apr 16 nicklas 182         
3616 23 Nov 15 nicklas 183         // Load common items
3618 23 Nov 15 nicklas 184         Software software = softwareId != null ? Software.getById(dc, softwareId.intValue()) : null;
3618 23 Nov 15 nicklas 185         Map<String, Software> reports = new HashMap<String, Software>();
3618 23 Nov 15 nicklas 186         reports.put(report, software);
3618 23 Nov 15 nicklas 187         
3492 18 Sep 15 nicklas 188         List<RawBioAssay> toReport = new ArrayList<RawBioAssay>(jsonRawBioAssays.size());
3492 18 Sep 15 nicklas 189         for (int rawNo = 0; rawNo < jsonRawBioAssays.size(); rawNo++)
3492 18 Sep 15 nicklas 190         {
3492 18 Sep 15 nicklas 191           JSONObject jsonRaw = (JSONObject)jsonRawBioAssays.get(rawNo);
3492 18 Sep 15 nicklas 192           Number rawId = (Number)jsonRaw.get("id");
3492 18 Sep 15 nicklas 193           RawBioAssay raw = RawBioAssay.getById(dc, rawId.intValue());
3492 18 Sep 15 nicklas 194           toReport.add(raw);
3492 18 Sep 15 nicklas 195         }
3492 18 Sep 15 nicklas 196         
5615 19 Sep 19 nicklas 197         Job j = GeneReportPlugin.createReportJob(dc, toReport, reports, false);
3492 18 Sep 15 nicklas 198         dc.commit();
3492 18 Sep 15 nicklas 199         
3825 05 Apr 16 nicklas 200         json.put("job", JsonUtil.getJobAsJSON(j));
3492 18 Sep 15 nicklas 201         jsonMessages.add("Successfully scheduled job for creating " + toReport.size() + " reports" );
3492 18 Sep 15 nicklas 202       }
3492 18 Sep 15 nicklas 203
3492 18 Sep 15 nicklas 204       json.put("messages", jsonMessages);
3492 18 Sep 15 nicklas 205       CounterService.getInstance().setForceCount();
3492 18 Sep 15 nicklas 206     }
3492 18 Sep 15 nicklas 207     catch (Throwable t)
3492 18 Sep 15 nicklas 208     {
3492 18 Sep 15 nicklas 209       t.printStackTrace();
3492 18 Sep 15 nicklas 210       json.clear();
3492 18 Sep 15 nicklas 211       json.put("status", "error");
3492 18 Sep 15 nicklas 212       json.put("message", t.getMessage());
3492 18 Sep 15 nicklas 213       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3492 18 Sep 15 nicklas 214     }
3492 18 Sep 15 nicklas 215     finally
3492 18 Sep 15 nicklas 216     {
3492 18 Sep 15 nicklas 217       if (dc != null) dc.close();
3492 18 Sep 15 nicklas 218       json.writeJSONString(resp.getWriter());
3492 18 Sep 15 nicklas 219     }
3492 18 Sep 15 nicklas 220     
3492 18 Sep 15 nicklas 221   }
3492 18 Sep 15 nicklas 222   
3492 18 Sep 15 nicklas 223   
3492 18 Sep 15 nicklas 224
3492 18 Sep 15 nicklas 225 }