extensions/net.sf.basedb.meludi/trunk/src/net/sf/basedb/meludi/servlet/ExtractionServlet.java

Code
Comments
Other
Rev Date Author Line
3176 06 Mar 15 olle 1 package net.sf.basedb.meludi.servlet;
3176 06 Mar 15 olle 2
3176 06 Mar 15 olle 3 import java.io.IOException;
3176 06 Mar 15 olle 4 import java.io.PrintWriter;
3176 06 Mar 15 olle 5 import java.util.ArrayList;
3176 06 Mar 15 olle 6 import java.util.Arrays;
3212 27 Mar 15 olle 7 import java.util.Collections;
3176 06 Mar 15 olle 8 import java.util.Date;
3176 06 Mar 15 olle 9 import java.util.List;
3176 06 Mar 15 olle 10
3176 06 Mar 15 olle 11 import javax.servlet.ServletException;
3176 06 Mar 15 olle 12 import javax.servlet.http.HttpServlet;
3176 06 Mar 15 olle 13 import javax.servlet.http.HttpServletRequest;
3176 06 Mar 15 olle 14 import javax.servlet.http.HttpServletResponse;
3176 06 Mar 15 olle 15
3176 06 Mar 15 olle 16 import org.json.simple.JSONArray;
3176 06 Mar 15 olle 17 import org.json.simple.JSONObject;
3176 06 Mar 15 olle 18 import org.json.simple.parser.JSONParser;
3176 06 Mar 15 olle 19
3176 06 Mar 15 olle 20 import net.sf.basedb.core.Application;
3432 29 Jun 15 olle 21 import net.sf.basedb.core.BasicItem;
3176 06 Mar 15 olle 22 import net.sf.basedb.core.BioMaterialEvent;
3176 06 Mar 15 olle 23 import net.sf.basedb.core.BioMaterialEventSource;
3176 06 Mar 15 olle 24 import net.sf.basedb.core.BioPlate;
3180 17 Mar 15 olle 25 import net.sf.basedb.core.BioPlateType;
3176 06 Mar 15 olle 26 import net.sf.basedb.core.BioWell;
3176 06 Mar 15 olle 27 import net.sf.basedb.core.DbControl;
3176 06 Mar 15 olle 28 import net.sf.basedb.core.Extract;
3432 29 Jun 15 olle 29 import net.sf.basedb.core.Include;
3432 29 Jun 15 olle 30 import net.sf.basedb.core.InvalidDataException;
3432 29 Jun 15 olle 31 import net.sf.basedb.core.Item;
3432 29 Jun 15 olle 32 import net.sf.basedb.core.ItemList;
3176 06 Mar 15 olle 33 import net.sf.basedb.core.ItemQuery;
3176 06 Mar 15 olle 34 import net.sf.basedb.core.ItemSubtype;
3176 06 Mar 15 olle 35 import net.sf.basedb.core.Permission;
3180 17 Mar 15 olle 36 import net.sf.basedb.core.PlateGeometry;
3176 06 Mar 15 olle 37 import net.sf.basedb.core.Protocol;
3176 06 Mar 15 olle 38 import net.sf.basedb.core.Sample;
3176 06 Mar 15 olle 39 import net.sf.basedb.core.SessionControl;
3176 06 Mar 15 olle 40 import net.sf.basedb.core.Type;
3176 06 Mar 15 olle 41 import net.sf.basedb.core.query.Annotations;
3176 06 Mar 15 olle 42 import net.sf.basedb.core.query.Expressions;
3176 06 Mar 15 olle 43 import net.sf.basedb.core.query.Hql;
3176 06 Mar 15 olle 44 import net.sf.basedb.core.query.Orders;
3176 06 Mar 15 olle 45 import net.sf.basedb.core.query.Restrictions;
3225 02 Apr 15 olle 46 import net.sf.basedb.core.snapshot.SnapshotManager;
3176 06 Mar 15 olle 47 import net.sf.basedb.meludi.JsonUtil;
3176 06 Mar 15 olle 48 import net.sf.basedb.meludi.Meludi;
3440 17 Jul 15 olle 49 import net.sf.basedb.meludi.converter.ValueConverter;
3176 06 Mar 15 olle 50 import net.sf.basedb.meludi.counter.CounterService;
3176 06 Mar 15 olle 51 import net.sf.basedb.meludi.dao.Annotationtype;
3176 06 Mar 15 olle 52 import net.sf.basedb.meludi.dao.BiomaterialList;
3180 17 Mar 15 olle 53 import net.sf.basedb.meludi.dao.BioplateType;
5158 30 Nov 18 olle 54 import net.sf.basedb.meludi.dao.Case;
4954 31 Aug 18 olle 55 import net.sf.basedb.meludi.dao.FfpeBlock;
5848 02 Mar 20 olle 56 import net.sf.basedb.meludi.dao.Histology;
3176 06 Mar 15 olle 57 import net.sf.basedb.meludi.dao.MeludiRole;
5158 30 Nov 18 olle 58 import net.sf.basedb.meludi.dao.Patient;
3176 06 Mar 15 olle 59 import net.sf.basedb.meludi.dao.Dna;
3180 17 Mar 15 olle 60 import net.sf.basedb.meludi.dao.ReactionPlate;
3176 06 Mar 15 olle 61 import net.sf.basedb.meludi.dao.Rna;
3176 06 Mar 15 olle 62 import net.sf.basedb.meludi.dao.SpecimenTube;
3432 29 Jun 15 olle 63 import net.sf.basedb.meludi.dao.StartList;
3176 06 Mar 15 olle 64 import net.sf.basedb.meludi.dao.StoragePlate;
3176 06 Mar 15 olle 65 import net.sf.basedb.meludi.dao.Subtype;
3176 06 Mar 15 olle 66 import net.sf.basedb.util.Values;
3176 06 Mar 15 olle 67 import net.sf.basedb.util.error.ThrowableUtil;
3176 06 Mar 15 olle 68 import net.sf.basedb.util.export.TableWriter;
3176 06 Mar 15 olle 69
3176 06 Mar 15 olle 70
3176 06 Mar 15 olle 71 public class ExtractionServlet 
3176 06 Mar 15 olle 72   extends HttpServlet 
3176 06 Mar 15 olle 73 {
3176 06 Mar 15 olle 74
3176 06 Mar 15 olle 75
3176 06 Mar 15 olle 76   private static final long serialVersionUID = 5374097270488331091L;
3176 06 Mar 15 olle 77
3176 06 Mar 15 olle 78   public ExtractionServlet()
3176 06 Mar 15 olle 79   {}
3176 06 Mar 15 olle 80
3176 06 Mar 15 olle 81   @SuppressWarnings("unchecked")
3176 06 Mar 15 olle 82   @Override
3176 06 Mar 15 olle 83   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
3176 06 Mar 15 olle 84     throws ServletException, IOException 
3176 06 Mar 15 olle 85   {
3176 06 Mar 15 olle 86
3176 06 Mar 15 olle 87     String ID = req.getParameter("ID");
3176 06 Mar 15 olle 88     String cmd = req.getParameter("cmd");
3176 06 Mar 15 olle 89     JsonUtil.setJsonResponseHeaders(resp);
3176 06 Mar 15 olle 90     
3176 06 Mar 15 olle 91     JSONObject json = new JSONObject();
3176 06 Mar 15 olle 92     json.put("status", "ok");
3176 06 Mar 15 olle 93   
5468 04 Jun 19 olle 94     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 95     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 96     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
3176 06 Mar 15 olle 97     DbControl dc = null;
3176 06 Mar 15 olle 98     try
3176 06 Mar 15 olle 99     {
3176 06 Mar 15 olle 100       if ("GetUnprocessedSpecimens".equals(cmd))
3176 06 Mar 15 olle 101       {
4246 21 Nov 16 olle 102         Boolean withExtracts = Values.getBoolean(req.getParameter("withExtracts"), true);
3176 06 Mar 15 olle 103         /*
3176 06 Mar 15 olle 104           Find SpecimenTube items that have no created date. Order by
3176 06 Mar 15 olle 105           bioplate position.
3176 06 Mar 15 olle 106         */
3176 06 Mar 15 olle 107         dc = sc.newDbControl();
4246 21 Nov 16 olle 108         List<SpecimenTube> specimenTubes = SpecimenTube.findUnProcessedTubes(dc, withExtracts);
3176 06 Mar 15 olle 109         
3176 06 Mar 15 olle 110         JSONArray jsonSpecimen = new JSONArray();
3176 06 Mar 15 olle 111         
3176 06 Mar 15 olle 112         for (SpecimenTube s : specimenTubes)
3176 06 Mar 15 olle 113         {
3176 06 Mar 15 olle 114           s.loadBioPlateLocation();
3176 06 Mar 15 olle 115           jsonSpecimen.add(s.asJSONObject());
3176 06 Mar 15 olle 116         }
3176 06 Mar 15 olle 117         
3176 06 Mar 15 olle 118         json.put("specimens", jsonSpecimen);
3176 06 Mar 15 olle 119       }      
3176 06 Mar 15 olle 120       else if ("GetUnprocessedItems".equals(cmd))
3176 06 Mar 15 olle 121       {
3212 27 Mar 15 olle 122         JSONObject jsonItemsByName = new JSONObject();
3212 27 Mar 15 olle 123         List<String> itemNameList = new ArrayList<String>();
3413 23 Jun 15 olle 124         List<String> meludiItemNameList = new ArrayList<String>();
3413 23 Jun 15 olle 125         List<String> extraItemNameList = new ArrayList<String>();
3212 27 Mar 15 olle 126
3176 06 Mar 15 olle 127         /*
3176 06 Mar 15 olle 128           Find SpecimenTube items that have no created date. Order by
3176 06 Mar 15 olle 129           bioplate position.
3176 06 Mar 15 olle 130         */
3176 06 Mar 15 olle 131         dc = sc.newDbControl();
3176 06 Mar 15 olle 132         List<SpecimenTube> specimenTubes = SpecimenTube.findUnProcessedTubes(dc);
3176 06 Mar 15 olle 133         
3176 06 Mar 15 olle 134         JSONArray jsonSpecimen = new JSONArray();
4150 03 Oct 16 olle 135
4150 03 Oct 16 olle 136         String itemNamePrefix = Meludi.fetchSampleItemPrefix(sc.getActiveProjectId());
3176 06 Mar 15 olle 137         for (SpecimenTube s : specimenTubes)
3176 06 Mar 15 olle 138         {
3176 06 Mar 15 olle 139           s.loadBioPlateLocation();
3276 29 Apr 15 olle 140           s.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
3276 29 Apr 15 olle 141           JSONObject jsonSpec = s.asJSONObject();
3276 29 Apr 15 olle 142           // Get DNA extract plate position
3276 29 Apr 15 olle 143           String specDnaName = s.getName() + ".d";
3276 29 Apr 15 olle 144           Dna dna = Dna.findByName(dc, specDnaName);
3276 29 Apr 15 olle 145           if (dna != null)
3276 29 Apr 15 olle 146           {
3276 29 Apr 15 olle 147             dna.loadBioPlateLocation();
3276 29 Apr 15 olle 148             jsonSpec.put("dna", dna.asJSONObject());
3276 29 Apr 15 olle 149           }
3276 29 Apr 15 olle 150           // Get RNA extract plate position
3276 29 Apr 15 olle 151           String specRnaName = s.getName() + ".r";
3276 29 Apr 15 olle 152           Rna rna = Rna.findByName(dc, specRnaName);
3276 29 Apr 15 olle 153           if (rna != null)
3276 29 Apr 15 olle 154           {
3276 29 Apr 15 olle 155             rna.loadBioPlateLocation();
3276 29 Apr 15 olle 156             jsonSpec.put("rna", rna.asJSONObject());
3276 29 Apr 15 olle 157           }
5848 02 Mar 20 olle 158           // Get histology info
5848 02 Mar 20 olle 159           String histologyName = s.getName() + ".his";
5848 02 Mar 20 olle 160           List<Histology> histology = Histology.findByName(dc, histologyName);
5848 02 Mar 20 olle 161           if (histology.size() > 0)
5848 02 Mar 20 olle 162           {
5848 02 Mar 20 olle 163             JSONArray jsonHistology = new JSONArray();
5848 02 Mar 20 olle 164             for (Histology his : histology)
5848 02 Mar 20 olle 165             {
5848 02 Mar 20 olle 166               loadHistologyInfo(dc, his);
5848 02 Mar 20 olle 167               //jsonHistology.add(his.asJSONObject());
5848 02 Mar 20 olle 168               jsonSpec.put("histology", his.asJSONObject());
5848 02 Mar 20 olle 169             }
5848 02 Mar 20 olle 170             //jsonSections.add(loadSectionInfo(sc, "histology"));
5848 02 Mar 20 olle 171             //json.put("histology", jsonHistology);
5848 02 Mar 20 olle 172           }
5848 02 Mar 20 olle 173           
3276 29 Apr 15 olle 174           // Add JSON data for specimen
3276 29 Apr 15 olle 175           jsonSpecimen.add(jsonSpec);
3212 27 Mar 15 olle 176           String itemName = s.getName();
3276 29 Apr 15 olle 177           jsonItemsByName.put(itemName, jsonSpec);
3212 27 Mar 15 olle 178           itemNameList.add(itemName);
4150 03 Oct 16 olle 179           if (itemName.startsWith(itemNamePrefix))
3413 23 Jun 15 olle 180           {
3413 23 Jun 15 olle 181             meludiItemNameList.add(itemName);
3413 23 Jun 15 olle 182           }
3413 23 Jun 15 olle 183           else
3413 23 Jun 15 olle 184           {
3413 23 Jun 15 olle 185             extraItemNameList.add(itemName);
3413 23 Jun 15 olle 186           }
3176 06 Mar 15 olle 187         }
3176 06 Mar 15 olle 188         json.put("specimens", jsonSpecimen);
3176 06 Mar 15 olle 189
3176 06 Mar 15 olle 190         /*
3176 06 Mar 15 olle 191           Find RNA items that have no original quantity. Order by
3176 06 Mar 15 olle 192           bioplate position.
3176 06 Mar 15 olle 193          */
3176 06 Mar 15 olle 194         List<Rna> rnas = Rna.findUnquantifiedRna(dc);
3176 06 Mar 15 olle 195       
3176 06 Mar 15 olle 196         JSONArray jsonRna = new JSONArray();
3176 06 Mar 15 olle 197       
3176 06 Mar 15 olle 198         for (Rna r : rnas)
3176 06 Mar 15 olle 199         {
3212 27 Mar 15 olle 200           String itemName = r.getName();
3433 29 Jun 15 olle 201           if (Meludi.itemIsExtractSourceItem(itemName))
3276 29 Apr 15 olle 202           {
3276 29 Apr 15 olle 203             r.loadBioPlateLocation();
3318 08 May 15 olle 204             r.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT_EXTRACT, null);
3276 29 Apr 15 olle 205             jsonRna.add(r.asJSONObject());
3276 29 Apr 15 olle 206             jsonItemsByName.put(itemName, r.asJSONObject());
3276 29 Apr 15 olle 207             itemNameList.add(itemName);
4150 03 Oct 16 olle 208             if (itemName.startsWith(itemNamePrefix))
3413 23 Jun 15 olle 209             {
3413 23 Jun 15 olle 210               meludiItemNameList.add(itemName);
3413 23 Jun 15 olle 211             }
3413 23 Jun 15 olle 212             else
3413 23 Jun 15 olle 213             {
3413 23 Jun 15 olle 214               extraItemNameList.add(itemName);
3413 23 Jun 15 olle 215             }
3276 29 Apr 15 olle 216           }
3212 27 Mar 15 olle 217         }      
3176 06 Mar 15 olle 218         json.put("rnas", jsonRna);
3176 06 Mar 15 olle 219
3176 06 Mar 15 olle 220         /*
3176 06 Mar 15 olle 221           Find DNA items that have no original quantity. Order by
3176 06 Mar 15 olle 222           bioplate position.
3176 06 Mar 15 olle 223          */
3176 06 Mar 15 olle 224         List<Dna> dnas = Dna.findUnquantifiedDna(dc);
3176 06 Mar 15 olle 225     
3176 06 Mar 15 olle 226         JSONArray jsonDna = new JSONArray();
3176 06 Mar 15 olle 227     
3176 06 Mar 15 olle 228         for (Dna d : dnas)
3176 06 Mar 15 olle 229         {
3212 27 Mar 15 olle 230           String itemName = d.getName();
3433 29 Jun 15 olle 231           if (Meludi.itemIsExtractSourceItem(itemName))
3276 29 Apr 15 olle 232           {
3276 29 Apr 15 olle 233             d.loadBioPlateLocation();
3318 08 May 15 olle 234             d.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT_EXTRACT, null);
3276 29 Apr 15 olle 235             jsonDna.add(d.asJSONObject());
3276 29 Apr 15 olle 236             jsonItemsByName.put(itemName, d.asJSONObject());
3276 29 Apr 15 olle 237             itemNameList.add(itemName);
4150 03 Oct 16 olle 238             if (itemName.startsWith(itemNamePrefix))
3413 23 Jun 15 olle 239             {
3413 23 Jun 15 olle 240               meludiItemNameList.add(itemName);
3413 23 Jun 15 olle 241             }
3413 23 Jun 15 olle 242             else
3413 23 Jun 15 olle 243             {
3413 23 Jun 15 olle 244               extraItemNameList.add(itemName);
3413 23 Jun 15 olle 245             }
3276 29 Apr 15 olle 246           }
3212 27 Mar 15 olle 247         }    
3212 27 Mar 15 olle 248         json.put("dnas", jsonDna);
3212 27 Mar 15 olle 249
3212 27 Mar 15 olle 250         // Sort item name list
3212 27 Mar 15 olle 251         Collections.sort(itemNameList);
3413 23 Jun 15 olle 252         Collections.sort(meludiItemNameList);
3413 23 Jun 15 olle 253         Collections.sort(extraItemNameList);
3212 27 Mar 15 olle 254         // Create JSON array of items sorted after name
3212 27 Mar 15 olle 255         JSONArray jsonItems = new JSONArray();
3413 23 Jun 15 olle 256         // List MeLuDI items first, then extra material, each sorted alphabetically in its group
3413 23 Jun 15 olle 257         for (int i = 0; i < meludiItemNameList.size(); i++)
3413 23 Jun 15 olle 258         {
3413 23 Jun 15 olle 259           String itemName = (String) meludiItemNameList.get(i);
3413 23 Jun 15 olle 260           JSONObject jsonItem = (JSONObject) jsonItemsByName.get(itemName);
3413 23 Jun 15 olle 261           jsonItems.add(jsonItem);
3413 23 Jun 15 olle 262         }
3413 23 Jun 15 olle 263         for (int i = 0; i < extraItemNameList.size(); i++)
3413 23 Jun 15 olle 264         {
3413 23 Jun 15 olle 265           String itemName = (String) extraItemNameList.get(i);
3413 23 Jun 15 olle 266           JSONObject jsonItem = (JSONObject) jsonItemsByName.get(itemName);
3413 23 Jun 15 olle 267           jsonItems.add(jsonItem);
3413 23 Jun 15 olle 268         }
3212 27 Mar 15 olle 269         json.put("items", jsonItems);
3176 06 Mar 15 olle 270       }      
3206 20 Mar 15 olle 271       else if ("GetStartPlates".equals(cmd))
3206 20 Mar 15 olle 272       {
3206 20 Mar 15 olle 273         /*
3206 20 Mar 15 olle 274           Find start DNA plates. Order by bioplate name.
3206 20 Mar 15 olle 275         */
3206 20 Mar 15 olle 276         dc = sc.newDbControl();
3206 20 Mar 15 olle 277         Subtype subtype = null;
4200 01 Nov 16 olle 278         String prefix = Meludi.fetchStartPlateItemPrefix(sc.getActiveProjectId());
3206 20 Mar 15 olle 279         int numFreeWells = 0;
3206 20 Mar 15 olle 280         Annotationtype runDateAnnotation = null;
3254 17 Apr 15 olle 281         Boolean destroyed = false;
3255 17 Apr 15 olle 282         Boolean orderDesc = Boolean.valueOf(req.getParameter("orderDesc"));
3255 17 Apr 15 olle 283         if (orderDesc == null)
3254 17 Apr 15 olle 284         {
3255 17 Apr 15 olle 285           orderDesc = false;
3254 17 Apr 15 olle 286         }
3206 20 Mar 15 olle 287         List<ReactionPlate> startPlates = ReactionPlate.findByBioPlateType(dc, 
3254 17 Apr 15 olle 288             BioplateType.DNA, subtype, prefix, numFreeWells, runDateAnnotation, destroyed, orderDesc);
3206 20 Mar 15 olle 289         
3206 20 Mar 15 olle 290         JSONArray jsonPlates = new JSONArray();
3206 20 Mar 15 olle 291         
3440 17 Jul 15 olle 292         // Use stored annotation snapshots for performance reasons
3440 17 Jul 15 olle 293         SnapshotManager manager = new SnapshotManager();        
3440 17 Jul 15 olle 294
3206 20 Mar 15 olle 295         for (ReactionPlate p : startPlates)
3206 20 Mar 15 olle 296         {
3225 02 Apr 15 olle 297           Date extractionDate = null;
3225 02 Apr 15 olle 298           BioPlate bioPlate = p.getBioPlate();
3225 02 Apr 15 olle 299           if (bioPlate != null)
3225 02 Apr 15 olle 300           {
3225 02 Apr 15 olle 301             extractionDate = bioPlate.getEventDate();
3225 02 Apr 15 olle 302           }
3233 09 Apr 15 olle 303           //p.loadAnnotations(dc, "sourceItemNames", Annotationtype.SOURCE_ITEM_NAMES, null);
3440 17 Jul 15 olle 304           p.loadAnnotations(dc, manager, "sourceItemNames", Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING, null, true, true);
3440 17 Jul 15 olle 305           p.loadAnnotations(dc, manager, "numberExtraQiacubeItems", Annotationtype.BATCH_NUMBER_EXTRA_QIACUBE_ITEMS, null);
3440 17 Jul 15 olle 306           p.loadAnnotations(dc, manager, "extraItemNames", Annotationtype.EXTRA_QIACUBE_ITEM_NAMES_LIST_STRING, null, true, true);
3225 02 Apr 15 olle 307           JSONObject jsonPlate = p.asJSONObject();
3225 02 Apr 15 olle 308           jsonPlate.put("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(extractionDate));
3225 02 Apr 15 olle 309           jsonPlates.add(jsonPlate);
3206 20 Mar 15 olle 310         }
3206 20 Mar 15 olle 311         
3206 20 Mar 15 olle 312         json.put("plates", jsonPlates);
3206 20 Mar 15 olle 313       }      
3180 17 Mar 15 olle 314       else if ("GetUnprocessedStartPlates".equals(cmd))
3180 17 Mar 15 olle 315       {
3180 17 Mar 15 olle 316         /*
3180 17 Mar 15 olle 317           Find start DNA plates that have no created date. Order by
3180 17 Mar 15 olle 318           bioplate name.
3180 17 Mar 15 olle 319         */
3180 17 Mar 15 olle 320         dc = sc.newDbControl();
3180 17 Mar 15 olle 321         Subtype subtype = null;
4200 01 Nov 16 olle 322         String prefix = Meludi.fetchStartPlateItemPrefix(sc.getActiveProjectId());
3180 17 Mar 15 olle 323         int numFreeWells = 0;
3180 17 Mar 15 olle 324         Annotationtype runDateAnnotation = null;
3180 17 Mar 15 olle 325         List<ReactionPlate> startPlates = ReactionPlate.findActiveByBioPlateType(dc, 
3180 17 Mar 15 olle 326             BioplateType.DNA, subtype, prefix, numFreeWells, runDateAnnotation);
3180 17 Mar 15 olle 327         
3180 17 Mar 15 olle 328         JSONArray jsonPlates = new JSONArray();
3180 17 Mar 15 olle 329         
3440 17 Jul 15 olle 330         // Use stored annotation snapshots for performance reasons
3440 17 Jul 15 olle 331         SnapshotManager manager = new SnapshotManager();        
3180 17 Mar 15 olle 332         for (ReactionPlate p : startPlates)
3180 17 Mar 15 olle 333         {
3440 17 Jul 15 olle 334           p.loadAnnotations(dc, manager, "sourceItemNames", Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING, null, true, true);
3440 17 Jul 15 olle 335           p.loadAnnotations(dc, manager, "numberExtraQiacubeItems", Annotationtype.BATCH_NUMBER_EXTRA_QIACUBE_ITEMS, null);
3440 17 Jul 15 olle 336           p.loadAnnotations(dc, manager, "extraItemNames", Annotationtype.EXTRA_QIACUBE_ITEM_NAMES_LIST_STRING, null, true, true);
3180 17 Mar 15 olle 337           jsonPlates.add(p.asJSONObject());
3180 17 Mar 15 olle 338         }
3180 17 Mar 15 olle 339         
3180 17 Mar 15 olle 340         json.put("plates", jsonPlates);
3180 17 Mar 15 olle 341       }      
3447 27 Jul 15 olle 342       else if ("CheckIfStartPlateNameUsed".equals(cmd))
3447 27 Jul 15 olle 343       {
3447 27 Jul 15 olle 344         dc = sc.newDbControl();
3447 27 Jul 15 olle 345         String startPlateName = req.getParameter("startPlateName");
3447 27 Jul 15 olle 346
3447 27 Jul 15 olle 347         Integer bioPlateId = null;
3447 27 Jul 15 olle 348         if (startPlateName != null && !startPlateName.equals(""))
3447 27 Jul 15 olle 349         {
3447 27 Jul 15 olle 350           ItemQuery<BioPlate> query = BioPlate.getQuery();
3447 27 Jul 15 olle 351           query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3447 27 Jul 15 olle 352           query.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(startPlateName)));
3447 27 Jul 15 olle 353           query.order(Orders.asc(Hql.property("name")));
3447 27 Jul 15 olle 354           List<BioPlate> plates = query.list(dc);
3447 27 Jul 15 olle 355           
3447 27 Jul 15 olle 356           if (plates.size() > 0)
3447 27 Jul 15 olle 357           {
3447 27 Jul 15 olle 358             BioPlate bioPlate = (BioPlate) plates.get(0);
3447 27 Jul 15 olle 359             bioPlateId = bioPlate.getId();
3447 27 Jul 15 olle 360           }
3447 27 Jul 15 olle 361         }
3447 27 Jul 15 olle 362         JSONObject jsonPlate = new JSONObject();
3447 27 Jul 15 olle 363         jsonPlate.put("name", startPlateName);
3447 27 Jul 15 olle 364         jsonPlate.put("id", bioPlateId);
3447 27 Jul 15 olle 365         json.put("startplate", jsonPlate);
3447 27 Jul 15 olle 366       }
3180 17 Mar 15 olle 367       else if ("GetSourceItemListFromStartPlate".equals(cmd))
3180 17 Mar 15 olle 368       {
3180 17 Mar 15 olle 369         /*
3180 17 Mar 15 olle 370           Find source items on start DNA plate from "SourceItemNames" annotation.
3180 17 Mar 15 olle 371         */
3180 17 Mar 15 olle 372         dc = sc.newDbControl();
3180 17 Mar 15 olle 373         Integer startPlateId = Values.getInteger(req.getParameter("startPlateId"), null);
3225 02 Apr 15 olle 374         Boolean includeSpecimenExtracts = Boolean.valueOf(req.getParameter("includeSpecimenExtracts"));
3225 02 Apr 15 olle 375         if (includeSpecimenExtracts == null)
3225 02 Apr 15 olle 376         {
3225 02 Apr 15 olle 377           includeSpecimenExtracts = false;
3225 02 Apr 15 olle 378         }
3180 17 Mar 15 olle 379
3180 17 Mar 15 olle 380         JSONObject jsonStartPlate = new JSONObject();
3180 17 Mar 15 olle 381         JSONArray jsonSpecimen = new JSONArray();
3180 17 Mar 15 olle 382         JSONArray jsonRna = new JSONArray();
3180 17 Mar 15 olle 383         JSONArray jsonDna = new JSONArray();
3212 27 Mar 15 olle 384         JSONArray jsonItems = new JSONArray();
3245 14 Apr 15 olle 385         JSONArray jsonExtraItems = new JSONArray();
3180 17 Mar 15 olle 386
3225 02 Apr 15 olle 387         // Use stored annotation snapshots for performance reasons
3225 02 Apr 15 olle 388         SnapshotManager manager = new SnapshotManager();        
3245 14 Apr 15 olle 389         Integer numExtraQiacubeItems = 0;
3180 17 Mar 15 olle 390         List<String> itemNameList = null;
3245 14 Apr 15 olle 391         List<String> extraItemNameList = null;
3180 17 Mar 15 olle 392         if (startPlateId != null)
3180 17 Mar 15 olle 393         {
3180 17 Mar 15 olle 394           //ReactionPlate startPlate = ReactionPlate.getById(dc, startPlateId, BioplateType.DNA);
3180 17 Mar 15 olle 395           BioPlate startPlate = BioPlate.getById(dc, startPlateId);
3180 17 Mar 15 olle 396           if (startPlate != null)
3180 17 Mar 15 olle 397           {
3233 09 Apr 15 olle 398             //itemNameList = (List<String>) Annotationtype.SOURCE_ITEM_NAMES.getAnnotationValues(dc, startPlate);
3233 09 Apr 15 olle 399             itemNameList = (List<String>) Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING.getAnnotationValues(dc, startPlate, true, true);
3245 14 Apr 15 olle 400             extraItemNameList = (List<String>) Annotationtype.EXTRA_QIACUBE_ITEM_NAMES_LIST_STRING.getAnnotationValues(dc, startPlate, true, true);
3180 17 Mar 15 olle 401             jsonStartPlate.put("id", startPlate.getId());
3180 17 Mar 15 olle 402             jsonStartPlate.put("name", (String) startPlate.getName());
3225 02 Apr 15 olle 403             Date extractionDate = startPlate.getEventDate();
3225 02 Apr 15 olle 404             jsonStartPlate.put("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(extractionDate));
3245 14 Apr 15 olle 405             numExtraQiacubeItems = (Integer)Annotationtype.BATCH_NUMBER_EXTRA_QIACUBE_ITEMS.getAnnotationValue(dc, manager, startPlate);
3180 17 Mar 15 olle 406             // Get start plate extraction annotations
3180 17 Mar 15 olle 407             Date isolationDate = (Date) Annotationtype.BATCH_ISOLATION_DATE.getAnnotationValue(dc, startPlate);
3225 02 Apr 15 olle 408             Number extractionProtocolId = (Number) Annotationtype.BATCH_EXTRACTION_PROTOCOL_ID.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 409             String extractionProtocolName = (String)Annotationtype.BATCH_EXTRACTION_PROTOCOL_NAME.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 410             Float rnaDefaultVolume = (Float)Annotationtype.BATCH_RNA_DEFAULT_VOLUME_IN_MICROLITRE.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 411             Float dnaDefaultVolume = (Float)Annotationtype.BATCH_DNA_DEFAULT_VOLUME_IN_MICROLITRE.getAnnotationValue(dc, manager, startPlate);
3180 17 Mar 15 olle 412             // Get start plate QIAcube annotations
3225 02 Apr 15 olle 413             Date qiacubeDate = (Date) Annotationtype.BATCH_QIACUBE_DATE.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 414             String qiacubePrimaryPrefix = (String)Annotationtype.BATCH_QIACUBE_PRIMARY_PREFIX.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 415             String qiacubeSecondaryPrefix = (String)Annotationtype.BATCH_QIACUBE_SECONDARY_PREFIX.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 416             Integer qiacubeRunNo = (Integer)Annotationtype.BATCH_QIACUBE_RUN_NO.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 417             String qiacubeOperator = (String)Annotationtype.BATCH_QIACUBE_OPERATOR.getAnnotationValue(dc, manager, startPlate);
3225 02 Apr 15 olle 418             String allPrepFfpeKit = (String)Annotationtype.BATCH_ALLPREP_FFPE_KIT.getAnnotationValue(dc, manager, startPlate);
3180 17 Mar 15 olle 419
3180 17 Mar 15 olle 420             jsonStartPlate.put("numExtraQiacubeItems", numExtraQiacubeItems);
3180 17 Mar 15 olle 421             jsonStartPlate.put("isolationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(isolationDate));
3180 17 Mar 15 olle 422             jsonStartPlate.put("extractionProtocolId", extractionProtocolId);
3180 17 Mar 15 olle 423             jsonStartPlate.put("extractionProtocolName", extractionProtocolName);
3180 17 Mar 15 olle 424             jsonStartPlate.put("rnaDefaultVolume", rnaDefaultVolume);
3180 17 Mar 15 olle 425             jsonStartPlate.put("dnaDefaultVolume", dnaDefaultVolume);
3180 17 Mar 15 olle 426             jsonStartPlate.put("qiacubeDate", Meludi.CONVERTER_DATE_TO_STRING.convert(qiacubeDate));
3180 17 Mar 15 olle 427             jsonStartPlate.put("qiacubePrimaryPrefix", qiacubePrimaryPrefix);
3180 17 Mar 15 olle 428             jsonStartPlate.put("qiacubeSecondaryPrefix", qiacubeSecondaryPrefix);
3180 17 Mar 15 olle 429             jsonStartPlate.put("qiacubeRunNo", qiacubeRunNo);
3180 17 Mar 15 olle 430             jsonStartPlate.put("qiacubeOperator", qiacubeOperator);
3180 17 Mar 15 olle 431             jsonStartPlate.put("allPrepFfpeKit", allPrepFfpeKit);
3180 17 Mar 15 olle 432           }
3180 17 Mar 15 olle 433         }
3180 17 Mar 15 olle 434         
3180 17 Mar 15 olle 435         // Convert item names to items
3180 17 Mar 15 olle 436         if (itemNameList != null)
3180 17 Mar 15 olle 437         {
3180 17 Mar 15 olle 438           for (String itemName : itemNameList)
3180 17 Mar 15 olle 439           {
3433 29 Jun 15 olle 440             if (Meludi.itemIsSpecimen(itemName))
3180 17 Mar 15 olle 441             {
3180 17 Mar 15 olle 442               SpecimenTube s = SpecimenTube.findByTubeName(dc, itemName);
5823 04 Feb 20 olle 443               if (s != null)
3225 02 Apr 15 olle 444               {
5823 04 Feb 20 olle 445                 s.loadBioPlateLocation();
5823 04 Feb 20 olle 446                 loadSpecimenInfo(dc, manager, s);
5823 04 Feb 20 olle 447                 jsonSpecimen.add(s.asJSONObject());
5823 04 Feb 20 olle 448                 JSONObject jsonSpec = s.asJSONObject();
5823 04 Feb 20 olle 449                 //jsonItems.add(s.asJSONObject());
5823 04 Feb 20 olle 450                 // Loading specimen extract data might decrease performance, only use when needed
5823 04 Feb 20 olle 451                 if (includeSpecimenExtracts)
3225 02 Apr 15 olle 452                 {
5823 04 Feb 20 olle 453                   String specQiacubePosition = null;
5823 04 Feb 20 olle 454                   // Load optional data for Rna for specimen
5823 04 Feb 20 olle 455                   String rnaName = itemName + ".r";
5823 04 Feb 20 olle 456                   Rna r = Rna.findByName(dc, rnaName);
5823 04 Feb 20 olle 457                   if (r != null)
5823 04 Feb 20 olle 458                   {
5823 04 Feb 20 olle 459                     r.loadBioPlateLocation();
5823 04 Feb 20 olle 460                     loadRnaInfo(dc, manager, r);
5823 04 Feb 20 olle 461                     jsonRna.add(r.asJSONObject());
5823 04 Feb 20 olle 462                     jsonSpec.put("rna", r.asJSONObject());
5823 04 Feb 20 olle 463                   }
5823 04 Feb 20 olle 464                   
5823 04 Feb 20 olle 465                   // Load optional data for Dna for specimen
5823 04 Feb 20 olle 466                   String dnaName = itemName + ".d";
5823 04 Feb 20 olle 467                   Dna d = Dna.findByName(dc, dnaName);
5823 04 Feb 20 olle 468                   if (d != null)
5823 04 Feb 20 olle 469                   {
5823 04 Feb 20 olle 470                     d.loadBioPlateLocation();
5823 04 Feb 20 olle 471                     loadDnaInfo(dc, manager, d);
5823 04 Feb 20 olle 472                     jsonDna.add(d.asJSONObject());
5823 04 Feb 20 olle 473                     jsonSpec.put("dna", d.asJSONObject());
5823 04 Feb 20 olle 474                   }
3225 02 Apr 15 olle 475                 }
5823 04 Feb 20 olle 476                 jsonItems.add(jsonSpec);                
3225 02 Apr 15 olle 477               }
5823 04 Feb 20 olle 478               else
5823 04 Feb 20 olle 479               {
5823 04 Feb 20 olle 480                 System.out.println(new Date() + " ExtractionServlet::doGet(): cmd = \"" + cmd + "\" itemName = \"" + itemName + "\" SpecimenTube s = null.");
5823 04 Feb 20 olle 481               }
3180 17 Mar 15 olle 482             }
3433 29 Jun 15 olle 483             else if (Meludi.itemIsRna(itemName))
3180 17 Mar 15 olle 484             {
3180 17 Mar 15 olle 485               Rna r = Rna.findByName(dc, itemName);
3180 17 Mar 15 olle 486               r.loadBioPlateLocation();
3318 08 May 15 olle 487               loadRnaInfo(dc, manager, r);
3180 17 Mar 15 olle 488               jsonRna.add(r.asJSONObject());
3225 02 Apr 15 olle 489               JSONObject jsonR = r.asJSONObject();
3225 02 Apr 15 olle 490               jsonItems.add(jsonR);
3180 17 Mar 15 olle 491             }
3433 29 Jun 15 olle 492             else if (Meludi.itemIsDna(itemName))
3180 17 Mar 15 olle 493             {
3180 17 Mar 15 olle 494               Dna d = Dna.findByName(dc, itemName);
3180 17 Mar 15 olle 495               d.loadBioPlateLocation();
3318 08 May 15 olle 496               loadDnaInfo(dc, manager, d);
3180 17 Mar 15 olle 497               jsonDna.add(d.asJSONObject());
3225 02 Apr 15 olle 498               JSONObject jsonD = d.asJSONObject();
3225 02 Apr 15 olle 499               jsonItems.add(jsonD);
3180 17 Mar 15 olle 500             }
3180 17 Mar 15 olle 501           }
3180 17 Mar 15 olle 502         }
3180 17 Mar 15 olle 503         
3245 14 Apr 15 olle 504         // Convert extra QIAcube item names to items
3245 14 Apr 15 olle 505         if (extraItemNameList != null && extraItemNameList.size() > 0)
3245 14 Apr 15 olle 506         {
3245 14 Apr 15 olle 507           for (String itemName : extraItemNameList)
3245 14 Apr 15 olle 508           {
3245 14 Apr 15 olle 509             JSONObject jsonExtra = new JSONObject(); 
3245 14 Apr 15 olle 510             jsonExtra.put("name", itemName);
3245 14 Apr 15 olle 511             jsonExtraItems.add(jsonExtra);
3245 14 Apr 15 olle 512           }
3245 14 Apr 15 olle 513         }
3245 14 Apr 15 olle 514         else if (numExtraQiacubeItems != null)
3245 14 Apr 15 olle 515         {
3245 14 Apr 15 olle 516           for (int i = 1; i <= numExtraQiacubeItems; i++)
3245 14 Apr 15 olle 517           {
3245 14 Apr 15 olle 518             // Create extra item names as E01, E02, ... ,E09, E10, E11, ... 
3245 14 Apr 15 olle 519             String itemName = "E";
3245 14 Apr 15 olle 520             if (i < 10)
3245 14 Apr 15 olle 521             {
3245 14 Apr 15 olle 522               itemName += "0";
3245 14 Apr 15 olle 523             }
3245 14 Apr 15 olle 524             itemName += "" + i;
3245 14 Apr 15 olle 525             JSONObject jsonExtra = new JSONObject(); 
3245 14 Apr 15 olle 526             jsonExtra.put("name", itemName);
3245 14 Apr 15 olle 527             jsonExtraItems.add(jsonExtra);
3245 14 Apr 15 olle 528           }
3245 14 Apr 15 olle 529         }
3245 14 Apr 15 olle 530         
3180 17 Mar 15 olle 531         json.put("startplate", jsonStartPlate);
3180 17 Mar 15 olle 532         json.put("specimens", jsonSpecimen);
3180 17 Mar 15 olle 533         json.put("rnas", jsonRna);
3180 17 Mar 15 olle 534         json.put("dnas", jsonDna);
3212 27 Mar 15 olle 535         json.put("items", jsonItems);
3245 14 Apr 15 olle 536         json.put("extraitems", jsonExtraItems);
3180 17 Mar 15 olle 537       }      
3432 29 Jun 15 olle 538       else if ("GetStartItemLists".equals(cmd))
3432 29 Jun 15 olle 539       {
3432 29 Jun 15 olle 540         /*
3432 29 Jun 15 olle 541           Find start item lists. Order by item list name.
3432 29 Jun 15 olle 542         */
3432 29 Jun 15 olle 543         dc = sc.newDbControl();
3440 17 Jul 15 olle 544         Boolean processed = null;
3432 29 Jun 15 olle 545         Boolean orderDesc = Boolean.valueOf(req.getParameter("orderDesc"));
3644 03 Dec 15 olle 546         Boolean anItemNotOnLibPlate = Boolean.valueOf(req.getParameter("anItemNotOnLibPlate"));
3719 22 Jan 16 olle 547         Boolean ignoreQpcrBranch = Boolean.valueOf(req.getParameter("ignoreQpcrBranch"));
3432 29 Jun 15 olle 548
3719 22 Jan 16 olle 549         JSONArray jsonItemLists = fetchStartItemLists(dc, processed, orderDesc, anItemNotOnLibPlate, ignoreQpcrBranch);
3432 29 Jun 15 olle 550         
3432 29 Jun 15 olle 551         json.put("itemLists", jsonItemLists);
3432 29 Jun 15 olle 552       }      
3432 29 Jun 15 olle 553       else if ("GetUnprocessedStartItemLists".equals(cmd))
3432 29 Jun 15 olle 554       {
3432 29 Jun 15 olle 555         /*
3432 29 Jun 15 olle 556           Find unprocessed start item lists. Order by item list name.
3432 29 Jun 15 olle 557          */
3432 29 Jun 15 olle 558         dc = sc.newDbControl();
3440 17 Jul 15 olle 559         Boolean processed = false;
3432 29 Jun 15 olle 560         Boolean orderDesc = Boolean.valueOf(req.getParameter("orderDesc"));
3644 03 Dec 15 olle 561         Boolean anItemNotOnLibPlate = Boolean.valueOf(req.getParameter("anItemNotOnLibPlate"));
3719 22 Jan 16 olle 562         Boolean ignoreQpcrBranch = Boolean.valueOf(req.getParameter("ignoreQpcrBranch"));
3432 29 Jun 15 olle 563
3719 22 Jan 16 olle 564         JSONArray jsonItemLists = fetchStartItemLists(dc, processed, orderDesc, anItemNotOnLibPlate, ignoreQpcrBranch);
3432 29 Jun 15 olle 565       
3440 17 Jul 15 olle 566         json.put("itemLists", jsonItemLists);
3440 17 Jul 15 olle 567       }      
3440 17 Jul 15 olle 568       else if ("GetProcessedStartItemLists".equals(cmd))
3440 17 Jul 15 olle 569       {
3440 17 Jul 15 olle 570         /*
3440 17 Jul 15 olle 571           Find processed start item lists. Order by item list name.
3440 17 Jul 15 olle 572          */
3440 17 Jul 15 olle 573         dc = sc.newDbControl();
3440 17 Jul 15 olle 574         Boolean processed = true;
3440 17 Jul 15 olle 575         Boolean orderDesc = Boolean.valueOf(req.getParameter("orderDesc"));
3644 03 Dec 15 olle 576         Boolean anItemNotOnLibPlate = Boolean.valueOf(req.getParameter("anItemNotOnLibPlate"));
3719 22 Jan 16 olle 577         Boolean ignoreQpcrBranch = Boolean.valueOf(req.getParameter("ignoreQpcrBranch"));
3440 17 Jul 15 olle 578
3719 22 Jan 16 olle 579         JSONArray jsonItemLists = fetchStartItemLists(dc, processed, orderDesc, anItemNotOnLibPlate, ignoreQpcrBranch);
3432 29 Jun 15 olle 580       
3432 29 Jun 15 olle 581         json.put("itemLists", jsonItemLists);
3432 29 Jun 15 olle 582       }      
3432 29 Jun 15 olle 583       else if ("GetSourceItemListFromStartItemList".equals(cmd))
3432 29 Jun 15 olle 584       {
3432 29 Jun 15 olle 585         /*
3432 29 Jun 15 olle 586           Find extract source items from start item list members.
3432 29 Jun 15 olle 587         */
3432 29 Jun 15 olle 588         dc = sc.newDbControl();
3432 29 Jun 15 olle 589         Integer startItemListId = Values.getInteger(req.getParameter("startItemListId"), null);
3432 29 Jun 15 olle 590         Boolean includeSpecimenExtracts = Boolean.valueOf(req.getParameter("includeSpecimenExtracts"));
3432 29 Jun 15 olle 591         if (includeSpecimenExtracts == null)
3432 29 Jun 15 olle 592         {
3432 29 Jun 15 olle 593           includeSpecimenExtracts = false;
3432 29 Jun 15 olle 594         }
3432 29 Jun 15 olle 595
3432 29 Jun 15 olle 596         JSONObject jsonStartItemList = new JSONObject();
3432 29 Jun 15 olle 597         JSONArray jsonSpecimen = new JSONArray();
3432 29 Jun 15 olle 598         JSONArray jsonRna = new JSONArray();
3432 29 Jun 15 olle 599         JSONArray jsonDna = new JSONArray();
3432 29 Jun 15 olle 600         JSONArray jsonItems = new JSONArray();
3432 29 Jun 15 olle 601         JSONArray jsonExtraItems = new JSONArray();
3432 29 Jun 15 olle 602
3432 29 Jun 15 olle 603         // Use stored annotation snapshots for performance reasons
3432 29 Jun 15 olle 604         SnapshotManager manager = new SnapshotManager();
3432 29 Jun 15 olle 605         List<String> itemNameList = null;
3432 29 Jun 15 olle 606         if (startItemListId != null)
3432 29 Jun 15 olle 607         {
3432 29 Jun 15 olle 608           ItemList startItemList = ItemList.getById(dc, startItemListId);
3432 29 Jun 15 olle 609           if (startItemList != null)
3432 29 Jun 15 olle 610           {
3433 29 Jun 15 olle 611             StartList sl = new StartList(startItemList);
3433 29 Jun 15 olle 612             itemNameList = sl.fetchExtractSourceNameListFromItemList(dc);
3432 29 Jun 15 olle 613             jsonStartItemList.put("id", startItemList.getId());
3432 29 Jun 15 olle 614             jsonStartItemList.put("name", (String) startItemList.getName());
3432 29 Jun 15 olle 615             Date extractionDate = (Date) Annotationtype.LIST_EXTRACTION_DATE.getAnnotationValue(dc, startItemList);
3432 29 Jun 15 olle 616             jsonStartItemList.put("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(extractionDate));
3432 29 Jun 15 olle 617             // Get start item list extraction annotations
3432 29 Jun 15 olle 618             Date isolationDate = (Date) Annotationtype.LIST_ISOLATION_DATE.getAnnotationValue(dc, startItemList);
3501 21 Sep 15 olle 619             String extractionOperator = (String)Annotationtype.LIST_EXTRACTION_OPERATOR.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 620             Number extractionProtocolId = (Number) Annotationtype.LIST_EXTRACTION_PROTOCOL_ID.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 621             String extractionProtocolName = (String)Annotationtype.LIST_EXTRACTION_PROTOCOL_NAME.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 622             Float rnaDefaultVolume = (Float)Annotationtype.LIST_RNA_DEFAULT_VOLUME_IN_MICROLITRE.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 623             Float dnaDefaultVolume = (Float)Annotationtype.LIST_DNA_DEFAULT_VOLUME_IN_MICROLITRE.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 624             // Get start item list QIAcube annotations
3432 29 Jun 15 olle 625             Date qiacubeDate = (Date) Annotationtype.LIST_QIACUBE_DATE.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 626             String qiacubeOperator = (String)Annotationtype.LIST_QIACUBE_OPERATOR.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 627             String qiacubePrimaryPrefix = (String)Annotationtype.LIST_QIACUBE_PRIMARY_PREFIX.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 628             String qiacubeSecondaryPrefix = (String)Annotationtype.LIST_QIACUBE_SECONDARY_PREFIX.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 629             Integer qiacubeRunNo = (Integer)Annotationtype.LIST_QIACUBE_RUN_NO.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 630
3501 21 Sep 15 olle 631             // Specimen reagents
3432 29 Jun 15 olle 632             String allPrepFfpeKit = (String)Annotationtype.LIST_ALLPREP_FFPE_KIT.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 633             String xyleneSpecimen = (String)Annotationtype.LIST_XYLENE_SPECIMEN.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 634             String etoh995Specimen = (String)Annotationtype.LIST_ETOH995_SPECIMEN.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 635             String bufferPkdSpecimen = (String)Annotationtype.LIST_BUFFER_PKD_SPECIMEN.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 636             String bufferAtlSpecimen = (String)Annotationtype.LIST_BUFFER_ATL_SPECIMEN.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 637             String proteinaseKSpecimen = (String)Annotationtype.LIST_PROTEINASE_K_SPECIMEN.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 638             String rnaseASpecimen = (String)Annotationtype.LIST_RNASE_A_SPECIMEN.getAnnotationValue(dc, manager, startItemList);
3432 29 Jun 15 olle 639
3501 21 Sep 15 olle 640             // QIAcube reagents
3501 21 Sep 15 olle 641             String etoh995 = (String)Annotationtype.LIST_ETOH995.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 642             String bufferRlt = (String)Annotationtype.LIST_BUFFER_RLT.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 643             String bufferFrn = (String)Annotationtype.LIST_BUFFER_FRN.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 644             String bufferRpe = (String)Annotationtype.LIST_BUFFER_RPE.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 645             String bufferAl = (String)Annotationtype.LIST_BUFFER_AL.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 646             String bufferAw1 = (String)Annotationtype.LIST_BUFFER_AW1.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 647             String bufferAw2 = (String)Annotationtype.LIST_BUFFER_AW2.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 648             String bufferAte = (String)Annotationtype.LIST_BUFFER_ATE.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 649             String bufferRdd = (String)Annotationtype.LIST_BUFFER_RDD.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 650             String rnaseFreeWater = (String)Annotationtype.LIST_RNASE_FREE_WATER.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 651             String dnaseMix = (String)Annotationtype.LIST_DNASE_MIX.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 652             String rneasyMinEluteSpinColumn = (String)Annotationtype.LIST_RNEASY_MIN_ELUTE_SPIN_COL.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 653             String qiaampMinEluteSpinColumn = (String)Annotationtype.LIST_QIAAMP_MIN_ELUTE_SPIN_COL.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 654
3501 21 Sep 15 olle 655             // Quality control reagents
3501 21 Sep 15 olle 656             String kapaSybrFast = (String)Annotationtype.LIST_KAPA_SYBR_FAST.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 657             String qualityControlPrimersQcpRgt = (String)Annotationtype.LIST_QUALITY_CONTROL_PRIMERS_QCP_RGT.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 658             String qualityControlTemplateQct = (String)Annotationtype.LIST_QUALITY_CONTROL_TEMPLATE_QCT.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 659             String qubitDnaHighSens = (String)Annotationtype.LIST_QUBIT_DNA_HIGH_SENS.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 660             String qubitDnaBroadRange = (String)Annotationtype.LIST_QUBIT_DNA_BROAD_RANGE.getAnnotationValue(dc, manager, startItemList);
3501 21 Sep 15 olle 661
3432 29 Jun 15 olle 662             jsonStartItemList.put("isolationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(isolationDate));
3501 21 Sep 15 olle 663             jsonStartItemList.put("extractionOperator", extractionOperator);
3432 29 Jun 15 olle 664             jsonStartItemList.put("extractionProtocolId", extractionProtocolId);
3432 29 Jun 15 olle 665             jsonStartItemList.put("extractionProtocolName", extractionProtocolName);
3432 29 Jun 15 olle 666             jsonStartItemList.put("rnaDefaultVolume", rnaDefaultVolume);
3432 29 Jun 15 olle 667             jsonStartItemList.put("dnaDefaultVolume", dnaDefaultVolume);
3432 29 Jun 15 olle 668             jsonStartItemList.put("qiacubeDate", Meludi.CONVERTER_DATE_TO_STRING.convert(qiacubeDate));
3501 21 Sep 15 olle 669             jsonStartItemList.put("qiacubeOperator", qiacubeOperator);
3432 29 Jun 15 olle 670             jsonStartItemList.put("qiacubePrimaryPrefix", qiacubePrimaryPrefix);
3432 29 Jun 15 olle 671             jsonStartItemList.put("qiacubeSecondaryPrefix", qiacubeSecondaryPrefix);
3432 29 Jun 15 olle 672             jsonStartItemList.put("qiacubeRunNo", qiacubeRunNo);
3501 21 Sep 15 olle 673             // Specimen reagents
3432 29 Jun 15 olle 674             jsonStartItemList.put("allPrepFfpeKit", allPrepFfpeKit);
3501 21 Sep 15 olle 675             jsonStartItemList.put("xyleneSpecimen", xyleneSpecimen);
3501 21 Sep 15 olle 676             jsonStartItemList.put("etoh995Specimen", etoh995Specimen);
3501 21 Sep 15 olle 677             jsonStartItemList.put("bufferPkdSpecimen", bufferPkdSpecimen);
3501 21 Sep 15 olle 678             jsonStartItemList.put("bufferAtlSpecimen", bufferAtlSpecimen);
3501 21 Sep 15 olle 679             jsonStartItemList.put("proteinaseKSpecimen", proteinaseKSpecimen);
3501 21 Sep 15 olle 680             jsonStartItemList.put("rnaseASpecimen", rnaseASpecimen);
3501 21 Sep 15 olle 681             // QIAcube reagents
3501 21 Sep 15 olle 682             jsonStartItemList.put("etoh995", etoh995);
3501 21 Sep 15 olle 683             jsonStartItemList.put("bufferRlt", bufferRlt);
3501 21 Sep 15 olle 684             jsonStartItemList.put("bufferFrn", bufferFrn);
3501 21 Sep 15 olle 685             jsonStartItemList.put("bufferRpe", bufferRpe);
3501 21 Sep 15 olle 686             jsonStartItemList.put("bufferAl", bufferAl);
3501 21 Sep 15 olle 687             jsonStartItemList.put("bufferAw1", bufferAw1);
3501 21 Sep 15 olle 688             jsonStartItemList.put("bufferAw2", bufferAw2);
3501 21 Sep 15 olle 689             jsonStartItemList.put("bufferAte", bufferAte);
3501 21 Sep 15 olle 690             jsonStartItemList.put("bufferRdd", bufferRdd);
3501 21 Sep 15 olle 691             jsonStartItemList.put("rnaseFreeWater", rnaseFreeWater);
3501 21 Sep 15 olle 692             jsonStartItemList.put("dnaseMix", dnaseMix);
3501 21 Sep 15 olle 693             jsonStartItemList.put("rneasyMinEluteSpinColumn", rneasyMinEluteSpinColumn);
3501 21 Sep 15 olle 694             jsonStartItemList.put("qiaampMinEluteSpinColumn", qiaampMinEluteSpinColumn);
3501 21 Sep 15 olle 695             // Quality control reagents
3501 21 Sep 15 olle 696             jsonStartItemList.put("kapaSybrFast", kapaSybrFast);
3501 21 Sep 15 olle 697             jsonStartItemList.put("qualityControlPrimersQcpRgt", qualityControlPrimersQcpRgt);
3501 21 Sep 15 olle 698             jsonStartItemList.put("qualityControlTemplateQct", qualityControlTemplateQct);
3501 21 Sep 15 olle 699             jsonStartItemList.put("qubitDnaHighSens", qubitDnaHighSens);
3501 21 Sep 15 olle 700             jsonStartItemList.put("qubitDnaBroadRange", qubitDnaBroadRange);
3432 29 Jun 15 olle 701           }
3432 29 Jun 15 olle 702         }
3432 29 Jun 15 olle 703         
3432 29 Jun 15 olle 704         // Convert item names to items
3432 29 Jun 15 olle 705         if (itemNameList != null)
3432 29 Jun 15 olle 706         {
3432 29 Jun 15 olle 707           for (String itemName : itemNameList)
3432 29 Jun 15 olle 708           {
3433 29 Jun 15 olle 709             if (Meludi.itemIsSpecimen(itemName))
3432 29 Jun 15 olle 710             {
3432 29 Jun 15 olle 711               SpecimenTube s = SpecimenTube.findByTubeName(dc, itemName);
5823 04 Feb 20 olle 712               if (s != null)
3432 29 Jun 15 olle 713               {
5823 04 Feb 20 olle 714                 s.loadBioPlateLocation();
5823 04 Feb 20 olle 715                 loadSpecimenInfo(dc, manager, s);
5823 04 Feb 20 olle 716                 jsonSpecimen.add(s.asJSONObject());
5823 04 Feb 20 olle 717                 JSONObject jsonSpec = s.asJSONObject();
5823 04 Feb 20 olle 718                 //jsonItems.add(s.asJSONObject());
5823 04 Feb 20 olle 719                 // Loading specimen extract data might decrease performance, only use when needed
5823 04 Feb 20 olle 720                 if (includeSpecimenExtracts)
3432 29 Jun 15 olle 721                 {
5823 04 Feb 20 olle 722                   String specQiacubePosition = null;
5823 04 Feb 20 olle 723                   // Load optional data for Rna for specimen
5823 04 Feb 20 olle 724                   String rnaName = itemName + ".r";
5823 04 Feb 20 olle 725                   Rna r = Rna.findByName(dc, rnaName);
5823 04 Feb 20 olle 726                   if (r != null)
5823 04 Feb 20 olle 727                   {
5823 04 Feb 20 olle 728                     r.loadBioPlateLocation();
5823 04 Feb 20 olle 729                     loadRnaInfo(dc, manager, r);
5823 04 Feb 20 olle 730                     jsonRna.add(r.asJSONObject());
5823 04 Feb 20 olle 731                     jsonSpec.put("rna", r.asJSONObject());
5823 04 Feb 20 olle 732                   }
5823 04 Feb 20 olle 733                   
5823 04 Feb 20 olle 734                   // Load optional data for Dna for specimen
5823 04 Feb 20 olle 735                   String dnaName = itemName + ".d";
5823 04 Feb 20 olle 736                   Dna d = Dna.findByName(dc, dnaName);
5823 04 Feb 20 olle 737                   if (d != null)
5823 04 Feb 20 olle 738                   {
5823 04 Feb 20 olle 739                     d.loadBioPlateLocation();
5823 04 Feb 20 olle 740                     loadDnaInfo(dc, manager, d);
5823 04 Feb 20 olle 741                     jsonDna.add(d.asJSONObject());
5823 04 Feb 20 olle 742                     jsonSpec.put("dna", d.asJSONObject());
5823 04 Feb 20 olle 743                   }
3432 29 Jun 15 olle 744                 }
5823 04 Feb 20 olle 745                 jsonItems.add(jsonSpec);                
3432 29 Jun 15 olle 746               }
5823 04 Feb 20 olle 747               else
5823 04 Feb 20 olle 748               {
5823 04 Feb 20 olle 749                 System.out.println(new Date() + " ExtractionServlet::doGet(): cmd = \"" + cmd + "\" itemName = \"" + itemName + "\" SpecimenTube s = null.");
5823 04 Feb 20 olle 750               }
3432 29 Jun 15 olle 751             }
3433 29 Jun 15 olle 752             else if (Meludi.itemIsRna(itemName))
3432 29 Jun 15 olle 753             {
3432 29 Jun 15 olle 754               Rna r = Rna.findByName(dc, itemName);
3432 29 Jun 15 olle 755               r.loadBioPlateLocation();
3432 29 Jun 15 olle 756               loadRnaInfo(dc, manager, r);
3432 29 Jun 15 olle 757               jsonRna.add(r.asJSONObject());
3432 29 Jun 15 olle 758               JSONObject jsonR = r.asJSONObject();
3432 29 Jun 15 olle 759               jsonItems.add(jsonR);
3432 29 Jun 15 olle 760             }
3433 29 Jun 15 olle 761             else if (Meludi.itemIsDna(itemName))
3432 29 Jun 15 olle 762             {
3432 29 Jun 15 olle 763               Dna d = Dna.findByName(dc, itemName);
3432 29 Jun 15 olle 764               d.loadBioPlateLocation();
3432 29 Jun 15 olle 765               loadDnaInfo(dc, manager, d);
3432 29 Jun 15 olle 766               jsonDna.add(d.asJSONObject());
3432 29 Jun 15 olle 767               JSONObject jsonD = d.asJSONObject();
3432 29 Jun 15 olle 768               jsonItems.add(jsonD);
3432 29 Jun 15 olle 769             }
3432 29 Jun 15 olle 770           }
3432 29 Jun 15 olle 771         }
3432 29 Jun 15 olle 772         
3432 29 Jun 15 olle 773         json.put("startitemlist", jsonStartItemList);
3432 29 Jun 15 olle 774         json.put("specimens", jsonSpecimen);
3432 29 Jun 15 olle 775         json.put("rnas", jsonRna);
3432 29 Jun 15 olle 776         json.put("dnas", jsonDna);
3432 29 Jun 15 olle 777         json.put("items", jsonItems);
3432 29 Jun 15 olle 778       }      
3440 17 Jul 15 olle 779       else if ("GetListOfItemsFromStartItemLists".equals(cmd))
3440 17 Jul 15 olle 780       {
3440 17 Jul 15 olle 781         /*
3440 17 Jul 15 olle 782           Find items from members of start item lists.
3440 17 Jul 15 olle 783         */
3440 17 Jul 15 olle 784         dc = sc.newDbControl();
3440 17 Jul 15 olle 785         List<Integer> startItemListIds = new ArrayList<Integer>();
3440 17 Jul 15 olle 786         String startListInfoStr = req.getParameter("startListIds");
3440 17 Jul 15 olle 787         JSONArray jsonStartItemListIds = (JSONArray)new JSONParser().parse(startListInfoStr);
3644 03 Dec 15 olle 788         Boolean itemNotOnLibPlate = Boolean.valueOf(req.getParameter("itemNotOnLibPlate"));
3644 03 Dec 15 olle 789         if (itemNotOnLibPlate == null)
3644 03 Dec 15 olle 790         {
3644 03 Dec 15 olle 791           itemNotOnLibPlate = false;
3644 03 Dec 15 olle 792         }
3719 22 Jan 16 olle 793         Boolean ignoreQpcrBranch = Boolean.valueOf(req.getParameter("ignoreQpcrBranch"));
3719 22 Jan 16 olle 794         if (ignoreQpcrBranch == null)
3719 22 Jan 16 olle 795         {
3719 22 Jan 16 olle 796           ignoreQpcrBranch = false;
3719 22 Jan 16 olle 797         }
3440 17 Jul 15 olle 798         int numSpecimen = 0;
3440 17 Jul 15 olle 799         int numRna = 0;
3440 17 Jul 15 olle 800         int numDna = 0;
3440 17 Jul 15 olle 801         for (int i = 0; i < jsonStartItemListIds.size(); i++)
3440 17 Jul 15 olle 802         {
3440 17 Jul 15 olle 803           JSONObject jsonStartItemListId = (JSONObject)jsonStartItemListIds.get(i);
3440 17 Jul 15 olle 804           String startItemListIdStr = (String)jsonStartItemListId.get("id");
3440 17 Jul 15 olle 805           Integer startItemListId = Values.getInteger(startItemListIdStr, null);
3440 17 Jul 15 olle 806           if (startItemListId != null)
3440 17 Jul 15 olle 807           {
3440 17 Jul 15 olle 808             startItemListIds.add(startItemListId);
3440 17 Jul 15 olle 809           }
3440 17 Jul 15 olle 810         }
3448 28 Jul 15 olle 811 //System.out.println(new Date() + " ExtractionServlet::doGet(): cmd = \"" + cmd + "\" startItemListIds = " + startItemListIds);
3440 17 Jul 15 olle 812         String itemType = req.getParameter("itemType");
3440 17 Jul 15 olle 813         //String itemType = (String)jsonReq.get("itemType");
3446 27 Jul 15 olle 814 //System.out.println(new Date() + " ExtractionServlet::doGet(): cmd = \"" + cmd + "\" itemType = " + itemType);
3440 17 Jul 15 olle 815         if (itemType == null || !(itemType.equals("Specimen") || itemType.equals("DNA") || itemType.equals("RNA")))
3440 17 Jul 15 olle 816         {
3440 17 Jul 15 olle 817           itemType = "DNA";
3440 17 Jul 15 olle 818         }
3440 17 Jul 15 olle 819         Boolean includeSpecimenExtracts = Boolean.valueOf(req.getParameter("includeSpecimenExtracts"));
3440 17 Jul 15 olle 820         if (includeSpecimenExtracts == null)
3440 17 Jul 15 olle 821         {
3440 17 Jul 15 olle 822           includeSpecimenExtracts = false;
3440 17 Jul 15 olle 823         }
3440 17 Jul 15 olle 824
3440 17 Jul 15 olle 825         JSONObject jsonStartItemList = new JSONObject();
3440 17 Jul 15 olle 826         JSONArray jsonSpecimen = new JSONArray();
3440 17 Jul 15 olle 827         JSONArray jsonRna = new JSONArray();
3440 17 Jul 15 olle 828         JSONArray jsonDna = new JSONArray();
3440 17 Jul 15 olle 829         JSONArray jsonItems = new JSONArray();
3440 17 Jul 15 olle 830         JSONArray jsonExtraItems = new JSONArray();
3440 17 Jul 15 olle 831
3440 17 Jul 15 olle 832         // Use stored annotation snapshots for performance reasons
3440 17 Jul 15 olle 833         SnapshotManager manager = new SnapshotManager();
3440 17 Jul 15 olle 834         // Store item names from start lists in lists of MeLuDI-items and extra items
4150 03 Oct 16 olle 835         int activeProjectId = sc.getActiveProjectId();
3440 17 Jul 15 olle 836         List<String> meludiItemNameList = new ArrayList<String>();
3440 17 Jul 15 olle 837         List<String> extraItemNameList = new ArrayList<String>();
3440 17 Jul 15 olle 838         for (Integer startItemListId: startItemListIds)
3440 17 Jul 15 olle 839         {
3440 17 Jul 15 olle 840           List<String> tmpItemNameList = null;
3440 17 Jul 15 olle 841           ItemList startItemList = ItemList.getById(dc, startItemListId);
3440 17 Jul 15 olle 842           if (startItemList != null)
3440 17 Jul 15 olle 843           {
3440 17 Jul 15 olle 844             StartList sl = new StartList(startItemList);
3440 17 Jul 15 olle 845             tmpItemNameList = sl.fetchItemNameListFromItemList(dc, itemType);
3440 17 Jul 15 olle 846             if (tmpItemNameList != null)
3440 17 Jul 15 olle 847             {
3440 17 Jul 15 olle 848               for (String itemName: tmpItemNameList)
3440 17 Jul 15 olle 849               {
4150 03 Oct 16 olle 850                 if (Meludi.itemIsMeludiItem(itemName, activeProjectId))
3440 17 Jul 15 olle 851                 {
3440 17 Jul 15 olle 852                   meludiItemNameList.add(itemName);
3440 17 Jul 15 olle 853                 }
3440 17 Jul 15 olle 854                 else
3440 17 Jul 15 olle 855                 {
3440 17 Jul 15 olle 856                   extraItemNameList.add(itemName);
3440 17 Jul 15 olle 857                 }
3440 17 Jul 15 olle 858               }
3440 17 Jul 15 olle 859             }
3440 17 Jul 15 olle 860             jsonStartItemList.put("id", startItemList.getId());
3440 17 Jul 15 olle 861             jsonStartItemList.put("name", (String) startItemList.getName());
3440 17 Jul 15 olle 862           }
3440 17 Jul 15 olle 863         }
3462 31 Jul 15 olle 864         // Sort each item name list
3462 31 Jul 15 olle 865         Collections.sort(meludiItemNameList);
3462 31 Jul 15 olle 866         Collections.sort(extraItemNameList);
3462 31 Jul 15 olle 867         
3440 17 Jul 15 olle 868         // Store item names from start lists in list with MeLuDI-items before extra items
3440 17 Jul 15 olle 869         List<String> itemNameList = new ArrayList<String>();
3440 17 Jul 15 olle 870         for (String itemName: meludiItemNameList)
3440 17 Jul 15 olle 871         {
3440 17 Jul 15 olle 872           itemNameList.add(itemName);
3440 17 Jul 15 olle 873         }
3440 17 Jul 15 olle 874         for (String itemName: extraItemNameList)
3440 17 Jul 15 olle 875         {
3440 17 Jul 15 olle 876           itemNameList.add(itemName);
3440 17 Jul 15 olle 877         }
3440 17 Jul 15 olle 878         // Convert item names to items
3440 17 Jul 15 olle 879         if (itemNameList != null)
3440 17 Jul 15 olle 880         {
3440 17 Jul 15 olle 881           for (String itemName : itemNameList)
3440 17 Jul 15 olle 882           {
3440 17 Jul 15 olle 883             if (Meludi.itemIsSpecimen(itemName))
3440 17 Jul 15 olle 884             {
3440 17 Jul 15 olle 885               SpecimenTube s = SpecimenTube.findByTubeName(dc, itemName);
5823 04 Feb 20 olle 886               if (s != null)
3440 17 Jul 15 olle 887               {
5823 04 Feb 20 olle 888                 s.loadBioPlateLocation();
5823 04 Feb 20 olle 889                 loadSpecimenInfo(dc, manager, s);
5823 04 Feb 20 olle 890                 jsonSpecimen.add(s.asJSONObject());
5823 04 Feb 20 olle 891                 JSONObject jsonSpec = s.asJSONObject();
5823 04 Feb 20 olle 892                 //jsonItems.add(s.asJSONObject());
5823 04 Feb 20 olle 893                 // Loading specimen extract data might decrease performance, only use when needed
5823 04 Feb 20 olle 894                 if (includeSpecimenExtracts)
3440 17 Jul 15 olle 895                 {
5823 04 Feb 20 olle 896                   String specQiacubePosition = null;
5823 04 Feb 20 olle 897                   // Load optional data for Rna for specimen
5823 04 Feb 20 olle 898                   String rnaName = itemName + ".r";
5823 04 Feb 20 olle 899                   Rna r = Rna.findByName(dc, rnaName);
5823 04 Feb 20 olle 900                   if (r != null)
3644 03 Dec 15 olle 901                   {
5823 04 Feb 20 olle 902                     r.loadBioPlateLocation();
5823 04 Feb 20 olle 903                     loadRnaInfo(dc, manager, r);
5823 04 Feb 20 olle 904                     jsonRna.add(r.asJSONObject());
5823 04 Feb 20 olle 905                     jsonSpec.put("rna", r.asJSONObject());
5823 04 Feb 20 olle 906                   }
5823 04 Feb 20 olle 907                   
5823 04 Feb 20 olle 908                   // Load optional data for Dna for specimen
5823 04 Feb 20 olle 909                   String dnaName = itemName + ".d";
5823 04 Feb 20 olle 910                   Dna d = Dna.findByName(dc, dnaName);
5823 04 Feb 20 olle 911                   if (d != null)
5823 04 Feb 20 olle 912                   {
5823 04 Feb 20 olle 913                     boolean includeDna = true;
5823 04 Feb 20 olle 914                     if (itemNotOnLibPlate)
3719 22 Jan 16 olle 915                     {
5823 04 Feb 20 olle 916                       // Check if DNA extract has FPA child extracts
5823 04 Feb 20 olle 917                       includeDna = dnaHasNoFpaChild(dc, d.getExtract());
5823 04 Feb 20 olle 918                       // Check if DNA extracts marked for qPCR processing should be ignored
5823 04 Feb 20 olle 919                       if (includeDna && ignoreQpcrBranch)
3719 22 Jan 16 olle 920                       {
5823 04 Feb 20 olle 921                         // Ignore DNA extracts marked for qPCR processing
5823 04 Feb 20 olle 922                         Boolean markedForQPcr = (Boolean) Annotationtype.QPCR_BRANCH.getAnnotationValue(dc, d.getExtract());
5823 04 Feb 20 olle 923                         if (markedForQPcr != null && markedForQPcr)
5823 04 Feb 20 olle 924                         {
5823 04 Feb 20 olle 925                           includeDna = false;
5823 04 Feb 20 olle 926                         }
3719 22 Jan 16 olle 927                       }
3719 22 Jan 16 olle 928                     }
5823 04 Feb 20 olle 929                     if (includeDna)
5823 04 Feb 20 olle 930                     {
5823 04 Feb 20 olle 931                       d.loadBioPlateLocation();
5823 04 Feb 20 olle 932                       loadDnaInfo(dc, manager, d);
5823 04 Feb 20 olle 933                       jsonDna.add(d.asJSONObject());
5823 04 Feb 20 olle 934                       jsonSpec.put("dna", d.asJSONObject());
5823 04 Feb 20 olle 935                     }
3644 03 Dec 15 olle 936                   }
3440 17 Jul 15 olle 937                 }
5823 04 Feb 20 olle 938                 jsonItems.add(jsonSpec);                
3440 17 Jul 15 olle 939               }
5823 04 Feb 20 olle 940               else
5823 04 Feb 20 olle 941               {
5823 04 Feb 20 olle 942                 System.out.println(new Date() + " ExtractionServlet::doGet(): cmd = \"" + cmd + "\" itemName = \"" + itemName + "\" SpecimenTube s = null.");
5823 04 Feb 20 olle 943               }
3440 17 Jul 15 olle 944             }
3440 17 Jul 15 olle 945             else if (Meludi.itemIsRna(itemName))
3440 17 Jul 15 olle 946             {
3440 17 Jul 15 olle 947               Rna r = Rna.findByName(dc, itemName);
3440 17 Jul 15 olle 948               r.loadBioPlateLocation();
3440 17 Jul 15 olle 949               loadRnaInfo(dc, manager, r);
3440 17 Jul 15 olle 950               jsonRna.add(r.asJSONObject());
3440 17 Jul 15 olle 951               JSONObject jsonR = r.asJSONObject();
3440 17 Jul 15 olle 952               jsonItems.add(jsonR);
3440 17 Jul 15 olle 953             }
3440 17 Jul 15 olle 954             else if (Meludi.itemIsDna(itemName))
3440 17 Jul 15 olle 955             {
3440 17 Jul 15 olle 956               Dna d = Dna.findByName(dc, itemName);
3644 03 Dec 15 olle 957               boolean includeDna = true;
3644 03 Dec 15 olle 958               if (itemNotOnLibPlate)
3644 03 Dec 15 olle 959               {
3644 03 Dec 15 olle 960                 // Check if DNA extract has FPA child extracts
3644 03 Dec 15 olle 961                 includeDna = dnaHasNoFpaChild(dc, d.getExtract());
3719 22 Jan 16 olle 962                 // Check if DNA extracts marked for qPCR processing should be ignored
3719 22 Jan 16 olle 963                 if (includeDna && ignoreQpcrBranch)
3719 22 Jan 16 olle 964                 {
3719 22 Jan 16 olle 965                   // Ignore DNA extracts marked for qPCR processing
3719 22 Jan 16 olle 966                   Boolean markedForQPcr = (Boolean) Annotationtype.QPCR_BRANCH.getAnnotationValue(dc, d.getExtract());
3719 22 Jan 16 olle 967                   if (markedForQPcr != null && markedForQPcr)
3719 22 Jan 16 olle 968                   {
3719 22 Jan 16 olle 969                     includeDna = false;
3719 22 Jan 16 olle 970                   }
3719 22 Jan 16 olle 971                 }
3644 03 Dec 15 olle 972               }
3644 03 Dec 15 olle 973               if (includeDna)
3644 03 Dec 15 olle 974               {
3644 03 Dec 15 olle 975                 d.loadBioPlateLocation();
3644 03 Dec 15 olle 976                 loadDnaInfo(dc, manager, d);
3644 03 Dec 15 olle 977                 jsonDna.add(d.asJSONObject());
3644 03 Dec 15 olle 978                 JSONObject jsonD = d.asJSONObject();
3644 03 Dec 15 olle 979                 jsonItems.add(jsonD);
3644 03 Dec 15 olle 980               }
3440 17 Jul 15 olle 981             }
3440 17 Jul 15 olle 982           }
3440 17 Jul 15 olle 983         }
3446 27 Jul 15 olle 984
3446 27 Jul 15 olle 985 /*
3440 17 Jul 15 olle 986         json.put("startitemlist", jsonStartItemList);
3440 17 Jul 15 olle 987         json.put("specimens", jsonSpecimen);
3440 17 Jul 15 olle 988         json.put("rnas", jsonRna);
3440 17 Jul 15 olle 989         json.put("dnas", jsonDna);
3446 27 Jul 15 olle 990 */
3446 27 Jul 15 olle 991         json.put("startListIds", jsonStartItemListIds);
3440 17 Jul 15 olle 992         json.put("items", jsonItems);
3440 17 Jul 15 olle 993       }      
5162 03 Dec 18 olle 994       else if ("GetCases".equals(cmd))
5162 03 Dec 18 olle 995       {
5162 03 Dec 18 olle 996         /*
5162 03 Dec 18 olle 997           All cases
5162 03 Dec 18 olle 998         */
5162 03 Dec 18 olle 999         dc = sc.newDbControl();
5162 03 Dec 18 olle 1000
5162 03 Dec 18 olle 1001         JSONArray jsonCases = new JSONArray();
5162 03 Dec 18 olle 1002
5162 03 Dec 18 olle 1003         ItemQuery<Sample> query = Sample.getQuery();
5162 03 Dec 18 olle 1004         Subtype.CASE.addFilter(dc, query);
5162 03 Dec 18 olle 1005         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5162 03 Dec 18 olle 1006         query.order(Orders.desc(Hql.property("name")));
5162 03 Dec 18 olle 1007         query.setCacheResult(true);
5162 03 Dec 18 olle 1008         List<Sample> cases = query.list(dc);
5162 03 Dec 18 olle 1009         for (int i = 0; i < cases.size(); i++)
5162 03 Dec 18 olle 1010         {
5162 03 Dec 18 olle 1011           Sample caseSample = (Sample) cases.get(i);
5162 03 Dec 18 olle 1012           Case theCase = Case.findByName(dc, caseSample.getName());
5162 03 Dec 18 olle 1013           String personalNumber = "";
5162 03 Dec 18 olle 1014           Patient patient = Patient.findByCase(dc, theCase);
5162 03 Dec 18 olle 1015           if (patient != null)
5162 03 Dec 18 olle 1016           {
5162 03 Dec 18 olle 1017             try
5162 03 Dec 18 olle 1018             {
5162 03 Dec 18 olle 1019               patient.loadDefaultAnnotations(dc);
5162 03 Dec 18 olle 1020               personalNumber = (String) Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient.getBioSource());
5162 03 Dec 18 olle 1021             }
5162 03 Dec 18 olle 1022             catch (Exception e)
5162 03 Dec 18 olle 1023             {}
5162 03 Dec 18 olle 1024           }
5162 03 Dec 18 olle 1025           String referralId = "";
5162 03 Dec 18 olle 1026           try
5162 03 Dec 18 olle 1027           {
5162 03 Dec 18 olle 1028             theCase.loadAnnotations(dc, "referralId", Annotationtype.REFERRAL_ID, null);
5162 03 Dec 18 olle 1029             referralId = (String) Annotationtype.REFERRAL_ID.getAnnotationValue(dc, caseSample);
5162 03 Dec 18 olle 1030           }
5162 03 Dec 18 olle 1031           catch (Exception e)
5162 03 Dec 18 olle 1032           {}
5325 05 Mar 19 olle 1033           try
5325 05 Mar 19 olle 1034           {
5325 05 Mar 19 olle 1035             theCase.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(caseSample.getEntryDate()));
5325 05 Mar 19 olle 1036           }
5325 05 Mar 19 olle 1037           catch (Exception e)
5325 05 Mar 19 olle 1038           {}
5325 05 Mar 19 olle 1039           if (theCase != null)
5325 05 Mar 19 olle 1040           {
5325 05 Mar 19 olle 1041             JSONObject jsonCase = new JSONObject();
5325 05 Mar 19 olle 1042             jsonCase = theCase.asJSONObject();
5325 05 Mar 19 olle 1043             jsonCase.put("personalNumber", personalNumber);
5325 05 Mar 19 olle 1044             jsonCases.add(jsonCase);            
5325 05 Mar 19 olle 1045           }
5162 03 Dec 18 olle 1046         }
5162 03 Dec 18 olle 1047
5162 03 Dec 18 olle 1048         json.put("items", jsonCases);
5162 03 Dec 18 olle 1049       }      
5158 30 Nov 18 olle 1050       else if ("GetCasesUnusedForFfpeBlockOrderForm".equals(cmd))
5158 30 Nov 18 olle 1051       {
5158 30 Nov 18 olle 1052         /*
5158 30 Nov 18 olle 1053           All cases with consent not used for FFPE block order forms
5158 30 Nov 18 olle 1054         */
5158 30 Nov 18 olle 1055         dc = sc.newDbControl();
5158 30 Nov 18 olle 1056
5158 30 Nov 18 olle 1057         JSONArray jsonCases = new JSONArray();
5158 30 Nov 18 olle 1058
5158 30 Nov 18 olle 1059         ItemQuery<Sample> query = Sample.getQuery();
5158 30 Nov 18 olle 1060         Subtype.CASE.addFilter(dc, query);
5158 30 Nov 18 olle 1061         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5158 30 Nov 18 olle 1062         query.setCacheResult(true);
5158 30 Nov 18 olle 1063         List<Sample> cases = query.list(dc);
5158 30 Nov 18 olle 1064         // Case items unused for FFPE block order form
5158 30 Nov 18 olle 1065         for (int i = 0; i < cases.size(); i++)
5158 30 Nov 18 olle 1066         {
5158 30 Nov 18 olle 1067           Sample caseSample = (Sample) cases.get(i);
5158 30 Nov 18 olle 1068           Case theCase = Case.findByName(dc, caseSample.getName());
5158 30 Nov 18 olle 1069           Date consentDate = null;
5158 30 Nov 18 olle 1070           if (theCase != null)
5158 30 Nov 18 olle 1071           {
5158 30 Nov 18 olle 1072             theCase.loadAnnotations(dc, "consentDate", Annotationtype.CONSENT_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5158 30 Nov 18 olle 1073             consentDate = (Date) Annotationtype.CONSENT_DATE.getAnnotationValue(dc, theCase.getSample());        
5158 30 Nov 18 olle 1074           }
5158 30 Nov 18 olle 1075           if (consentDate != null)
5158 30 Nov 18 olle 1076           {
5243 17 Jan 19 olle 1077             Date ffpeBlockOrderDate = (Date) Annotationtype.FFPE_BLOCK_ORDER_DATE.getAnnotationValue(dc, caseSample);
5243 17 Jan 19 olle 1078             if (ffpeBlockOrderDate == null)
5158 30 Nov 18 olle 1079             {
5158 30 Nov 18 olle 1080               String personalNumber = "";
5158 30 Nov 18 olle 1081               Patient patient = Patient.findByCase(dc, theCase);
5158 30 Nov 18 olle 1082               if (patient != null)
5158 30 Nov 18 olle 1083               {
5158 30 Nov 18 olle 1084                 patient.loadDefaultAnnotations(dc);
5158 30 Nov 18 olle 1085                 personalNumber = (String) Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient.getBioSource());
5158 30 Nov 18 olle 1086               }
5158 30 Nov 18 olle 1087               theCase.loadAnnotations(dc, "referralId", Annotationtype.REFERRAL_ID, null);
5158 30 Nov 18 olle 1088               String referralId = (String) Annotationtype.REFERRAL_ID.getAnnotationValue(dc, caseSample);
5158 30 Nov 18 olle 1089               if (referralId != null && !referralId.equals(""))
5158 30 Nov 18 olle 1090               {
5158 30 Nov 18 olle 1091                 JSONObject jsonCase = new JSONObject();
5158 30 Nov 18 olle 1092                 jsonCase = theCase.asJSONObject();
5158 30 Nov 18 olle 1093                 jsonCase.put("personalNumber", personalNumber);
5158 30 Nov 18 olle 1094                 jsonCases.add(jsonCase);
5158 30 Nov 18 olle 1095               }
5158 30 Nov 18 olle 1096             }
5158 30 Nov 18 olle 1097           }
5158 30 Nov 18 olle 1098         }
5158 30 Nov 18 olle 1099
5158 30 Nov 18 olle 1100         json.put("items", jsonCases);
5158 30 Nov 18 olle 1101       }      
5237 16 Jan 19 olle 1102       else if ("GetCasesWithUnprocessedFfpeBlockOrders".equals(cmd))
5237 16 Jan 19 olle 1103       {
5237 16 Jan 19 olle 1104         /*
5237 16 Jan 19 olle 1105           All cases with consent used for FFPE block order forms
5237 16 Jan 19 olle 1106         */
5237 16 Jan 19 olle 1107         dc = sc.newDbControl();
5237 16 Jan 19 olle 1108
5237 16 Jan 19 olle 1109         JSONArray jsonCases = new JSONArray();
5237 16 Jan 19 olle 1110
5237 16 Jan 19 olle 1111         ItemQuery<Sample> query = Sample.getQuery();
5237 16 Jan 19 olle 1112         Subtype.CASE.addFilter(dc, query);
5237 16 Jan 19 olle 1113         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5237 16 Jan 19 olle 1114         query.setCacheResult(true);
5237 16 Jan 19 olle 1115         List<Sample> cases = query.list(dc);
5237 16 Jan 19 olle 1116         // Case items unused for FFPE block order form
5237 16 Jan 19 olle 1117         for (int i = 0; i < cases.size(); i++)
5237 16 Jan 19 olle 1118         {
5237 16 Jan 19 olle 1119           Sample caseSample = (Sample) cases.get(i);
5237 16 Jan 19 olle 1120           Case theCase = Case.findByName(dc, caseSample.getName());
5237 16 Jan 19 olle 1121           Date consentDate = null;
5237 16 Jan 19 olle 1122           if (theCase != null)
5237 16 Jan 19 olle 1123           {
5237 16 Jan 19 olle 1124             theCase.loadAnnotations(dc, "consentDate", Annotationtype.CONSENT_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5237 16 Jan 19 olle 1125             consentDate = (Date) Annotationtype.CONSENT_DATE.getAnnotationValue(dc, theCase.getSample());        
5237 16 Jan 19 olle 1126           }
5237 16 Jan 19 olle 1127           if (consentDate != null)
5237 16 Jan 19 olle 1128           {
5243 17 Jan 19 olle 1129             Date ffpeBlockOrderDate = (Date) Annotationtype.FFPE_BLOCK_ORDER_DATE.getAnnotationValue(dc, caseSample);
5243 17 Jan 19 olle 1130             String ffpeBlockOrderDateStr = Meludi.CONVERTER_DATE_TO_STRING.convert(ffpeBlockOrderDate);
5243 17 Jan 19 olle 1131
5243 17 Jan 19 olle 1132             if (ffpeBlockOrderDate != null)
5237 16 Jan 19 olle 1133             {
5237 16 Jan 19 olle 1134               String personalNumber = "";
5237 16 Jan 19 olle 1135               Patient patient = Patient.findByCase(dc, theCase);
5237 16 Jan 19 olle 1136               if (patient != null)
5237 16 Jan 19 olle 1137               {
5237 16 Jan 19 olle 1138                 patient.loadDefaultAnnotations(dc);
5237 16 Jan 19 olle 1139                 personalNumber = (String) Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient.getBioSource());
5237 16 Jan 19 olle 1140               }
5237 16 Jan 19 olle 1141               // Case is used for FFPE block order form
5237 16 Jan 19 olle 1142               boolean ffpeBlockExistsForCase = false;
5237 16 Jan 19 olle 1143               theCase.loadAnnotations(dc, "referralId", Annotationtype.REFERRAL_ID, null);
5237 16 Jan 19 olle 1144               String referralId = (String) Annotationtype.REFERRAL_ID.getAnnotationValue(dc, caseSample);
5237 16 Jan 19 olle 1145               if (referralId != null && !referralId.equals(""))
5237 16 Jan 19 olle 1146               {
5237 16 Jan 19 olle 1147                 // Check if FFPE block item exists for case
5237 16 Jan 19 olle 1148                 String caseName = theCase.getName();
5237 16 Jan 19 olle 1149                 query = Sample.getQuery();
5237 16 Jan 19 olle 1150                 Subtype.FFPE_BLOCK.addFilter(dc, query);
5237 16 Jan 19 olle 1151                 query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5237 16 Jan 19 olle 1152                 query.setCacheResult(true);
5237 16 Jan 19 olle 1153                 List<Sample> ffpeBlocks = query.list(dc);
5237 16 Jan 19 olle 1154                 for (int j = 0; j < ffpeBlocks.size(); j++)
5237 16 Jan 19 olle 1155                 {
5237 16 Jan 19 olle 1156                   Sample ffpeBlockSample = (Sample) ffpeBlocks.get(j);
5237 16 Jan 19 olle 1157                   FfpeBlock ffpeBlock = FfpeBlock.getByName(dc, ffpeBlockSample.getName());
5237 16 Jan 19 olle 1158                   ffpeBlock.loadAnnotations(dc, "caseId", Annotationtype.CASE_ID, null);
5237 16 Jan 19 olle 1159                   String caseId = (String) Annotationtype.CASE_ID.getAnnotationValue(dc, ffpeBlockSample);
5237 16 Jan 19 olle 1160                   if (caseId != null && caseId.equals(caseName))
5237 16 Jan 19 olle 1161                   {
5237 16 Jan 19 olle 1162                     ffpeBlockExistsForCase = true;
5237 16 Jan 19 olle 1163                     break;
5237 16 Jan 19 olle 1164                   }
5237 16 Jan 19 olle 1165                 }
5237 16 Jan 19 olle 1166                 if (!ffpeBlockExistsForCase)
5237 16 Jan 19 olle 1167                 {
5237 16 Jan 19 olle 1168                   JSONObject jsonCase = new JSONObject();
5237 16 Jan 19 olle 1169                   jsonCase = theCase.asJSONObject();
5237 16 Jan 19 olle 1170                   jsonCase.put("personalNumber", personalNumber);
5243 17 Jan 19 olle 1171                   jsonCase.put("ffpeBlockOrderDate", ffpeBlockOrderDateStr);
5237 16 Jan 19 olle 1172                   jsonCases.add(jsonCase);
5237 16 Jan 19 olle 1173                 }
5237 16 Jan 19 olle 1174               }
5237 16 Jan 19 olle 1175             }
5237 16 Jan 19 olle 1176           }
5237 16 Jan 19 olle 1177         }
5237 16 Jan 19 olle 1178
5237 16 Jan 19 olle 1179         json.put("items", jsonCases);
5237 16 Jan 19 olle 1180       }      
4954 31 Aug 18 olle 1181       else if ("GetFfpeBlocks".equals(cmd))
4954 31 Aug 18 olle 1182       {
4954 31 Aug 18 olle 1183         /*
4954 31 Aug 18 olle 1184           All free FFPE blocks items
4954 31 Aug 18 olle 1185         */
4954 31 Aug 18 olle 1186         dc = sc.newDbControl();
4954 31 Aug 18 olle 1187         List<FfpeBlock> ffpeBlocks = FfpeBlock.findAll(dc);
4954 31 Aug 18 olle 1188         
4954 31 Aug 18 olle 1189         JSONArray jsonFfpeBlock = new JSONArray();
4954 31 Aug 18 olle 1190         
4954 31 Aug 18 olle 1191         for (FfpeBlock f : ffpeBlocks)
4954 31 Aug 18 olle 1192         {
5048 22 Oct 18 olle 1193           f.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
5095 14 Nov 18 olle 1194           f.loadAnnotations(dc, "materialNumber", Annotationtype.MATERIAL_NUMBER, null);
5048 22 Oct 18 olle 1195           f.loadAnnotations(dc, "yellowLabel", Annotationtype.YELLOW_LABEL, null);
4954 31 Aug 18 olle 1196           jsonFfpeBlock.add(f.asJSONObject());
4954 31 Aug 18 olle 1197         }
4954 31 Aug 18 olle 1198         
4954 31 Aug 18 olle 1199         json.put("items", jsonFfpeBlock);
4954 31 Aug 18 olle 1200       }      
5061 30 Oct 18 olle 1201       else if ("GetFfpeBlocksUnusedForSectionOrderForm".equals(cmd))
5052 25 Oct 18 olle 1202       {
5052 25 Oct 18 olle 1203         /*
5061 30 Oct 18 olle 1204           All free FFPE block items unused for FFPE section order forms
5052 25 Oct 18 olle 1205         */
5052 25 Oct 18 olle 1206         dc = sc.newDbControl();
5052 25 Oct 18 olle 1207         List<FfpeBlock> ffpeBlocks = FfpeBlock.findAll(dc);
5052 25 Oct 18 olle 1208         
5052 25 Oct 18 olle 1209         JSONArray jsonFfpeBlock = new JSONArray();
5052 25 Oct 18 olle 1210         
5052 25 Oct 18 olle 1211         for (FfpeBlock f : ffpeBlocks)
5052 25 Oct 18 olle 1212         {
5060 29 Oct 18 olle 1213           // Only include unreturned FFPE blocks
5060 29 Oct 18 olle 1214           f.loadAnnotations(dc, "returnedDate", Annotationtype.RETURNED_DATE, null);
5060 29 Oct 18 olle 1215           Date returnedDate = (Date)f.asJSONObject().get("returnedDate");
5060 29 Oct 18 olle 1216           if (returnedDate == null)
5052 25 Oct 18 olle 1217           {
5061 30 Oct 18 olle 1218             // Only include FFPE blocks not used for FFPE section order forms
5061 30 Oct 18 olle 1219             f.loadAnnotations(dc, "usedForFfpeSectionOrderForm", Annotationtype.USED_FOR_FFPE_SECTION_ORDER_FORM, null);
5061 30 Oct 18 olle 1220             Boolean usedFfpeBlockValue = (Boolean)f.asJSONObject().get("usedForFfpeSectionOrderForm");
5060 29 Oct 18 olle 1221             if (usedFfpeBlockValue == null || !usedFfpeBlockValue)
5060 29 Oct 18 olle 1222             {
5060 29 Oct 18 olle 1223               f.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
5095 14 Nov 18 olle 1224               f.loadAnnotations(dc, "materialNumber", Annotationtype.MATERIAL_NUMBER, null);
5060 29 Oct 18 olle 1225               f.loadAnnotations(dc, "yellowLabel", Annotationtype.YELLOW_LABEL, null);
5060 29 Oct 18 olle 1226               f.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
5060 29 Oct 18 olle 1227               jsonFfpeBlock.add(f.asJSONObject());            
5060 29 Oct 18 olle 1228             }
5060 29 Oct 18 olle 1229           }
5060 29 Oct 18 olle 1230         }
5060 29 Oct 18 olle 1231         
5060 29 Oct 18 olle 1232         json.put("items", jsonFfpeBlock);
5060 29 Oct 18 olle 1233       }      
5061 30 Oct 18 olle 1234       else if ("GetFfpeBlocksUnusedForSection".equals(cmd))
5060 29 Oct 18 olle 1235       {
5060 29 Oct 18 olle 1236         /*
5061 30 Oct 18 olle 1237           All free FFPE block items unused for FFPE section
5060 29 Oct 18 olle 1238         */
5060 29 Oct 18 olle 1239         dc = sc.newDbControl();
5060 29 Oct 18 olle 1240         List<FfpeBlock> ffpeBlocks = FfpeBlock.findAll(dc);
5060 29 Oct 18 olle 1241         
5060 29 Oct 18 olle 1242         JSONArray jsonFfpeBlock = new JSONArray();
5060 29 Oct 18 olle 1243         
5060 29 Oct 18 olle 1244         for (FfpeBlock f : ffpeBlocks)
5060 29 Oct 18 olle 1245         {
5060 29 Oct 18 olle 1246           // Only include unreturned FFPE blocks
5060 29 Oct 18 olle 1247           f.loadAnnotations(dc, "returnedDate", Annotationtype.RETURNED_DATE, null);
5060 29 Oct 18 olle 1248           Date returnedDate = (Date)f.asJSONObject().get("returnedDate");
5060 29 Oct 18 olle 1249           if (returnedDate == null)
5060 29 Oct 18 olle 1250           {
5824 04 Feb 20 olle 1251             // Only include FFPE blocks with approved FFPE section order forms
5824 04 Feb 20 olle 1252             f.loadAnnotations(dc, "usedForFfpeSectionOrderForm", Annotationtype.USED_FOR_FFPE_SECTION_ORDER_FORM, null);
5824 04 Feb 20 olle 1253             Boolean usedForFfpeSectionOrderFormValue = (Boolean)f.asJSONObject().get("usedForFfpeSectionOrderForm");
5824 04 Feb 20 olle 1254             //System.out.println(new Date() + " ExtractionServlet::doGet() cmd = \"" + cmd + "\" f.getName() = \"" + f.getName() + "\" usedForFfpeSectionOrderFormValue = \"" + usedForFfpeSectionOrderFormValue + "\"");
5824 04 Feb 20 olle 1255             if (usedForFfpeSectionOrderFormValue != null && usedForFfpeSectionOrderFormValue)
5060 29 Oct 18 olle 1256             {
5824 04 Feb 20 olle 1257               // Only include FFPE blocks not used for FFPE section
5824 04 Feb 20 olle 1258               f.loadAnnotations(dc, "usedForFfpeSection", Annotationtype.USED_FOR_FFPE_SECTION, null);
5824 04 Feb 20 olle 1259               Boolean usedFfpeBlockValue = (Boolean)f.asJSONObject().get("usedForFfpeSection");
5824 04 Feb 20 olle 1260               if (usedFfpeBlockValue == null || !usedFfpeBlockValue)
5824 04 Feb 20 olle 1261               {
5824 04 Feb 20 olle 1262                 f.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
5824 04 Feb 20 olle 1263                 f.loadAnnotations(dc, "materialNumber", Annotationtype.MATERIAL_NUMBER, null);
5824 04 Feb 20 olle 1264                 f.loadAnnotations(dc, "yellowLabel", Annotationtype.YELLOW_LABEL, null);
5824 04 Feb 20 olle 1265                 f.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
5824 04 Feb 20 olle 1266                 jsonFfpeBlock.add(f.asJSONObject());            
5824 04 Feb 20 olle 1267               }              
5060 29 Oct 18 olle 1268             }
5060 29 Oct 18 olle 1269           }
5060 29 Oct 18 olle 1270         }
5060 29 Oct 18 olle 1271         
5060 29 Oct 18 olle 1272         json.put("items", jsonFfpeBlock);
5060 29 Oct 18 olle 1273       }      
5060 29 Oct 18 olle 1274       else if ("GetUnreturnedFfpeBlocks".equals(cmd))
5060 29 Oct 18 olle 1275       {
5060 29 Oct 18 olle 1276         /*
5060 29 Oct 18 olle 1277           All free unreturned FFPE block items
5060 29 Oct 18 olle 1278         */
5060 29 Oct 18 olle 1279         dc = sc.newDbControl();
5060 29 Oct 18 olle 1280         List<FfpeBlock> ffpeBlocks = FfpeBlock.findAll(dc);
5060 29 Oct 18 olle 1281         
5060 29 Oct 18 olle 1282         JSONArray jsonFfpeBlock = new JSONArray();
5060 29 Oct 18 olle 1283         
5060 29 Oct 18 olle 1284         for (FfpeBlock f : ffpeBlocks)
5060 29 Oct 18 olle 1285         {
5060 29 Oct 18 olle 1286           // Only include unreturned FFPE blocks
5060 29 Oct 18 olle 1287           f.loadAnnotations(dc, "returnedDate", Annotationtype.RETURNED_DATE, null);
5060 29 Oct 18 olle 1288           Date returnedDate = (Date)f.asJSONObject().get("returnedDate");
5060 29 Oct 18 olle 1289           if (returnedDate == null)
5060 29 Oct 18 olle 1290           {
5061 30 Oct 18 olle 1291             f.loadAnnotations(dc, "usedForFfpeSection", Annotationtype.USED_FOR_FFPE_SECTION, null);
5052 25 Oct 18 olle 1292             f.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
5095 14 Nov 18 olle 1293             f.loadAnnotations(dc, "materialNumber", Annotationtype.MATERIAL_NUMBER, null);
5052 25 Oct 18 olle 1294             f.loadAnnotations(dc, "yellowLabel", Annotationtype.YELLOW_LABEL, null);
5052 25 Oct 18 olle 1295             jsonFfpeBlock.add(f.asJSONObject());            
5052 25 Oct 18 olle 1296           }
5052 25 Oct 18 olle 1297         }
5052 25 Oct 18 olle 1298         
5052 25 Oct 18 olle 1299         json.put("items", jsonFfpeBlock);
5052 25 Oct 18 olle 1300       }      
5060 29 Oct 18 olle 1301       else if ("GetReturnedFfpeBlocks".equals(cmd))
5060 29 Oct 18 olle 1302       {
5060 29 Oct 18 olle 1303         /*
5060 29 Oct 18 olle 1304           All free returned FFPE block items
5060 29 Oct 18 olle 1305         */
5060 29 Oct 18 olle 1306         dc = sc.newDbControl();
5060 29 Oct 18 olle 1307         List<FfpeBlock> ffpeBlocks = FfpeBlock.findAll(dc);
5060 29 Oct 18 olle 1308         
5060 29 Oct 18 olle 1309         JSONArray jsonFfpeBlock = new JSONArray();
5060 29 Oct 18 olle 1310         
5060 29 Oct 18 olle 1311         for (FfpeBlock f : ffpeBlocks)
5060 29 Oct 18 olle 1312         {
5060 29 Oct 18 olle 1313           // Only include unreturned FFPE blocks
5060 29 Oct 18 olle 1314           f.loadAnnotations(dc, "returnedDate", Annotationtype.RETURNED_DATE, null);
5060 29 Oct 18 olle 1315           Date returnedDate = (Date)f.asJSONObject().get("returnedDate");
5060 29 Oct 18 olle 1316           if (returnedDate != null)
5060 29 Oct 18 olle 1317           {
5061 30 Oct 18 olle 1318             f.loadAnnotations(dc, "usedForFfpeSection", Annotationtype.USED_FOR_FFPE_SECTION, null);
5060 29 Oct 18 olle 1319             f.loadAnnotations(dc, "pad", Annotationtype.PAD, null);
5095 14 Nov 18 olle 1320             f.loadAnnotations(dc, "materialNumber", Annotationtype.MATERIAL_NUMBER, null);
5060 29 Oct 18 olle 1321             f.loadAnnotations(dc, "yellowLabel", Annotationtype.YELLOW_LABEL, null);
5060 29 Oct 18 olle 1322             jsonFfpeBlock.add(f.asJSONObject());            
5060 29 Oct 18 olle 1323           }
5060 29 Oct 18 olle 1324         }
5060 29 Oct 18 olle 1325         
5060 29 Oct 18 olle 1326         json.put("items", jsonFfpeBlock);
5060 29 Oct 18 olle 1327       }      
3225 02 Apr 15 olle 1328       else if ("GetDna".equals(cmd))
3225 02 Apr 15 olle 1329       {
3225 02 Apr 15 olle 1330         /*
3225 02 Apr 15 olle 1331           Find DNA extract from name.
3225 02 Apr 15 olle 1332         */
3225 02 Apr 15 olle 1333         dc = sc.newDbControl();
3225 02 Apr 15 olle 1334         String name = req.getParameter("name");
3225 02 Apr 15 olle 1335
3318 08 May 15 olle 1336         // Use stored annotation snapshots for performance reasons
3318 08 May 15 olle 1337         SnapshotManager manager = new SnapshotManager();        
3318 08 May 15 olle 1338
3225 02 Apr 15 olle 1339         Dna dna = Dna.findByName(dc, name);
3225 02 Apr 15 olle 1340         JSONObject jsonDna = new JSONObject();
3225 02 Apr 15 olle 1341         if (dna != null)
3225 02 Apr 15 olle 1342         {
3318 08 May 15 olle 1343           loadDnaInfo(dc, manager, dna);
3225 02 Apr 15 olle 1344           jsonDna = dna.asJSONObject();
3225 02 Apr 15 olle 1345         }
3225 02 Apr 15 olle 1346         json.put("dna", jsonDna);
3225 02 Apr 15 olle 1347       }      
3176 06 Mar 15 olle 1348       else if ("GetLastProcessedDna".equals(cmd))
3176 06 Mar 15 olle 1349       {
3176 06 Mar 15 olle 1350         /*
3176 06 Mar 15 olle 1351           Find SpecimenTube items that have extracts.
3176 06 Mar 15 olle 1352         */
3176 06 Mar 15 olle 1353         dc = sc.newDbControl();
3176 06 Mar 15 olle 1354         List<SpecimenTube> specimenTubes = SpecimenTube.findProcessedTubes(dc);
3176 06 Mar 15 olle 1355         
3176 06 Mar 15 olle 1356         JSONArray jsonDna = new JSONArray();
3176 06 Mar 15 olle 1357
3176 06 Mar 15 olle 1358         Dna lastExtractedDna = null;
3176 06 Mar 15 olle 1359         Date lastExtractionDate = null;
3176 06 Mar 15 olle 1360
3176 06 Mar 15 olle 1361         for (SpecimenTube tube : specimenTubes)
3176 06 Mar 15 olle 1362         {
3176 06 Mar 15 olle 1363           Sample s = tube.getSample();
3176 06 Mar 15 olle 1364           String specimenName = s.getName();
3176 06 Mar 15 olle 1365           if (specimenName != null && !specimenName.equals(""))
3176 06 Mar 15 olle 1366           {
3176 06 Mar 15 olle 1367             int dotIndex = specimenName.indexOf(".");
3176 06 Mar 15 olle 1368             if (dotIndex >= 0)
3176 06 Mar 15 olle 1369             {
3176 06 Mar 15 olle 1370               String caseName = specimenName.substring(0,dotIndex);
3176 06 Mar 15 olle 1371               List<Dna> dnas = Dna.findByCaseName(dc, caseName);
3176 06 Mar 15 olle 1372               if (dnas.size() > 0)
3176 06 Mar 15 olle 1373               {
3176 06 Mar 15 olle 1374                 Dna dna = (Dna) dnas.get(0);
3176 06 Mar 15 olle 1375                 Extract e = dna.getExtract();
3176 06 Mar 15 olle 1376                 BioMaterialEvent created = e.getCreationEvent();
3176 06 Mar 15 olle 1377                 Date extractionDate = created.getEventDate();
3176 06 Mar 15 olle 1378                 if (lastExtractionDate != null)
3176 06 Mar 15 olle 1379                 {
3176 06 Mar 15 olle 1380                   if (extractionDate != null && (extractionDate.after(lastExtractionDate) || extractionDate.equals(lastExtractionDate)))
3176 06 Mar 15 olle 1381                   {
3176 06 Mar 15 olle 1382                     lastExtractedDna = dna;
3176 06 Mar 15 olle 1383                     lastExtractionDate = extractionDate;
3176 06 Mar 15 olle 1384                   }
3176 06 Mar 15 olle 1385                 }
3176 06 Mar 15 olle 1386                 else
3176 06 Mar 15 olle 1387                 {
3176 06 Mar 15 olle 1388                   if (extractionDate != null)
3176 06 Mar 15 olle 1389                   {
3176 06 Mar 15 olle 1390                     lastExtractedDna = dna;
3176 06 Mar 15 olle 1391                     lastExtractionDate = extractionDate;
3176 06 Mar 15 olle 1392                   }
3176 06 Mar 15 olle 1393                 }
3176 06 Mar 15 olle 1394               }
3176 06 Mar 15 olle 1395             }
3176 06 Mar 15 olle 1396           }
3176 06 Mar 15 olle 1397         }
3176 06 Mar 15 olle 1398         if (lastExtractedDna != null)
3176 06 Mar 15 olle 1399         {
3318 08 May 15 olle 1400           // Use stored annotation snapshots for performance reasons
3318 08 May 15 olle 1401           SnapshotManager manager = new SnapshotManager();        
3318 08 May 15 olle 1402
3318 08 May 15 olle 1403           loadDnaInfo(dc, manager, lastExtractedDna);
3176 06 Mar 15 olle 1404           jsonDna.add(lastExtractedDna.asJSONObject());
3176 06 Mar 15 olle 1405         }
3176 06 Mar 15 olle 1406         json.put("dnas", jsonDna);
3176 06 Mar 15 olle 1407       }      
3176 06 Mar 15 olle 1408       else if ("GetNextQiacubeRunNo".equals(cmd))
3176 06 Mar 15 olle 1409       {
3176 06 Mar 15 olle 1410         /*
3176 06 Mar 15 olle 1411           Get the next qiacube run number for the given date
3176 06 Mar 15 olle 1412         */
5823 04 Feb 20 olle 1413         String qiacubeDateStr = req.getParameter("qiacubeDate");
5823 04 Feb 20 olle 1414         System.out.println("ExtractionServlet::doGet(): cmd = \"" + cmd + "\" qiacubeDateStr = \"" + qiacubeDateStr + "\".");
3176 06 Mar 15 olle 1415         Date date = Meludi.CONVERTER_STRING_TO_DATE.convert(req.getParameter("qiacubeDate"));
3176 06 Mar 15 olle 1416         dc = sc.newDbControl();
3176 06 Mar 15 olle 1417         
3176 06 Mar 15 olle 1418         // Find existing RNA with QiacubeDate
3176 06 Mar 15 olle 1419         ItemQuery<Extract> query = Extract.getQuery();
3176 06 Mar 15 olle 1420         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3176 06 Mar 15 olle 1421         // Filter on RNA subtype
3176 06 Mar 15 olle 1422         Subtype.RNA.addFilter(dc, query);
3176 06 Mar 15 olle 1423         
3176 06 Mar 15 olle 1424         // Filter on given date
3176 06 Mar 15 olle 1425         query.join(Hql.innerJoin("creationEvent", "ce"));
3176 06 Mar 15 olle 1426         query.restrict(Restrictions.eq(Hql.property("ce", "eventDate"), Expressions.parameter("created", date, Type.DATE)));
3176 06 Mar 15 olle 1427         
3176 06 Mar 15 olle 1428         int nextQiacubeRunNumber = 1;
3176 06 Mar 15 olle 1429 /*
3176 06 Mar 15 olle 1430         for (Extract e : query.list(dc))
3176 06 Mar 15 olle 1431         {
3176 06 Mar 15 olle 1432           Integer qiacubeRunNumber = (Integer)Annotationtype.QIACUBE_RUN_NO.getAnnotationValue(dc, e);
3176 06 Mar 15 olle 1433           if (qiacubeRunNumber != null && qiacubeRunNumber >= nextQiacubeRunNumber)
3176 06 Mar 15 olle 1434           {
3176 06 Mar 15 olle 1435             nextQiacubeRunNumber = qiacubeRunNumber + 1;
3176 06 Mar 15 olle 1436           }
3176 06 Mar 15 olle 1437           
3176 06 Mar 15 olle 1438         }
3176 06 Mar 15 olle 1439 */
3176 06 Mar 15 olle 1440         json.put("nextQiacubeRunNo", nextQiacubeRunNumber);
3176 06 Mar 15 olle 1441       }      
3176 06 Mar 15 olle 1442       else if ("DownloadNanoDropSampleIdFile".equals(cmd))
3176 06 Mar 15 olle 1443       {
3176 06 Mar 15 olle 1444         json = null; // No JSON output
3432 29 Jun 15 olle 1445
3432 29 Jun 15 olle 1446         String startListName = req.getParameter("startListName");
3176 06 Mar 15 olle 1447         String qiacubeDate = req.getParameter("qiacubeDate");
3176 06 Mar 15 olle 1448         String qiacubeRunNo = req.getParameter("qiacubeRunNo");
3176 06 Mar 15 olle 1449         String[] itemNames = req.getParameter("itemNames").split(",");
3255 17 Apr 15 olle 1450         Boolean useBlankControlColumn = Boolean.valueOf(req.getParameter("blankControlColumn"));
3255 17 Apr 15 olle 1451         if (useBlankControlColumn == null)
3255 17 Apr 15 olle 1452         {
3255 17 Apr 15 olle 1453           useBlankControlColumn = true;
3255 17 Apr 15 olle 1454         }
3176 06 Mar 15 olle 1455         
3432 29 Jun 15 olle 1456         resp.setHeader("Content-Disposition", "attachment; filename=nd-sample-id-" + startListName + "-" + qiacubeDate + "-" + qiacubeRunNo + ".csv");
3176 06 Mar 15 olle 1457         resp.setContentType("text/plain");
3176 06 Mar 15 olle 1458         resp.setCharacterEncoding("UTF-8");
3176 06 Mar 15 olle 1459         
3176 06 Mar 15 olle 1460         PrintWriter out = resp.getWriter();
3176 06 Mar 15 olle 1461         TableWriter tw = new TableWriter(out);
3176 06 Mar 15 olle 1462
3275 24 Apr 15 olle 1463         String[] cols = new String[12];
3176 06 Mar 15 olle 1464         String[] itemNamesNonNull = new String[24];
3176 06 Mar 15 olle 1465         
3176 06 Mar 15 olle 1466         int numItems = 0;
3176 06 Mar 15 olle 1467         for (int i = 0; i < itemNames.length; i++)
3176 06 Mar 15 olle 1468         {
3176 06 Mar 15 olle 1469           String itemName = itemNames[i];
3176 06 Mar 15 olle 1470           if (itemName != null && !itemName.equals(""))
3176 06 Mar 15 olle 1471           {
3176 06 Mar 15 olle 1472             // Only add newly created and input RNA items
3433 29 Jun 15 olle 1473             if (Meludi.itemIsSpecimen(itemName) || Meludi.itemIsRna(itemName))
3176 06 Mar 15 olle 1474             {
3176 06 Mar 15 olle 1475               if (numItems < 24)
3176 06 Mar 15 olle 1476               {
3176 06 Mar 15 olle 1477                 itemNamesNonNull[numItems] = itemName;
3176 06 Mar 15 olle 1478                 numItems++;
3176 06 Mar 15 olle 1479               }
3176 06 Mar 15 olle 1480             }
3176 06 Mar 15 olle 1481           }
3176 06 Mar 15 olle 1482         }
3176 06 Mar 15 olle 1483
3176 06 Mar 15 olle 1484         // NanoDrop sample id file for RNA extracts from 1-24 specimens
3176 06 Mar 15 olle 1485         for (int row = 0; row < 8; ++row)
3176 06 Mar 15 olle 1486         {
3176 06 Mar 15 olle 1487           for (int col = 0; col < 4; ++col)
3176 06 Mar 15 olle 1488           {
3255 17 Apr 15 olle 1489             // Column to the right of last column with sample, should be filled with blank controls 
3255 17 Apr 15 olle 1490             int blankColumn = (numItems - 1)/8 + 1;
3255 17 Apr 15 olle 1491             int firstNonSampleColumnPos = numItems - 8*col;
3255 17 Apr 15 olle 1492             // Note: firstNonSampleColumnPos < 0 => cols[col] is set to null for all rows
3176 06 Mar 15 olle 1493
3255 17 Apr 15 olle 1494             if (row < firstNonSampleColumnPos)
3176 06 Mar 15 olle 1495             {
3176 06 Mar 15 olle 1496               String itemName = itemNamesNonNull[row+8*col];
3433 29 Jun 15 olle 1497               if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 1498               {
3176 06 Mar 15 olle 1499                 cols[col] = itemName + ".r";
3176 06 Mar 15 olle 1500               }
3176 06 Mar 15 olle 1501               else
3176 06 Mar 15 olle 1502               {
3176 06 Mar 15 olle 1503                 cols[col] = itemName;
3176 06 Mar 15 olle 1504               }
3176 06 Mar 15 olle 1505             }
3255 17 Apr 15 olle 1506             else if ((useBlankControlColumn && col == blankColumn) ||
3255 17 Apr 15 olle 1507                 (!useBlankControlColumn && row == firstNonSampleColumnPos))
3176 06 Mar 15 olle 1508             {
3176 06 Mar 15 olle 1509               cols[col] = "blank";
3176 06 Mar 15 olle 1510             }
3176 06 Mar 15 olle 1511             else
3255 17 Apr 15 olle 1512             {              
3176 06 Mar 15 olle 1513               cols[col] = null;
3176 06 Mar 15 olle 1514             }
3176 06 Mar 15 olle 1515           }
3176 06 Mar 15 olle 1516           tw.tablePrintData((Object[])cols);
3176 06 Mar 15 olle 1517         }
3176 06 Mar 15 olle 1518         
3176 06 Mar 15 olle 1519         out.flush();
3176 06 Mar 15 olle 1520         out.close();
3176 06 Mar 15 olle 1521         
3176 06 Mar 15 olle 1522       }
3176 06 Mar 15 olle 1523       else if ("DownloadQPcrSampleIdFile".equals(cmd))
3176 06 Mar 15 olle 1524       {
3176 06 Mar 15 olle 1525         json = null; // No JSON output
3176 06 Mar 15 olle 1526         
3432 29 Jun 15 olle 1527         String startListName = req.getParameter("startListName");
3176 06 Mar 15 olle 1528         String qiacubeDate = req.getParameter("qiacubeDate");
3176 06 Mar 15 olle 1529         String qiacubeRunNo = req.getParameter("qiacubeRunNo");
3176 06 Mar 15 olle 1530         String[] itemNames = req.getParameter("itemNames").split(",");
3176 06 Mar 15 olle 1531         
3432 29 Jun 15 olle 1532         resp.setHeader("Content-Disposition", "attachment; filename=qpcr-sample-id-" + startListName + "-" + qiacubeDate + "-" + qiacubeRunNo+ ".csv");
3176 06 Mar 15 olle 1533         resp.setContentType("text/plain");
3176 06 Mar 15 olle 1534         resp.setCharacterEncoding("UTF-8");
3176 06 Mar 15 olle 1535         
3176 06 Mar 15 olle 1536         PrintWriter out = resp.getWriter();
3176 06 Mar 15 olle 1537         TableWriter tw = new TableWriter(out);
3176 06 Mar 15 olle 1538
3176 06 Mar 15 olle 1539         String[] cols = new String[1];
3176 06 Mar 15 olle 1540         String[] itemNamesNonNull = new String[24];
3176 06 Mar 15 olle 1541         
3176 06 Mar 15 olle 1542         int numItems = 0;
3176 06 Mar 15 olle 1543         for (int i = 0; i < itemNames.length; i++)
3176 06 Mar 15 olle 1544         {
3176 06 Mar 15 olle 1545           String itemName = itemNames[i];
3176 06 Mar 15 olle 1546           if (itemName != null && !itemName.equals(""))
3176 06 Mar 15 olle 1547           {
3176 06 Mar 15 olle 1548             // Only add newly created and input DNA items
3433 29 Jun 15 olle 1549             if (Meludi.itemIsSpecimen(itemName) || Meludi.itemIsDna(itemName))
3176 06 Mar 15 olle 1550             {
3433 29 Jun 15 olle 1551               if (Meludi.itemIsSpecimen(itemName))
3230 08 Apr 15 olle 1552               {
3230 08 Apr 15 olle 1553                 itemName += ".d";
3230 08 Apr 15 olle 1554               }
3176 06 Mar 15 olle 1555               if (numItems < 24)
3176 06 Mar 15 olle 1556               {
3176 06 Mar 15 olle 1557                 itemNamesNonNull[numItems] = itemName;
3176 06 Mar 15 olle 1558                 numItems++;
3176 06 Mar 15 olle 1559               }
3176 06 Mar 15 olle 1560             }
3176 06 Mar 15 olle 1561           }
3176 06 Mar 15 olle 1562         }
3176 06 Mar 15 olle 1563
3205 20 Mar 15 olle 1564         // qPCR sample id file header
3205 20 Mar 15 olle 1565         String[] headerCols = new String[1];
3205 20 Mar 15 olle 1566         headerCols[0] = "Row,Column,*Target Name,*Sample Name";
3205 20 Mar 15 olle 1567         tw.tablePrintData((Object[])headerCols);
3205 20 Mar 15 olle 1568         
3176 06 Mar 15 olle 1569         // qPCR sample id file for DNA extracts from 1-24 specimens
3213 27 Mar 15 olle 1570         int sampleNo = 0;
3176 06 Mar 15 olle 1571         for (int row = 0; row < 8; ++row)
3176 06 Mar 15 olle 1572         {
3176 06 Mar 15 olle 1573           for (int col = 0; col < 12; ++col)
3176 06 Mar 15 olle 1574           {
3213 27 Mar 15 olle 1575             int tripletColNo = col/3;
3213 27 Mar 15 olle 1576             sampleNo = 8*tripletColNo + row + 1;
3213 27 Mar 15 olle 1577             if (sampleNo <= (numItems + 2))
3176 06 Mar 15 olle 1578             {
3176 06 Mar 15 olle 1579               String rowId = getPlateRowId(row);
3176 06 Mar 15 olle 1580               int colId = col+1;
3176 06 Mar 15 olle 1581               String targetName = "";
3176 06 Mar 15 olle 1582               String sampleName = "";
3591 11 Nov 15 olle 1583               if (sampleNo == 1)
3176 06 Mar 15 olle 1584               {
3591 11 Nov 15 olle 1585                 // QCT (Quality Control Template)
3230 08 Apr 15 olle 1586                 targetName = "UNKN";
3591 11 Nov 15 olle 1587                 sampleName = "QCT";
3176 06 Mar 15 olle 1588               }
3591 11 Nov 15 olle 1589               else if (sampleNo <= (numItems + 1))
3176 06 Mar 15 olle 1590               {
3230 08 Apr 15 olle 1591                 targetName = "UNKN";
3591 11 Nov 15 olle 1592                 sampleName = itemNamesNonNull[sampleNo - 2];
3176 06 Mar 15 olle 1593               }
3213 27 Mar 15 olle 1594               else if (sampleNo == (numItems + 2))
3176 06 Mar 15 olle 1595               {
3213 27 Mar 15 olle 1596                 // NTC (Non Template Control)
3230 08 Apr 15 olle 1597                 targetName = "NTC";
3176 06 Mar 15 olle 1598                 sampleName = "";
3176 06 Mar 15 olle 1599               }
3176 06 Mar 15 olle 1600               cols[0] = rowId + "," + colId + "," + targetName + "," + sampleName;
3176 06 Mar 15 olle 1601               tw.tablePrintData((Object[])cols);
3176 06 Mar 15 olle 1602             }
3176 06 Mar 15 olle 1603           }
3176 06 Mar 15 olle 1604         }
3176 06 Mar 15 olle 1605         
3176 06 Mar 15 olle 1606         out.flush();
3176 06 Mar 15 olle 1607         out.close();
3176 06 Mar 15 olle 1608         
3176 06 Mar 15 olle 1609       }
3332 12 May 15 olle 1610       else if ("DownloadReagentLotNoFile".equals(cmd))
3332 12 May 15 olle 1611       {
3332 12 May 15 olle 1612         json = null; // No JSON output
3332 12 May 15 olle 1613
3432 29 Jun 15 olle 1614         String startListName = req.getParameter("startListName");
3332 12 May 15 olle 1615         String isolationDate = req.getParameter("isolationDate");
3332 12 May 15 olle 1616         String reagentInfoStr = req.getParameter("reagentInfo");
3332 12 May 15 olle 1617         JSONObject reagentInfo = (JSONObject)new JSONParser().parse(reagentInfoStr);
3332 12 May 15 olle 1618         
3432 29 Jun 15 olle 1619         resp.setHeader("Content-Disposition", "attachment; filename=extraction-reagents-" + startListName + "-" + isolationDate + ".csv");
3332 12 May 15 olle 1620         resp.setContentType("text/plain");
3332 12 May 15 olle 1621         resp.setCharacterEncoding("UTF-8");
3332 12 May 15 olle 1622         
3332 12 May 15 olle 1623         PrintWriter out = resp.getWriter();
3332 12 May 15 olle 1624         out.println("MeLuDI Extraction Reagents;");
3332 12 May 15 olle 1625         out.println(";");
3332 12 May 15 olle 1626         out.println("Specimen Reagents;");
3332 12 May 15 olle 1627         out.println("Xylen;" + reagentInfo.get("xyleneSpecimen"));
3332 12 May 15 olle 1628         out.println("ETOH 99,5%;" + reagentInfo.get("etoh995Specimen"));
3332 12 May 15 olle 1629         out.println("Buffer PKD;" + reagentInfo.get("bufferPkdSpecimen"));
3332 12 May 15 olle 1630         out.println("Buffer ATL;" + reagentInfo.get("bufferAtlSpecimen"));
3332 12 May 15 olle 1631         out.println("Proteinase K;" + reagentInfo.get("proteinaseKSpecimen"));
3332 12 May 15 olle 1632         out.println("Rnase A;" + reagentInfo.get("rnaseASpecimen"));
3332 12 May 15 olle 1633         out.println(";");
3332 12 May 15 olle 1634         out.println("Qiacube Reagents;");
3332 12 May 15 olle 1635         out.println("AllPrep DNA/RNA FFPE kit;" + reagentInfo.get("allPrepFfpeKit"));
3332 12 May 15 olle 1636         out.println("ETOH 99,5%;" + reagentInfo.get("etoh995"));
3332 12 May 15 olle 1637         out.println("Buffer RLT;" + reagentInfo.get("bufferRlt"));
3332 12 May 15 olle 1638         out.println("Buffer FRN;" + reagentInfo.get("bufferFrn"));
3332 12 May 15 olle 1639         out.println("Buffer RPE;" + reagentInfo.get("bufferRpe"));
3332 12 May 15 olle 1640         out.println("Buffer AL;" + reagentInfo.get("bufferAl"));
3332 12 May 15 olle 1641         out.println("Buffer AW1;" + reagentInfo.get("bufferAw1"));
3332 12 May 15 olle 1642         out.println("Buffer AW2;" + reagentInfo.get("bufferAw2"));
3332 12 May 15 olle 1643         out.println("Buffer ATE;" + reagentInfo.get("bufferAte"));
3332 12 May 15 olle 1644         out.println("RDD;" + reagentInfo.get("bufferRdd"));
3332 12 May 15 olle 1645         out.println("Rnase-free Water;" + reagentInfo.get("rnaseFreeWater"));
3332 12 May 15 olle 1646         out.println("DNAse mix;" + reagentInfo.get("dnaseMix"));
3332 12 May 15 olle 1647         out.println("Rneasy MinElute Spin Column;" + reagentInfo.get("rneasyMinEluteSpinColumn"));
3332 12 May 15 olle 1648         out.println("QIAamp MinElute Spin Column;" + reagentInfo.get("qiaampMinEluteSpinColumn"));
3332 12 May 15 olle 1649         out.println(";");
3332 12 May 15 olle 1650         out.println("Quality Control Reagents;");
3332 12 May 15 olle 1651         out.println("Kapa SYBR Fast;" + reagentInfo.get("kapaSybrFast"));
3332 12 May 15 olle 1652         out.println("Quality Control Primers (QCP) RGT;" + reagentInfo.get("qualityControlPrimersQcpRgt"));
3438 03 Jul 15 olle 1653         out.println("Quality Control Template (QCT);" + reagentInfo.get("qualityControlTemplateQct"));
3332 12 May 15 olle 1654         out.println("QUBIT DNA High Sens;" + reagentInfo.get("qubitDnaHighSens"));
3332 12 May 15 olle 1655         out.println("QUBIT DNA Broad Range;" + reagentInfo.get("qubitDnaBroadRange"));
3332 12 May 15 olle 1656
3332 12 May 15 olle 1657         out.flush();
3332 12 May 15 olle 1658         out.close();
3332 12 May 15 olle 1659       }
3176 06 Mar 15 olle 1660       else if ("GetRNAToNormalize".equals(cmd) || "GetRNAInfoForNormalize".equals(cmd))
3176 06 Mar 15 olle 1661       {
3176 06 Mar 15 olle 1662         /*
3176 06 Mar 15 olle 1663           Find RNA items that have been selected for pre-normalization.
3176 06 Mar 15 olle 1664         */
3176 06 Mar 15 olle 1665         dc = sc.newDbControl();
3176 06 Mar 15 olle 1666         // Create a query that load all Lysate extracts without created date
3176 06 Mar 15 olle 1667         ItemQuery<Extract> query = Extract.getQuery();
3176 06 Mar 15 olle 1668         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3176 06 Mar 15 olle 1669         // Filter on RNA subtype
3176 06 Mar 15 olle 1670         Subtype.RNA.addFilter(dc, query);
3176 06 Mar 15 olle 1671
3176 06 Mar 15 olle 1672 /*
3176 06 Mar 15 olle 1673         if ("GetRNAToNormalize".equals(cmd))
3176 06 Mar 15 olle 1674         {
3176 06 Mar 15 olle 1675           // Join AutoProcessing annotation
3176 06 Mar 15 olle 1676           query.join(Annotations.leftJoin(null, Annotationtype.AUTO_PROCESSING.load(dc), "ap"));
3176 06 Mar 15 olle 1677           query.restrict(Restrictions.eq(Hql.alias("ap"), Expressions.string(Rna.AUTO_PROCESSING_PRE_NORMALIZE)));
3176 06 Mar 15 olle 1678         }
3176 06 Mar 15 olle 1679         else
3176 06 Mar 15 olle 1680         {
3176 06 Mar 15 olle 1681 */
3176 06 Mar 15 olle 1682           String[] selected = req.getParameter("rnaIds").split(",");
3176 06 Mar 15 olle 1683           query.restrict(Restrictions.in(Hql.property("id"), Expressions.parameter("selected")));
3176 06 Mar 15 olle 1684           query.setParameter("selected", Arrays.asList(Values.getInt(selected)), Type.INT);
3176 06 Mar 15 olle 1685 /*
3176 06 Mar 15 olle 1686         }
3176 06 Mar 15 olle 1687 */
3176 06 Mar 15 olle 1688
3176 06 Mar 15 olle 1689         JSONArray jsonRna = new JSONArray();
3176 06 Mar 15 olle 1690         List<Rna> rna = Rna.toRna(query.list(dc));
3176 06 Mar 15 olle 1691         
3176 06 Mar 15 olle 1692         for (Rna r : rna)
3176 06 Mar 15 olle 1693         {
3176 06 Mar 15 olle 1694           r.loadBioPlateLocation();
3176 06 Mar 15 olle 1695           r.setAnnotation("remainingQuantity", r.getItem().getRemainingQuantity());
3176 06 Mar 15 olle 1696 /*
3176 06 Mar 15 olle 1697           r.setAnnotation("nextAliquotName", r.getNextNormalizedAliquotName(dc));
3176 06 Mar 15 olle 1698           r.loadAnnotations(dc, "NDConc", Annotationtype.ND_CONC, null);
3176 06 Mar 15 olle 1699           r.loadAnnotations(dc, "QiacubeDate", Annotationtype.QIACUBE_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3176 06 Mar 15 olle 1700           r.loadAnnotations(dc, "QiacubeRunNo", Annotationtype.QIACUBE_RUN_NO, null);
3176 06 Mar 15 olle 1701           r.loadAnnotations(dc, "QiacubePosition", Annotationtype.QIACUBE_POSITION, null);
3176 06 Mar 15 olle 1702 */
3176 06 Mar 15 olle 1703           
3176 06 Mar 15 olle 1704           jsonRna.add(r.asJSONObject());
3176 06 Mar 15 olle 1705         }
3176 06 Mar 15 olle 1706         
3176 06 Mar 15 olle 1707         json.put("rnaExtracts", jsonRna);
3176 06 Mar 15 olle 1708       }      
3180 17 Mar 15 olle 1709       else if ("GetNextStartPlateName".equals(cmd))
3176 06 Mar 15 olle 1710       {
3176 06 Mar 15 olle 1711         dc = sc.newDbControl();
4200 01 Nov 16 olle 1712         String startPlatePrefix = Meludi.fetchStartPlateItemPrefix(sc.getActiveProjectId());
4225 10 Nov 16 olle 1713         Integer startPlateNumDigits = Meludi.fetchStartPlateItemNumDigits(sc.getActiveProjectId());
3180 17 Mar 15 olle 1714         BioPlate highestPlate = findPlateWithHighestNumber(dc, startPlatePrefix);
3180 17 Mar 15 olle 1715         long highestPlateNum = 0;
3180 17 Mar 15 olle 1716         if (highestPlate != null)
3176 06 Mar 15 olle 1717         {
3180 17 Mar 15 olle 1718           String highestPlateName = highestPlate.getName();
3180 17 Mar 15 olle 1719           // Remove library plate prefix
3180 17 Mar 15 olle 1720           if (highestPlateName.indexOf(startPlatePrefix) >= 0)
3180 17 Mar 15 olle 1721           {
3180 17 Mar 15 olle 1722             highestPlateName = highestPlateName.substring(startPlatePrefix.length());
3180 17 Mar 15 olle 1723           }
3180 17 Mar 15 olle 1724           // Remove initial zeroes
3180 17 Mar 15 olle 1725           while (highestPlateName.indexOf("0") == 0)
3180 17 Mar 15 olle 1726           {
3180 17 Mar 15 olle 1727             highestPlateName = highestPlateName.substring(1);
3180 17 Mar 15 olle 1728           }
3180 17 Mar 15 olle 1729           if (!highestPlateName.equals(""))
3180 17 Mar 15 olle 1730           {
3180 17 Mar 15 olle 1731             highestPlateNum = Long.parseLong(highestPlateName);
3180 17 Mar 15 olle 1732           }
3176 06 Mar 15 olle 1733         }
3180 17 Mar 15 olle 1734         long nextStartPlateNumber = highestPlateNum + 1;
3180 17 Mar 15 olle 1735         String nextStartPlateNumberStr = "" + nextStartPlateNumber;
4225 10 Nov 16 olle 1736         // Pad with initial zeros to get right number of digits
4225 10 Nov 16 olle 1737         while (nextStartPlateNumberStr.length() < startPlateNumDigits)
3176 06 Mar 15 olle 1738         {
3180 17 Mar 15 olle 1739           nextStartPlateNumberStr = "0" + nextStartPlateNumberStr;
3180 17 Mar 15 olle 1740         }
3180 17 Mar 15 olle 1741         String nextStartPlateName = startPlatePrefix + nextStartPlateNumberStr;
3180 17 Mar 15 olle 1742         json.put("nextStartPlateName", nextStartPlateName);
3176 06 Mar 15 olle 1743       }      
3432 29 Jun 15 olle 1744       else if ("GetNextStartListName".equals(cmd))
3432 29 Jun 15 olle 1745       {
3432 29 Jun 15 olle 1746         dc = sc.newDbControl();
4150 03 Oct 16 olle 1747         String startListPrefix = Meludi.fetchStartListItemPrefix(sc.getActiveProjectId());
4225 10 Nov 16 olle 1748         Integer startListNumDigits = Meludi.fetchStartListItemNumDigits(sc.getActiveProjectId());
3432 29 Jun 15 olle 1749         ItemList highestItemList = findItemListWithHighestNumber(dc, startListPrefix);
3432 29 Jun 15 olle 1750         long highestItemListNum = 0;
3432 29 Jun 15 olle 1751         if (highestItemList != null)
3432 29 Jun 15 olle 1752         {
3432 29 Jun 15 olle 1753           String highestItemListName = highestItemList.getName();
3432 29 Jun 15 olle 1754           // Remove item list prefix
3432 29 Jun 15 olle 1755           if (highestItemListName.indexOf(startListPrefix) >= 0)
3432 29 Jun 15 olle 1756           {
3432 29 Jun 15 olle 1757             highestItemListName = highestItemListName.substring(startListPrefix.length());
3432 29 Jun 15 olle 1758           }
3432 29 Jun 15 olle 1759           // Remove initial zeroes
3432 29 Jun 15 olle 1760           while (highestItemListName.indexOf("0") == 0)
3432 29 Jun 15 olle 1761           {
3432 29 Jun 15 olle 1762             highestItemListName = highestItemListName.substring(1);
3432 29 Jun 15 olle 1763           }
3432 29 Jun 15 olle 1764           if (!highestItemListName.equals(""))
3432 29 Jun 15 olle 1765           {
3432 29 Jun 15 olle 1766             highestItemListNum = Long.parseLong(highestItemListName);
3432 29 Jun 15 olle 1767           }
3432 29 Jun 15 olle 1768         }
3432 29 Jun 15 olle 1769         long nextStartListNumber = highestItemListNum + 1;
3432 29 Jun 15 olle 1770         String nextStartListNumberStr = "" + nextStartListNumber;
4225 10 Nov 16 olle 1771         // Pad with initial zeros to get right number of digits
4225 10 Nov 16 olle 1772         while (nextStartListNumberStr.length() < startListNumDigits)
3432 29 Jun 15 olle 1773         {
3432 29 Jun 15 olle 1774           nextStartListNumberStr = "0" + nextStartListNumberStr;
3432 29 Jun 15 olle 1775         }
3432 29 Jun 15 olle 1776         String nextStartListName = startListPrefix + nextStartListNumberStr;
3432 29 Jun 15 olle 1777         json.put("nextStartListName", nextStartListName);
3432 29 Jun 15 olle 1778       }      
3176 06 Mar 15 olle 1779     }
3176 06 Mar 15 olle 1780     catch (Throwable t)
3176 06 Mar 15 olle 1781     {
3176 06 Mar 15 olle 1782       t.printStackTrace();
3176 06 Mar 15 olle 1783       if (json != null)
3176 06 Mar 15 olle 1784       {
3176 06 Mar 15 olle 1785         json.clear();
3176 06 Mar 15 olle 1786         json.put("status", "error");
3176 06 Mar 15 olle 1787         json.put("message", t.getMessage());
3176 06 Mar 15 olle 1788         json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3176 06 Mar 15 olle 1789       }
3176 06 Mar 15 olle 1790       else
3176 06 Mar 15 olle 1791       {
3176 06 Mar 15 olle 1792         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
3176 06 Mar 15 olle 1793       }
3176 06 Mar 15 olle 1794     }
3176 06 Mar 15 olle 1795     finally
3176 06 Mar 15 olle 1796     {
3176 06 Mar 15 olle 1797       if (dc != null) dc.close();
3176 06 Mar 15 olle 1798       if (json != null)
3176 06 Mar 15 olle 1799       {
3176 06 Mar 15 olle 1800         json.writeJSONString(resp.getWriter());
3176 06 Mar 15 olle 1801       }
3176 06 Mar 15 olle 1802     }
3176 06 Mar 15 olle 1803     
3176 06 Mar 15 olle 1804   }
3176 06 Mar 15 olle 1805
3440 17 Jul 15 olle 1806   /**
3440 17 Jul 15 olle 1807    * Returns a JSONArray of item lists.
3440 17 Jul 15 olle 1808    * 
3440 17 Jul 15 olle 1809    * @param dc DbControl The DbControl to use.
3440 17 Jul 15 olle 1810    * @param processed Boolean Flag indicating whether only processed or unprocessed item lists should be included. `null` gets both types. 
3440 17 Jul 15 olle 1811    * @param orderDesc Boolean Flag indicating whether ordering should be descendant. Default is `false`.
3644 03 Dec 15 olle 1812    * @param anItemNotOnLibPlate Boolean Flag indicating that item list should be included if one item is without FPA child.
3719 22 Jan 16 olle 1813    * @param ignoreQpcrBranch Boolean Flag indicating that DNA extracts marked for qPCR should be ignored when checking if one item is without FPA child.
3440 17 Jul 15 olle 1814    * @return JSONArray A JSONArray with item lists.
3440 17 Jul 15 olle 1815    */
3719 22 Jan 16 olle 1816   private JSONArray fetchStartItemLists(DbControl dc, Boolean processed, Boolean orderDesc, Boolean anItemNotOnLibPlate, Boolean ignoreQpcrBranch)
3440 17 Jul 15 olle 1817   {    
3440 17 Jul 15 olle 1818     /*
3440 17 Jul 15 olle 1819       Find start item lists. Order by item list name.
3440 17 Jul 15 olle 1820      */
4150 03 Oct 16 olle 1821     int activeProjectId = dc.getSessionControl().getActiveProjectId();
4150 03 Oct 16 olle 1822     String prefix = Meludi.fetchStartListItemPrefix(activeProjectId);
3440 17 Jul 15 olle 1823     // Default ordering is ascending
3440 17 Jul 15 olle 1824     if (orderDesc == null)
3440 17 Jul 15 olle 1825     {
3440 17 Jul 15 olle 1826       orderDesc = false;
3440 17 Jul 15 olle 1827     }
3644 03 Dec 15 olle 1828     if (anItemNotOnLibPlate == null)
3644 03 Dec 15 olle 1829     {
3644 03 Dec 15 olle 1830       anItemNotOnLibPlate = false;
3644 03 Dec 15 olle 1831     }
3719 22 Jan 16 olle 1832     if (ignoreQpcrBranch == null)
3719 22 Jan 16 olle 1833     {
3719 22 Jan 16 olle 1834       ignoreQpcrBranch = false;
3719 22 Jan 16 olle 1835     }
3440 17 Jul 15 olle 1836     List<ItemList> startItemLists = null;
3440 17 Jul 15 olle 1837     if (processed == null)
3440 17 Jul 15 olle 1838     {
3440 17 Jul 15 olle 1839       startItemLists = StartList.findByPrefix(dc, 
3440 17 Jul 15 olle 1840           prefix, orderDesc);
3440 17 Jul 15 olle 1841     }
3440 17 Jul 15 olle 1842     else
3440 17 Jul 15 olle 1843     {
3440 17 Jul 15 olle 1844       // Use stored annotation snapshots for performance reasons
3440 17 Jul 15 olle 1845       SnapshotManager manager = new SnapshotManager();        
3440 17 Jul 15 olle 1846       startItemLists = StartList.findActiveByPrefix(dc, 
3440 17 Jul 15 olle 1847         manager, prefix, orderDesc);
3440 17 Jul 15 olle 1848     }
3440 17 Jul 15 olle 1849
3440 17 Jul 15 olle 1850     JSONArray jsonItemLists = new JSONArray();
3440 17 Jul 15 olle 1851
3440 17 Jul 15 olle 1852     for (ItemList itemList : startItemLists)
3440 17 Jul 15 olle 1853     {
3440 17 Jul 15 olle 1854       Date extractionDate = null;
3440 17 Jul 15 olle 1855       if (itemList != null)
3440 17 Jul 15 olle 1856       {
3440 17 Jul 15 olle 1857         // Check if item list should be included
3440 17 Jul 15 olle 1858         boolean includeItemList = false;
3440 17 Jul 15 olle 1859         if (processed == null)
3440 17 Jul 15 olle 1860         {
3440 17 Jul 15 olle 1861           includeItemList = true;
3440 17 Jul 15 olle 1862         }
3440 17 Jul 15 olle 1863         else
3440 17 Jul 15 olle 1864         {
3440 17 Jul 15 olle 1865           Boolean isProcessed = (Boolean) Annotationtype.SAMPLE_PREP_LIST_IS_PROCESSED.getAnnotationValue(dc, itemList);
3440 17 Jul 15 olle 1866           if (processed)
3440 17 Jul 15 olle 1867           {
3440 17 Jul 15 olle 1868             if (isProcessed != null && isProcessed)
3440 17 Jul 15 olle 1869             {
3440 17 Jul 15 olle 1870               includeItemList = true;
3440 17 Jul 15 olle 1871             }
3440 17 Jul 15 olle 1872           }
3440 17 Jul 15 olle 1873           else
3440 17 Jul 15 olle 1874           {
3440 17 Jul 15 olle 1875             if (isProcessed == null || !isProcessed)
3440 17 Jul 15 olle 1876             {
3440 17 Jul 15 olle 1877               includeItemList = true;
3440 17 Jul 15 olle 1878             }
3440 17 Jul 15 olle 1879           }
3440 17 Jul 15 olle 1880         }
3644 03 Dec 15 olle 1881         if (includeItemList && anItemNotOnLibPlate)
3644 03 Dec 15 olle 1882         {
3644 03 Dec 15 olle 1883           // Check if all DNA extract items in list have FPA child extracts
3644 03 Dec 15 olle 1884           boolean oneItemWithoutFpaChild = false;
3644 03 Dec 15 olle 1885           // Construct a query to get all member extract items in the item list, sorted alphabetically after name
3644 03 Dec 15 olle 1886           ItemQuery<Extract> query = (ItemQuery<Extract>)itemList.getMembers();
3644 03 Dec 15 olle 1887           query.include(Include.ALL);
3644 03 Dec 15 olle 1888           query.order(Orders.asc(Hql.property("name")));
3644 03 Dec 15 olle 1889           List<Extract> extractList = query.list(dc);
3644 03 Dec 15 olle 1890           for (int i = 0; i < extractList.size() && !oneItemWithoutFpaChild; i++)
3644 03 Dec 15 olle 1891           {
3644 03 Dec 15 olle 1892             Extract e = (Extract) extractList.get(i);
3719 22 Jan 16 olle 1893
3719 22 Jan 16 olle 1894             // Check if DNA extracts marked for qPCR processing should be ignored
3719 22 Jan 16 olle 1895             if (ignoreQpcrBranch)
3719 22 Jan 16 olle 1896             {
3719 22 Jan 16 olle 1897               // Ignore DNA extracts marked for qPCR processing
3719 22 Jan 16 olle 1898               Boolean markedForQPcr = (Boolean) Annotationtype.QPCR_BRANCH.getAnnotationValue(dc, e);
3719 22 Jan 16 olle 1899               if (markedForQPcr == null || !markedForQPcr)
3719 22 Jan 16 olle 1900               {
3719 22 Jan 16 olle 1901                 oneItemWithoutFpaChild = dnaHasNoFpaChild(dc, e);
3719 22 Jan 16 olle 1902               }
3719 22 Jan 16 olle 1903             }
3719 22 Jan 16 olle 1904             else
3719 22 Jan 16 olle 1905             {
3719 22 Jan 16 olle 1906               oneItemWithoutFpaChild = dnaHasNoFpaChild(dc, e);
3719 22 Jan 16 olle 1907             }
3644 03 Dec 15 olle 1908           }
3644 03 Dec 15 olle 1909           if (!oneItemWithoutFpaChild)
3644 03 Dec 15 olle 1910           {
3644 03 Dec 15 olle 1911             includeItemList = false;
3644 03 Dec 15 olle 1912           }
3644 03 Dec 15 olle 1913         }
3440 17 Jul 15 olle 1914         if (includeItemList)
3440 17 Jul 15 olle 1915         {
3440 17 Jul 15 olle 1916           extractionDate = (Date) Annotationtype.LIST_EXTRACTION_DATE.getAnnotationValue(dc, itemList);
3440 17 Jul 15 olle 1917           StartList sl = new StartList(itemList);
3440 17 Jul 15 olle 1918           List<String> itemNameList = sl.fetchExtractSourceNameListFromItemList(dc);
3440 17 Jul 15 olle 1919           JSONObject jsonItemList = sl.asJSONObject();
3440 17 Jul 15 olle 1920           jsonItemList.put("sourceItemNames", itemNameList);
3440 17 Jul 15 olle 1921           jsonItemList.put("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(extractionDate));
3440 17 Jul 15 olle 1922           // Get number of MeLuDI and extra material items in source extract item list
3440 17 Jul 15 olle 1923           int numMeludiItems = 0;
3440 17 Jul 15 olle 1924           int numExtraItems = 0;
3440 17 Jul 15 olle 1925           for (String itemName: itemNameList)
3440 17 Jul 15 olle 1926           {
4150 03 Oct 16 olle 1927             if (Meludi.itemIsMeludiItem(itemName, activeProjectId))
3440 17 Jul 15 olle 1928             {
3440 17 Jul 15 olle 1929               numMeludiItems++;
3440 17 Jul 15 olle 1930             }
3440 17 Jul 15 olle 1931             else
3440 17 Jul 15 olle 1932             {
3440 17 Jul 15 olle 1933               numExtraItems++;
3440 17 Jul 15 olle 1934             }
3440 17 Jul 15 olle 1935           }
3440 17 Jul 15 olle 1936           jsonItemList.put("numMeludiItems", numMeludiItems);
3440 17 Jul 15 olle 1937           jsonItemList.put("numExtraItems", numExtraItems);
3440 17 Jul 15 olle 1938           //
3440 17 Jul 15 olle 1939           jsonItemLists.add(jsonItemList);
3440 17 Jul 15 olle 1940         }
3440 17 Jul 15 olle 1941       }
3440 17 Jul 15 olle 1942     }
3440 17 Jul 15 olle 1943
3440 17 Jul 15 olle 1944     return jsonItemLists;
3440 17 Jul 15 olle 1945   }
3440 17 Jul 15 olle 1946
3644 03 Dec 15 olle 1947   /**
3644 03 Dec 15 olle 1948    * Checks whether an extract is of type DNA and has no FPA child items.
3644 03 Dec 15 olle 1949    * 
3644 03 Dec 15 olle 1950    * @param dc DbControl The DbControl to use.
3644 03 Dec 15 olle 1951    * @param e Extract The extract to check.
3644 03 Dec 15 olle 1952    * @return boolean Returns `true` if the extract is of type DNA and has no FPA child items, else false 
3644 03 Dec 15 olle 1953    */
3644 03 Dec 15 olle 1954   private boolean dnaHasNoFpaChild(DbControl dc, Extract e)
3644 03 Dec 15 olle 1955   {
3644 03 Dec 15 olle 1956     boolean hasNoFpaChild = false;
3644 03 Dec 15 olle 1957     if (e != null)
3644 03 Dec 15 olle 1958     {
3644 03 Dec 15 olle 1959       if (Meludi.itemIsDna(e.getName()))
3644 03 Dec 15 olle 1960       {
3644 03 Dec 15 olle 1961         ItemQuery<Extract> childQuery = e.getChildExtracts();
3644 03 Dec 15 olle 1962         List<Extract> childExtracts = childQuery.list(dc);
3644 03 Dec 15 olle 1963         if (childExtracts == null)
3644 03 Dec 15 olle 1964         {
3644 03 Dec 15 olle 1965           hasNoFpaChild = true;
3644 03 Dec 15 olle 1966         }
3644 03 Dec 15 olle 1967         else
3644 03 Dec 15 olle 1968         {
3644 03 Dec 15 olle 1969           // Search for FPA aliquot child extracts
3644 03 Dec 15 olle 1970           boolean fpaAliquotFound = false;
3644 03 Dec 15 olle 1971           for (int j = 0; j < childExtracts.size() && !fpaAliquotFound; j++)
3644 03 Dec 15 olle 1972           {
3644 03 Dec 15 olle 1973             Extract childExtract = (Extract) childExtracts.get(j);
3644 03 Dec 15 olle 1974             String childName = childExtract.getName();
3644 03 Dec 15 olle 1975             if (childName.endsWith(".fpa"))
3644 03 Dec 15 olle 1976             {
3644 03 Dec 15 olle 1977               fpaAliquotFound = true;
3644 03 Dec 15 olle 1978             }
3644 03 Dec 15 olle 1979           }
3644 03 Dec 15 olle 1980           if (!fpaAliquotFound)
3644 03 Dec 15 olle 1981           {
3644 03 Dec 15 olle 1982             hasNoFpaChild = true;
3644 03 Dec 15 olle 1983           }
3644 03 Dec 15 olle 1984         }
3644 03 Dec 15 olle 1985       }
3644 03 Dec 15 olle 1986     }
3644 03 Dec 15 olle 1987     return hasNoFpaChild;
3644 03 Dec 15 olle 1988   }
3644 03 Dec 15 olle 1989
3176 06 Mar 15 olle 1990   private String getPlateRowId(int row)
3176 06 Mar 15 olle 1991   {
3176 06 Mar 15 olle 1992     String[] idArr = {"A","B","C","D","E","F","G","H"};
3176 06 Mar 15 olle 1993     String rowId = null;
3176 06 Mar 15 olle 1994     if (row >= 0 && row < 8)
3176 06 Mar 15 olle 1995     {
3176 06 Mar 15 olle 1996       rowId = idArr[row];
3176 06 Mar 15 olle 1997     }
3176 06 Mar 15 olle 1998     return rowId;
3176 06 Mar 15 olle 1999   }
3176 06 Mar 15 olle 2000
3318 08 May 15 olle 2001   private void loadSpecimenInfo(DbControl dc, SnapshotManager manager, SpecimenTube sp)
3225 02 Apr 15 olle 2002   {
3225 02 Apr 15 olle 2003     Sample s = sp.getSample();
3225 02 Apr 15 olle 2004     BioMaterialEvent created = s.getCreationEvent();
3225 02 Apr 15 olle 2005     
3225 02 Apr 15 olle 2006     sp.loadBioPlateLocation();
3225 02 Apr 15 olle 2007     sp.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
3225 02 Apr 15 olle 2008     sp.setAnnotation("editable", s.hasPermission(Permission.WRITE));
3318 08 May 15 olle 2009     sp.setAnnotation("specimenType", fetchSpecimenType(dc, manager, s));
3225 02 Apr 15 olle 2010     Float usedQuantity = null;
3225 02 Apr 15 olle 2011     if (s.getParent() instanceof Sample)
3225 02 Apr 15 olle 2012     {
3225 02 Apr 15 olle 2013       usedQuantity = created.getUsedQuantity(s);
3225 02 Apr 15 olle 2014     }
3225 02 Apr 15 olle 2015     sp.setAnnotation("usedQuantity", usedQuantity);
3225 02 Apr 15 olle 2016     sp.setAnnotation("originalQuantity", s.getOriginalQuantity());
3225 02 Apr 15 olle 2017     sp.setAnnotation("remainingQuantity", s.getRemainingQuantity());
3225 02 Apr 15 olle 2018     sp.setAnnotation("description", s.getDescription());
3225 02 Apr 15 olle 2019
3225 02 Apr 15 olle 2020     sp.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3225 02 Apr 15 olle 2021     sp.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3225 02 Apr 15 olle 2022     sp.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT, null);
3225 02 Apr 15 olle 2023     sp.loadAnnotations(dc, "otherPathNote", Annotationtype.OTHER_PATH_NOTE, null);
3225 02 Apr 15 olle 2024     sp.loadAnnotations(dc, "viableTumourCellsPercent", Annotationtype.VIABLE_TUMOUR_CELLS_PERCENT, null);
3225 02 Apr 15 olle 2025     sp.loadAnnotations(dc, "nofSections", Annotationtype.NOF_SECTIONS, null);
3225 02 Apr 15 olle 2026   }
3225 02 Apr 15 olle 2027
3318 08 May 15 olle 2028   private void loadRnaInfo(DbControl dc, SnapshotManager manager, Rna rna)
3225 02 Apr 15 olle 2029   {
3225 02 Apr 15 olle 2030     Extract e = rna.getExtract();
3225 02 Apr 15 olle 2031     BioMaterialEvent created = e.getCreationEvent();
3225 02 Apr 15 olle 2032     
3225 02 Apr 15 olle 2033     rna.loadBioPlateLocation();
3225 02 Apr 15 olle 2034     rna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
3225 02 Apr 15 olle 2035     rna.setAnnotation("editable", e.hasPermission(Permission.WRITE));
3318 08 May 15 olle 2036     rna.setAnnotation("specimenType", fetchSpecimenTypeExtract(dc, manager, e));
3225 02 Apr 15 olle 2037     Float usedQuantity = null;
3225 02 Apr 15 olle 2038     if (e.getParent() instanceof Sample)
3225 02 Apr 15 olle 2039     {
3225 02 Apr 15 olle 2040       usedQuantity = created.getUsedQuantity((Sample)e.getParent());
3225 02 Apr 15 olle 2041     }
3225 02 Apr 15 olle 2042     else if (e.getParent() instanceof Extract)
3225 02 Apr 15 olle 2043     {
3225 02 Apr 15 olle 2044       usedQuantity = created.getUsedQuantity((Extract)e.getParent());
3225 02 Apr 15 olle 2045     }
3225 02 Apr 15 olle 2046     rna.setAnnotation("usedQuantity", usedQuantity);
3225 02 Apr 15 olle 2047     rna.setAnnotation("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
3225 02 Apr 15 olle 2048     rna.setAnnotation("extractionProtocol", JsonUtil.getProtocolAsJSON(created.getProtocol()));
3225 02 Apr 15 olle 2049     rna.setAnnotation("originalQuantity", e.getOriginalQuantity());
3225 02 Apr 15 olle 2050     rna.setAnnotation("remainingQuantity", e.getRemainingQuantity());
3225 02 Apr 15 olle 2051     rna.setAnnotation("description", e.getDescription());
3225 02 Apr 15 olle 2052
3318 08 May 15 olle 2053     rna.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE_EXTRACT, Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 2054     rna.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE_EXTRACT, Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 2055     rna.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT_EXTRACT, null);
3318 08 May 15 olle 2056     rna.loadAnnotations(dc, "viableTumourCellsPercent", Annotationtype.VIABLE_TUMOUR_CELLS_PERCENT_EXTRACT, null);
3318 08 May 15 olle 2057
3225 02 Apr 15 olle 2058     rna.loadAnnotations(dc, "isolationDate", Annotationtype.ISOLATION_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3501 21 Sep 15 olle 2059     rna.loadAnnotations(dc, "extractionOperator", Annotationtype.EXTRACTION_OPERATOR, null);
3225 02 Apr 15 olle 2060     rna.loadAnnotations(dc, "qiacubeDate", Annotationtype.QIACUBE_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3501 21 Sep 15 olle 2061     rna.loadAnnotations(dc, "qiacubeOperator", Annotationtype.QIACUBE_OPERATOR, null);
3225 02 Apr 15 olle 2062     rna.loadAnnotations(dc, "qiacubeInstrumentPrefix", Annotationtype.QIACUBE_INSTRUMENT_PREFIX, null);
3225 02 Apr 15 olle 2063     rna.loadAnnotations(dc, "qiacubePosition", Annotationtype.QIACUBE_POSITION, null);
3225 02 Apr 15 olle 2064     rna.loadAnnotations(dc, "qiacubeRunNo", Annotationtype.QIACUBE_RUN_NO, null);
3225 02 Apr 15 olle 2065     rna.loadAnnotations(dc, "ndConc", Annotationtype.ND_CONC, null);
3225 02 Apr 15 olle 2066     rna.loadAnnotations(dc, "nd260by230", Annotationtype.ND_260_BY_230, null);
3225 02 Apr 15 olle 2067     rna.loadAnnotations(dc, "nd260by280", Annotationtype.ND_260_BY_280, null);
3225 02 Apr 15 olle 2068     rna.loadAnnotations(dc, "qubitConc", Annotationtype.QUBIT_CONC, null);
3225 02 Apr 15 olle 2069     rna.loadAnnotations(dc, "deltaCt", Annotationtype.DELTA_CT, null);
3225 02 Apr 15 olle 2070     // Specimen reagents
3225 02 Apr 15 olle 2071     rna.loadAnnotations(dc, "xyleneSpecimen", Annotationtype.XYLENE_SPECIMEN, null);
3225 02 Apr 15 olle 2072     rna.loadAnnotations(dc, "etoh995Specimen", Annotationtype.ETOH995_SPECIMEN, null);
3225 02 Apr 15 olle 2073     rna.loadAnnotations(dc, "bufferPkdSpecimen", Annotationtype.BUFFER_PKD_SPECIMEN, null);
3225 02 Apr 15 olle 2074     rna.loadAnnotations(dc, "bufferAtlSpecimen", Annotationtype.BUFFER_ATL_SPECIMEN, null);
3225 02 Apr 15 olle 2075     rna.loadAnnotations(dc, "proteinaseKSpecimen", Annotationtype.PROTEINASE_K_SPECIMEN, null);
3225 02 Apr 15 olle 2076     rna.loadAnnotations(dc, "rnaseASpecimen", Annotationtype.RNASE_A_SPECIMEN, null);
3225 02 Apr 15 olle 2077     // QIAcube reagents
3225 02 Apr 15 olle 2078     rna.loadAnnotations(dc, "allPrepFfpeKit", Annotationtype.ALLPREP_FFPE_KIT, null);
3225 02 Apr 15 olle 2079     rna.loadAnnotations(dc, "etoh995", Annotationtype.ETOH995, null);
3225 02 Apr 15 olle 2080     rna.loadAnnotations(dc, "bufferRlt", Annotationtype.BUFFER_RLT, null);
3225 02 Apr 15 olle 2081     rna.loadAnnotations(dc, "bufferFrn", Annotationtype.BUFFER_FRN, null);
3225 02 Apr 15 olle 2082     rna.loadAnnotations(dc, "bufferRpe", Annotationtype.BUFFER_RPE, null);
3225 02 Apr 15 olle 2083     rna.loadAnnotations(dc, "bufferAl", Annotationtype.BUFFER_AL, null);
3225 02 Apr 15 olle 2084     rna.loadAnnotations(dc, "bufferAw1", Annotationtype.BUFFER_AW1, null);
3225 02 Apr 15 olle 2085     rna.loadAnnotations(dc, "bufferAw2", Annotationtype.BUFFER_AW2, null);
3225 02 Apr 15 olle 2086     rna.loadAnnotations(dc, "bufferAte", Annotationtype.BUFFER_ATE, null);
3225 02 Apr 15 olle 2087     rna.loadAnnotations(dc, "bufferRdd", Annotationtype.BUFFER_RDD, null);
3225 02 Apr 15 olle 2088     rna.loadAnnotations(dc, "rnaseFreeWater", Annotationtype.RNASE_FREE_WATER, null);
3225 02 Apr 15 olle 2089     rna.loadAnnotations(dc, "dnaseMix", Annotationtype.DNASE_MIX, null);
3225 02 Apr 15 olle 2090     rna.loadAnnotations(dc, "rneasyMinEluteSpinColumn", Annotationtype.RNEASY_MIN_ELUTE_SPIN_COL, null);
3324 11 May 15 olle 2091     rna.loadAnnotations(dc, "qiaampMinEluteSpinColumn", Annotationtype.QIAAMP_MIN_ELUTE_SPIN_COL, null);
3324 11 May 15 olle 2092     rna.loadAnnotations(dc, "kapaSybrFast", Annotationtype.KAPA_SYBR_FAST, null);
3324 11 May 15 olle 2093     rna.loadAnnotations(dc, "qualityControlPrimersQcpRgt", Annotationtype.QUALITY_CONTROL_PRIMERS_QCP_RGT, null);
3438 03 Jul 15 olle 2094     rna.loadAnnotations(dc, "qualityControlTemplateQct", Annotationtype.QUALITY_CONTROL_TEMPLATE_QCT, null);
3324 11 May 15 olle 2095     rna.loadAnnotations(dc, "qubitDnaHighSens", Annotationtype.QUBIT_DNA_HIGH_SENS, null);
3324 11 May 15 olle 2096     rna.loadAnnotations(dc, "qubitDnaBroadRange", Annotationtype.QUBIT_DNA_BROAD_RANGE, null);
3225 02 Apr 15 olle 2097   }
3225 02 Apr 15 olle 2098
3318 08 May 15 olle 2099   private void loadDnaInfo(DbControl dc, SnapshotManager manager, Dna dna)
3176 06 Mar 15 olle 2100   {
3176 06 Mar 15 olle 2101     Extract e = dna.getExtract();
3176 06 Mar 15 olle 2102     BioMaterialEvent created = e.getCreationEvent();
3176 06 Mar 15 olle 2103     
3176 06 Mar 15 olle 2104     dna.loadBioPlateLocation();
3176 06 Mar 15 olle 2105     dna.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(e.getEntryDate()));
3176 06 Mar 15 olle 2106     dna.setAnnotation("editable", e.hasPermission(Permission.WRITE));
3318 08 May 15 olle 2107     dna.setAnnotation("specimenType", fetchSpecimenTypeExtract(dc, manager, e));
3176 06 Mar 15 olle 2108     Float usedQuantity = null;
3176 06 Mar 15 olle 2109     if (e.getParent() instanceof Sample)
3176 06 Mar 15 olle 2110     {
3176 06 Mar 15 olle 2111       usedQuantity = created.getUsedQuantity((Sample)e.getParent());
3176 06 Mar 15 olle 2112     }
3176 06 Mar 15 olle 2113     else if (e.getParent() instanceof Extract)
3176 06 Mar 15 olle 2114     {
3176 06 Mar 15 olle 2115       usedQuantity = created.getUsedQuantity((Extract)e.getParent());
3176 06 Mar 15 olle 2116     }
3176 06 Mar 15 olle 2117     dna.setAnnotation("usedQuantity", usedQuantity);
3176 06 Mar 15 olle 2118     dna.setAnnotation("extractionDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
3176 06 Mar 15 olle 2119     dna.setAnnotation("extractionProtocol", JsonUtil.getProtocolAsJSON(created.getProtocol()));
3176 06 Mar 15 olle 2120     dna.setAnnotation("originalQuantity", e.getOriginalQuantity());
3176 06 Mar 15 olle 2121     dna.setAnnotation("remainingQuantity", e.getRemainingQuantity());
3176 06 Mar 15 olle 2122     dna.setAnnotation("description", e.getDescription());
3176 06 Mar 15 olle 2123
3318 08 May 15 olle 2124     dna.loadAnnotations(dc, "arrivalDate", Annotationtype.ARRIVAL_DATE_EXTRACT, Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 2125     dna.loadAnnotations(dc, "samplingDate", Annotationtype.SAMPLING_DATE_EXTRACT, Meludi.CONVERTER_DATE_TO_STRING);
3318 08 May 15 olle 2126     dna.loadAnnotations(dc, "operatorDeliveryComment", Annotationtype.OPERATOR_DELIVERY_COMMENT_EXTRACT, null);
3318 08 May 15 olle 2127     dna.loadAnnotations(dc, "viableTumourCellsPercent", Annotationtype.VIABLE_TUMOUR_CELLS_PERCENT_EXTRACT, null);
3318 08 May 15 olle 2128
3225 02 Apr 15 olle 2129     dna.loadAnnotations(dc, "isolationDate", Annotationtype.ISOLATION_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3501 21 Sep 15 olle 2130     dna.loadAnnotations(dc, "extractionOperator", Annotationtype.EXTRACTION_OPERATOR, null);
3225 02 Apr 15 olle 2131     dna.loadAnnotations(dc, "qiacubeDate", Annotationtype.QIACUBE_DATE, Meludi.CONVERTER_DATE_TO_STRING);
3501 21 Sep 15 olle 2132     dna.loadAnnotations(dc, "qiacubeOperator", Annotationtype.QIACUBE_OPERATOR, null);
3225 02 Apr 15 olle 2133     dna.loadAnnotations(dc, "qiacubeInstrumentPrefix", Annotationtype.QIACUBE_INSTRUMENT_PREFIX, null);
3225 02 Apr 15 olle 2134     dna.loadAnnotations(dc, "qiacubePosition", Annotationtype.QIACUBE_POSITION, null);
3225 02 Apr 15 olle 2135     dna.loadAnnotations(dc, "qiacubeRunNo", Annotationtype.QIACUBE_RUN_NO, null);
3225 02 Apr 15 olle 2136     dna.loadAnnotations(dc, "ndConc", Annotationtype.ND_CONC, null);
3225 02 Apr 15 olle 2137     dna.loadAnnotations(dc, "nd260by230", Annotationtype.ND_260_BY_230, null);
3225 02 Apr 15 olle 2138     dna.loadAnnotations(dc, "nd260by280", Annotationtype.ND_260_BY_280, null);
3176 06 Mar 15 olle 2139     dna.loadAnnotations(dc, "qubitConc", Annotationtype.QUBIT_CONC, null);
3176 06 Mar 15 olle 2140     dna.loadAnnotations(dc, "deltaCt", Annotationtype.DELTA_CT, null);
3176 06 Mar 15 olle 2141     // Specimen reagents
3176 06 Mar 15 olle 2142     dna.loadAnnotations(dc, "xyleneSpecimen", Annotationtype.XYLENE_SPECIMEN, null);
3176 06 Mar 15 olle 2143     dna.loadAnnotations(dc, "etoh995Specimen", Annotationtype.ETOH995_SPECIMEN, null);
3176 06 Mar 15 olle 2144     dna.loadAnnotations(dc, "bufferPkdSpecimen", Annotationtype.BUFFER_PKD_SPECIMEN, null);
3176 06 Mar 15 olle 2145     dna.loadAnnotations(dc, "bufferAtlSpecimen", Annotationtype.BUFFER_ATL_SPECIMEN, null);
3176 06 Mar 15 olle 2146     dna.loadAnnotations(dc, "proteinaseKSpecimen", Annotationtype.PROTEINASE_K_SPECIMEN, null);
3176 06 Mar 15 olle 2147     dna.loadAnnotations(dc, "rnaseASpecimen", Annotationtype.RNASE_A_SPECIMEN, null);
3176 06 Mar 15 olle 2148     // QIAcube reagents
3176 06 Mar 15 olle 2149     dna.loadAnnotations(dc, "allPrepFfpeKit", Annotationtype.ALLPREP_FFPE_KIT, null);
3176 06 Mar 15 olle 2150     dna.loadAnnotations(dc, "etoh995", Annotationtype.ETOH995, null);
3176 06 Mar 15 olle 2151     dna.loadAnnotations(dc, "bufferRlt", Annotationtype.BUFFER_RLT, null);
3176 06 Mar 15 olle 2152     dna.loadAnnotations(dc, "bufferFrn", Annotationtype.BUFFER_FRN, null);
3176 06 Mar 15 olle 2153     dna.loadAnnotations(dc, "bufferRpe", Annotationtype.BUFFER_RPE, null);
3176 06 Mar 15 olle 2154     dna.loadAnnotations(dc, "bufferAl", Annotationtype.BUFFER_AL, null);
3176 06 Mar 15 olle 2155     dna.loadAnnotations(dc, "bufferAw1", Annotationtype.BUFFER_AW1, null);
3176 06 Mar 15 olle 2156     dna.loadAnnotations(dc, "bufferAw2", Annotationtype.BUFFER_AW2, null);
3176 06 Mar 15 olle 2157     dna.loadAnnotations(dc, "bufferAte", Annotationtype.BUFFER_ATE, null);
3176 06 Mar 15 olle 2158     dna.loadAnnotations(dc, "bufferRdd", Annotationtype.BUFFER_RDD, null);
3176 06 Mar 15 olle 2159     dna.loadAnnotations(dc, "rnaseFreeWater", Annotationtype.RNASE_FREE_WATER, null);
3176 06 Mar 15 olle 2160     dna.loadAnnotations(dc, "dnaseMix", Annotationtype.DNASE_MIX, null);
3176 06 Mar 15 olle 2161     dna.loadAnnotations(dc, "rneasyMinEluteSpinColumn", Annotationtype.RNEASY_MIN_ELUTE_SPIN_COL, null);
3324 11 May 15 olle 2162     dna.loadAnnotations(dc, "qiaampMinEluteSpinColumn", Annotationtype.QIAAMP_MIN_ELUTE_SPIN_COL, null);
3324 11 May 15 olle 2163     dna.loadAnnotations(dc, "kapaSybrFast", Annotationtype.KAPA_SYBR_FAST, null);
3324 11 May 15 olle 2164     dna.loadAnnotations(dc, "qualityControlPrimersQcpRgt", Annotationtype.QUALITY_CONTROL_PRIMERS_QCP_RGT, null);
3438 03 Jul 15 olle 2165     dna.loadAnnotations(dc, "qualityControlTemplateQct", Annotationtype.QUALITY_CONTROL_TEMPLATE_QCT, null);
3324 11 May 15 olle 2166     dna.loadAnnotations(dc, "qubitDnaHighSens", Annotationtype.QUBIT_DNA_HIGH_SENS, null);
3324 11 May 15 olle 2167     dna.loadAnnotations(dc, "qubitDnaBroadRange", Annotationtype.QUBIT_DNA_BROAD_RANGE, null);
3719 22 Jan 16 olle 2168     // qPCR branch
3719 22 Jan 16 olle 2169     dna.loadAnnotations(dc, "qpcrBranch", Annotationtype.QPCR_BRANCH, null);
3176 06 Mar 15 olle 2170   }
3176 06 Mar 15 olle 2171
3176 06 Mar 15 olle 2172   @SuppressWarnings("unchecked")
3176 06 Mar 15 olle 2173   @Override
3176 06 Mar 15 olle 2174   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
3176 06 Mar 15 olle 2175     throws ServletException, IOException 
3176 06 Mar 15 olle 2176   {
3176 06 Mar 15 olle 2177     String ID = req.getParameter("ID");
3176 06 Mar 15 olle 2178     String cmd = req.getParameter("cmd");
3176 06 Mar 15 olle 2179     JsonUtil.setJsonResponseHeaders(resp);
3176 06 Mar 15 olle 2180     
3176 06 Mar 15 olle 2181     JSONObject json = new JSONObject();
3176 06 Mar 15 olle 2182     json.put("status", "ok");
3176 06 Mar 15 olle 2183     
3176 06 Mar 15 olle 2184     JSONArray jsonMessages = new JSONArray();
3176 06 Mar 15 olle 2185   
5468 04 Jun 19 olle 2186     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 2187     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 2188     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
3176 06 Mar 15 olle 2189     DbControl dc = null;
3176 06 Mar 15 olle 2190     try
3176 06 Mar 15 olle 2191     {
3180 17 Mar 15 olle 2192       if ("RegisterStartPlate".equals(cmd))
3176 06 Mar 15 olle 2193       {
3176 06 Mar 15 olle 2194         dc = sc.newDbControl();
3180 17 Mar 15 olle 2195
3180 17 Mar 15 olle 2196         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3180 17 Mar 15 olle 2197
3180 17 Mar 15 olle 2198         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3180 17 Mar 15 olle 2199         String plateName = (String)jsonReq.get("startPlateName");
3180 17 Mar 15 olle 2200
3180 17 Mar 15 olle 2201         // Get parameter values
3180 17 Mar 15 olle 2202         Date isolationDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("isolationDate"));
3180 17 Mar 15 olle 2203         Number extractionProtocolId = (Number)jsonReq.get("extractionProtocol");
3180 17 Mar 15 olle 2204         Protocol extractionProtocol = extractionProtocolId != null ? Protocol.getById(dc, extractionProtocolId.intValue()) : null;
3180 17 Mar 15 olle 2205         String extractionProtocolName = (String)jsonReq.get("extractionProtocolName");
3180 17 Mar 15 olle 2206         Number numExtraQiacubeItems = (Number)jsonReq.get("numExtraQiacubeItems");
3180 17 Mar 15 olle 2207         Date qiacubeDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("qiacubeDate"));
3180 17 Mar 15 olle 2208         String qiacubePrimaryPrefix = (String)jsonReq.get("qiacubePrimaryPrefix");
3180 17 Mar 15 olle 2209         String qiacubeSecondaryPrefix = (String)jsonReq.get("qiacubeSecondaryPrefix");
3180 17 Mar 15 olle 2210         Number qiacubeRunNo = (Number)jsonReq.get("qiacubeRunNo");
3180 17 Mar 15 olle 2211         String qiacubeOperator = (String)jsonReq.get("qiacubeOperator");
3180 17 Mar 15 olle 2212         String allPrepFfpeKit = Values.getStringOrNull((String)jsonReq.get("allPrepFfpeKit"));
3176 06 Mar 15 olle 2213         
3180 17 Mar 15 olle 2214         // Get list of extract source item names
3180 17 Mar 15 olle 2215         List<String> itemNameList = new ArrayList<String>();
3180 17 Mar 15 olle 2216         JSONArray jsonItems = (JSONArray)jsonReq.get("items");
3180 17 Mar 15 olle 2217         for (int i = 0; i < jsonItems.size(); ++i)
3180 17 Mar 15 olle 2218         {
3180 17 Mar 15 olle 2219           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3180 17 Mar 15 olle 2220           String itemName = (String)jsonItem.get("name");
3180 17 Mar 15 olle 2221           itemNameList.add(itemName);
3180 17 Mar 15 olle 2222         }
3180 17 Mar 15 olle 2223
3245 14 Apr 15 olle 2224         // Get list of extra QIAcube item names
3245 14 Apr 15 olle 2225         List<String> extraItemNameList = new ArrayList<String>();
3245 14 Apr 15 olle 2226         JSONArray jsonExtraItems = (JSONArray)jsonReq.get("extraItems");
3245 14 Apr 15 olle 2227         for (int i = 0; i < jsonExtraItems.size(); ++i)
3245 14 Apr 15 olle 2228         {
3245 14 Apr 15 olle 2229           JSONObject jsonItem = (JSONObject)jsonExtraItems.get(i);
3245 14 Apr 15 olle 2230           String itemName = (String)jsonItem.get("name");
3245 14 Apr 15 olle 2231           extraItemNameList.add(itemName);
3245 14 Apr 15 olle 2232         }
3245 14 Apr 15 olle 2233
3180 17 Mar 15 olle 2234         // Create start DNA reaction plate
3180 17 Mar 15 olle 2235         JSONArray jsonCreatedDnaPlate = new JSONArray();
3180 17 Mar 15 olle 2236         if (plateName != null && !plateName.equals(""))
3180 17 Mar 15 olle 2237         {
3180 17 Mar 15 olle 2238           PlateGeometry geometry = BioplateType.DNA.getPlateGeometry(dc);
3180 17 Mar 15 olle 2239           BioPlateType plateType = BioplateType.DNA.load(dc);
3180 17 Mar 15 olle 2240
3180 17 Mar 15 olle 2241           BioPlate dnaPlate = BioPlate.getNew(dc, geometry, plateType);
3180 17 Mar 15 olle 2242           dnaPlate.setName(plateName);
3180 17 Mar 15 olle 2243           //dnaPlate.setDescription(Values.getStringOrNull((String)jsonPlate.get("comments")));
3180 17 Mar 15 olle 2244           //Annotationtype.POOL_SCHEMA.setAnnotationValue(dc, dnaPlate, jsonPlate.get("poolSchema"));
3180 17 Mar 15 olle 2245           jsonMessages.add("Created '" + dnaPlate.getName() + "' DNA reaction plate");
3233 09 Apr 15 olle 2246           //Annotationtype.SOURCE_ITEM_NAMES.setAnnotationValues(dc, dnaPlate, itemNameList);
3233 09 Apr 15 olle 2247           Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING.setAnnotationValues(dc, dnaPlate, itemNameList);
3180 17 Mar 15 olle 2248           Annotationtype.BATCH_NUMBER_EXTRA_QIACUBE_ITEMS.setAnnotationValue(dc, dnaPlate, numExtraQiacubeItems);
3245 14 Apr 15 olle 2249           Annotationtype.EXTRA_QIACUBE_ITEM_NAMES_LIST_STRING.setAnnotationValues(dc, dnaPlate, extraItemNameList);
3180 17 Mar 15 olle 2250           Annotationtype.BATCH_ISOLATION_DATE.setAnnotationValue(dc, dnaPlate, isolationDate);
3180 17 Mar 15 olle 2251           Annotationtype.BATCH_EXTRACTION_PROTOCOL_ID.setAnnotationValue(dc, dnaPlate, extractionProtocolId);
3180 17 Mar 15 olle 2252           Annotationtype.BATCH_EXTRACTION_PROTOCOL_NAME.setAnnotationValue(dc, dnaPlate, extractionProtocolName);
3180 17 Mar 15 olle 2253           Annotationtype.BATCH_QIACUBE_DATE.setAnnotationValue(dc, dnaPlate, qiacubeDate);
3180 17 Mar 15 olle 2254           Annotationtype.BATCH_QIACUBE_PRIMARY_PREFIX.setAnnotationValue(dc, dnaPlate, qiacubePrimaryPrefix);
3180 17 Mar 15 olle 2255           Annotationtype.BATCH_QIACUBE_SECONDARY_PREFIX.setAnnotationValue(dc, dnaPlate, qiacubeSecondaryPrefix);
3180 17 Mar 15 olle 2256           Annotationtype.BATCH_QIACUBE_RUN_NO.setAnnotationValue(dc, dnaPlate, qiacubeRunNo);
3180 17 Mar 15 olle 2257           Annotationtype.BATCH_QIACUBE_OPERATOR.setAnnotationValue(dc, dnaPlate, qiacubeOperator);
3180 17 Mar 15 olle 2258           Annotationtype.BATCH_ALLPREP_FFPE_KIT.setAnnotationValue(dc, dnaPlate, allPrepFfpeKit);
3180 17 Mar 15 olle 2259           dc.saveItem(dnaPlate);
3180 17 Mar 15 olle 2260           jsonCreatedDnaPlate.add(dnaPlate.getId());
3180 17 Mar 15 olle 2261         }
3180 17 Mar 15 olle 2262
3180 17 Mar 15 olle 2263         // Set original quantity for extract source item to 0,0, in order to mark them as processed
3180 17 Mar 15 olle 2264         for (int i = 0; i < jsonItems.size(); ++i)
3180 17 Mar 15 olle 2265         {
3180 17 Mar 15 olle 2266           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3180 17 Mar 15 olle 2267           String itemName = (String)jsonItem.get("name");
3180 17 Mar 15 olle 2268           Number itemId = (Number)jsonItem.get("id");
3180 17 Mar 15 olle 2269           Sample specimen = null;
3180 17 Mar 15 olle 2270           Extract dna = null;
3180 17 Mar 15 olle 2271           Extract rna = null;
3180 17 Mar 15 olle 2272
3433 29 Jun 15 olle 2273           if (Meludi.itemIsSpecimen(itemName))
3180 17 Mar 15 olle 2274           {
3180 17 Mar 15 olle 2275             specimen = Sample.getById(dc, itemId.intValue());
3180 17 Mar 15 olle 2276             specimen.setItemSubtype(Subtype.SPECIMEN.load(dc));
3180 17 Mar 15 olle 2277             specimen.setOriginalQuantity(0.0f);
3180 17 Mar 15 olle 2278           }
3180 17 Mar 15 olle 2279
3180 17 Mar 15 olle 2280           // DNA
3433 29 Jun 15 olle 2281           if (Meludi.itemIsDna(itemName))
3180 17 Mar 15 olle 2282           {
3180 17 Mar 15 olle 2283             JSONObject jsonDna = (JSONObject)jsonItem.get("dna");
3180 17 Mar 15 olle 2284             dna = Extract.getById(dc, itemId.intValue());
3180 17 Mar 15 olle 2285             dna.setItemSubtype(Subtype.DNA.load(dc));
3180 17 Mar 15 olle 2286             dna.setOriginalQuantity(0.0f);
3180 17 Mar 15 olle 2287           }
3180 17 Mar 15 olle 2288           
3180 17 Mar 15 olle 2289           // RNA
3433 29 Jun 15 olle 2290           if (Meludi.itemIsRna(itemName))
3180 17 Mar 15 olle 2291           {
3180 17 Mar 15 olle 2292             JSONObject jsonRna = (JSONObject)jsonItem.get("rna");
3180 17 Mar 15 olle 2293             rna = Extract.getById(dc, itemId.intValue());
3180 17 Mar 15 olle 2294             rna.setItemSubtype(Subtype.RNA.load(dc));
3180 17 Mar 15 olle 2295             rna.setOriginalQuantity(0.0f);
3180 17 Mar 15 olle 2296           }
3180 17 Mar 15 olle 2297           
3180 17 Mar 15 olle 2298           String msg = "";
3433 29 Jun 15 olle 2299           if (Meludi.itemIsSpecimen(itemName))
3180 17 Mar 15 olle 2300           {
3180 17 Mar 15 olle 2301             msg += "Original quantity set to 0.0 for specimen " + itemName;
3180 17 Mar 15 olle 2302           }
3433 29 Jun 15 olle 2303           else if (Meludi.itemIsRna(itemName))
3180 17 Mar 15 olle 2304           {
3180 17 Mar 15 olle 2305             msg += "Original quantity set to 0.0 for RNA " + itemName;
3180 17 Mar 15 olle 2306           }
3433 29 Jun 15 olle 2307           else if (Meludi.itemIsDna(itemName))
3180 17 Mar 15 olle 2308           {
3180 17 Mar 15 olle 2309             msg += "Original quantity set to 0.0 for DNA " + itemName;
3180 17 Mar 15 olle 2310           }
3180 17 Mar 15 olle 2311
3180 17 Mar 15 olle 2312           jsonMessages.add(msg);
3180 17 Mar 15 olle 2313         }
3180 17 Mar 15 olle 2314         dc.commit();
3180 17 Mar 15 olle 2315
3180 17 Mar 15 olle 2316         json.put("createdDnaPlate", jsonCreatedDnaPlate);
3180 17 Mar 15 olle 2317       }
3432 29 Jun 15 olle 2318       else if ("RegisterStartItemList".equals(cmd))
3432 29 Jun 15 olle 2319       {
3432 29 Jun 15 olle 2320         dc = sc.newDbControl();
3432 29 Jun 15 olle 2321
3432 29 Jun 15 olle 2322         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3432 29 Jun 15 olle 2323
3432 29 Jun 15 olle 2324         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3432 29 Jun 15 olle 2325         String itemListName = (String)jsonReq.get("startListName");
3432 29 Jun 15 olle 2326
3432 29 Jun 15 olle 2327         // Get parameter values
3432 29 Jun 15 olle 2328         Date isolationDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("isolationDate"));
3501 21 Sep 15 olle 2329         String extractionOperator = (String)jsonReq.get("extractionOperator");
3432 29 Jun 15 olle 2330         Number extractionProtocolId = (Number)jsonReq.get("extractionProtocol");
3432 29 Jun 15 olle 2331         Protocol extractionProtocol = extractionProtocolId != null ? Protocol.getById(dc, extractionProtocolId.intValue()) : null;
3432 29 Jun 15 olle 2332         String extractionProtocolName = (String)jsonReq.get("extractionProtocolName");
3432 29 Jun 15 olle 2333         Number numExtraQiacubeItems = (Number)jsonReq.get("numExtraQiacubeItems");
3432 29 Jun 15 olle 2334         Date qiacubeDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("qiacubeDate"));
3501 21 Sep 15 olle 2335         String qiacubeOperator = (String)jsonReq.get("qiacubeOperator");
3432 29 Jun 15 olle 2336         String qiacubePrimaryPrefix = (String)jsonReq.get("qiacubePrimaryPrefix");
3432 29 Jun 15 olle 2337         String qiacubeSecondaryPrefix = (String)jsonReq.get("qiacubeSecondaryPrefix");
3432 29 Jun 15 olle 2338         Number qiacubeRunNo = (Number)jsonReq.get("qiacubeRunNo");
3501 21 Sep 15 olle 2339
3501 21 Sep 15 olle 2340         // Specimen reagents
3501 21 Sep 15 olle 2341         String xyleneSpecimen = Values.getStringOrNull((String)jsonReq.get("xyleneSpecimen"));
3501 21 Sep 15 olle 2342         String etoh995Specimen = Values.getStringOrNull((String)jsonReq.get("etoh995Specimen"));
3432 29 Jun 15 olle 2343         String allPrepFfpeKit = Values.getStringOrNull((String)jsonReq.get("allPrepFfpeKit"));
3501 21 Sep 15 olle 2344         String bufferPkdSpecimen = Values.getStringOrNull((String)jsonReq.get("bufferPkdSpecimen"));
3501 21 Sep 15 olle 2345         String bufferAtlSpecimen = Values.getStringOrNull((String)jsonReq.get("bufferAtlSpecimen"));
3501 21 Sep 15 olle 2346         String proteinaseKSpecimen = Values.getStringOrNull((String)jsonReq.get("proteinaseKSpecimen"));
3501 21 Sep 15 olle 2347         String rnaseASpecimen = Values.getStringOrNull((String)jsonReq.get("rnaseASpecimen"));
3501 21 Sep 15 olle 2348
3501 21 Sep 15 olle 2349         // QIAcube reagents
3501 21 Sep 15 olle 2350         String etoh995 = Values.getStringOrNull((String)jsonReq.get("etoh995"));
3501 21 Sep 15 olle 2351         String bufferRlt = Values.getStringOrNull((String)jsonReq.get("bufferRlt"));
3501 21 Sep 15 olle 2352         String bufferFrn = Values.getStringOrNull((String)jsonReq.get("bufferFrn"));
3501 21 Sep 15 olle 2353         String bufferRpe = Values.getStringOrNull((String)jsonReq.get("bufferRpe"));
3501 21 Sep 15 olle 2354         String bufferAl = Values.getStringOrNull((String)jsonReq.get("bufferAl"));
3501 21 Sep 15 olle 2355         String bufferAw1 = Values.getStringOrNull((String)jsonReq.get("bufferAw1"));
3501 21 Sep 15 olle 2356         String bufferAw2 = Values.getStringOrNull((String)jsonReq.get("bufferAw2"));
3501 21 Sep 15 olle 2357         String bufferAte = Values.getStringOrNull((String)jsonReq.get("bufferAte"));
3501 21 Sep 15 olle 2358         String bufferRdd = Values.getStringOrNull((String)jsonReq.get("bufferRdd"));
3501 21 Sep 15 olle 2359         String rnaseFreeWater = Values.getStringOrNull((String)jsonReq.get("rnaseFreeWater"));
3501 21 Sep 15 olle 2360         String dnaseMix = Values.getStringOrNull((String)jsonReq.get("dnaseMix"));
3501 21 Sep 15 olle 2361         String rneasyMinEluteSpinColumn = Values.getStringOrNull((String)jsonReq.get("rneasyMinEluteSpinColumn"));
3501 21 Sep 15 olle 2362         String qiaampMinEluteSpinColumn = Values.getStringOrNull((String)jsonReq.get("qiaampMinEluteSpinColumn"));
3501 21 Sep 15 olle 2363
3501 21 Sep 15 olle 2364         // Quality control reagents
3501 21 Sep 15 olle 2365         String kapaSybrFast = Values.getStringOrNull((String)jsonReq.get("kapaSybrFast"));
3501 21 Sep 15 olle 2366         String qualityControlPrimersQcpRgt = Values.getStringOrNull((String)jsonReq.get("qualityControlPrimersQcpRgt"));
3501 21 Sep 15 olle 2367         String qualityControlTemplateQct = Values.getStringOrNull((String)jsonReq.get("qualityControlTemplateQct"));
3501 21 Sep 15 olle 2368         String qubitDnaHighSens = Values.getStringOrNull((String)jsonReq.get("qubitDnaHighSens"));
3501 21 Sep 15 olle 2369         String qubitDnaBroadRange = Values.getStringOrNull((String)jsonReq.get("qubitDnaBroadRange"));
3501 21 Sep 15 olle 2370
3432 29 Jun 15 olle 2371         // Get list of extract source item names
3432 29 Jun 15 olle 2372         List<String> itemNameList = new ArrayList<String>();
3432 29 Jun 15 olle 2373         JSONArray jsonItems = (JSONArray)jsonReq.get("items");
3432 29 Jun 15 olle 2374         for (int i = 0; i < jsonItems.size(); ++i)
3432 29 Jun 15 olle 2375         {
3432 29 Jun 15 olle 2376           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3432 29 Jun 15 olle 2377           String itemName = (String)jsonItem.get("name");
3432 29 Jun 15 olle 2378           itemNameList.add(itemName);
3432 29 Jun 15 olle 2379         }
3432 29 Jun 15 olle 2380
3432 29 Jun 15 olle 2381         // Create start item list
3432 29 Jun 15 olle 2382         ItemList startItemList = null;
3432 29 Jun 15 olle 2383         JSONArray jsonCreatedStartItemList = new JSONArray();
3432 29 Jun 15 olle 2384         if (itemListName != null && !itemListName.equals(""))
3432 29 Jun 15 olle 2385         {
3432 29 Jun 15 olle 2386           startItemList = ItemList.getNew(dc, Item.EXTRACT);
3432 29 Jun 15 olle 2387           startItemList.setName(itemListName);
3432 29 Jun 15 olle 2388           jsonMessages.add("Created '" + startItemList.getName() + "' Start item list");
3432 29 Jun 15 olle 2389           Annotationtype.LIST_ISOLATION_DATE.setAnnotationValue(dc, startItemList, isolationDate);
3501 21 Sep 15 olle 2390           Annotationtype.LIST_EXTRACTION_OPERATOR.setAnnotationValue(dc, startItemList, extractionOperator);
3432 29 Jun 15 olle 2391           Annotationtype.LIST_EXTRACTION_PROTOCOL_ID.setAnnotationValue(dc, startItemList, extractionProtocolId);
3432 29 Jun 15 olle 2392           Annotationtype.LIST_EXTRACTION_PROTOCOL_NAME.setAnnotationValue(dc, startItemList, extractionProtocolName);
3432 29 Jun 15 olle 2393           Annotationtype.LIST_QIACUBE_DATE.setAnnotationValue(dc, startItemList, qiacubeDate);
3501 21 Sep 15 olle 2394           Annotationtype.LIST_QIACUBE_OPERATOR.setAnnotationValue(dc, startItemList, qiacubeOperator);
3432 29 Jun 15 olle 2395           Annotationtype.LIST_QIACUBE_PRIMARY_PREFIX.setAnnotationValue(dc, startItemList, qiacubePrimaryPrefix);
3432 29 Jun 15 olle 2396           Annotationtype.LIST_QIACUBE_SECONDARY_PREFIX.setAnnotationValue(dc, startItemList, qiacubeSecondaryPrefix);
3432 29 Jun 15 olle 2397           Annotationtype.LIST_QIACUBE_RUN_NO.setAnnotationValue(dc, startItemList, qiacubeRunNo);
3501 21 Sep 15 olle 2398
3501 21 Sep 15 olle 2399           // Specimen reagents
3432 29 Jun 15 olle 2400           Annotationtype.LIST_ALLPREP_FFPE_KIT.setAnnotationValue(dc, startItemList, allPrepFfpeKit);
3501 21 Sep 15 olle 2401           Annotationtype.LIST_XYLENE_SPECIMEN.setAnnotationValue(dc, startItemList, xyleneSpecimen);
3501 21 Sep 15 olle 2402           Annotationtype.LIST_ETOH995_SPECIMEN.setAnnotationValue(dc, startItemList, etoh995Specimen);
3501 21 Sep 15 olle 2403           Annotationtype.LIST_BUFFER_PKD_SPECIMEN.setAnnotationValue(dc, startItemList, bufferPkdSpecimen);
3501 21 Sep 15 olle 2404           Annotationtype.LIST_BUFFER_ATL_SPECIMEN.setAnnotationValue(dc, startItemList, bufferAtlSpecimen);
3501 21 Sep 15 olle 2405           Annotationtype.LIST_PROTEINASE_K_SPECIMEN.setAnnotationValue(dc, startItemList, proteinaseKSpecimen);
3501 21 Sep 15 olle 2406           Annotationtype.LIST_RNASE_A_SPECIMEN.setAnnotationValue(dc, startItemList, rnaseASpecimen);
3501 21 Sep 15 olle 2407
3501 21 Sep 15 olle 2408           // QIAcube reagents
3501 21 Sep 15 olle 2409           Annotationtype.LIST_ETOH995.setAnnotationValue(dc, startItemList, etoh995);
3501 21 Sep 15 olle 2410           Annotationtype.LIST_BUFFER_RLT.setAnnotationValue(dc, startItemList, bufferRlt);
3501 21 Sep 15 olle 2411           Annotationtype.LIST_BUFFER_FRN.setAnnotationValue(dc, startItemList, bufferFrn);
3501 21 Sep 15 olle 2412           Annotationtype.LIST_BUFFER_RPE.setAnnotationValue(dc, startItemList, bufferRpe);
3501 21 Sep 15 olle 2413           Annotationtype.LIST_BUFFER_AL.setAnnotationValue(dc, startItemList, bufferAl);
3501 21 Sep 15 olle 2414           Annotationtype.LIST_BUFFER_AW1.setAnnotationValue(dc, startItemList, bufferAw1);
3501 21 Sep 15 olle 2415           Annotationtype.LIST_BUFFER_AW2.setAnnotationValue(dc, startItemList, bufferAw2);
3501 21 Sep 15 olle 2416           Annotationtype.LIST_BUFFER_ATE.setAnnotationValue(dc, startItemList, bufferAte);
3501 21 Sep 15 olle 2417           Annotationtype.LIST_BUFFER_RDD.setAnnotationValue(dc, startItemList, bufferRdd);
3501 21 Sep 15 olle 2418           Annotationtype.LIST_RNASE_FREE_WATER.setAnnotationValue(dc, startItemList, rnaseFreeWater);
3501 21 Sep 15 olle 2419           Annotationtype.LIST_DNASE_MIX.setAnnotationValue(dc, startItemList, dnaseMix);
3501 21 Sep 15 olle 2420           Annotationtype.LIST_RNEASY_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, startItemList, rneasyMinEluteSpinColumn);          
3501 21 Sep 15 olle 2421           Annotationtype.LIST_QIAAMP_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, startItemList, qiaampMinEluteSpinColumn);
3501 21 Sep 15 olle 2422           
3501 21 Sep 15 olle 2423           // Quality control reagents
3501 21 Sep 15 olle 2424           Annotationtype.LIST_KAPA_SYBR_FAST.setAnnotationValue(dc, startItemList, kapaSybrFast);          
3501 21 Sep 15 olle 2425           Annotationtype.LIST_QUALITY_CONTROL_PRIMERS_QCP_RGT.setAnnotationValue(dc, startItemList, qualityControlPrimersQcpRgt);          
3501 21 Sep 15 olle 2426           Annotationtype.LIST_QUALITY_CONTROL_TEMPLATE_QCT.setAnnotationValue(dc, startItemList, qualityControlTemplateQct);          
3501 21 Sep 15 olle 2427           Annotationtype.LIST_QUBIT_DNA_HIGH_SENS.setAnnotationValue(dc, startItemList, qubitDnaHighSens);          
3501 21 Sep 15 olle 2428           Annotationtype.LIST_QUBIT_DNA_BROAD_RANGE.setAnnotationValue(dc, startItemList, qubitDnaBroadRange);          
3501 21 Sep 15 olle 2429
3432 29 Jun 15 olle 2430           dc.saveItem(startItemList);
3432 29 Jun 15 olle 2431           jsonCreatedStartItemList.add(startItemList.getId());
3432 29 Jun 15 olle 2432         }
3432 29 Jun 15 olle 2433
3432 29 Jun 15 olle 2434         // Add input DNA/RNA extracts to start item list, and child DNA extract for specimen
3432 29 Jun 15 olle 2435         // Set original quantity for extract source item to 0,0, in order to mark them as processed
3432 29 Jun 15 olle 2436         for (int i = 0; i < jsonItems.size(); ++i)
3432 29 Jun 15 olle 2437         {
3432 29 Jun 15 olle 2438           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3432 29 Jun 15 olle 2439           String itemName = (String)jsonItem.get("name");
3432 29 Jun 15 olle 2440           Number itemId = (Number)jsonItem.get("id");
3432 29 Jun 15 olle 2441           Sample specimen = null;
3432 29 Jun 15 olle 2442           Extract dna = null;
3432 29 Jun 15 olle 2443           Extract rna = null;
3432 29 Jun 15 olle 2444
3433 29 Jun 15 olle 2445           if (Meludi.itemIsSpecimen(itemName))
3432 29 Jun 15 olle 2446           {
3432 29 Jun 15 olle 2447             specimen = Sample.getById(dc, itemId.intValue());
3432 29 Jun 15 olle 2448             specimen.setItemSubtype(Subtype.SPECIMEN.load(dc));
3432 29 Jun 15 olle 2449             specimen.setOriginalQuantity(0.0f);
3432 29 Jun 15 olle 2450             // Add child DNA extract to start item list
3432 29 Jun 15 olle 2451             if (startItemList != null)
3432 29 Jun 15 olle 2452             {
3432 29 Jun 15 olle 2453               Dna d = Dna.findByName(dc, itemName + ".d");
3432 29 Jun 15 olle 2454               if (d == null)
3432 29 Jun 15 olle 2455               {
3432 29 Jun 15 olle 2456                 throw new InvalidDataException("Extract " + itemName + ".d" + " not found in database.");
3432 29 Jun 15 olle 2457               }
3432 29 Jun 15 olle 2458               dna = d.getExtract();
3432 29 Jun 15 olle 2459               startItemList.add(dna);
3432 29 Jun 15 olle 2460             }
3432 29 Jun 15 olle 2461           }
3432 29 Jun 15 olle 2462
3432 29 Jun 15 olle 2463           // DNA
3433 29 Jun 15 olle 2464           if (Meludi.itemIsDna(itemName))
3432 29 Jun 15 olle 2465           {
3432 29 Jun 15 olle 2466             JSONObject jsonDna = (JSONObject)jsonItem.get("dna");
3432 29 Jun 15 olle 2467             dna = Extract.getById(dc, itemId.intValue());
3432 29 Jun 15 olle 2468             dna.setItemSubtype(Subtype.DNA.load(dc));
3432 29 Jun 15 olle 2469             dna.setOriginalQuantity(0.0f);
3432 29 Jun 15 olle 2470             // Add to start item list
3432 29 Jun 15 olle 2471             if (startItemList != null)
3432 29 Jun 15 olle 2472             {
3432 29 Jun 15 olle 2473               startItemList.add(dna);
3432 29 Jun 15 olle 2474             }
3432 29 Jun 15 olle 2475           }
3432 29 Jun 15 olle 2476           
3432 29 Jun 15 olle 2477           // RNA
3433 29 Jun 15 olle 2478           if (Meludi.itemIsRna(itemName))
3432 29 Jun 15 olle 2479           {
3432 29 Jun 15 olle 2480             JSONObject jsonRna = (JSONObject)jsonItem.get("rna");
3432 29 Jun 15 olle 2481             rna = Extract.getById(dc, itemId.intValue());
3432 29 Jun 15 olle 2482             rna.setItemSubtype(Subtype.RNA.load(dc));
3432 29 Jun 15 olle 2483             rna.setOriginalQuantity(0.0f);
3432 29 Jun 15 olle 2484             // Add to start item list
3432 29 Jun 15 olle 2485             if (startItemList != null)
3432 29 Jun 15 olle 2486             {
3432 29 Jun 15 olle 2487               startItemList.add(rna);
3432 29 Jun 15 olle 2488             }
3432 29 Jun 15 olle 2489           }
3432 29 Jun 15 olle 2490           
3432 29 Jun 15 olle 2491           String msg = "";
3433 29 Jun 15 olle 2492           if (Meludi.itemIsSpecimen(itemName))
3432 29 Jun 15 olle 2493           {
3432 29 Jun 15 olle 2494             msg += "Original quantity set to 0.0 for specimen " + itemName;
3432 29 Jun 15 olle 2495           }
3433 29 Jun 15 olle 2496           else if (Meludi.itemIsRna(itemName))
3432 29 Jun 15 olle 2497           {
3432 29 Jun 15 olle 2498             msg += "Original quantity set to 0.0 for RNA " + itemName;
3432 29 Jun 15 olle 2499           }
3433 29 Jun 15 olle 2500           else if (Meludi.itemIsDna(itemName))
3432 29 Jun 15 olle 2501           {
3432 29 Jun 15 olle 2502             msg += "Original quantity set to 0.0 for DNA " + itemName;
3432 29 Jun 15 olle 2503           }
3432 29 Jun 15 olle 2504           jsonMessages.add(msg);
3432 29 Jun 15 olle 2505         }
3432 29 Jun 15 olle 2506         dc.commit();
3432 29 Jun 15 olle 2507
3432 29 Jun 15 olle 2508         json.put("createdStartItemList", jsonCreatedStartItemList);
3502 22 Sep 15 olle 2509         CounterService.getInstance().setForceCount();
3432 29 Jun 15 olle 2510       }
3502 22 Sep 15 olle 2511       else if ("RemoveStartItemList".equals(cmd))
3502 22 Sep 15 olle 2512       {
3502 22 Sep 15 olle 2513         dc = sc.newDbControl();
3502 22 Sep 15 olle 2514
3502 22 Sep 15 olle 2515         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3502 22 Sep 15 olle 2516
3502 22 Sep 15 olle 2517         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3502 22 Sep 15 olle 2518         Integer startItemListId = Values.getInteger(req.getParameter("startItemListId"), null);
3502 22 Sep 15 olle 2519         JSONArray jsonRemovedStartItemList = new JSONArray();
3502 22 Sep 15 olle 2520         JSONObject jsonStartItemList = new JSONObject();
3502 22 Sep 15 olle 2521
3502 22 Sep 15 olle 2522         // Use stored annotation snapshots for performance reasons
3502 22 Sep 15 olle 2523         SnapshotManager manager = new SnapshotManager();
3502 22 Sep 15 olle 2524         List<String> extractSourceItemNameList = null;
3502 22 Sep 15 olle 2525         // Remove start item list
3502 22 Sep 15 olle 2526         if (startItemListId != null)
3502 22 Sep 15 olle 2527         {
3502 22 Sep 15 olle 2528           ItemList startItemList = ItemList.getById(dc, startItemListId);
3502 22 Sep 15 olle 2529           if (startItemList != null)
3502 22 Sep 15 olle 2530           {
3502 22 Sep 15 olle 2531             StartList sl = new StartList(startItemList);
3502 22 Sep 15 olle 2532             extractSourceItemNameList = sl.fetchExtractSourceNameListFromItemList(dc);
3502 22 Sep 15 olle 2533             // Construct a query to get all member extract items in the item list, sorted alphabetically after name
3502 22 Sep 15 olle 2534             ItemQuery<Extract> query = (ItemQuery<Extract>)startItemList.getMembers();
3502 22 Sep 15 olle 2535             query.include(Include.ALL);
3502 22 Sep 15 olle 2536             query.order(Orders.asc(Hql.property("name")));
3502 22 Sep 15 olle 2537             List<Extract> extractList = query.list(dc);
3502 22 Sep 15 olle 2538             if (extractList != null)
3502 22 Sep 15 olle 2539             {
3502 22 Sep 15 olle 2540               for (Extract extract: extractList)
3502 22 Sep 15 olle 2541               {
3502 22 Sep 15 olle 2542                 startItemList.remove(extract);
3502 22 Sep 15 olle 2543               }
3502 22 Sep 15 olle 2544             }
3502 22 Sep 15 olle 2545             startItemList.setRemoved(true);
3502 22 Sep 15 olle 2546             String msg = "Removed start item list " + startItemList.getName();
3502 22 Sep 15 olle 2547             jsonMessages.add(msg);
3502 22 Sep 15 olle 2548             jsonRemovedStartItemList.add(startItemList.getId());
3502 22 Sep 15 olle 2549             dc.deleteItem(startItemList);
3502 22 Sep 15 olle 2550             List<String> itemNameList = null;
3502 22 Sep 15 olle 2551           }
3502 22 Sep 15 olle 2552         }
3502 22 Sep 15 olle 2553         // Get list of extract source item names
3502 22 Sep 15 olle 2554         List<String> itemNameList = new ArrayList<String>();
3502 22 Sep 15 olle 2555         JSONArray jsonItems = (JSONArray)jsonReq.get("items");
3502 22 Sep 15 olle 2556         for (int i = 0; i < jsonItems.size(); ++i)
3502 22 Sep 15 olle 2557         {
3502 22 Sep 15 olle 2558           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3502 22 Sep 15 olle 2559           String itemName = (String)jsonItem.get("name");
3502 22 Sep 15 olle 2560           itemNameList.add(itemName);
3502 22 Sep 15 olle 2561         }
3502 22 Sep 15 olle 2562
3502 22 Sep 15 olle 2563         // Set original quantity for extract source item to null, in order to mark them as unprocessed
3502 22 Sep 15 olle 2564         for (int i = 0; i < jsonItems.size(); ++i)
3502 22 Sep 15 olle 2565         {
3502 22 Sep 15 olle 2566           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3502 22 Sep 15 olle 2567           String itemName = (String)jsonItem.get("name");
3502 22 Sep 15 olle 2568           Number itemId = (Number)jsonItem.get("id");
3502 22 Sep 15 olle 2569           Sample specimen = null;
3502 22 Sep 15 olle 2570           Extract dna = null;
3502 22 Sep 15 olle 2571           Extract rna = null;
3502 22 Sep 15 olle 2572
3502 22 Sep 15 olle 2573           if (Meludi.itemIsSpecimen(itemName))
3502 22 Sep 15 olle 2574           {
3502 22 Sep 15 olle 2575             specimen = Sample.getById(dc, itemId.intValue());
3502 22 Sep 15 olle 2576             specimen.setItemSubtype(Subtype.SPECIMEN.load(dc));
3502 22 Sep 15 olle 2577             specimen.setOriginalQuantity(null);
3502 22 Sep 15 olle 2578           }
3502 22 Sep 15 olle 2579
3502 22 Sep 15 olle 2580           // DNA
3502 22 Sep 15 olle 2581           if (Meludi.itemIsDna(itemName))
3502 22 Sep 15 olle 2582           {
3502 22 Sep 15 olle 2583             dna = Extract.getById(dc, itemId.intValue());
3502 22 Sep 15 olle 2584             dna.setItemSubtype(Subtype.DNA.load(dc));
3502 22 Sep 15 olle 2585             dna.setOriginalQuantity(null);
3502 22 Sep 15 olle 2586           }
3502 22 Sep 15 olle 2587           
3502 22 Sep 15 olle 2588           // RNA
3502 22 Sep 15 olle 2589           if (Meludi.itemIsRna(itemName))
3502 22 Sep 15 olle 2590           {
3502 22 Sep 15 olle 2591             rna = Extract.getById(dc, itemId.intValue());
3502 22 Sep 15 olle 2592             rna.setItemSubtype(Subtype.RNA.load(dc));
3502 22 Sep 15 olle 2593             rna.setOriginalQuantity(null);
3502 22 Sep 15 olle 2594           }
3502 22 Sep 15 olle 2595           
3502 22 Sep 15 olle 2596           String msg = "";
3502 22 Sep 15 olle 2597           if (Meludi.itemIsSpecimen(itemName))
3502 22 Sep 15 olle 2598           {
3502 22 Sep 15 olle 2599             msg += "Original quantity set to null for specimen " + itemName;
3502 22 Sep 15 olle 2600           }
3502 22 Sep 15 olle 2601           else if (Meludi.itemIsRna(itemName))
3502 22 Sep 15 olle 2602           {
3502 22 Sep 15 olle 2603             msg += "Original quantity set to null for RNA " + itemName;
3502 22 Sep 15 olle 2604           }
3502 22 Sep 15 olle 2605           else if (Meludi.itemIsDna(itemName))
3502 22 Sep 15 olle 2606           {
3502 22 Sep 15 olle 2607             msg += "Original quantity set to null for DNA " + itemName;
3502 22 Sep 15 olle 2608           }
3502 22 Sep 15 olle 2609           jsonMessages.add(msg);
3502 22 Sep 15 olle 2610         }
3502 22 Sep 15 olle 2611         dc.commit();
3502 22 Sep 15 olle 2612
3502 22 Sep 15 olle 2613         json.put("removedStartItemList", jsonRemovedStartItemList);
3502 22 Sep 15 olle 2614         CounterService.getInstance().setForceCount();
3502 22 Sep 15 olle 2615       }
3180 17 Mar 15 olle 2616       else if ("RegisterDnaRna".equals(cmd))
3180 17 Mar 15 olle 2617       {
3180 17 Mar 15 olle 2618         dc = sc.newDbControl();
3180 17 Mar 15 olle 2619         
3176 06 Mar 15 olle 2620         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3176 06 Mar 15 olle 2621         
3176 06 Mar 15 olle 2622         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3176 06 Mar 15 olle 2623         JSONArray jsonItems = (JSONArray)jsonReq.get("items");
3176 06 Mar 15 olle 2624         int numSpecimen = 0;
3176 06 Mar 15 olle 2625         int numRna = 0;
3176 06 Mar 15 olle 2626         int numDna = 0;
3176 06 Mar 15 olle 2627         for (int i = 0; i < jsonItems.size(); ++i)
3176 06 Mar 15 olle 2628         {
3176 06 Mar 15 olle 2629           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3176 06 Mar 15 olle 2630           String itemName = (String)jsonItem.get("name");
3433 29 Jun 15 olle 2631           if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2632           {
3176 06 Mar 15 olle 2633             numSpecimen++;
3176 06 Mar 15 olle 2634             numRna++;
3176 06 Mar 15 olle 2635             numDna++;
3176 06 Mar 15 olle 2636           }
3433 29 Jun 15 olle 2637           else if (Meludi.itemIsRna(itemName))
3176 06 Mar 15 olle 2638           {
3176 06 Mar 15 olle 2639             numRna++;
3176 06 Mar 15 olle 2640           }
3433 29 Jun 15 olle 2641           else if (Meludi.itemIsDna(itemName))
3176 06 Mar 15 olle 2642           {
3176 06 Mar 15 olle 2643             numDna++;
3176 06 Mar 15 olle 2644           }
3176 06 Mar 15 olle 2645         }
3176 06 Mar 15 olle 2646         JSONArray jsonCreatedRna = new JSONArray();
3176 06 Mar 15 olle 2647         JSONArray jsonCreatedDna = new JSONArray();
3176 06 Mar 15 olle 2648         List<Extract> newRna = new ArrayList<Extract>(numRna);
3176 06 Mar 15 olle 2649         List<Extract> newDna = new ArrayList<Extract>(numDna);
3276 29 Apr 15 olle 2650 /*
3176 06 Mar 15 olle 2651         SpecimenTubeServlet specimenTubeServlet = new SpecimenTubeServlet();
3253 17 Apr 15 olle 2652         List<BioWell> freeRnaWells = specimenTubeServlet.findFreeWells2(dc, numSpecimen, "_rna");
3253 17 Apr 15 olle 2653         List<BioWell> freeDnaWells = specimenTubeServlet.findFreeWells2(dc, numSpecimen, "_dna");
3276 29 Apr 15 olle 2654 */
3176 06 Mar 15 olle 2655
3180 17 Mar 15 olle 2656         Date isolationDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("isolationDate"));
3501 21 Sep 15 olle 2657         String extractionOperator = (String)jsonReq.get("extractionOperator");
3176 06 Mar 15 olle 2658         Number extractionProtocolId = (Number)jsonReq.get("extractionProtocol");
3176 06 Mar 15 olle 2659         Protocol extractionProtocol = extractionProtocolId != null ? Protocol.getById(dc, extractionProtocolId.intValue()) : null;
3180 17 Mar 15 olle 2660         Number rnaDefaultVolume = (Number)jsonReq.get("rnaDefaultVolume");
3180 17 Mar 15 olle 2661         Number dnaDefaultVolume = (Number)jsonReq.get("dnaDefaultVolume");
3176 06 Mar 15 olle 2662
3176 06 Mar 15 olle 2663         // QIAcube properties
3176 06 Mar 15 olle 2664         Date qiacubeDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("qiacubeDate"));
3501 21 Sep 15 olle 2665         String qiacubeOperator = (String)jsonReq.get("qiacubeOperator");
3176 06 Mar 15 olle 2666         Number qiacubeRunNo = (Number)jsonReq.get("qiacubeRunNo");
3180 17 Mar 15 olle 2667
3501 21 Sep 15 olle 2668         // Specimen reagents
3501 21 Sep 15 olle 2669         String d_allPrepFfpeKit = Values.getStringOrNull((String)jsonReq.get("allPrepFfpeKit"));
3176 06 Mar 15 olle 2670         String d_xyleneSpecimen = Values.getStringOrNull((String)jsonReq.get("xyleneSpecimen"));
3176 06 Mar 15 olle 2671         String d_etoh995Specimen = Values.getStringOrNull((String)jsonReq.get("etoh995Specimen"));
3176 06 Mar 15 olle 2672         String d_bufferPkdSpecimen = Values.getStringOrNull((String)jsonReq.get("bufferPkdSpecimen"));
3176 06 Mar 15 olle 2673         String d_bufferAtlSpecimen = Values.getStringOrNull((String)jsonReq.get("bufferAtlSpecimen"));
3176 06 Mar 15 olle 2674         String d_proteinaseKSpecimen = Values.getStringOrNull((String)jsonReq.get("proteinaseKSpecimen"));
3176 06 Mar 15 olle 2675         String d_rnaseASpecimen = Values.getStringOrNull((String)jsonReq.get("rnaseASpecimen"));
3176 06 Mar 15 olle 2676
3501 21 Sep 15 olle 2677         // QIAcube reagents
3176 06 Mar 15 olle 2678         String d_etoh995 = Values.getStringOrNull((String)jsonReq.get("etoh995"));
3176 06 Mar 15 olle 2679         String d_bufferRlt = Values.getStringOrNull((String)jsonReq.get("bufferRlt"));
3176 06 Mar 15 olle 2680         String d_bufferFrn = Values.getStringOrNull((String)jsonReq.get("bufferFrn"));
3176 06 Mar 15 olle 2681         String d_bufferRpe = Values.getStringOrNull((String)jsonReq.get("bufferRpe"));
3176 06 Mar 15 olle 2682         String d_bufferAl = Values.getStringOrNull((String)jsonReq.get("bufferAl"));
3176 06 Mar 15 olle 2683         String d_bufferAw1 = Values.getStringOrNull((String)jsonReq.get("bufferAw1"));
3176 06 Mar 15 olle 2684         String d_bufferAw2 = Values.getStringOrNull((String)jsonReq.get("bufferAw2"));
3176 06 Mar 15 olle 2685         String d_bufferAte = Values.getStringOrNull((String)jsonReq.get("bufferAte"));
3176 06 Mar 15 olle 2686         String d_bufferRdd = Values.getStringOrNull((String)jsonReq.get("bufferRdd"));
3176 06 Mar 15 olle 2687         String d_rnaseFreeWater = Values.getStringOrNull((String)jsonReq.get("rnaseFreeWater"));
3176 06 Mar 15 olle 2688         String d_dnaseMix = Values.getStringOrNull((String)jsonReq.get("dnaseMix"));
3176 06 Mar 15 olle 2689         String d_rneasyMinEluteSpinColumn = Values.getStringOrNull((String)jsonReq.get("rneasyMinEluteSpinColumn"));
3176 06 Mar 15 olle 2690         String d_qiaampMinEluteSpinColumn = Values.getStringOrNull((String)jsonReq.get("qiaampMinEluteSpinColumn"));
3176 06 Mar 15 olle 2691
3501 21 Sep 15 olle 2692         // Quality control reagents
3324 11 May 15 olle 2693         String d_kapaSybrFast = Values.getStringOrNull((String)jsonReq.get("kapaSybrFast"));
3324 11 May 15 olle 2694         String d_qualityControlPrimersQcpRgt = Values.getStringOrNull((String)jsonReq.get("qualityControlPrimersQcpRgt"));
3438 03 Jul 15 olle 2695         String d_qualityControlTemplateQct = Values.getStringOrNull((String)jsonReq.get("qualityControlTemplateQct"));
3324 11 May 15 olle 2696         String d_qubitDnaHighSens = Values.getStringOrNull((String)jsonReq.get("qubitDnaHighSens"));
3324 11 May 15 olle 2697         String d_qubitDnaBroadRange = Values.getStringOrNull((String)jsonReq.get("qubitDnaBroadRange"));
3324 11 May 15 olle 2698
3501 21 Sep 15 olle 2699         // Set start item list extraction date annotation
3501 21 Sep 15 olle 2700         Integer startItemListId = Values.getInteger((String)jsonReq.get("startListId"),1);
3501 21 Sep 15 olle 2701         ItemList startItemList = null;
3501 21 Sep 15 olle 2702         if (startItemListId != null)
3501 21 Sep 15 olle 2703         {
3501 21 Sep 15 olle 2704           startItemList = ItemList.getById(dc, startItemListId);
3501 21 Sep 15 olle 2705           if (startItemList != null)
3501 21 Sep 15 olle 2706           {
3501 21 Sep 15 olle 2707             // Set start item list extraction date annotation to QIAcube date and mark the item list as processed
3501 21 Sep 15 olle 2708             Annotationtype.LIST_EXTRACTION_DATE.setAnnotationValue(dc, startItemList, qiacubeDate);
3501 21 Sep 15 olle 2709             Annotationtype.LIST_EXTRACTION_OPERATOR.setAnnotationValue(dc, startItemList, extractionOperator);
3501 21 Sep 15 olle 2710             Annotationtype.SAMPLE_PREP_LIST_IS_PROCESSED.setAnnotationValue(dc, startItemList, true);
3501 21 Sep 15 olle 2711             jsonMessages.add("Extraction date updated for start item list " + startItemList.getName());
3501 21 Sep 15 olle 2712
3501 21 Sep 15 olle 2713             // Set start list QIAcube annotations
3501 21 Sep 15 olle 2714             Annotationtype.LIST_QIACUBE_DATE.setAnnotationValue(dc, startItemList, qiacubeDate);
3501 21 Sep 15 olle 2715             Annotationtype.LIST_QIACUBE_OPERATOR.setAnnotationValue(dc, startItemList, qiacubeOperator);
3501 21 Sep 15 olle 2716             //Annotationtype.LIST_QIACUBE_PRIMARY_PREFIX.setAnnotationValue(dc, startItemList, qiacubePrimaryPrefix);
3501 21 Sep 15 olle 2717             //Annotationtype.LIST_QIACUBE_SECONDARY_PREFIX.setAnnotationValue(dc, startItemList, qiacubeSecondaryPrefix);
3501 21 Sep 15 olle 2718             Annotationtype.LIST_QIACUBE_RUN_NO.setAnnotationValue(dc, startItemList, qiacubeRunNo);
3501 21 Sep 15 olle 2719
3501 21 Sep 15 olle 2720             // Specimen reagents
3501 21 Sep 15 olle 2721             Annotationtype.LIST_ALLPREP_FFPE_KIT.setAnnotationValue(dc, startItemList, d_allPrepFfpeKit);
3501 21 Sep 15 olle 2722             Annotationtype.LIST_XYLENE_SPECIMEN.setAnnotationValue(dc, startItemList, d_xyleneSpecimen);
3501 21 Sep 15 olle 2723             Annotationtype.LIST_ETOH995_SPECIMEN.setAnnotationValue(dc, startItemList, d_etoh995Specimen);
3501 21 Sep 15 olle 2724             Annotationtype.LIST_BUFFER_PKD_SPECIMEN.setAnnotationValue(dc, startItemList, d_bufferPkdSpecimen);
3501 21 Sep 15 olle 2725             Annotationtype.LIST_BUFFER_ATL_SPECIMEN.setAnnotationValue(dc, startItemList, d_bufferAtlSpecimen);
3501 21 Sep 15 olle 2726             Annotationtype.LIST_PROTEINASE_K_SPECIMEN.setAnnotationValue(dc, startItemList, d_proteinaseKSpecimen);
3501 21 Sep 15 olle 2727             Annotationtype.LIST_RNASE_A_SPECIMEN.setAnnotationValue(dc, startItemList, d_rnaseASpecimen);
3501 21 Sep 15 olle 2728
3501 21 Sep 15 olle 2729             // QIAcube reagents
3501 21 Sep 15 olle 2730             Annotationtype.LIST_ETOH995.setAnnotationValue(dc, startItemList, d_etoh995);
3501 21 Sep 15 olle 2731             Annotationtype.LIST_BUFFER_RLT.setAnnotationValue(dc, startItemList, d_bufferRlt);
3501 21 Sep 15 olle 2732             Annotationtype.LIST_BUFFER_FRN.setAnnotationValue(dc, startItemList, d_bufferFrn);
3501 21 Sep 15 olle 2733             Annotationtype.LIST_BUFFER_RPE.setAnnotationValue(dc, startItemList, d_bufferRpe);
3501 21 Sep 15 olle 2734             Annotationtype.LIST_BUFFER_AL.setAnnotationValue(dc, startItemList, d_bufferAl);
3501 21 Sep 15 olle 2735             Annotationtype.LIST_BUFFER_AW1.setAnnotationValue(dc, startItemList, d_bufferAw1);
3501 21 Sep 15 olle 2736             Annotationtype.LIST_BUFFER_AW2.setAnnotationValue(dc, startItemList, d_bufferAw2);
3501 21 Sep 15 olle 2737             Annotationtype.LIST_BUFFER_ATE.setAnnotationValue(dc, startItemList, d_bufferAte);
3501 21 Sep 15 olle 2738             Annotationtype.LIST_BUFFER_RDD.setAnnotationValue(dc, startItemList, d_bufferRdd);
3501 21 Sep 15 olle 2739             Annotationtype.LIST_RNASE_FREE_WATER.setAnnotationValue(dc, startItemList, d_rnaseFreeWater);
3501 21 Sep 15 olle 2740             Annotationtype.LIST_DNASE_MIX.setAnnotationValue(dc, startItemList, d_dnaseMix);
3501 21 Sep 15 olle 2741             Annotationtype.LIST_RNEASY_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, startItemList, d_rneasyMinEluteSpinColumn);          
3501 21 Sep 15 olle 2742             Annotationtype.LIST_QIAAMP_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, startItemList, d_qiaampMinEluteSpinColumn);
3501 21 Sep 15 olle 2743             
3501 21 Sep 15 olle 2744             // Quality control reagents
3501 21 Sep 15 olle 2745             Annotationtype.LIST_KAPA_SYBR_FAST.setAnnotationValue(dc, startItemList, d_kapaSybrFast);          
3501 21 Sep 15 olle 2746             Annotationtype.LIST_QUALITY_CONTROL_PRIMERS_QCP_RGT.setAnnotationValue(dc, startItemList, d_qualityControlPrimersQcpRgt);          
3501 21 Sep 15 olle 2747             Annotationtype.LIST_QUALITY_CONTROL_TEMPLATE_QCT.setAnnotationValue(dc, startItemList, d_qualityControlTemplateQct);          
3501 21 Sep 15 olle 2748             Annotationtype.LIST_QUBIT_DNA_HIGH_SENS.setAnnotationValue(dc, startItemList, d_qubitDnaHighSens);          
3501 21 Sep 15 olle 2749             Annotationtype.LIST_QUBIT_DNA_BROAD_RANGE.setAnnotationValue(dc, startItemList, d_qubitDnaBroadRange);          
3501 21 Sep 15 olle 2750
3501 21 Sep 15 olle 2751             // Set annotation values for new properties
3501 21 Sep 15 olle 2752             Annotationtype.LIST_RNA_DEFAULT_VOLUME_IN_MICROLITRE.setAnnotationValue(dc, startItemList, rnaDefaultVolume);
3501 21 Sep 15 olle 2753             Annotationtype.LIST_DNA_DEFAULT_VOLUME_IN_MICROLITRE.setAnnotationValue(dc, startItemList, dnaDefaultVolume);
3501 21 Sep 15 olle 2754           }
3501 21 Sep 15 olle 2755         }
3501 21 Sep 15 olle 2756         
3176 06 Mar 15 olle 2757 /*
3176 06 Mar 15 olle 2758         BioMaterialList flaggedRna = BiomaterialList.FLAGGED_RNA.load(dc);
3176 06 Mar 15 olle 2759 */
3176 06 Mar 15 olle 2760         boolean preNormalize = false;
3176 06 Mar 15 olle 2761         boolean flagged = false;
3176 06 Mar 15 olle 2762         int numFlagged = 0;
3253 17 Apr 15 olle 2763         int numStoredDna = 0;
3176 06 Mar 15 olle 2764         int numStoredRna = 0;
3176 06 Mar 15 olle 2765
3176 06 Mar 15 olle 2766         for (int i = 0; i < jsonItems.size(); ++i)
3176 06 Mar 15 olle 2767         {
3176 06 Mar 15 olle 2768           JSONObject jsonItem = (JSONObject)jsonItems.get(i);
3176 06 Mar 15 olle 2769           String itemName = (String)jsonItem.get("name");
3176 06 Mar 15 olle 2770           Number itemId = (Number)jsonItem.get("id");
3176 06 Mar 15 olle 2771 /*
3176 06 Mar 15 olle 2772           float itemOriginalQuantity = ((Number)jsonItem.get("originalQuantity")).floatValue();
3176 06 Mar 15 olle 2773           float itemProcessedQuantity = ((Number)jsonItem.get("processedQuantity")).floatValue();
3176 06 Mar 15 olle 2774 */
3176 06 Mar 15 olle 2775           float itemOriginalQuantity = fetchFloatValue(jsonItem.get("originalQuantity"));
3176 06 Mar 15 olle 2776           float itemProcessedQuantity = fetchFloatValue(jsonItem.get("processedQuantity"));
3176 06 Mar 15 olle 2777           BioWell specimenWell = null;
3176 06 Mar 15 olle 2778           Sample specimen = null;
3176 06 Mar 15 olle 2779
3433 29 Jun 15 olle 2780           if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2781           {
3176 06 Mar 15 olle 2782             specimen = Sample.getById(dc, itemId.intValue());
3176 06 Mar 15 olle 2783             specimen.setItemSubtype(Subtype.SPECIMEN.load(dc));
3176 06 Mar 15 olle 2784 /*
3176 06 Mar 15 olle 2785             BioMaterialEvent specimenEvent = specimen.getCreationEvent();
3176 06 Mar 15 olle 2786             specimenEvent.setEventDate(samplingDate);
3176 06 Mar 15 olle 2787             specimenEvent.setProtocol(specimenProtocol);
3176 06 Mar 15 olle 2788 */
3176 06 Mar 15 olle 2789             specimen.setOriginalQuantity(itemOriginalQuantity);
3176 06 Mar 15 olle 2790             specimen.setDescription((String)jsonItem.get("comment"));
3176 06 Mar 15 olle 2791             specimenWell = specimen.getBioWell();
3176 06 Mar 15 olle 2792           }
3176 06 Mar 15 olle 2793
3501 21 Sep 15 olle 2794           // Specimen reagents
3501 21 Sep 15 olle 2795           String allPrepFfpeKit = Values.getString((String)jsonItem.get("allPrepFfpeKit"), d_allPrepFfpeKit);
3176 06 Mar 15 olle 2796           String xyleneSpecimen = Values.getString((String)jsonItem.get("xyleneSpecimen"), d_xyleneSpecimen);
3176 06 Mar 15 olle 2797           String etoh995Specimen = Values.getString((String)jsonItem.get("etoh995Specimen"), d_etoh995Specimen);
3176 06 Mar 15 olle 2798           String bufferPkdSpecimen = Values.getString((String)jsonItem.get("bufferPkdSpecimen"), d_bufferPkdSpecimen);
3176 06 Mar 15 olle 2799           String bufferAtlSpecimen = Values.getString((String)jsonItem.get("bufferAtlSpecimen"), d_bufferAtlSpecimen);
3176 06 Mar 15 olle 2800           String proteinaseKSpecimen = Values.getString((String)jsonItem.get("proteinaseKSpecimen"), d_proteinaseKSpecimen);
3176 06 Mar 15 olle 2801           String rnaseASpecimen = Values.getString((String)jsonItem.get("rnaseASpecimen"), d_rnaseASpecimen);
3176 06 Mar 15 olle 2802
3501 21 Sep 15 olle 2803           // QIAcube reagents
3176 06 Mar 15 olle 2804           String etoh995 = Values.getString((String)jsonItem.get("etoh995"), d_etoh995);
3176 06 Mar 15 olle 2805           String bufferRlt = Values.getString((String)jsonItem.get("bufferRlt"), d_bufferRlt);
3176 06 Mar 15 olle 2806           String bufferFrn = Values.getString((String)jsonItem.get("bufferFrn"), d_bufferFrn);
3176 06 Mar 15 olle 2807           String bufferRpe = Values.getString((String)jsonItem.get("bufferRpe"), d_bufferRpe);
3176 06 Mar 15 olle 2808           String bufferAl = Values.getString((String)jsonItem.get("bufferAl"), d_bufferAl);
3176 06 Mar 15 olle 2809           String bufferAw1 = Values.getString((String)jsonItem.get("bufferAw1"), d_bufferAw1);
3176 06 Mar 15 olle 2810           String bufferAw2 = Values.getString((String)jsonItem.get("bufferAw2"), d_bufferAw2);
3176 06 Mar 15 olle 2811           String bufferAte = Values.getString((String)jsonItem.get("bufferAte"), d_bufferAte);
3176 06 Mar 15 olle 2812           String bufferRdd = Values.getString((String)jsonItem.get("bufferRdd"), d_bufferRdd);
3176 06 Mar 15 olle 2813           String rnaseFreeWater = Values.getString((String)jsonItem.get("rnaseFreeWater"), d_rnaseFreeWater);
3176 06 Mar 15 olle 2814           String dnaseMix = Values.getString((String)jsonItem.get("dnaseMix"), d_dnaseMix);
3176 06 Mar 15 olle 2815           String rneasyMinEluteSpinColumn = Values.getString((String)jsonItem.get("rneasyMinEluteSpinColumn"), d_rneasyMinEluteSpinColumn);
3324 11 May 15 olle 2816           String qiaampMinEluteSpinColumn = Values.getString((String)jsonItem.get("qiaampMinEluteSpinColumn"), d_qiaampMinEluteSpinColumn);
3176 06 Mar 15 olle 2817
3501 21 Sep 15 olle 2818           // Quality control reagents
3324 11 May 15 olle 2819           String kapaSybrFast = Values.getString((String)jsonItem.get("kapaSybrFast"), d_kapaSybrFast);
3324 11 May 15 olle 2820           String qualityControlPrimersQcpRgt = Values.getString((String)jsonItem.get("qualityControlPrimersQcpRgt"), d_qualityControlPrimersQcpRgt);
3438 03 Jul 15 olle 2821           String qualityControlTemplateQct = Values.getString((String)jsonItem.get("qualityControlTemplateQct"), d_qualityControlTemplateQct);
3324 11 May 15 olle 2822           String qubitDnaHighSens = Values.getString((String)jsonItem.get("qubitDnaHighSens"), d_qubitDnaHighSens);
3324 11 May 15 olle 2823           String qubitDnaBroadRange = Values.getString((String)jsonItem.get("qubitDnaBroadRange"), d_qubitDnaBroadRange);
3324 11 May 15 olle 2824
3176 06 Mar 15 olle 2825           // Qiacube
3176 06 Mar 15 olle 2826           //Number qiacubePosition = (Number)jsonItem.get("qiacubePosition");
3176 06 Mar 15 olle 2827           String qiacubePositionStr = (String)jsonItem.get("qiacubePosition");
3176 06 Mar 15 olle 2828           String qiacubeInstrumentPrefix = fetchQiacubeInstrumentPrefix(qiacubePositionStr);
3176 06 Mar 15 olle 2829           Number qiacubePosition = fetchQiacubePosition(qiacubePositionStr);
3176 06 Mar 15 olle 2830           
3176 06 Mar 15 olle 2831           // DNA
3433 29 Jun 15 olle 2832           if (Meludi.itemIsSpecimen(itemName) || Meludi.itemIsDna(itemName))
3176 06 Mar 15 olle 2833           {
3176 06 Mar 15 olle 2834             JSONObject jsonDna = (JSONObject)jsonItem.get("dna");
3176 06 Mar 15 olle 2835             Extract dna = null;
3433 29 Jun 15 olle 2836             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2837             {
3276 29 Apr 15 olle 2838               Dna d = Dna.findByName(dc, itemName + ".d");
3276 29 Apr 15 olle 2839               dna = d.getExtract();
3176 06 Mar 15 olle 2840             }
3433 29 Jun 15 olle 2841             else if (Meludi.itemIsDna(itemName))
3176 06 Mar 15 olle 2842             {
3176 06 Mar 15 olle 2843               dna = Extract.getById(dc, itemId.intValue());
3432 29 Jun 15 olle 2844               if (dna == null)
3432 29 Jun 15 olle 2845               {
3517 01 Oct 15 olle 2846                 Dna d = Dna.findByName(dc, itemName);
3432 29 Jun 15 olle 2847                 dna = d.getExtract();
3432 29 Jun 15 olle 2848               }
3176 06 Mar 15 olle 2849             }
3176 06 Mar 15 olle 2850             dna.setItemSubtype(Subtype.DNA.load(dc));
3176 06 Mar 15 olle 2851             dna.setDescription((String)jsonDna.get("comment"));
3176 06 Mar 15 olle 2852             BioMaterialEvent dnaEvent = dna.getCreationEvent();
3433 29 Jun 15 olle 2853             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2854             {
3176 06 Mar 15 olle 2855               dnaEvent.setEventDate(qiacubeDate);
3176 06 Mar 15 olle 2856               dnaEvent.setProtocol(extractionProtocol);
3176 06 Mar 15 olle 2857               dnaEvent.setSource(specimen).setUsedQuantity(itemProcessedQuantity / 3);
3176 06 Mar 15 olle 2858             }
3432 29 Jun 15 olle 2859             //float dnaVolume = ((Number)jsonDna.get("volume")).floatValue();
3432 29 Jun 15 olle 2860             //float dnaQubitConc = ((Number)jsonDna.get("qubitConc")).floatValue();
3432 29 Jun 15 olle 2861             float dnaVolume = fetchFloatValue(jsonDna.get("volume"));
3432 29 Jun 15 olle 2862             float dnaQubitConc = fetchFloatValue(jsonDna.get("qubitConc"));
3176 06 Mar 15 olle 2863             dna.setOriginalQuantity(dnaVolume * dnaQubitConc / 1000);
3432 29 Jun 15 olle 2864             //float dnaDeltaCt = ((Number)jsonDna.get("deltaCt")).floatValue();
3432 29 Jun 15 olle 2865             float dnaDeltaCt = fetchFloatValue(jsonDna.get("deltaCt"));
3433 29 Jun 15 olle 2866             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2867             {
3276 29 Apr 15 olle 2868 /*
3176 06 Mar 15 olle 2869               //dna.setBioWell(StoragePlate.getChildBioWell(dc, specimenWell, "_sp", "_dna"));
3253 17 Apr 15 olle 2870               dna.setBioWell((BioWell)freeDnaWells.get(numStoredDna));
3253 17 Apr 15 olle 2871               numStoredDna++;
3276 29 Apr 15 olle 2872 */
3180 17 Mar 15 olle 2873               Annotationtype.ISOLATION_DATE.setAnnotationValue(dc, dna, isolationDate);
3501 21 Sep 15 olle 2874               Annotationtype.EXTRACTION_OPERATOR.setAnnotationValue(dc, dna, extractionOperator);
3176 06 Mar 15 olle 2875               Annotationtype.QIACUBE_DATE.setAnnotationValue(dc, dna, qiacubeDate);
3501 21 Sep 15 olle 2876               Annotationtype.QIACUBE_OPERATOR.setAnnotationValue(dc, dna, qiacubeOperator);
3176 06 Mar 15 olle 2877               Annotationtype.QIACUBE_RUN_NO.setAnnotationValue(dc, dna, qiacubeRunNo);
3176 06 Mar 15 olle 2878               Annotationtype.QIACUBE_INSTRUMENT_PREFIX.setAnnotationValue(dc, dna, qiacubeInstrumentPrefix);
3176 06 Mar 15 olle 2879               Annotationtype.QIACUBE_POSITION.setAnnotationValue(dc, dna, qiacubePosition);
3176 06 Mar 15 olle 2880             }
3176 06 Mar 15 olle 2881             Annotationtype.QUBIT_CONC.setAnnotationValue(dc, dna, dnaQubitConc);
3176 06 Mar 15 olle 2882             Annotationtype.DELTA_CT.setAnnotationValue(dc, dna, dnaDeltaCt);
3176 06 Mar 15 olle 2883             // Reagents
3433 29 Jun 15 olle 2884             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2885             {
3176 06 Mar 15 olle 2886               // Specimen reagents
3501 21 Sep 15 olle 2887               Annotationtype.ALLPREP_FFPE_KIT.setAnnotationValue(dc, dna, allPrepFfpeKit);
3176 06 Mar 15 olle 2888               Annotationtype.XYLENE_SPECIMEN.setAnnotationValue(dc, dna, xyleneSpecimen);
3176 06 Mar 15 olle 2889               Annotationtype.ETOH995_SPECIMEN.setAnnotationValue(dc, dna, etoh995Specimen);
3176 06 Mar 15 olle 2890               Annotationtype.BUFFER_PKD_SPECIMEN.setAnnotationValue(dc, dna, bufferPkdSpecimen);
3176 06 Mar 15 olle 2891               Annotationtype.BUFFER_ATL_SPECIMEN.setAnnotationValue(dc, dna, bufferAtlSpecimen);
3176 06 Mar 15 olle 2892               Annotationtype.PROTEINASE_K_SPECIMEN.setAnnotationValue(dc, dna, proteinaseKSpecimen);
3176 06 Mar 15 olle 2893               Annotationtype.RNASE_A_SPECIMEN.setAnnotationValue(dc, dna, rnaseASpecimen);
3176 06 Mar 15 olle 2894
3176 06 Mar 15 olle 2895               // QIAcube reagents
3176 06 Mar 15 olle 2896               Annotationtype.ETOH995.setAnnotationValue(dc, dna, etoh995);
3176 06 Mar 15 olle 2897               Annotationtype.BUFFER_RLT.setAnnotationValue(dc, dna, bufferRlt);
3176 06 Mar 15 olle 2898               Annotationtype.BUFFER_FRN.setAnnotationValue(dc, dna, bufferFrn);
3176 06 Mar 15 olle 2899               Annotationtype.BUFFER_RPE.setAnnotationValue(dc, dna, bufferRpe);
3176 06 Mar 15 olle 2900               Annotationtype.BUFFER_AL.setAnnotationValue(dc, dna, bufferAl);
3176 06 Mar 15 olle 2901               Annotationtype.BUFFER_AW1.setAnnotationValue(dc, dna, bufferAw1);
3176 06 Mar 15 olle 2902               Annotationtype.BUFFER_AW2.setAnnotationValue(dc, dna, bufferAw2);
3176 06 Mar 15 olle 2903               Annotationtype.BUFFER_ATE.setAnnotationValue(dc, dna, bufferAte);
3176 06 Mar 15 olle 2904               Annotationtype.BUFFER_RDD.setAnnotationValue(dc, dna, bufferRdd);
3176 06 Mar 15 olle 2905               Annotationtype.RNASE_FREE_WATER.setAnnotationValue(dc, dna, rnaseFreeWater);
3176 06 Mar 15 olle 2906               Annotationtype.DNASE_MIX.setAnnotationValue(dc, dna, dnaseMix);
3176 06 Mar 15 olle 2907               Annotationtype.RNEASY_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, dna, rneasyMinEluteSpinColumn);          
3501 21 Sep 15 olle 2908               Annotationtype.QIAAMP_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, dna, qiaampMinEluteSpinColumn);
3176 06 Mar 15 olle 2909             }
3517 01 Oct 15 olle 2910             // Quality control reagents
3517 01 Oct 15 olle 2911             Annotationtype.KAPA_SYBR_FAST.setAnnotationValue(dc, dna, kapaSybrFast);          
3517 01 Oct 15 olle 2912             Annotationtype.QUALITY_CONTROL_PRIMERS_QCP_RGT.setAnnotationValue(dc, dna, qualityControlPrimersQcpRgt);          
3517 01 Oct 15 olle 2913             Annotationtype.QUALITY_CONTROL_TEMPLATE_QCT.setAnnotationValue(dc, dna, qualityControlTemplateQct);          
3517 01 Oct 15 olle 2914             Annotationtype.QUBIT_DNA_HIGH_SENS.setAnnotationValue(dc, dna, qubitDnaHighSens);          
3517 01 Oct 15 olle 2915             Annotationtype.QUBIT_DNA_BROAD_RANGE.setAnnotationValue(dc, dna, qubitDnaBroadRange);          
3176 06 Mar 15 olle 2916           }
3176 06 Mar 15 olle 2917           
3176 06 Mar 15 olle 2918           // RNA
3433 29 Jun 15 olle 2919           if (Meludi.itemIsSpecimen(itemName) || Meludi.itemIsRna(itemName))
3176 06 Mar 15 olle 2920           {
3176 06 Mar 15 olle 2921             JSONObject jsonRna = (JSONObject)jsonItem.get("rna");
3176 06 Mar 15 olle 2922             Extract rna = null;
3433 29 Jun 15 olle 2923             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2924             {
3276 29 Apr 15 olle 2925               Rna r = Rna.findByName(dc, itemName + ".r");
3276 29 Apr 15 olle 2926               rna = r.getExtract();
3176 06 Mar 15 olle 2927             }
3433 29 Jun 15 olle 2928             else if (Meludi.itemIsRna(itemName))
3176 06 Mar 15 olle 2929             {
3176 06 Mar 15 olle 2930               rna = Extract.getById(dc, itemId.intValue());
3176 06 Mar 15 olle 2931             }
3176 06 Mar 15 olle 2932             rna.setItemSubtype(Subtype.RNA.load(dc));
3176 06 Mar 15 olle 2933             rna.setDescription((String)jsonRna.get("comment"));
3176 06 Mar 15 olle 2934             BioMaterialEvent rnaEvent = rna.getCreationEvent();
3433 29 Jun 15 olle 2935             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2936             {
3176 06 Mar 15 olle 2937               rnaEvent.setEventDate(qiacubeDate);
3176 06 Mar 15 olle 2938               rnaEvent.setProtocol(extractionProtocol);
3176 06 Mar 15 olle 2939               rnaEvent.setSource(specimen).setUsedQuantity(itemProcessedQuantity / 3);
3176 06 Mar 15 olle 2940             }
3432 29 Jun 15 olle 2941             //float rnaVolume = ((Number)jsonRna.get("volume")).floatValue();
3432 29 Jun 15 olle 2942             //float rnaNdConc = ((Number)jsonRna.get("ndConc")).floatValue();
3432 29 Jun 15 olle 2943             float rnaVolume = fetchFloatValue(jsonRna.get("volume"));
3432 29 Jun 15 olle 2944             float rnaNdConc = fetchFloatValue(jsonRna.get("ndConc"));
3176 06 Mar 15 olle 2945             rna.setOriginalQuantity(rnaVolume * rnaNdConc / 1000);
3433 29 Jun 15 olle 2946             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2947             {
3276 29 Apr 15 olle 2948 /*
3176 06 Mar 15 olle 2949               //rna.setBioWell(StoragePlate.getChildBioWell(dc, specimenWell, "_sp", "_rna"));
3253 17 Apr 15 olle 2950               rna.setBioWell((BioWell)freeRnaWells.get(numStoredRna));
3176 06 Mar 15 olle 2951               numStoredRna++;
3276 29 Apr 15 olle 2952 */
3180 17 Mar 15 olle 2953               Annotationtype.ISOLATION_DATE.setAnnotationValue(dc, rna, isolationDate);
3501 21 Sep 15 olle 2954               Annotationtype.EXTRACTION_OPERATOR.setAnnotationValue(dc, rna, extractionOperator);
3176 06 Mar 15 olle 2955               Annotationtype.QIACUBE_DATE.setAnnotationValue(dc, rna, qiacubeDate);
3501 21 Sep 15 olle 2956               Annotationtype.QIACUBE_OPERATOR.setAnnotationValue(dc, rna, qiacubeOperator);
3176 06 Mar 15 olle 2957               Annotationtype.QIACUBE_RUN_NO.setAnnotationValue(dc, rna, qiacubeRunNo);
3176 06 Mar 15 olle 2958               Annotationtype.QIACUBE_INSTRUMENT_PREFIX.setAnnotationValue(dc, rna, qiacubeInstrumentPrefix);
3176 06 Mar 15 olle 2959               Annotationtype.QIACUBE_POSITION.setAnnotationValue(dc, rna, qiacubePosition);
3176 06 Mar 15 olle 2960             }
3176 06 Mar 15 olle 2961             preNormalize = Boolean.TRUE.equals(jsonRna.get("preNormalize"));
3176 06 Mar 15 olle 2962             flagged = jsonRna.get("flag") != null;
3176 06 Mar 15 olle 2963 /*
3176 06 Mar 15 olle 2964             if (preNormalize)
3176 06 Mar 15 olle 2965             {
3176 06 Mar 15 olle 2966               Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, Rna.AUTO_PROCESSING_PRE_NORMALIZE);
3176 06 Mar 15 olle 2967             }
3176 06 Mar 15 olle 2968 */
3176 06 Mar 15 olle 2969 /*
3176 06 Mar 15 olle 2970             else if (flagged)
3176 06 Mar 15 olle 2971             {
3176 06 Mar 15 olle 2972               Annotationtype.FLAG.setAnnotationValue(dc, rna, jsonRna.get("flag"));
3176 06 Mar 15 olle 2973               Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, rna, "Disable");
3176 06 Mar 15 olle 2974               flagged = true;
3176 06 Mar 15 olle 2975               numFlagged++;
3176 06 Mar 15 olle 2976               flaggedRna.add(rna);
3176 06 Mar 15 olle 2977             }
3176 06 Mar 15 olle 2978 */
3176 06 Mar 15 olle 2979             Annotationtype.ND_CONC.setAnnotationValue(dc, rna, rnaNdConc);
3176 06 Mar 15 olle 2980             Annotationtype.ND_260_BY_280.setAnnotationValue(dc, rna, jsonRna.get("nd260by280"));
3176 06 Mar 15 olle 2981             Annotationtype.ND_260_BY_230.setAnnotationValue(dc, rna, jsonRna.get("nd260by230"));
3176 06 Mar 15 olle 2982             // Reagents
3433 29 Jun 15 olle 2983             if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 2984             {
3176 06 Mar 15 olle 2985               // Specimen reagents
3501 21 Sep 15 olle 2986               Annotationtype.ALLPREP_FFPE_KIT.setAnnotationValue(dc, rna, allPrepFfpeKit);
3176 06 Mar 15 olle 2987               Annotationtype.XYLENE_SPECIMEN.setAnnotationValue(dc, rna, xyleneSpecimen);
3176 06 Mar 15 olle 2988               Annotationtype.ETOH995_SPECIMEN.setAnnotationValue(dc, rna, etoh995Specimen);
3176 06 Mar 15 olle 2989               Annotationtype.BUFFER_PKD_SPECIMEN.setAnnotationValue(dc, rna, bufferPkdSpecimen);
3176 06 Mar 15 olle 2990               Annotationtype.BUFFER_ATL_SPECIMEN.setAnnotationValue(dc, rna, bufferAtlSpecimen);
3176 06 Mar 15 olle 2991               Annotationtype.PROTEINASE_K_SPECIMEN.setAnnotationValue(dc, rna, proteinaseKSpecimen);
3176 06 Mar 15 olle 2992               Annotationtype.RNASE_A_SPECIMEN.setAnnotationValue(dc, rna, rnaseASpecimen);
3176 06 Mar 15 olle 2993
3176 06 Mar 15 olle 2994               // QIAcube reagents
3176 06 Mar 15 olle 2995               Annotationtype.ETOH995.setAnnotationValue(dc, rna, etoh995);
3176 06 Mar 15 olle 2996               Annotationtype.BUFFER_RLT.setAnnotationValue(dc, rna, bufferRlt);
3176 06 Mar 15 olle 2997               Annotationtype.BUFFER_FRN.setAnnotationValue(dc, rna, bufferFrn);
3176 06 Mar 15 olle 2998               Annotationtype.BUFFER_RPE.setAnnotationValue(dc, rna, bufferRpe);
3176 06 Mar 15 olle 2999               Annotationtype.BUFFER_AL.setAnnotationValue(dc, rna, bufferAl);
3176 06 Mar 15 olle 3000               Annotationtype.BUFFER_AW1.setAnnotationValue(dc, rna, bufferAw1);
3176 06 Mar 15 olle 3001               Annotationtype.BUFFER_AW2.setAnnotationValue(dc, rna, bufferAw2);
3176 06 Mar 15 olle 3002               Annotationtype.BUFFER_ATE.setAnnotationValue(dc, rna, bufferAte);
3176 06 Mar 15 olle 3003               Annotationtype.BUFFER_RDD.setAnnotationValue(dc, rna, bufferRdd);
3176 06 Mar 15 olle 3004               Annotationtype.RNASE_FREE_WATER.setAnnotationValue(dc, rna, rnaseFreeWater);
3176 06 Mar 15 olle 3005               Annotationtype.DNASE_MIX.setAnnotationValue(dc, rna, dnaseMix);
3176 06 Mar 15 olle 3006               Annotationtype.RNEASY_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, rna, rneasyMinEluteSpinColumn);          
3501 21 Sep 15 olle 3007               Annotationtype.QIAAMP_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, rna, qiaampMinEluteSpinColumn);
3176 06 Mar 15 olle 3008             }
3517 01 Oct 15 olle 3009             // Quality control reagents
3517 01 Oct 15 olle 3010             Annotationtype.KAPA_SYBR_FAST.setAnnotationValue(dc, rna, kapaSybrFast);          
3517 01 Oct 15 olle 3011             Annotationtype.QUALITY_CONTROL_PRIMERS_QCP_RGT.setAnnotationValue(dc, rna, qualityControlPrimersQcpRgt);          
3517 01 Oct 15 olle 3012             Annotationtype.QUALITY_CONTROL_TEMPLATE_QCT.setAnnotationValue(dc, rna, qualityControlTemplateQct);          
3517 01 Oct 15 olle 3013             Annotationtype.QUBIT_DNA_HIGH_SENS.setAnnotationValue(dc, rna, qubitDnaHighSens);          
3517 01 Oct 15 olle 3014             Annotationtype.QUBIT_DNA_BROAD_RANGE.setAnnotationValue(dc, rna, qubitDnaBroadRange);          
3176 06 Mar 15 olle 3015           }
3176 06 Mar 15 olle 3016           
3176 06 Mar 15 olle 3017           String msg = "";
3433 29 Jun 15 olle 3018           if (Meludi.itemIsSpecimen(itemName))
3176 06 Mar 15 olle 3019           {
3276 29 Apr 15 olle 3020             msg += "RNA and DNA updated for " + itemName;
3176 06 Mar 15 olle 3021           }
3433 29 Jun 15 olle 3022           else if (Meludi.itemIsRna(itemName))
3176 06 Mar 15 olle 3023           {
3176 06 Mar 15 olle 3024             msg += "RNA data updated for " + itemName;
3176 06 Mar 15 olle 3025           }
3433 29 Jun 15 olle 3026           else if (Meludi.itemIsDna(itemName))
3176 06 Mar 15 olle 3027           {
3176 06 Mar 15 olle 3028             msg += "DNA data updated for " + itemName;
3176 06 Mar 15 olle 3029           }
3180 17 Mar 15 olle 3030 /*
3176 06 Mar 15 olle 3031           if (flagged)
3176 06 Mar 15 olle 3032           {
3176 06 Mar 15 olle 3033              msg += " (Flagged)";
3176 06 Mar 15 olle 3034           }
3180 17 Mar 15 olle 3035 */
3176 06 Mar 15 olle 3036           else if (!preNormalize)
3176 06 Mar 15 olle 3037           {
3176 06 Mar 15 olle 3038             msg += " (No RNA normalization)";
3176 06 Mar 15 olle 3039           }
3176 06 Mar 15 olle 3040           jsonMessages.add(msg);
3176 06 Mar 15 olle 3041         }
3176 06 Mar 15 olle 3042
3176 06 Mar 15 olle 3043 /*
3176 06 Mar 15 olle 3044         if (numFlagged > 0)
3176 06 Mar 15 olle 3045         {
3176 06 Mar 15 olle 3046           jsonMessages.add(numFlagged + " RNA added to " + flaggedRna.getName() + " list");
3176 06 Mar 15 olle 3047         }
3176 06 Mar 15 olle 3048 */
3176 06 Mar 15 olle 3049         
3176 06 Mar 15 olle 3050         dc.commit();
3276 29 Apr 15 olle 3051 /*        
3176 06 Mar 15 olle 3052         for (Extract rna : newRna)
3176 06 Mar 15 olle 3053         {
3176 06 Mar 15 olle 3054           jsonCreatedRna.add(rna.getId());
3176 06 Mar 15 olle 3055         }
3176 06 Mar 15 olle 3056         json.put("createdRna", jsonCreatedRna);
3176 06 Mar 15 olle 3057         for (Extract dna : newDna)
3176 06 Mar 15 olle 3058         {
3176 06 Mar 15 olle 3059           jsonCreatedDna.add(dna.getId());
3176 06 Mar 15 olle 3060         }
3176 06 Mar 15 olle 3061         json.put("createdDna", jsonCreatedDna);
3276 29 Apr 15 olle 3062 */
3502 22 Sep 15 olle 3063         CounterService.getInstance().setForceCount();
3176 06 Mar 15 olle 3064       }
3176 06 Mar 15 olle 3065       else if ("RegisterNormalizedRNA".equals(cmd))
3176 06 Mar 15 olle 3066       {
3176 06 Mar 15 olle 3067         dc = sc.newDbControl();
3176 06 Mar 15 olle 3068
3176 06 Mar 15 olle 3069         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3176 06 Mar 15 olle 3070
3176 06 Mar 15 olle 3071         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3176 06 Mar 15 olle 3072         JSONArray jsonRnaAliquots = (JSONArray)jsonReq.get("rnaAliquots");
3176 06 Mar 15 olle 3073
3176 06 Mar 15 olle 3074 /*
3176 06 Mar 15 olle 3075         ItemSubtype subtypeNormalized = Subtype.RNA_NORMALIZED_ALIQUOT.load(dc);
3176 06 Mar 15 olle 3076 */
3176 06 Mar 15 olle 3077         
3176 06 Mar 15 olle 3078         Date normalizationDate = Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("normalizationDate"));
3176 06 Mar 15 olle 3079         String operator = (String)jsonReq.get("normalizationOperator");
3176 06 Mar 15 olle 3080
3176 06 Mar 15 olle 3081         for (int i = 0; i < jsonRnaAliquots.size(); ++i)
3176 06 Mar 15 olle 3082         {
3176 06 Mar 15 olle 3083           JSONObject jsonRnaAliquot = (JSONObject)jsonRnaAliquots.get(i);
3176 06 Mar 15 olle 3084           
3176 06 Mar 15 olle 3085           // Get the RNA extract which is the parent
3176 06 Mar 15 olle 3086           Number parentRnaId = (Number)jsonRnaAliquot.get("parentId");
3176 06 Mar 15 olle 3087           Extract parentRna = Extract.getById(dc, parentRnaId.intValue());
3176 06 Mar 15 olle 3088
3176 06 Mar 15 olle 3089 /*
3176 06 Mar 15 olle 3090           // Disable further processing of parent RNA
3176 06 Mar 15 olle 3091           Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, parentRna, "Disable");
3176 06 Mar 15 olle 3092 */
3176 06 Mar 15 olle 3093           
3176 06 Mar 15 olle 3094           // Create a child RNAQC extract
3176 06 Mar 15 olle 3095           Extract normalizedRna = Extract.getNew(dc);
3176 06 Mar 15 olle 3096 /*
3176 06 Mar 15 olle 3097           normalizedRna.setItemSubtype(subtypeNormalized);
3176 06 Mar 15 olle 3098 */
3176 06 Mar 15 olle 3099           normalizedRna.setName((String)jsonRnaAliquot.get("name"));
3176 06 Mar 15 olle 3100
3176 06 Mar 15 olle 3101 /*
3176 06 Mar 15 olle 3102           Annotationtype.DILUTION_DATE.setAnnotationValue(dc, normalizedRna, normalizationDate);
3176 06 Mar 15 olle 3103           Annotationtype.DILUTION_OPERATOR.setAnnotationValue(dc, normalizedRna, operator);
3176 06 Mar 15 olle 3104 */
3176 06 Mar 15 olle 3105
3176 06 Mar 15 olle 3106           BioMaterialEvent creationEvent = normalizedRna.getCreationEvent();
3176 06 Mar 15 olle 3107           creationEvent.setEventDate(normalizationDate);
3176 06 Mar 15 olle 3108
3176 06 Mar 15 olle 3109           Number usedQuantity = (Number)jsonRnaAliquot.get("usedQuantity");
3432 29 Jun 15 olle 3110           //normalizedRna.setOriginalQuantity(usedQuantity.floatValue());
3432 29 Jun 15 olle 3111           normalizedRna.setOriginalQuantity(fetchFloatValue(usedQuantity));
3176 06 Mar 15 olle 3112           BioMaterialEventSource evtSrc = creationEvent.setSource(parentRna);
3432 29 Jun 15 olle 3113           //evtSrc.setUsedQuantity(usedQuantity.floatValue());
3432 29 Jun 15 olle 3114           evtSrc.setUsedQuantity(fetchFloatValue(usedQuantity));
3176 06 Mar 15 olle 3115
3176 06 Mar 15 olle 3116 /*
3176 06 Mar 15 olle 3117           Number ndConc = (Number)jsonRnaAliquot.get("NDConc");
3176 06 Mar 15 olle 3118           Annotationtype.ND_CONC.setAnnotationValue(dc, normalizedRna, ndConc.floatValue());
3176 06 Mar 15 olle 3119 */
3176 06 Mar 15 olle 3120           
3176 06 Mar 15 olle 3121           jsonMessages.add("Created '" + normalizedRna.getName() + "' normalized RNA");
3176 06 Mar 15 olle 3122           dc.saveItem(normalizedRna);
3176 06 Mar 15 olle 3123         }
3176 06 Mar 15 olle 3124         
3176 06 Mar 15 olle 3125         dc.commit();
3176 06 Mar 15 olle 3126       }
3441 21 Jul 15 olle 3127       else if ("CreateStartListsFromStartPlates".equals(cmd))
3441 21 Jul 15 olle 3128       {
3441 21 Jul 15 olle 3129         dc = sc.newDbControl();
3176 06 Mar 15 olle 3130
3441 21 Jul 15 olle 3131         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3441 21 Jul 15 olle 3132
3441 21 Jul 15 olle 3133         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3441 21 Jul 15 olle 3134         JSONArray jsonStartPlateIds = (JSONArray)jsonReq.get("startplates");
3441 21 Jul 15 olle 3135
3441 21 Jul 15 olle 3136 //System.out.println(new Date() + " ExtractionServlet::doPost(): cmd = \"" + cmd + "\" jsonStartPlateIds = " + jsonStartPlateIds);
3441 21 Jul 15 olle 3137
3441 21 Jul 15 olle 3138         List<Integer> startPlateIds = new ArrayList<Integer>();
3441 21 Jul 15 olle 3139         int numSpecimen = 0;
3441 21 Jul 15 olle 3140         int numRna = 0;
3441 21 Jul 15 olle 3141         int numDna = 0;
3441 21 Jul 15 olle 3142         for (int i = 0; i < jsonStartPlateIds.size(); i++)
3441 21 Jul 15 olle 3143         {
3441 21 Jul 15 olle 3144           JSONObject jsonStartPlateId = (JSONObject)jsonStartPlateIds.get(i);
3441 21 Jul 15 olle 3145           String startPlateIdStr = (String)jsonStartPlateId.get("id");
3441 21 Jul 15 olle 3146           Integer startPlateId = Values.getInteger(startPlateIdStr, null);
3441 21 Jul 15 olle 3147           if (startPlateId != null)
3441 21 Jul 15 olle 3148           {
3441 21 Jul 15 olle 3149             startPlateIds.add(startPlateId);
3441 21 Jul 15 olle 3150           }
3441 21 Jul 15 olle 3151         }
3441 21 Jul 15 olle 3152 //System.out.println(new Date() + " ExtractionServlet::doPost(): cmd = \"" + cmd + "\" startPlateIds = " + startPlateIds);
3441 21 Jul 15 olle 3153
3441 21 Jul 15 olle 3154         JSONObject jsonStartItemList = new JSONObject();
3441 21 Jul 15 olle 3155         JSONArray jsonSpecimen = new JSONArray();
3441 21 Jul 15 olle 3156         JSONArray jsonRna = new JSONArray();
3441 21 Jul 15 olle 3157         JSONArray jsonDna = new JSONArray();
3441 21 Jul 15 olle 3158         JSONArray jsonItems = new JSONArray();
3441 21 Jul 15 olle 3159         JSONArray jsonExtraItems = new JSONArray();
3441 21 Jul 15 olle 3160
3441 21 Jul 15 olle 3161         // Use stored annotation snapshots for performance reasons
3441 21 Jul 15 olle 3162         SnapshotManager manager = new SnapshotManager();
3441 21 Jul 15 olle 3163
3441 21 Jul 15 olle 3164         for (Integer startPlateId: startPlateIds)
3441 21 Jul 15 olle 3165         {
3441 21 Jul 15 olle 3166           BioPlate startPlate = BioPlate.getById(dc, startPlateId);
3441 21 Jul 15 olle 3167           if (startPlate != null)
3441 21 Jul 15 olle 3168           {
3441 21 Jul 15 olle 3169 //System.out.println(new Date() + " ExtractionServlet::doPost(): cmd = \"" + cmd + "\" startPlate.getName() = " + startPlate.getName());
3441 21 Jul 15 olle 3170             // Get start plate extraction date
3441 21 Jul 15 olle 3171             Date extractionDate = startPlate.getEventDate();
3441 21 Jul 15 olle 3172
3441 21 Jul 15 olle 3173             // Get start plate extraction annotations
3441 21 Jul 15 olle 3174             Date isolationDate = (Date) Annotationtype.BATCH_ISOLATION_DATE.getAnnotationValue(dc, startPlate);
3441 21 Jul 15 olle 3175             Number extractionProtocolId = (Number) Annotationtype.BATCH_EXTRACTION_PROTOCOL_ID.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3176             Protocol extractionProtocol = extractionProtocolId != null ? Protocol.getById(dc, extractionProtocolId.intValue()) : null;
3441 21 Jul 15 olle 3177             String extractionProtocolName = (String)Annotationtype.BATCH_EXTRACTION_PROTOCOL_NAME.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3178             Float rnaDefaultVolume = (Float)Annotationtype.BATCH_RNA_DEFAULT_VOLUME_IN_MICROLITRE.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3179             Float dnaDefaultVolume = (Float)Annotationtype.BATCH_DNA_DEFAULT_VOLUME_IN_MICROLITRE.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3180
3441 21 Jul 15 olle 3181             // Get start plate QIAcube annotations
3441 21 Jul 15 olle 3182             Date qiacubeDate = (Date) Annotationtype.BATCH_QIACUBE_DATE.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3183             String qiacubePrimaryPrefix = (String)Annotationtype.BATCH_QIACUBE_PRIMARY_PREFIX.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3184             String qiacubeSecondaryPrefix = (String)Annotationtype.BATCH_QIACUBE_SECONDARY_PREFIX.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3185             Integer qiacubeRunNo = (Integer)Annotationtype.BATCH_QIACUBE_RUN_NO.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3186             String qiacubeOperator = (String)Annotationtype.BATCH_QIACUBE_OPERATOR.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3187             String allPrepFfpeKit = (String)Annotationtype.BATCH_ALLPREP_FFPE_KIT.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3188
3441 21 Jul 15 olle 3189             // Get start plate item names annotations
3441 21 Jul 15 olle 3190             List<String> itemNameList = (List<String>) Annotationtype.SOURCE_ITEM_NAMES_LIST_STRING.getAnnotationValues(dc, startPlate, true, true);
3441 21 Jul 15 olle 3191             List<String> extraItemNameList = (List<String>) Annotationtype.EXTRA_QIACUBE_ITEM_NAMES_LIST_STRING.getAnnotationValues(dc, startPlate, true, true);
3441 21 Jul 15 olle 3192             Integer numExtraQiacubeItems = (Integer)Annotationtype.BATCH_NUMBER_EXTRA_QIACUBE_ITEMS.getAnnotationValue(dc, manager, startPlate);
3441 21 Jul 15 olle 3193
3441 21 Jul 15 olle 3194             // Convert item names to items
3441 21 Jul 15 olle 3195             List<Extract> meludiItemList = new ArrayList<Extract>();
3441 21 Jul 15 olle 3196             List<Extract> extraItemList = new ArrayList<Extract>();
3441 21 Jul 15 olle 3197             if (itemNameList != null)
3441 21 Jul 15 olle 3198             {
3441 21 Jul 15 olle 3199               for (String itemName : itemNameList)
3441 21 Jul 15 olle 3200               {
3441 21 Jul 15 olle 3201                 if (Meludi.itemIsSpecimen(itemName))
3441 21 Jul 15 olle 3202                 {
3441 21 Jul 15 olle 3203                   // A specimen is represented by its DNA child extract
3441 21 Jul 15 olle 3204                   String dnaName = itemName + ".d";
3441 21 Jul 15 olle 3205                   Dna d = Dna.findByName(dc, dnaName);
3441 21 Jul 15 olle 3206                   if (d != null)
3441 21 Jul 15 olle 3207                   {
3441 21 Jul 15 olle 3208                     meludiItemList.add(d.getExtract());
3441 21 Jul 15 olle 3209                   }
3441 21 Jul 15 olle 3210                 }
3441 21 Jul 15 olle 3211                 else if (Meludi.itemIsRna(itemName))
3441 21 Jul 15 olle 3212                 {
3441 21 Jul 15 olle 3213                   Rna r = Rna.findByName(dc, itemName);
3441 21 Jul 15 olle 3214                   if (r != null)
3441 21 Jul 15 olle 3215                   {                    
3441 21 Jul 15 olle 3216                     meludiItemList.add(r.getExtract());
3441 21 Jul 15 olle 3217                   }
3441 21 Jul 15 olle 3218                 }
3441 21 Jul 15 olle 3219                 else if (Meludi.itemIsDna(itemName))
3441 21 Jul 15 olle 3220                 {
3441 21 Jul 15 olle 3221                   Dna d = Dna.findByName(dc, itemName);
3441 21 Jul 15 olle 3222                   if (d != null)
3441 21 Jul 15 olle 3223                   {
3441 21 Jul 15 olle 3224                     meludiItemList.add(d.getExtract());
3441 21 Jul 15 olle 3225                   }
3441 21 Jul 15 olle 3226                 }
3441 21 Jul 15 olle 3227               }
3441 21 Jul 15 olle 3228             }
3441 21 Jul 15 olle 3229             
3441 21 Jul 15 olle 3230             // Create default names for extra QIAcube items, if none are specified
3441 21 Jul 15 olle 3231             if (extraItemNameList == null || extraItemNameList.size() == 0)
3441 21 Jul 15 olle 3232             {
3441 21 Jul 15 olle 3233               extraItemNameList = new ArrayList<String>();
3441 21 Jul 15 olle 3234               if (numExtraQiacubeItems != null)
3441 21 Jul 15 olle 3235               {
3441 21 Jul 15 olle 3236                 for (int i = 1; i <= numExtraQiacubeItems; i++)
3441 21 Jul 15 olle 3237                 {
3441 21 Jul 15 olle 3238                   // Create extra item names as E01, E02, ... ,E09, E10, E11, ...
3441 21 Jul 15 olle 3239                   //
3441 21 Jul 15 olle 3240                   // Note that these names will only be unique for a single start list
3441 21 Jul 15 olle 3241                   //
3441 21 Jul 15 olle 3242                   String itemName = "E";
3441 21 Jul 15 olle 3243                   if (i < 10)
3441 21 Jul 15 olle 3244                   {
3441 21 Jul 15 olle 3245                     itemName += "0";
3441 21 Jul 15 olle 3246                   }
3441 21 Jul 15 olle 3247                   extraItemNameList.add(itemName);
3441 21 Jul 15 olle 3248                 }
3441 21 Jul 15 olle 3249               }
3441 21 Jul 15 olle 3250             }
3441 21 Jul 15 olle 3251
3441 21 Jul 15 olle 3252             // Convert extra QIAcube item names to items
3441 21 Jul 15 olle 3253             //
3441 21 Jul 15 olle 3254             // Extra QIAcube items were not registered as items in MeLuDI
3441 21 Jul 15 olle 3255             // prior to version 1.3, so these items may have to be created
3441 21 Jul 15 olle 3256             // using the "Register new extra material" wizard, before running
3441 21 Jul 15 olle 3257             // this command.
3441 21 Jul 15 olle 3258             //
3441 21 Jul 15 olle 3259             if (extraItemNameList != null && extraItemNameList.size() > 0)
3441 21 Jul 15 olle 3260             {
3441 21 Jul 15 olle 3261               for (String itemName : extraItemNameList)
3441 21 Jul 15 olle 3262               {
3441 21 Jul 15 olle 3263                 if (Meludi.itemIsSpecimen(itemName))
3441 21 Jul 15 olle 3264                 {
3441 21 Jul 15 olle 3265                   // A specimen is represented by its DNA child extract
3441 21 Jul 15 olle 3266                   String dnaName = itemName + ".d";
3441 21 Jul 15 olle 3267                   Dna d = Dna.findByName(dc, dnaName);
3441 21 Jul 15 olle 3268                   if (d != null)
3441 21 Jul 15 olle 3269                   {
3441 21 Jul 15 olle 3270                     extraItemList.add(d.getExtract());
3441 21 Jul 15 olle 3271                   }
3441 21 Jul 15 olle 3272                 }
3441 21 Jul 15 olle 3273                 else if (Meludi.itemIsRna(itemName))
3441 21 Jul 15 olle 3274                 {
3441 21 Jul 15 olle 3275                   Rna r = Rna.findByName(dc, itemName);
3441 21 Jul 15 olle 3276                   if (r != null)
3441 21 Jul 15 olle 3277                   {                    
3441 21 Jul 15 olle 3278                     extraItemList.add(r.getExtract());
3441 21 Jul 15 olle 3279                   }
3441 21 Jul 15 olle 3280                 }
3441 21 Jul 15 olle 3281                 else if (Meludi.itemIsDna(itemName))
3441 21 Jul 15 olle 3282                 {
3441 21 Jul 15 olle 3283                   Dna d = Dna.findByName(dc, itemName);
3441 21 Jul 15 olle 3284                   if (d != null)
3441 21 Jul 15 olle 3285                   {
3441 21 Jul 15 olle 3286                     extraItemList.add(d.getExtract());
3441 21 Jul 15 olle 3287                   }
3441 21 Jul 15 olle 3288                 }
3441 21 Jul 15 olle 3289               }
3441 21 Jul 15 olle 3290             }
3441 21 Jul 15 olle 3291
3441 21 Jul 15 olle 3292             // Create start item list
3441 21 Jul 15 olle 3293             ItemList startItemList = null;
3441 21 Jul 15 olle 3294             JSONArray jsonCreatedStartItemList = new JSONArray();
3441 21 Jul 15 olle 3295
4200 01 Nov 16 olle 3296             // Set start item list name to start plate name with start plate prefix exchanged for start list prefix
3441 21 Jul 15 olle 3297             String startPlateName = startPlate.getName();
4150 03 Oct 16 olle 3298             String startPlatePrefix = Meludi.fetchStartPlateItemPrefix(sc.getActiveProjectId());
4150 03 Oct 16 olle 3299             String nameWithoutPrefix = startPlateName.substring(startPlatePrefix.length());
4150 03 Oct 16 olle 3300             String itemListName = Meludi.fetchStartListItemPrefix(sc.getActiveProjectId()) + nameWithoutPrefix;
3441 21 Jul 15 olle 3301             if (itemListName != null && !itemListName.equals(""))
3441 21 Jul 15 olle 3302             {
3441 21 Jul 15 olle 3303               startItemList = ItemList.getNew(dc, Item.EXTRACT);
3441 21 Jul 15 olle 3304               startItemList.setName(itemListName);
3441 21 Jul 15 olle 3305               jsonMessages.add("Created '" + startItemList.getName() + "' Start item list");
3441 21 Jul 15 olle 3306 //System.out.println(new Date() + " ExtractionServlet::doPost(): cmd = \"" + cmd + "\" startItemList.getName() = " + startItemList.getName());
3441 21 Jul 15 olle 3307
3441 21 Jul 15 olle 3308               // Set start list extraction annotations
3441 21 Jul 15 olle 3309               Annotationtype.LIST_EXTRACTION_DATE.setAnnotationValue(dc, startItemList, extractionDate);
3441 21 Jul 15 olle 3310               Annotationtype.LIST_ISOLATION_DATE.setAnnotationValue(dc, startItemList, isolationDate);
3441 21 Jul 15 olle 3311               Annotationtype.LIST_EXTRACTION_PROTOCOL_ID.setAnnotationValue(dc, startItemList, extractionProtocolId);
3441 21 Jul 15 olle 3312               Annotationtype.LIST_EXTRACTION_PROTOCOL_NAME.setAnnotationValue(dc, startItemList, extractionProtocolName);
3441 21 Jul 15 olle 3313               Annotationtype.LIST_RNA_DEFAULT_VOLUME_IN_MICROLITRE.setAnnotationValue(dc, startItemList, rnaDefaultVolume);
3441 21 Jul 15 olle 3314               Annotationtype.LIST_DNA_DEFAULT_VOLUME_IN_MICROLITRE.setAnnotationValue(dc, startItemList, dnaDefaultVolume);
3441 21 Jul 15 olle 3315
3441 21 Jul 15 olle 3316               // Set start list QIAcube annotations
3441 21 Jul 15 olle 3317               Annotationtype.LIST_QIACUBE_DATE.setAnnotationValue(dc, startItemList, qiacubeDate);
3441 21 Jul 15 olle 3318               Annotationtype.LIST_QIACUBE_PRIMARY_PREFIX.setAnnotationValue(dc, startItemList, qiacubePrimaryPrefix);
3441 21 Jul 15 olle 3319               Annotationtype.LIST_QIACUBE_SECONDARY_PREFIX.setAnnotationValue(dc, startItemList, qiacubeSecondaryPrefix);
3441 21 Jul 15 olle 3320               Annotationtype.LIST_QIACUBE_RUN_NO.setAnnotationValue(dc, startItemList, qiacubeRunNo);
3441 21 Jul 15 olle 3321               Annotationtype.LIST_QIACUBE_OPERATOR.setAnnotationValue(dc, startItemList, qiacubeOperator);
3441 21 Jul 15 olle 3322               Annotationtype.LIST_ALLPREP_FFPE_KIT.setAnnotationValue(dc, startItemList, allPrepFfpeKit);
3441 21 Jul 15 olle 3323
3445 23 Jul 15 olle 3324               if (extractionDate != null)
3445 23 Jul 15 olle 3325               {
3445 23 Jul 15 olle 3326                 // A set extraction date = start plate creation date indicates
3445 23 Jul 15 olle 3327                 // that the plate is processed, i.e. that concentrations and delta-Ct
3445 23 Jul 15 olle 3328                 // values have been registered for extracts related to the plate.
3445 23 Jul 15 olle 3329                 // However, in MeLuDI v1.2.* the latter data was only registered for MeLuDI items,
3445 23 Jul 15 olle 3330                 // not for extra material.
3445 23 Jul 15 olle 3331                 //
3445 23 Jul 15 olle 3332                 // A processed start item list is indicated by annotation
3445 23 Jul 15 olle 3333                 // "SamplePrepListIsProcessed" having value `true`.
3445 23 Jul 15 olle 3334                 // If the start plate was processed and not contained extra material,
3445 23 Jul 15 olle 3335                 // the created item list should also be indicated as processed.
3445 23 Jul 15 olle 3336                 // This should not be done for start lists with extra material, as it would
3446 27 Jul 15 olle 3337                 // prevent the start list from being opened in the "DNA/RNA registration/quantification"
3445 23 Jul 15 olle 3338                 // wizard, in order to register concentrations and delta-Ct values for the extra material.
3445 23 Jul 15 olle 3339                 if (extraItemNameList == null || extraItemNameList.size() == 0)
3445 23 Jul 15 olle 3340                 {
3445 23 Jul 15 olle 3341                   // Mark created start item list as processed
3445 23 Jul 15 olle 3342                   Annotationtype.SAMPLE_PREP_LIST_IS_PROCESSED.setAnnotationValue(dc, startItemList, true);
3445 23 Jul 15 olle 3343                 }
3445 23 Jul 15 olle 3344               }
3445 23 Jul 15 olle 3345
3441 21 Jul 15 olle 3346               // Add extract member items to start item list
3441 21 Jul 15 olle 3347               for (Extract item: meludiItemList)
3441 21 Jul 15 olle 3348               {
3441 21 Jul 15 olle 3349                 startItemList.add(item);
3441 21 Jul 15 olle 3350                 jsonMessages.add("Extract " + item.getName() + " added as member to start item list " + startItemList.getName());
3441 21 Jul 15 olle 3351               }
3441 21 Jul 15 olle 3352               for (Extract item: extraItemList)
3441 21 Jul 15 olle 3353               {
3441 21 Jul 15 olle 3354                 startItemList.add(item);
3441 21 Jul 15 olle 3355                 jsonMessages.add("Extract " + item.getName() + " added as member to start item list " + startItemList.getName());
3441 21 Jul 15 olle 3356               }
3441 21 Jul 15 olle 3357               //
3441 21 Jul 15 olle 3358               dc.saveItem(startItemList);
3441 21 Jul 15 olle 3359             }
3441 21 Jul 15 olle 3360           }
3441 21 Jul 15 olle 3361         }
3441 21 Jul 15 olle 3362         
3441 21 Jul 15 olle 3363         dc.commit();
3502 22 Sep 15 olle 3364         CounterService.getInstance().setForceCount();
3441 21 Jul 15 olle 3365       }
3501 21 Sep 15 olle 3366       else if ("UpdateStartListsWithLotNumbersFromExtracts".equals(cmd))
3501 21 Sep 15 olle 3367       {
3501 21 Sep 15 olle 3368         dc = sc.newDbControl();
3501 21 Sep 15 olle 3369
3501 21 Sep 15 olle 3370         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.SAMPLE_PREP, MeludiRole.ADMINISTRATOR);
3501 21 Sep 15 olle 3371
3501 21 Sep 15 olle 3372         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3501 21 Sep 15 olle 3373         JSONArray jsonStartItemListIds = (JSONArray)jsonReq.get("startitemlists");
3501 21 Sep 15 olle 3374
3501 21 Sep 15 olle 3375         // Use stored annotation snapshots for performance reasons
3501 21 Sep 15 olle 3376         SnapshotManager manager = new SnapshotManager();
3501 21 Sep 15 olle 3377
3501 21 Sep 15 olle 3378         for (int i = 0; i < jsonStartItemListIds.size(); i++)
3501 21 Sep 15 olle 3379         {
3501 21 Sep 15 olle 3380           JSONObject jsonStartItemListId = (JSONObject)jsonStartItemListIds.get(i);
3501 21 Sep 15 olle 3381           String startItemListIdStr = (String)jsonStartItemListId.get("id");
3501 21 Sep 15 olle 3382           Integer startItemListId = Values.getInteger(startItemListIdStr, null);
3501 21 Sep 15 olle 3383           if (startItemListId != null)
3501 21 Sep 15 olle 3384           {
3501 21 Sep 15 olle 3385             ItemList startItemList = ItemList.getById(dc, startItemListId);
3501 21 Sep 15 olle 3386             if (startItemList != null)
3501 21 Sep 15 olle 3387             {
3501 21 Sep 15 olle 3388               StartList sl = new StartList(startItemList);
3501 21 Sep 15 olle 3389               boolean meludiItemsFirst = true;
3501 21 Sep 15 olle 3390               List<Extract> extractList = sl.fetchExtractListFromItemList(dc, StartList.ITEM_DNA, meludiItemsFirst);
3501 21 Sep 15 olle 3391               if (extractList == null)
3501 21 Sep 15 olle 3392               {
3501 21 Sep 15 olle 3393                 // Try to find any RNA extract member items
3501 21 Sep 15 olle 3394                 extractList = sl.fetchExtractListFromItemList(dc, StartList.ITEM_RNA, meludiItemsFirst);
3501 21 Sep 15 olle 3395               }
3501 21 Sep 15 olle 3396               if (extractList != null)
3501 21 Sep 15 olle 3397               {
3516 01 Oct 15 olle 3398                 // Get first extract member item that was processed from specimen
3516 01 Oct 15 olle 3399                 Extract e = null;
3516 01 Oct 15 olle 3400                 for (Extract extract: extractList)
3516 01 Oct 15 olle 3401                 {
3516 01 Oct 15 olle 3402                   // Exclude input DNA and RNA, since these lack lot number annotations
3516 01 Oct 15 olle 3403                   if (extract != null && !Meludi.itemIsExtractSourceItem(extract.getName()))
3516 01 Oct 15 olle 3404                   {
3516 01 Oct 15 olle 3405                     e = extract;
3516 01 Oct 15 olle 3406                     break;
3516 01 Oct 15 olle 3407                   }
3516 01 Oct 15 olle 3408                 }
3501 21 Sep 15 olle 3409                 if (e != null)
3501 21 Sep 15 olle 3410                 {
3501 21 Sep 15 olle 3411 //System.out.println(new Date() + " ExtractionServlet::doPost(): cmd = \"" + cmd + "\" startItemList.getName() = " + startItemList.getName() + " e.getName() = " + e.getName());
3501 21 Sep 15 olle 3412                   // Get lot numbers etc from extract annotations
3501 21 Sep 15 olle 3413
3501 21 Sep 15 olle 3414                   // Specimen reagents
3501 21 Sep 15 olle 3415                   String xyleneSpecimen = (String) Annotationtype.XYLENE_SPECIMEN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3416                   String etoh995Specimen = (String) Annotationtype.ETOH995_SPECIMEN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3417                   String allPrepFfpeKit = (String) Annotationtype.ALLPREP_FFPE_KIT.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3418                   String bufferPkdSpecimen = (String) Annotationtype.BUFFER_PKD_SPECIMEN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3419                   String bufferAtlSpecimen = (String) Annotationtype.BUFFER_ATL_SPECIMEN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3420                   String proteinaseKSpecimen = (String) Annotationtype.PROTEINASE_K_SPECIMEN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3421                   String rnaseASpecimen = (String) Annotationtype.RNASE_A_SPECIMEN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3422
3501 21 Sep 15 olle 3423                   // QIAcube reagents
3501 21 Sep 15 olle 3424                   String etoh995 = (String) Annotationtype.ETOH995.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3425                   String bufferRlt = (String) Annotationtype.BUFFER_RLT.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3426                   String bufferFrn = (String) Annotationtype.BUFFER_FRN.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3427                   String bufferRpe = (String) Annotationtype.BUFFER_RPE.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3428                   String bufferAl = (String) Annotationtype.BUFFER_AL.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3429                   String bufferAw1 = (String) Annotationtype.BUFFER_AW1.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3430                   String bufferAw2 = (String) Annotationtype.BUFFER_AW2.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3431                   String bufferAte = (String) Annotationtype.BUFFER_ATE.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3432                   String bufferRdd = (String) Annotationtype.BUFFER_RDD.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3433                   String rnaseFreeWater = (String) Annotationtype.RNASE_FREE_WATER.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3434                   String dnaseMix = (String) Annotationtype.DNASE_MIX.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3435                   String rneasyMinEluteSpinColumn = (String) Annotationtype.RNEASY_MIN_ELUTE_SPIN_COL.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3436                   String qiaampMinEluteSpinColumn = (String) Annotationtype.QIAAMP_MIN_ELUTE_SPIN_COL.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3437
3501 21 Sep 15 olle 3438                   // Quality control reagents
3501 21 Sep 15 olle 3439                   String kapaSybrFast = (String) Annotationtype.KAPA_SYBR_FAST.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3440                   String qualityControlPrimersQcpRgt = (String) Annotationtype.QUALITY_CONTROL_PRIMERS_QCP_RGT.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3441                   String qualityControlTemplateQct = (String) Annotationtype.QUALITY_CONTROL_TEMPLATE_QCT.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3442                   String qubitDnaHighSens = (String) Annotationtype.QUBIT_DNA_HIGH_SENS.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3443                   String qubitDnaBroadRange = (String) Annotationtype.QUBIT_DNA_BROAD_RANGE.getAnnotationValue(dc, manager, e);
3501 21 Sep 15 olle 3444
3501 21 Sep 15 olle 3445                   // Set start list specimen reagents annotations
3501 21 Sep 15 olle 3446                   Annotationtype.LIST_XYLENE_SPECIMEN.setAnnotationValue(dc, startItemList, xyleneSpecimen);
3501 21 Sep 15 olle 3447                   Annotationtype.LIST_ETOH995_SPECIMEN.setAnnotationValue(dc, startItemList, etoh995Specimen);
3501 21 Sep 15 olle 3448                   Annotationtype.LIST_ALLPREP_FFPE_KIT.setAnnotationValue(dc, startItemList, allPrepFfpeKit);
3501 21 Sep 15 olle 3449                   Annotationtype.LIST_BUFFER_PKD_SPECIMEN.setAnnotationValue(dc, startItemList, bufferPkdSpecimen);
3501 21 Sep 15 olle 3450                   Annotationtype.LIST_BUFFER_ATL_SPECIMEN.setAnnotationValue(dc, startItemList, bufferAtlSpecimen);
3501 21 Sep 15 olle 3451                   Annotationtype.LIST_PROTEINASE_K_SPECIMEN.setAnnotationValue(dc, startItemList, proteinaseKSpecimen);
3501 21 Sep 15 olle 3452                   Annotationtype.LIST_RNASE_A_SPECIMEN.setAnnotationValue(dc, startItemList, rnaseASpecimen);
3501 21 Sep 15 olle 3453
3501 21 Sep 15 olle 3454                   // Set start list QIAcube reagents annotations
3501 21 Sep 15 olle 3455                   Annotationtype.LIST_ETOH995.setAnnotationValue(dc, startItemList, etoh995);
3501 21 Sep 15 olle 3456                   Annotationtype.LIST_BUFFER_RLT.setAnnotationValue(dc, startItemList, bufferRlt);
3501 21 Sep 15 olle 3457                   Annotationtype.LIST_BUFFER_FRN.setAnnotationValue(dc, startItemList, bufferFrn);
3501 21 Sep 15 olle 3458                   Annotationtype.LIST_BUFFER_RPE.setAnnotationValue(dc, startItemList, bufferRpe);
3501 21 Sep 15 olle 3459                   Annotationtype.LIST_BUFFER_AL.setAnnotationValue(dc, startItemList, bufferAl);
3501 21 Sep 15 olle 3460                   Annotationtype.LIST_BUFFER_AW1.setAnnotationValue(dc, startItemList, bufferAw1);
3501 21 Sep 15 olle 3461                   Annotationtype.LIST_BUFFER_AW2.setAnnotationValue(dc, startItemList, bufferAw2);
3501 21 Sep 15 olle 3462                   Annotationtype.LIST_BUFFER_ATE.setAnnotationValue(dc, startItemList, bufferAte);
3501 21 Sep 15 olle 3463                   Annotationtype.LIST_BUFFER_RDD.setAnnotationValue(dc, startItemList, bufferRdd);
3501 21 Sep 15 olle 3464                   Annotationtype.LIST_RNASE_FREE_WATER.setAnnotationValue(dc, startItemList, rnaseFreeWater);
3501 21 Sep 15 olle 3465                   Annotationtype.LIST_DNASE_MIX.setAnnotationValue(dc, startItemList, dnaseMix);
3501 21 Sep 15 olle 3466                   Annotationtype.LIST_RNEASY_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, startItemList, rneasyMinEluteSpinColumn);
3501 21 Sep 15 olle 3467                   Annotationtype.LIST_QIAAMP_MIN_ELUTE_SPIN_COL.setAnnotationValue(dc, startItemList, qiaampMinEluteSpinColumn);
3501 21 Sep 15 olle 3468
3501 21 Sep 15 olle 3469                   // Set start list quality control reagents annotations
3501 21 Sep 15 olle 3470                   Annotationtype.LIST_KAPA_SYBR_FAST.setAnnotationValue(dc, startItemList, kapaSybrFast);
3501 21 Sep 15 olle 3471                   Annotationtype.LIST_QUALITY_CONTROL_PRIMERS_QCP_RGT.setAnnotationValue(dc, startItemList, qualityControlPrimersQcpRgt);
3501 21 Sep 15 olle 3472                   Annotationtype.LIST_QUALITY_CONTROL_TEMPLATE_QCT.setAnnotationValue(dc, startItemList, qualityControlTemplateQct);
3501 21 Sep 15 olle 3473                   Annotationtype.LIST_QUBIT_DNA_HIGH_SENS.setAnnotationValue(dc, startItemList, qubitDnaHighSens);
3501 21 Sep 15 olle 3474                   Annotationtype.LIST_QUBIT_DNA_BROAD_RANGE.setAnnotationValue(dc, startItemList, qubitDnaBroadRange);
3501 21 Sep 15 olle 3475
3501 21 Sep 15 olle 3476                   jsonMessages.add("Updated '" + startItemList.getName() + "' Start item list with lot number annotations from first DNA extract");
3501 21 Sep 15 olle 3477                 }
3501 21 Sep 15 olle 3478               }
3501 21 Sep 15 olle 3479             }
3501 21 Sep 15 olle 3480           }
3501 21 Sep 15 olle 3481         }
3501 21 Sep 15 olle 3482         
3501 21 Sep 15 olle 3483         dc.commit();
3501 21 Sep 15 olle 3484       }
3441 21 Jul 15 olle 3485       
3176 06 Mar 15 olle 3486       json.put("messages", jsonMessages);
3176 06 Mar 15 olle 3487     }
3176 06 Mar 15 olle 3488     catch (Throwable t)
3176 06 Mar 15 olle 3489     {
3176 06 Mar 15 olle 3490       t.printStackTrace();
3176 06 Mar 15 olle 3491       json.clear();
3176 06 Mar 15 olle 3492       json.put("status", "error");
3176 06 Mar 15 olle 3493       json.put("message", t.getMessage());
3176 06 Mar 15 olle 3494       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3176 06 Mar 15 olle 3495     }
3176 06 Mar 15 olle 3496     finally
3176 06 Mar 15 olle 3497     {
3176 06 Mar 15 olle 3498       if (dc != null) dc.close();
3176 06 Mar 15 olle 3499       json.writeJSONString(resp.getWriter());
3176 06 Mar 15 olle 3500     }
3176 06 Mar 15 olle 3501     
3176 06 Mar 15 olle 3502   }
3176 06 Mar 15 olle 3503
3176 06 Mar 15 olle 3504   /**
3318 08 May 15 olle 3505    * Returns specimen type for the sample, based on the latter's specimenType annotation.
3318 08 May 15 olle 3506    * If a specimen type annotation is found, a more readable string of this
3318 08 May 15 olle 3507    * content is returned.
3318 08 May 15 olle 3508    * 
3318 08 May 15 olle 3509    * @param dc DbControl The DbControl to use.
3318 08 May 15 olle 3510    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
3318 08 May 15 olle 3511    * @param s Sample The sample to find the specimen type for.
3318 08 May 15 olle 3512    * @return String The specimen type for the sample.
3318 08 May 15 olle 3513    */
3318 08 May 15 olle 3514   private String fetchSpecimenType(DbControl dc, SnapshotManager manager, Sample s)
3318 08 May 15 olle 3515   {
3318 08 May 15 olle 3516     String specimenType = (String) Annotationtype.SPECIMEN_TYPE.getAnnotationValue(dc, manager, s);
3318 08 May 15 olle 3517     specimenType = fetchSpecimenType(specimenType);
3318 08 May 15 olle 3518     return specimenType;
3318 08 May 15 olle 3519   }
3318 08 May 15 olle 3520
3318 08 May 15 olle 3521   /**
3318 08 May 15 olle 3522    * Returns specimen type for the extract, based on the latter's specimenTypeExtract annotation.
3318 08 May 15 olle 3523    * If a specimen type annotation is found, a more readable string of this
3318 08 May 15 olle 3524    * content is returned.
3318 08 May 15 olle 3525    * 
3318 08 May 15 olle 3526    * @param dc DbControl The DbControl to use.
3318 08 May 15 olle 3527    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
3318 08 May 15 olle 3528    * @param e Extract The extract to find the specimen type for.
3318 08 May 15 olle 3529    * @return String The specimen type for the extract.
3318 08 May 15 olle 3530    */
3318 08 May 15 olle 3531   private String fetchSpecimenTypeExtract(DbControl dc, SnapshotManager manager, Extract e)
3318 08 May 15 olle 3532   {
3318 08 May 15 olle 3533     String specimenType = (String) Annotationtype.SPECIMEN_TYPE_EXTRACT.getAnnotationValue(dc, manager, e);
3318 08 May 15 olle 3534     specimenType = fetchSpecimenType(specimenType);
3318 08 May 15 olle 3535     return specimenType;
3318 08 May 15 olle 3536   }
3318 08 May 15 olle 3537
3318 08 May 15 olle 3538   /**
3318 08 May 15 olle 3539    * Returns a more readable string of a specimen type.
3318 08 May 15 olle 3540    * 
3318 08 May 15 olle 3541    * @param dc DbControl The DbControl to use.
3318 08 May 15 olle 3542    * @param manager SnapshotManager The SnapshotManager to use for speeding up annotation retrieval.
3318 08 May 15 olle 3543    * @param rawSpecimenType String The original specimen type string.
3318 08 May 15 olle 3544    * @return String A more readable string of the input specimen type string.
3318 08 May 15 olle 3545    */
3318 08 May 15 olle 3546   private String fetchSpecimenType(String rawSpecimenType)
3318 08 May 15 olle 3547   {
3318 08 May 15 olle 3548     String specimenType = rawSpecimenType;
3318 08 May 15 olle 3549     if (specimenType != null)
3318 08 May 15 olle 3550     {
3318 08 May 15 olle 3551       if (specimenType.equals(""))
3318 08 May 15 olle 3552       {
3318 08 May 15 olle 3553         specimenType = "unknown";
3318 08 May 15 olle 3554       }
3318 08 May 15 olle 3555       else if (specimenType.equals("Primary"))
3318 08 May 15 olle 3556       {
3318 08 May 15 olle 3557         specimenType = "Primary tumour";
3318 08 May 15 olle 3558       }
3318 08 May 15 olle 3559       else if (specimenType.equals("MetastasisDistant"))
3318 08 May 15 olle 3560       {
3318 08 May 15 olle 3561         specimenType = "Metastasis";
3318 08 May 15 olle 3562       }
3318 08 May 15 olle 3563     }
3318 08 May 15 olle 3564     return specimenType;
3318 08 May 15 olle 3565   }
3318 08 May 15 olle 3566
3318 08 May 15 olle 3567   /**
3176 06 Mar 15 olle 3568    * Returns the first character in input QIAcube position string,
3176 06 Mar 15 olle 3569    * if uppercase letter followed by QIAcube position number, else `null`.
3176 06 Mar 15 olle 3570    * 
3176 06 Mar 15 olle 3571    * @param qiacubePositionStr String
3176 06 Mar 15 olle 3572    * @return String The QIAcube instrument prefix, if capital letter followed by QIAcube position number, else `null`.
3176 06 Mar 15 olle 3573    */
3176 06 Mar 15 olle 3574   private String fetchQiacubeInstrumentPrefix(String qiacubePositionStr)
3176 06 Mar 15 olle 3575   {
3176 06 Mar 15 olle 3576     String qiacubeInstrumentPrefix = null;
3176 06 Mar 15 olle 3577     if (qiacubePositionStr != null && !qiacubePositionStr.equals(""))
3176 06 Mar 15 olle 3578     {
3176 06 Mar 15 olle 3579       // Check if string contains uppercase letter + QIAcube position number 1-12
3176 06 Mar 15 olle 3580       if (qiacubePositionStr.matches("^[A-Z]([2-9]|1[0-2]?)$"))
3176 06 Mar 15 olle 3581       {
3176 06 Mar 15 olle 3582         qiacubeInstrumentPrefix = qiacubePositionStr.substring(0,1);
3176 06 Mar 15 olle 3583       }
3176 06 Mar 15 olle 3584     }
3176 06 Mar 15 olle 3585     return qiacubeInstrumentPrefix;
3176 06 Mar 15 olle 3586   }
3176 06 Mar 15 olle 3587
3176 06 Mar 15 olle 3588   /**
3176 06 Mar 15 olle 3589    * Returns the QIAcube position, after removal of optional QIAcube instrument prefix.
3176 06 Mar 15 olle 3590    * 
3176 06 Mar 15 olle 3591    * @param qiacubePositionStr String
3176 06 Mar 15 olle 3592    * @return Number The QIAcube position, after removal of optional QIAcube instrument prefix.
3176 06 Mar 15 olle 3593    */
3176 06 Mar 15 olle 3594   private Number fetchQiacubePosition(String qiacubePositionStr)
3176 06 Mar 15 olle 3595   {
3176 06 Mar 15 olle 3596     Number qiacubePosition = null;
3176 06 Mar 15 olle 3597     if (qiacubePositionStr != null && !qiacubePositionStr.equals(""))
3176 06 Mar 15 olle 3598     {
3176 06 Mar 15 olle 3599       String qiacubePositionNumberStr = qiacubePositionStr;
3176 06 Mar 15 olle 3600       // Check if string contains uppercase letter + QIAcube position number 1-12
3176 06 Mar 15 olle 3601       if (qiacubePositionStr.matches("^[A-Z]([2-9]|1[0-2]?)$"))
3176 06 Mar 15 olle 3602       {
3176 06 Mar 15 olle 3603         qiacubePositionNumberStr = qiacubePositionStr.substring(1, qiacubePositionStr.length());
3176 06 Mar 15 olle 3604       }
3176 06 Mar 15 olle 3605       qiacubePosition = (Number)Integer.parseInt(qiacubePositionNumberStr);
3176 06 Mar 15 olle 3606     }
3176 06 Mar 15 olle 3607     return qiacubePosition;
3176 06 Mar 15 olle 3608   }
3176 06 Mar 15 olle 3609
3221 01 Apr 15 olle 3610   /**
3221 01 Apr 15 olle 3611    * Returns the float value for a number object,
3221 01 Apr 15 olle 3612    * or 0.0, if the object is `null` or not a number. 
3221 01 Apr 15 olle 3613    *
3221 01 Apr 15 olle 3614    * @param object The number object.
3221 01 Apr 15 olle 3615    * @return The object as float value, or 0.0, if the value is `null` or not a number. 
3221 01 Apr 15 olle 3616    */
3176 06 Mar 15 olle 3617   private float fetchFloatValue(Object object)
3176 06 Mar 15 olle 3618   {
3176 06 Mar 15 olle 3619     float value = 0.0f;
3176 06 Mar 15 olle 3620     if (object != null)
3176 06 Mar 15 olle 3621     {
3176 06 Mar 15 olle 3622       try
3176 06 Mar 15 olle 3623       {
3221 01 Apr 15 olle 3624         value = ((Number)object).floatValue();
3176 06 Mar 15 olle 3625       }
3176 06 Mar 15 olle 3626       catch (NumberFormatException e)
3176 06 Mar 15 olle 3627       {}
3176 06 Mar 15 olle 3628     }
3176 06 Mar 15 olle 3629     return value;
3176 06 Mar 15 olle 3630   }
3176 06 Mar 15 olle 3631
3176 06 Mar 15 olle 3632   @SuppressWarnings("unchecked")
3180 17 Mar 15 olle 3633   long findNumberOfPlates(DbControl dc, String platePrefix)
3176 06 Mar 15 olle 3634   {
3176 06 Mar 15 olle 3635     String queryString = "%";
3180 17 Mar 15 olle 3636     if (platePrefix != null)
3176 06 Mar 15 olle 3637     {
3180 17 Mar 15 olle 3638       queryString = platePrefix + "%";
3176 06 Mar 15 olle 3639     }
3176 06 Mar 15 olle 3640
3176 06 Mar 15 olle 3641     ItemQuery<BioPlate> plateQuery = BioPlate.getQuery();
3176 06 Mar 15 olle 3642     plateQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string(queryString)));
3176 06 Mar 15 olle 3643     plateQuery.order(Orders.asc(Hql.property("name")));
3176 06 Mar 15 olle 3644     long numPlates = plateQuery.count(dc);
3176 06 Mar 15 olle 3645     return numPlates;
3176 06 Mar 15 olle 3646   }
3176 06 Mar 15 olle 3647
3176 06 Mar 15 olle 3648   @SuppressWarnings("unchecked")
3180 17 Mar 15 olle 3649   List<BioPlate> findPlates(DbControl dc, String platePrefix)
3176 06 Mar 15 olle 3650   {
3176 06 Mar 15 olle 3651     String queryString = "%";
3180 17 Mar 15 olle 3652     if (platePrefix != null)
3176 06 Mar 15 olle 3653     {
3180 17 Mar 15 olle 3654       queryString = platePrefix + "%";
3176 06 Mar 15 olle 3655     }
3176 06 Mar 15 olle 3656
3176 06 Mar 15 olle 3657     ItemQuery<BioPlate> plateQuery = BioPlate.getQuery();
3176 06 Mar 15 olle 3658     plateQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string(queryString)));
3176 06 Mar 15 olle 3659     plateQuery.order(Orders.asc(Hql.property("name")));
3176 06 Mar 15 olle 3660     List<BioPlate> plates = plateQuery.list(dc);
3176 06 Mar 15 olle 3661     return plates;
3176 06 Mar 15 olle 3662   }
3180 17 Mar 15 olle 3663
3180 17 Mar 15 olle 3664   @SuppressWarnings("unchecked")
3180 17 Mar 15 olle 3665   BioPlate findPlateWithHighestNumber(DbControl dc, String platePrefix)
3180 17 Mar 15 olle 3666   {
3180 17 Mar 15 olle 3667     String queryString = "%";
3180 17 Mar 15 olle 3668     if (platePrefix != null)
3180 17 Mar 15 olle 3669     {
3180 17 Mar 15 olle 3670       queryString = platePrefix + "%";
3180 17 Mar 15 olle 3671     }
3180 17 Mar 15 olle 3672
3180 17 Mar 15 olle 3673     ItemQuery<BioPlate> plateQuery = BioPlate.getQuery();
3180 17 Mar 15 olle 3674     plateQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string(queryString)));
3180 17 Mar 15 olle 3675     plateQuery.order(Orders.asc(Hql.property("name")));
3180 17 Mar 15 olle 3676     List<BioPlate> plates = plateQuery.list(dc);
3180 17 Mar 15 olle 3677     BioPlate plateWithHighestNumber = null;
3180 17 Mar 15 olle 3678     int numPlates = plates.size();
3180 17 Mar 15 olle 3679     if (numPlates > 0)
3180 17 Mar 15 olle 3680     {
3180 17 Mar 15 olle 3681       plateWithHighestNumber = (BioPlate) plates.get(numPlates-1);
3180 17 Mar 15 olle 3682     }
3180 17 Mar 15 olle 3683     return plateWithHighestNumber;
3180 17 Mar 15 olle 3684   }
3432 29 Jun 15 olle 3685
3432 29 Jun 15 olle 3686   @SuppressWarnings("unchecked")
3432 29 Jun 15 olle 3687   private ItemList findItemListWithHighestNumber(DbControl dc, String itemListPrefix)
3432 29 Jun 15 olle 3688   {
3432 29 Jun 15 olle 3689     String queryString = "%";
3432 29 Jun 15 olle 3690     if (itemListPrefix != null)
3432 29 Jun 15 olle 3691     {
3432 29 Jun 15 olle 3692       queryString = itemListPrefix + "%";
3432 29 Jun 15 olle 3693     }
3432 29 Jun 15 olle 3694
3432 29 Jun 15 olle 3695     ItemQuery<ItemList> itemListQuery = ItemList.getQuery();
3432 29 Jun 15 olle 3696     itemListQuery.restrict(Restrictions.like(Hql.property("name"), Expressions.string(queryString)));
3432 29 Jun 15 olle 3697     itemListQuery.order(Orders.asc(Hql.property("name")));
3432 29 Jun 15 olle 3698     List<ItemList> itemLists = itemListQuery.list(dc);
3432 29 Jun 15 olle 3699     ItemList itemListWithHighestNumber = null;
3432 29 Jun 15 olle 3700     int numItemLists = itemLists.size();
3432 29 Jun 15 olle 3701     if (numItemLists > 0)
3432 29 Jun 15 olle 3702     {
3432 29 Jun 15 olle 3703       itemListWithHighestNumber = (ItemList) itemLists.get(numItemLists-1);
3432 29 Jun 15 olle 3704     }
3432 29 Jun 15 olle 3705     return itemListWithHighestNumber;
3432 29 Jun 15 olle 3706   }
5848 02 Mar 20 olle 3707
5848 02 Mar 20 olle 3708   @SuppressWarnings("unchecked")
5848 02 Mar 20 olle 3709   private void loadHistologyInfo(DbControl dc, Histology his)
5848 02 Mar 20 olle 3710   {
5848 02 Mar 20 olle 3711     Sample s = his.getSample();
5848 02 Mar 20 olle 3712     BioMaterialEvent created = s.getCreationEvent();
5848 02 Mar 20 olle 3713     
5848 02 Mar 20 olle 3714     his.loadBioPlateLocation();
5848 02 Mar 20 olle 3715     his.setAnnotation("registrationDate", Meludi.CONVERTER_DATE_TO_STRING.convert(s.getEntryDate()));
5848 02 Mar 20 olle 3716     his.setAnnotation("editable", s.hasPermission(Permission.WRITE));
5848 02 Mar 20 olle 3717     //his.loadDoNotUseAnnotations(dc, null);
5848 02 Mar 20 olle 3718
5848 02 Mar 20 olle 3719     //his.loadAnnotations(dc, "partitionDate", Annotationtype.PARTITION_DATE, Meludi.CONVERTER_DATE_TO_STRING);
5848 02 Mar 20 olle 3720     his.setAnnotation("originalQuantity", s.getOriginalQuantity());
5848 02 Mar 20 olle 3721     his.setAnnotation("remainingQuantity", s.getRemainingQuantity());
5848 02 Mar 20 olle 3722     
5848 02 Mar 20 olle 3723     if (created.getEventDate() != null)
5848 02 Mar 20 olle 3724     {
5848 02 Mar 20 olle 3725       his.setAnnotation("embedDate", Meludi.CONVERTER_DATE_TO_STRING.convert(created.getEventDate()));
5848 02 Mar 20 olle 3726 /*
5848 02 Mar 20 olle 3727       his.setAnnotation("embedProtocol", JsonUtil.getProtocolAsJSON(created.getProtocol()));
5848 02 Mar 20 olle 3728 */
5848 02 Mar 20 olle 3729     }
5848 02 Mar 20 olle 3730
5848 02 Mar 20 olle 3731     ItemQuery<Sample> stainedQuery = s.getChildSamples();
5848 02 Mar 20 olle 3732     Subtype.STAINED.addFilter(dc, stainedQuery);
5848 02 Mar 20 olle 3733     stainedQuery.include(Meludi.INCLUDE_IN_CURRENT_PROJECT);
5848 02 Mar 20 olle 3734     List<Sample> stained = stainedQuery.list(dc);
5848 02 Mar 20 olle 3735 System.out.println("CaseSummaryServlet::loadHistologyInfo(): stained.size() = " + stained.size());
5848 02 Mar 20 olle 3736     if (stained.size() > 0)
5848 02 Mar 20 olle 3737     {
5848 02 Mar 20 olle 3738 /*
5848 02 Mar 20 olle 3739       Sample good = stained.get(0);
5848 02 Mar 20 olle 3740       boolean goodIsGood = false;
5848 02 Mar 20 olle 3741       for (Sample he : stained)
5848 02 Mar 20 olle 3742       {
5848 02 Mar 20 olle 3743         if (Boolean.TRUE.equals(Annotationtype.GOOD_STAIN.getAnnotationValue(dc, he)))
5848 02 Mar 20 olle 3744         {
5848 02 Mar 20 olle 3745           good = he;
5848 02 Mar 20 olle 3746           goodIsGood = true;
5848 02 Mar 20 olle 3747           break;
5848 02 Mar 20 olle 3748         }
5848 02 Mar 20 olle 3749       }
5848 02 Mar 20 olle 3750       BioMaterialEvent heCreated = good.getCreationEvent();
5848 02 Mar 20 olle 3751       his.setAnnotation("stainDate", Meludi.CONVERTER_DATE_TO_STRING.convert(heCreated.getEventDate()));
5848 02 Mar 20 olle 3752       his.setAnnotation("stainProtocol", JsonUtil.getProtocolAsJSON(good.getProtocol()));
5848 02 Mar 20 olle 3753       his.setAnnotation("numStains", stained.size());
5848 02 Mar 20 olle 3754       if (goodIsGood)
5848 02 Mar 20 olle 3755       {
5848 02 Mar 20 olle 3756         // Image
5848 02 Mar 20 olle 3757         File imageFile = null;
5848 02 Mar 20 olle 3758         AnyToAny imageLink = null;
5848 02 Mar 20 olle 3759         try
5848 02 Mar 20 olle 3760         {
5848 02 Mar 20 olle 3761           imageLink = AnyToAny.getByName(dc, good, "image");
5848 02 Mar 20 olle 3762           if (imageLink.getToType() == Item.FILE)
5848 02 Mar 20 olle 3763           {
5848 02 Mar 20 olle 3764             // This is linked to an existing file already, replace it
5848 02 Mar 20 olle 3765             imageFile = (File)imageLink.getTo();
5848 02 Mar 20 olle 3766           }
5848 02 Mar 20 olle 3767         }
5848 02 Mar 20 olle 3768         catch (RuntimeException ex)
5848 02 Mar 20 olle 3769         {}
5848 02 Mar 20 olle 3770         
5848 02 Mar 20 olle 3771         JSONObject jsonGood = new JSONObject();
5848 02 Mar 20 olle 3772         jsonGood.put("id", good.getId());
5848 02 Mar 20 olle 3773         jsonGood.put("name", good.getName());
5848 02 Mar 20 olle 3774         jsonGood.put("bioWell", JsonUtil.getBioWellAsJSON(good.getBioWell(), true));
5848 02 Mar 20 olle 3775         jsonGood.put("comments", good.getDescription());
5848 02 Mar 20 olle 3776         
5848 02 Mar 20 olle 3777         jsonGood.put("ScoreComplete", Annotationtype.SCORE_COMPLETE.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3778         jsonGood.put("ScoreInvasiveCancer", Annotationtype.SCORE_INVASIVE_CANCER.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3779         jsonGood.put("ScoreInsituCancer", Annotationtype.SCORE_INSITU_CANCER.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3780         jsonGood.put("ScoreLymphocytes", Annotationtype.SCORE_LYMPHOCYTES.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3781         jsonGood.put("ScoreNormal", Annotationtype.SCORE_NORMAL.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3782         jsonGood.put("ScoreStroma", Annotationtype.SCORE_STROMA.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3783         jsonGood.put("ScoreFat", Annotationtype.SCORE_FAT.getAnnotationValue(dc, good));
5848 02 Mar 20 olle 3784         if (imageFile != null)
5848 02 Mar 20 olle 3785         {
5848 02 Mar 20 olle 3786           JSONObject jsonImage = new JSONObject();
5848 02 Mar 20 olle 3787           jsonImage.put("id", imageFile.getId());
5848 02 Mar 20 olle 3788           jsonImage.put("path", imageFile.getPath().toString());
5848 02 Mar 20 olle 3789           jsonGood.put("image", jsonImage);
5848 02 Mar 20 olle 3790         }
5848 02 Mar 20 olle 3791         
5848 02 Mar 20 olle 3792         his.setAnnotation("bestStain", jsonGood);
5848 02 Mar 20 olle 3793       }
5848 02 Mar 20 olle 3794 */
5848 02 Mar 20 olle 3795
5848 02 Mar 20 olle 3796       his.setAnnotation("numStains", stained.size());
5848 02 Mar 20 olle 3797       if (stained.size() > 1)
5848 02 Mar 20 olle 3798       {
5848 02 Mar 20 olle 3799         for (int i = 0; i < 2; i++)
5848 02 Mar 20 olle 3800         {
5848 02 Mar 20 olle 3801           Sample he = (Sample)stained.get(i);
5848 02 Mar 20 olle 3802           Integer tumourCellsPct = (Integer)Annotationtype.TUMOUR_CELLS_PERCENT.getAnnotationValue(dc, he);
5848 02 Mar 20 olle 3803           String heName = he.getName();
5848 02 Mar 20 olle 3804           if (heName.contains(".F"))
5848 02 Mar 20 olle 3805           {
5848 02 Mar 20 olle 3806             JSONObject jsonF = new JSONObject();
5848 02 Mar 20 olle 3807             jsonF.put("id", he.getId());
5848 02 Mar 20 olle 3808             jsonF.put("name", he.getName());
5848 02 Mar 20 olle 3809             jsonF.put("bioWell", JsonUtil.getBioWellAsJSON(he.getBioWell(), true));
5848 02 Mar 20 olle 3810             jsonF.put("tumourCellsPct", tumourCellsPct);
5848 02 Mar 20 olle 3811             his.setAnnotation("FStain", jsonF);
5848 02 Mar 20 olle 3812           }
5848 02 Mar 20 olle 3813           else if (heName.contains(".E"))
5848 02 Mar 20 olle 3814           {
5848 02 Mar 20 olle 3815             JSONObject jsonE = new JSONObject();
5848 02 Mar 20 olle 3816             jsonE.put("id", he.getId());
5848 02 Mar 20 olle 3817             jsonE.put("name", he.getName());
5848 02 Mar 20 olle 3818             jsonE.put("bioWell", JsonUtil.getBioWellAsJSON(he.getBioWell(), true));
5848 02 Mar 20 olle 3819             jsonE.put("tumourCellsPct", tumourCellsPct);
5848 02 Mar 20 olle 3820             his.setAnnotation("EStain", jsonE);
5848 02 Mar 20 olle 3821           }
5848 02 Mar 20 olle 3822         }
5848 02 Mar 20 olle 3823       }
5848 02 Mar 20 olle 3824     }
5848 02 Mar 20 olle 3825   }
3176 06 Mar 15 olle 3826 }