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

Code
Comments
Other
Rev Date Author Line
3449 28 Jul 15 olle 1 package net.sf.basedb.meludi.servlet;
3449 28 Jul 15 olle 2
3538 13 Oct 15 olle 3 import java.io.FileWriter;
3449 28 Jul 15 olle 4 import java.io.IOException;
3871 22 Apr 16 olle 5 import java.io.OutputStream;
3449 28 Jul 15 olle 6 import java.io.OutputStreamWriter;
3538 13 Oct 15 olle 7 import java.io.PrintWriter;
3449 28 Jul 15 olle 8 import java.io.Writer;
3449 28 Jul 15 olle 9 import java.text.SimpleDateFormat;
3538 13 Oct 15 olle 10 import java.util.ArrayList;
3538 13 Oct 15 olle 11 import java.util.Collections;
3449 28 Jul 15 olle 12 import java.util.Date;
3449 28 Jul 15 olle 13 import java.util.HashMap;
3449 28 Jul 15 olle 14 import java.util.Iterator;
3449 28 Jul 15 olle 15 import java.util.List;
3449 28 Jul 15 olle 16 import java.util.Map;
3449 28 Jul 15 olle 17 import java.util.regex.Pattern;
3449 28 Jul 15 olle 18
3449 28 Jul 15 olle 19 import javax.servlet.ServletException;
3449 28 Jul 15 olle 20 import javax.servlet.http.HttpServlet;
3449 28 Jul 15 olle 21 import javax.servlet.http.HttpServletRequest;
3449 28 Jul 15 olle 22 import javax.servlet.http.HttpServletResponse;
3449 28 Jul 15 olle 23
3449 28 Jul 15 olle 24 import org.json.simple.JSONArray;
3449 28 Jul 15 olle 25 import org.json.simple.JSONObject;
3449 28 Jul 15 olle 26 import org.json.simple.parser.JSONParser;
3449 28 Jul 15 olle 27
3449 28 Jul 15 olle 28 import net.sf.basedb.core.AnnotationType;
3449 28 Jul 15 olle 29 import net.sf.basedb.core.AnyToAny;
3449 28 Jul 15 olle 30 import net.sf.basedb.core.Application;
3449 28 Jul 15 olle 31 import net.sf.basedb.core.BioMaterial;
3449 28 Jul 15 olle 32 import net.sf.basedb.core.BioMaterialEvent;
3449 28 Jul 15 olle 33 import net.sf.basedb.core.BioMaterialEventSource;
3449 28 Jul 15 olle 34 import net.sf.basedb.core.BioPlateEvent;
3624 25 Nov 15 olle 35 import net.sf.basedb.core.BioPlateEventParticipant;
3449 28 Jul 15 olle 36 import net.sf.basedb.core.BioPlateEventType;
3573 05 Nov 15 olle 37 import net.sf.basedb.core.BioPlateType;
3449 28 Jul 15 olle 38 import net.sf.basedb.core.ItemList;
3449 28 Jul 15 olle 39 import net.sf.basedb.core.BioPlate;
3449 28 Jul 15 olle 40 import net.sf.basedb.core.BioWell;
3449 28 Jul 15 olle 41 import net.sf.basedb.core.DbControl;
3449 28 Jul 15 olle 42 import net.sf.basedb.core.Extract;
3449 28 Jul 15 olle 43 import net.sf.basedb.core.File;
3624 25 Nov 15 olle 44 import net.sf.basedb.core.Include;
3449 28 Jul 15 olle 45 import net.sf.basedb.core.InvalidDataException;
3449 28 Jul 15 olle 46 import net.sf.basedb.core.Item;
3449 28 Jul 15 olle 47 import net.sf.basedb.core.ItemQuery;
3449 28 Jul 15 olle 48 import net.sf.basedb.core.ItemSubtype;
3610 20 Nov 15 olle 49 import net.sf.basedb.core.Kit;
3573 05 Nov 15 olle 50 import net.sf.basedb.core.MeasuredBioMaterial;
3449 28 Jul 15 olle 51 import net.sf.basedb.core.Path;
3449 28 Jul 15 olle 52 import net.sf.basedb.core.Permission;
3449 28 Jul 15 olle 53 import net.sf.basedb.core.PermissionDeniedException;
3573 05 Nov 15 olle 54 import net.sf.basedb.core.PlateGeometry;
3449 28 Jul 15 olle 55 import net.sf.basedb.core.PluginConfiguration;
3449 28 Jul 15 olle 56 import net.sf.basedb.core.PluginDefinition;
3449 28 Jul 15 olle 57 import net.sf.basedb.core.Protocol;
3449 28 Jul 15 olle 58 import net.sf.basedb.core.SessionControl;
3449 28 Jul 15 olle 59 import net.sf.basedb.core.Tag;
3449 28 Jul 15 olle 60 import net.sf.basedb.core.Type;
3449 28 Jul 15 olle 61 import net.sf.basedb.core.plugin.ParameterValues;
3449 28 Jul 15 olle 62 import net.sf.basedb.core.query.Annotations;
3449 28 Jul 15 olle 63 import net.sf.basedb.core.query.Expressions;
3449 28 Jul 15 olle 64 import net.sf.basedb.core.query.Hql;
3449 28 Jul 15 olle 65 import net.sf.basedb.core.query.Orders;
3449 28 Jul 15 olle 66 import net.sf.basedb.core.query.Restrictions;
3449 28 Jul 15 olle 67 import net.sf.basedb.meludi.JsonUtil;
3449 28 Jul 15 olle 68 import net.sf.basedb.meludi.Meludi;
4230 10 Nov 16 olle 69 import net.sf.basedb.meludi.MeludiConfiguration;
3449 28 Jul 15 olle 70 import net.sf.basedb.meludi.converter.DateToStringConverter;
3449 28 Jul 15 olle 71 import net.sf.basedb.meludi.converter.LookupConverter;
3610 20 Nov 15 olle 72 import net.sf.basedb.meludi.converter.StringToDateConverter;
3449 28 Jul 15 olle 73 /*
3449 28 Jul 15 olle 74 import net.sf.basedb.meludi.converter.RequiredValueConverter;
3449 28 Jul 15 olle 75 */
3449 28 Jul 15 olle 76 import net.sf.basedb.meludi.converter.ValueConverter;
3449 28 Jul 15 olle 77 import net.sf.basedb.meludi.converter.WellCoordinateConverter;
3449 28 Jul 15 olle 78 import net.sf.basedb.meludi.converter.WellCoordinateRemap96To384;
3449 28 Jul 15 olle 79 import net.sf.basedb.meludi.counter.CounterService;
3449 28 Jul 15 olle 80 import net.sf.basedb.meludi.dao.Annotationtype;
3449 28 Jul 15 olle 81 import net.sf.basedb.meludi.dao.BiomaterialList;
3449 28 Jul 15 olle 82 import net.sf.basedb.meludi.dao.BioplateType;
3449 28 Jul 15 olle 83 import net.sf.basedb.meludi.dao.Dna;
3449 28 Jul 15 olle 84 /*
3449 28 Jul 15 olle 85 import net.sf.basedb.meludi.dao.CDna;
3449 28 Jul 15 olle 86 */
3449 28 Jul 15 olle 87 import net.sf.basedb.meludi.dao.Dna;
3449 28 Jul 15 olle 88 import net.sf.basedb.meludi.dao.Library;
3449 28 Jul 15 olle 89 import net.sf.basedb.meludi.dao.ReactionPlate;
3449 28 Jul 15 olle 90 import net.sf.basedb.meludi.dao.MeludiRole;
3449 28 Jul 15 olle 91 import net.sf.basedb.meludi.dao.Rna;
3449 28 Jul 15 olle 92 import net.sf.basedb.meludi.dao.Subtype;
3449 28 Jul 15 olle 93 /*
3449 28 Jul 15 olle 94 import net.sf.basedb.meludi.plugins.CaliperLibPrepParametersExporter;
3449 28 Jul 15 olle 95 import net.sf.basedb.meludi.plugins.CaliperPlateImporter;
3449 28 Jul 15 olle 96 import net.sf.basedb.meludi.plugins.CaliperSampleNameExporter;
3449 28 Jul 15 olle 97 */
3449 28 Jul 15 olle 98 import net.sf.basedb.meludi.plugins.CsvEncoderDecoder;
3676 18 Dec 15 olle 99 import net.sf.basedb.util.MD5;
3871 22 Apr 16 olle 100 import net.sf.basedb.util.StaticCache;
3449 28 Jul 15 olle 101 /*
3449 28 Jul 15 olle 102 import net.sf.basedb.meludi.plugins.QubitSampleNameExporter;
3449 28 Jul 15 olle 103 */
3449 28 Jul 15 olle 104 import net.sf.basedb.util.Values;
3449 28 Jul 15 olle 105 import net.sf.basedb.util.error.ThrowableUtil;
3449 28 Jul 15 olle 106 import net.sf.basedb.util.export.TableWriter;
3449 28 Jul 15 olle 107 import net.sf.basedb.util.parser.FlatFileParser;
3449 28 Jul 15 olle 108 import net.sf.basedb.util.parser.Mapper;
3449 28 Jul 15 olle 109 import net.sf.basedb.util.parser.FlatFileParser.LineType;
3449 28 Jul 15 olle 110
3449 28 Jul 15 olle 111
3449 28 Jul 15 olle 112 public class LibPrepServlet 
3449 28 Jul 15 olle 113   extends HttpServlet 
3449 28 Jul 15 olle 114 {
3449 28 Jul 15 olle 115
3449 28 Jul 15 olle 116   private static final long serialVersionUID = 2133738736983511172L;
3546 16 Oct 15 olle 117   
3547 16 Oct 15 olle 118   public static String LIB_PREP_KIT_FILTER_ALL = "all";
3610 20 Nov 15 olle 119   public static String LIB_PREP_KIT_FILTER_USED_ACTIVE = "used_active";
3610 20 Nov 15 olle 120   public static String LIB_PREP_KIT_FILTER_USED_INACTIVE = "used_inactive";
3547 16 Oct 15 olle 121   public static String LIB_PREP_KIT_FILTER_USED = "used";
3547 16 Oct 15 olle 122   public static String LIB_PREP_KIT_FILTER_UNUSED = "unused";
3610 20 Nov 15 olle 123   public static String LIB_PREP_KIT_FILTER_ACTIVE = "active";
3610 20 Nov 15 olle 124   public static String LIB_PREP_KIT_FILTER_INACTIVE = "inactive";
3547 16 Oct 15 olle 125
3573 05 Nov 15 olle 126   private Map<Integer,String> indexToRowLetterHashMap = new HashMap<Integer,String>();
3573 05 Nov 15 olle 127   private Map<String,Integer> rowLetterToIndexHashMap = new HashMap<String,Integer>();
3573 05 Nov 15 olle 128
3449 28 Jul 15 olle 129   public LibPrepServlet()
3573 05 Nov 15 olle 130   {
3573 05 Nov 15 olle 131     // Initialize hash maps
3573 05 Nov 15 olle 132     indexToRowLetterHashMap.put(0,"A");
3573 05 Nov 15 olle 133     indexToRowLetterHashMap.put(1,"B");
3573 05 Nov 15 olle 134     indexToRowLetterHashMap.put(2,"C");
3573 05 Nov 15 olle 135     indexToRowLetterHashMap.put(3,"D");
3573 05 Nov 15 olle 136     indexToRowLetterHashMap.put(4,"E");
3573 05 Nov 15 olle 137     indexToRowLetterHashMap.put(5,"F");
3573 05 Nov 15 olle 138     indexToRowLetterHashMap.put(6,"G");
3573 05 Nov 15 olle 139     indexToRowLetterHashMap.put(7,"H");
3573 05 Nov 15 olle 140     //
3573 05 Nov 15 olle 141     rowLetterToIndexHashMap.put("A",0);
3573 05 Nov 15 olle 142     rowLetterToIndexHashMap.put("B",1);
3573 05 Nov 15 olle 143     rowLetterToIndexHashMap.put("C",2);
3573 05 Nov 15 olle 144     rowLetterToIndexHashMap.put("D",3);
3573 05 Nov 15 olle 145     rowLetterToIndexHashMap.put("E",4);
3573 05 Nov 15 olle 146     rowLetterToIndexHashMap.put("F",5);
3573 05 Nov 15 olle 147     rowLetterToIndexHashMap.put("G",6);
3573 05 Nov 15 olle 148     rowLetterToIndexHashMap.put("H",7);
3573 05 Nov 15 olle 149   }
3449 28 Jul 15 olle 150
3449 28 Jul 15 olle 151   @SuppressWarnings("unchecked")
3449 28 Jul 15 olle 152   @Override
3449 28 Jul 15 olle 153   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
3449 28 Jul 15 olle 154     throws ServletException, IOException 
3449 28 Jul 15 olle 155   {
3449 28 Jul 15 olle 156
3449 28 Jul 15 olle 157     String ID = req.getParameter("ID");
3449 28 Jul 15 olle 158     String cmd = req.getParameter("cmd");
3449 28 Jul 15 olle 159     JsonUtil.setJsonResponseHeaders(resp);
3449 28 Jul 15 olle 160     
3449 28 Jul 15 olle 161     JSONObject json = new JSONObject();
3449 28 Jul 15 olle 162     json.put("status", "ok");
3449 28 Jul 15 olle 163     
5468 04 Jun 19 olle 164     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 165     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 166     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
3449 28 Jul 15 olle 167     DbControl dc = null;
3449 28 Jul 15 olle 168     try
3449 28 Jul 15 olle 169     {
3450 28 Jul 15 olle 170 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\"");
3449 28 Jul 15 olle 171       if ("GetDNAPlatesForBarcoding".equals(cmd))
3449 28 Jul 15 olle 172       {
3449 28 Jul 15 olle 173         /*
3449 28 Jul 15 olle 174           Find all DNA bioplates that has not yet been assigned a
3449 28 Jul 15 olle 175           barcoding scheme.
3449 28 Jul 15 olle 176         */
3449 28 Jul 15 olle 177         dc = sc.newDbControl();
3449 28 Jul 15 olle 178         ItemQuery<BioPlate> query = BioPlate.getQuery();
3449 28 Jul 15 olle 179         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 180         // Only DNA bioplates that are not destroyed
3449 28 Jul 15 olle 181         BioplateType.DNA.addFilter(dc, query, true);
3449 28 Jul 15 olle 182         // Must have a creation date
3449 28 Jul 15 olle 183         query.restrict(Restrictions.neq(Hql.property("eventDate"), null));
3449 28 Jul 15 olle 184         // Must not have a BARCODE_VARIANT annotation
3449 28 Jul 15 olle 185         query.join(Annotations.leftJoin(null, Annotationtype.BARCODE_VARIANT.load(dc), "bv"));
3449 28 Jul 15 olle 186         query.restrict(Restrictions.eq(Hql.alias("bv"), null));
3449 28 Jul 15 olle 187         
3449 28 Jul 15 olle 188         List<BioPlate> plates = query.list(dc);
3449 28 Jul 15 olle 189         
3449 28 Jul 15 olle 190         JSONArray jsonPlates = new JSONArray();
3449 28 Jul 15 olle 191         for (BioPlate plate : plates)
3449 28 Jul 15 olle 192         {
3449 28 Jul 15 olle 193           JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 194           jsonPlate.put("id", plate.getId());
3449 28 Jul 15 olle 195           jsonPlate.put("name", plate.getName());
3449 28 Jul 15 olle 196           jsonPlate.put("comments", plate.getDescription());
3449 28 Jul 15 olle 197           jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 198           jsonPlate.put("barcodeVariant", Annotationtype.BARCODE_VARIANT.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 199           jsonPlates.add(jsonPlate);
3449 28 Jul 15 olle 200         }
3449 28 Jul 15 olle 201         json.put("bioplates", jsonPlates);
3449 28 Jul 15 olle 202       }
3449 28 Jul 15 olle 203 /*
3449 28 Jul 15 olle 204       if ("GetCDNAPlatesForBarcoding".equals(cmd))
3449 28 Jul 15 olle 205       {
3449 28 Jul 15 olle 206 */
3449 28 Jul 15 olle 207         /*
3449 28 Jul 15 olle 208           Find all cDNA bioplates that has not yet been assigned a
3449 28 Jul 15 olle 209           barcoding scheme.
3449 28 Jul 15 olle 210         */
3449 28 Jul 15 olle 211 /*
3449 28 Jul 15 olle 212         dc = sc.newDbControl();
3449 28 Jul 15 olle 213         ItemQuery<BioPlate> query = BioPlate.getQuery();
3449 28 Jul 15 olle 214         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 215         // Only cDNA bioplates that are not destroyed
3449 28 Jul 15 olle 216         BioplateType.CDNA.addFilter(dc, query, true);
3449 28 Jul 15 olle 217         // Must have a creation date
3449 28 Jul 15 olle 218         query.restrict(Restrictions.neq(Hql.property("eventDate"), null));
3449 28 Jul 15 olle 219         // Must not have a BARCODE_VARIANT annotation
3449 28 Jul 15 olle 220         query.join(Annotations.leftJoin(null, Annotationtype.BARCODE_VARIANT.load(dc), "bv"));
3449 28 Jul 15 olle 221         query.restrict(Restrictions.eq(Hql.alias("bv"), null));
3449 28 Jul 15 olle 222         
3449 28 Jul 15 olle 223         List<BioPlate> plates = query.list(dc);
3449 28 Jul 15 olle 224         
3449 28 Jul 15 olle 225         JSONArray jsonPlates = new JSONArray();
3449 28 Jul 15 olle 226         for (BioPlate plate : plates)
3449 28 Jul 15 olle 227         {
3449 28 Jul 15 olle 228           JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 229           jsonPlate.put("id", plate.getId());
3449 28 Jul 15 olle 230           jsonPlate.put("name", plate.getName());
3449 28 Jul 15 olle 231           jsonPlate.put("comments", plate.getDescription());
3449 28 Jul 15 olle 232           jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 233           jsonPlate.put("barcodeVariant", Annotationtype.BARCODE_VARIANT.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 234           jsonPlates.add(jsonPlate);
3449 28 Jul 15 olle 235         }
3449 28 Jul 15 olle 236         json.put("bioplates", jsonPlates);
3449 28 Jul 15 olle 237       }
3449 28 Jul 15 olle 238 */
3449 28 Jul 15 olle 239       else if ("GetLibraryPlatesForLibPrep".equals(cmd))
3449 28 Jul 15 olle 240       {
3449 28 Jul 15 olle 241         /*
3449 28 Jul 15 olle 242           Find all Library bioplates without creation date.
3449 28 Jul 15 olle 243         */
3449 28 Jul 15 olle 244         dc = sc.newDbControl();
3449 28 Jul 15 olle 245         BioplateType plateType = BioplateType.getByCName(req.getParameter("plateType"));
3449 28 Jul 15 olle 246         //if (plateType == null) plateType = BioplateType.LIBRARY;
3449 28 Jul 15 olle 247         if (plateType == null) plateType = BioplateType.DNA;
3449 28 Jul 15 olle 248         
3449 28 Jul 15 olle 249         ItemQuery<BioPlate> query = BioPlate.getQuery();
3449 28 Jul 15 olle 250         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 251         // Only Library bioplates that are not destroyed
3449 28 Jul 15 olle 252         plateType.addFilter(dc, query, true);
3449 28 Jul 15 olle 253         // Must NOT have a creation date
3449 28 Jul 15 olle 254         //query.restrict(Restrictions.eq(Hql.property("eventDate"), null));
3449 28 Jul 15 olle 255         // Must have a BARCODE_VARIANT annotation
3449 28 Jul 15 olle 256 /*
3449 28 Jul 15 olle 257         query.join(Annotations.leftJoin(null, Annotationtype.BARCODE_VARIANT.load(dc), "bv"));
3449 28 Jul 15 olle 258         query.restrict(Restrictions.neq(Hql.alias("bv"), null));
3449 28 Jul 15 olle 259 */
3449 28 Jul 15 olle 260         
3449 28 Jul 15 olle 261         List<BioPlate> plates = query.list(dc);
3449 28 Jul 15 olle 262         JSONArray jsonPlates = new JSONArray();
3449 28 Jul 15 olle 263         for (BioPlate plate : plates)
3449 28 Jul 15 olle 264         {
3449 28 Jul 15 olle 265           ItemQuery<AnyToAny> fileQuery = AnyToAny.getLinksFrom(plate);
3449 28 Jul 15 olle 266           Restrictions.eq(Hql.property("toType"), Expressions.integer(Item.FILE.getValue()));
3449 28 Jul 15 olle 267           long fileCount = fileQuery.count(dc);
3449 28 Jul 15 olle 268
3449 28 Jul 15 olle 269           JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 270           jsonPlate.put("id", plate.getId());
3449 28 Jul 15 olle 271           jsonPlate.put("name", plate.getName());
3449 28 Jul 15 olle 272           jsonPlate.put("comments", plate.getDescription());
3449 28 Jul 15 olle 273           jsonPlate.put("numFiles", fileCount);
3449 28 Jul 15 olle 274 /*
3449 28 Jul 15 olle 275           jsonPlate.put("AutoProcessing", Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 276 */
3449 28 Jul 15 olle 277           jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, plate)));
3449 28 Jul 15 olle 278           jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 279           jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 280           jsonPlate.put("barcodeVariant", Annotationtype.BARCODE_VARIANT.getAnnotationValue(dc, plate));
3449 28 Jul 15 olle 281           jsonPlates.add(jsonPlate);
3449 28 Jul 15 olle 282         }
3449 28 Jul 15 olle 283         json.put("bioplates", jsonPlates);
3449 28 Jul 15 olle 284       }
3449 28 Jul 15 olle 285       else if ("GetAllBarcodeInfo".equals(cmd))
3449 28 Jul 15 olle 286       {
3449 28 Jul 15 olle 287         dc = sc.newDbControl();
3449 28 Jul 15 olle 288         
3449 28 Jul 15 olle 289         ItemQuery<Tag> query = Tag.getQuery();
3449 28 Jul 15 olle 290         Subtype.BARCODE.addFilter(dc, query);
3449 28 Jul 15 olle 291         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 292         query.order(Orders.asc(Hql.property("name")));
3449 28 Jul 15 olle 293         
3449 28 Jul 15 olle 294         JSONArray jsonBarcodes = new JSONArray();
3450 28 Jul 15 olle 295 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" query.list(dc).size() = " + query.list(dc).size());
3449 28 Jul 15 olle 296         for (Tag t : query.list(dc))
3449 28 Jul 15 olle 297         {
3449 28 Jul 15 olle 298           JSONObject jsonTag = new JSONObject();
3449 28 Jul 15 olle 299           jsonTag.put("id", t.getId());
3449 28 Jul 15 olle 300           jsonTag.put("name", t.getName());
3449 28 Jul 15 olle 301           jsonBarcodes.add(jsonTag);
3450 28 Jul 15 olle 302 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" name = \"" + t.getName() + "\"");
3449 28 Jul 15 olle 303         }
3449 28 Jul 15 olle 304         json.put("barcodes", jsonBarcodes);
3449 28 Jul 15 olle 305         
3449 28 Jul 15 olle 306       }
3449 28 Jul 15 olle 307       else if ("GetDnaInfoForPlate".equals(cmd))
3449 28 Jul 15 olle 308       {
3449 28 Jul 15 olle 309         int dnaPlateId = Values.getInt(req.getParameter("bioplate"));
3449 28 Jul 15 olle 310         dc = sc.newDbControl();
3449 28 Jul 15 olle 311
3449 28 Jul 15 olle 312         BioPlate dnaPlate = BioPlate.getById(dc, dnaPlateId);
3449 28 Jul 15 olle 313
3449 28 Jul 15 olle 314         ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 315         query.join(Hql.innerJoin(null, "bioWell", "bw", true));
3449 28 Jul 15 olle 316         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
3449 28 Jul 15 olle 317         query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(dnaPlate)));
3449 28 Jul 15 olle 318         query.order(Orders.asc(Hql.property("bw", "column")));
3449 28 Jul 15 olle 319         query.order(Orders.asc(Hql.property("bw", "row")));
3449 28 Jul 15 olle 320
3449 28 Jul 15 olle 321         List<Dna> dna = Dna.toList(query.list(dc));
3449 28 Jul 15 olle 322         JSONArray jsonDna = new JSONArray();
3449 28 Jul 15 olle 323         
3449 28 Jul 15 olle 324         for (Dna d : dna)
3449 28 Jul 15 olle 325         {
3449 28 Jul 15 olle 326           d.loadBioPlateLocation();
3449 28 Jul 15 olle 327           jsonDna.add(d.asJSONObject());
3449 28 Jul 15 olle 328         }                
3449 28 Jul 15 olle 329         json.put("dna", jsonDna);
3449 28 Jul 15 olle 330       }
3449 28 Jul 15 olle 331       else if ("GetLibraryInfoForPlate".equals(cmd))
3449 28 Jul 15 olle 332       {
3449 28 Jul 15 olle 333         int libPlateId = Values.getInt(req.getParameter("bioplate"));
3449 28 Jul 15 olle 334         boolean loadDilutionInfo = Values.getBoolean(req.getParameter("loadDilutionInfo"));
3449 28 Jul 15 olle 335         dc = sc.newDbControl();
3449 28 Jul 15 olle 336
3449 28 Jul 15 olle 337         BioPlate libPlate = BioPlate.getById(dc, libPlateId);
3449 28 Jul 15 olle 338       
3449 28 Jul 15 olle 339         JSONObject jsonPlate = new JSONObject();
3449 28 Jul 15 olle 340         jsonPlate.put("id", libPlate.getId());
3449 28 Jul 15 olle 341         jsonPlate.put("name", libPlate.getName());
3449 28 Jul 15 olle 342         jsonPlate.put("columns", libPlate.getColumns());
3449 28 Jul 15 olle 343         jsonPlate.put("rows", libPlate.getRows());
3449 28 Jul 15 olle 344         jsonPlate.put("comments", libPlate.getDescription());
3449 28 Jul 15 olle 345         jsonPlate.put("DilutionDate", Meludi.CONVERTER_DATE_TO_STRING.convert((Date)Annotationtype.DILUTION_DATE.getAnnotationValue(dc, libPlate)));
3449 28 Jul 15 olle 346         jsonPlate.put("DilutionOperator", Annotationtype.DILUTION_OPERATOR.getAnnotationValue(dc, libPlate));
3449 28 Jul 15 olle 347         jsonPlate.put("poolSchema", Annotationtype.POOL_SCHEMA.getAnnotationValue(dc, libPlate));
3449 28 Jul 15 olle 348         jsonPlate.put("barcodeVariant", Annotationtype.BARCODE_VARIANT.getAnnotationValue(dc, libPlate));
3449 28 Jul 15 olle 349         
3449 28 Jul 15 olle 350         ItemQuery<Extract> query = Extract.getQuery();
3449 28 Jul 15 olle 351         query.join(Hql.innerJoin(null, "bioWell", "bw", true));
3449 28 Jul 15 olle 352         query.join(Hql.innerJoin("bw", "bioPlate", "bp"));
3449 28 Jul 15 olle 353         query.restrict(Restrictions.eq(Hql.alias("bp"), Hql.entity(libPlate)));
3449 28 Jul 15 olle 354         query.order(Orders.asc(Hql.property("bw", "column")));
3449 28 Jul 15 olle 355         query.order(Orders.asc(Hql.property("bw", "row")));
3449 28 Jul 15 olle 356
3449 28 Jul 15 olle 357         List<Library> libs = Library.toList(query.list(dc));
3449 28 Jul 15 olle 358         
3449 28 Jul 15 olle 359         JSONArray jsonLibs = new JSONArray();
3449 28 Jul 15 olle 360         for (Library lib : libs)
3449 28 Jul 15 olle 361         {
3449 28 Jul 15 olle 362           lib.loadBioPlateLocation();
3449 28 Jul 15 olle 363           if (loadDilutionInfo)
3449 28 Jul 15 olle 364           {
3449 28 Jul 15 olle 365 /*
3449 28 Jul 15 olle 366             lib.loadAnnotations(dc, "DilutionConc", Annotationtype.DILUTION_CONC, null);
3449 28 Jul 15 olle 367 */
3449 28 Jul 15 olle 368 /*
3449 28 Jul 15 olle 369             lib.loadAnnotations(dc, "UseForQC", Annotationtype.USE_FOR_QC, null);
3449 28 Jul 15 olle 370 */
3449 28 Jul 15 olle 371
3449 28 Jul 15 olle 372             Dna dna = lib.getDna(dc);
3449 28 Jul 15 olle 373 /*
3449 28 Jul 15 olle 374             Rna rna = lib.getRna(dc, true);
3449 28 Jul 15 olle 375 */
3449 28 Jul 15 olle 376 /*
3449 28 Jul 15 olle 377             boolean preNormalized = rna.isPreNormalized(dc);
3449 28 Jul 15 olle 378             rna.setAnnotation("preNormalized", preNormalized);
3449 28 Jul 15 olle 379             if (!preNormalized)
3449 28 Jul 15 olle 380             {
3449 28 Jul 15 olle 381 */
3449 28 Jul 15 olle 382               dna.loadBioPlateLocation();
3449 28 Jul 15 olle 383 /*
3449 28 Jul 15 olle 384               rna.loadBioPlateLocation();
3449 28 Jul 15 olle 385 */
3449 28 Jul 15 olle 386 /*
3449 28 Jul 15 olle 387             }
3449 28 Jul 15 olle 388 */
3449 28 Jul 15 olle 389             dna.loadAnnotations(dc, "QubitConc", Annotationtype.QUBIT_CONC, null);
3449 28 Jul 15 olle 390             
3449 28 Jul 15 olle 391             Float used = lib.getItem().getCreationEvent().getUsedQuantity(dna.getItem());
3449 28 Jul 15 olle 392             Float remain = dna.getItem().getRemainingQuantity();
3449 28 Jul 15 olle 393             dna.setAnnotation("usedQuantity", used);
3449 28 Jul 15 olle 394             if (used != null && remain != null)
3449 28 Jul 15 olle 395             {
3449 28 Jul 15 olle 396               dna.setAnnotation("remainingQuantity", remain+used);
3449 28 Jul 15 olle 397             }
3449 28 Jul 15 olle 398             lib.setAnnotation("dna", dna.asJSONObject());
3449 28 Jul 15 olle 399 /*
3449 28 Jul 15 olle 400             Float used = lib.getItem().getCreationEvent().getUsedQuantity(rna.getItem());
3449 28 Jul 15 olle 401             Float remain = rna.getItem().getRemainingQuantity();
3449 28 Jul 15 olle 402             rna.setAnnotation("usedQuantity", used);
3449 28 Jul 15 olle 403             if (used != null && remain != null)
3449 28 Jul 15 olle 404             {
3449 28 Jul 15 olle 405               rna.setAnnotation("remainingQuantity", remain+used);
3449 28 Jul 15 olle 406             }
3449 28 Jul 15 olle 407             lib.setAnnotation("rna", rna.asJSONObject());
3449 28 Jul 15 olle 408 */
3449 28 Jul 15 olle 409           }
3449 28 Jul 15 olle 410           
3449 28 Jul 15 olle 411           jsonLibs.add(lib.asJSONObject());
3449 28 Jul 15 olle 412         }
3449 28 Jul 15 olle 413
3449 28 Jul 15 olle 414         json.put("libraries", jsonLibs);
3449 28 Jul 15 olle 415         json.put("plate", jsonPlate);
3449 28 Jul 15 olle 416       }
3538 13 Oct 15 olle 417       else if ("DownloadReagentLotNoFile".equals(cmd))
3538 13 Oct 15 olle 418       {
3553 20 Oct 15 olle 419         String referenceName = req.getParameter("referenceName");
3546 16 Oct 15 olle 420         String expirationDate = req.getParameter("expirationDate");
3538 13 Oct 15 olle 421         String tmpFilePath = req.getParameter("tmpFilePath");
3538 13 Oct 15 olle 422
3553 20 Oct 15 olle 423         // If reference name is file name, remove file extension
3553 20 Oct 15 olle 424         String sourceRefName = referenceName;
3624 25 Nov 15 olle 425         int lastDotIndex = referenceName.lastIndexOf(".");
3624 25 Nov 15 olle 426         if (lastDotIndex >= 0)
3538 13 Oct 15 olle 427         {
3538 13 Oct 15 olle 428           // Remove file extension
3624 25 Nov 15 olle 429           sourceRefName = referenceName.substring(0, lastDotIndex);
3538 13 Oct 15 olle 430         }
3538 13 Oct 15 olle 431
3553 20 Oct 15 olle 432         resp.setHeader("Content-Disposition", "attachment; filename=library-preparation-reagents-" + sourceRefName + "-exp-" + expirationDate + ".csv");
3538 13 Oct 15 olle 433         resp.setContentType("text/plain");
3538 13 Oct 15 olle 434         resp.setCharacterEncoding("UTF-8");
3538 13 Oct 15 olle 435
3871 22 Apr 16 olle 436         OutputStream out = resp.getOutputStream();
3538 13 Oct 15 olle 437
3871 22 Apr 16 olle 438         // Get contents from static cache
3871 22 Apr 16 olle 439         String cacheKey = tmpFilePath;
3871 22 Apr 16 olle 440         StaticCache cache = Application.getStaticCache();
3871 22 Apr 16 olle 441         int timeout = 1000;
3871 22 Apr 16 olle 442         cache.read(cacheKey, out, timeout);
3538 13 Oct 15 olle 443         out.flush();
3538 13 Oct 15 olle 444         out.close();
3871 22 Apr 16 olle 445         // Delete temporary cache after download
3871 22 Apr 16 olle 446         cache.delete(cacheKey, timeout);
3538 13 Oct 15 olle 447       }
3547 16 Oct 15 olle 448       else if ("DownloadLibPrepKitDataFile".equals(cmd))
3547 16 Oct 15 olle 449       {
3547 16 Oct 15 olle 450         String kitFilterValue = req.getParameter("kitFilterValue");
3547 16 Oct 15 olle 451         String tmpFilePath = req.getParameter("tmpFilePath");
3547 16 Oct 15 olle 452
3547 16 Oct 15 olle 453         DateToStringConverter d1 = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd_HHmm"));
3547 16 Oct 15 olle 454         Date now = new Date();
3547 16 Oct 15 olle 455         String dateStr1 = d1.convert(now);
3547 16 Oct 15 olle 456
3547 16 Oct 15 olle 457         resp.setHeader("Content-Disposition", "attachment; filename=library-preparation-kits-" + kitFilterValue + "-" + dateStr1 + ".csv");
3547 16 Oct 15 olle 458         resp.setContentType("text/plain");
3547 16 Oct 15 olle 459         resp.setCharacterEncoding("UTF-8");
3547 16 Oct 15 olle 460
3871 22 Apr 16 olle 461         OutputStream out = resp.getOutputStream();
3547 16 Oct 15 olle 462
3871 22 Apr 16 olle 463         // Get contents from static cache
3871 22 Apr 16 olle 464         String cacheKey = tmpFilePath;
3871 22 Apr 16 olle 465         StaticCache cache = Application.getStaticCache();
3871 22 Apr 16 olle 466         int timeout = 1000;
3871 22 Apr 16 olle 467         cache.read(cacheKey, out, timeout);
3547 16 Oct 15 olle 468         out.flush();
3547 16 Oct 15 olle 469         out.close();
3871 22 Apr 16 olle 470         // Delete temporary cache after download
3871 22 Apr 16 olle 471         cache.delete(cacheKey, timeout);
3547 16 Oct 15 olle 472       }
3624 25 Nov 15 olle 473       else if ("DownloadLibPrepFile".equals(cmd))
3624 25 Nov 15 olle 474       {
3624 25 Nov 15 olle 475         String referenceName = req.getParameter("referenceName");
3624 25 Nov 15 olle 476         String tmpFilePath = req.getParameter("tmpFilePath");
3624 25 Nov 15 olle 477
3624 25 Nov 15 olle 478         // Reference name is plate name, remove extensions and convert to lower case
3624 25 Nov 15 olle 479         String sourceRefName = referenceName;
3624 25 Nov 15 olle 480         int dotIndex = referenceName.indexOf(".");
3624 25 Nov 15 olle 481         if (dotIndex >= 0)
3624 25 Nov 15 olle 482         {
3624 25 Nov 15 olle 483           // Remove extension
3624 25 Nov 15 olle 484           sourceRefName = referenceName.substring(0, dotIndex);
3624 25 Nov 15 olle 485         }
3624 25 Nov 15 olle 486         sourceRefName = sourceRefName.toLowerCase();
3624 25 Nov 15 olle 487         String filename = sourceRefName + ".amp28" + ".plt";
3624 25 Nov 15 olle 488
3624 25 Nov 15 olle 489         resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
3624 25 Nov 15 olle 490         resp.setContentType("text/plain");
3624 25 Nov 15 olle 491         resp.setCharacterEncoding("UTF-8");
3624 25 Nov 15 olle 492
3871 22 Apr 16 olle 493         OutputStream out = resp.getOutputStream();
3624 25 Nov 15 olle 494
3871 22 Apr 16 olle 495         // Get contents from static cache
3871 22 Apr 16 olle 496         String cacheKey = tmpFilePath;
3871 22 Apr 16 olle 497         StaticCache cache = Application.getStaticCache();
3871 22 Apr 16 olle 498         int timeout = 1000;
3871 22 Apr 16 olle 499         cache.read(cacheKey, out, timeout);
3624 25 Nov 15 olle 500         out.flush();
3624 25 Nov 15 olle 501         out.close();
3871 22 Apr 16 olle 502         // Delete temporary cache after download
3871 22 Apr 16 olle 503         cache.delete(cacheKey, timeout);
3624 25 Nov 15 olle 504       }
3547 16 Oct 15 olle 505       else if ("GetLibPrepKitData".equals(cmd))
3547 16 Oct 15 olle 506       {
3547 16 Oct 15 olle 507         String libPrepKitNameFilter = req.getParameter("libPrepKitNameFilter");
3547 16 Oct 15 olle 508         if (libPrepKitNameFilter != null && libPrepKitNameFilter.equals(""))
3547 16 Oct 15 olle 509         {
3547 16 Oct 15 olle 510           libPrepKitNameFilter = null;
3547 16 Oct 15 olle 511         }
3547 16 Oct 15 olle 512         String libPrepKitFilter = req.getParameter("libPrepKitFilter");
3610 20 Nov 15 olle 513         Boolean activeFilter = null;
3547 16 Oct 15 olle 514         Boolean usedFilter = null;
3547 16 Oct 15 olle 515         if (libPrepKitFilter != null)
3547 16 Oct 15 olle 516         {
3610 20 Nov 15 olle 517           if (libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_USED_ACTIVE)
3610 20 Nov 15 olle 518             || libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_ACTIVE))
3547 16 Oct 15 olle 519           {
3610 20 Nov 15 olle 520             activeFilter = true;
3547 16 Oct 15 olle 521           }
3610 20 Nov 15 olle 522           else if (libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_USED_INACTIVE)
3610 20 Nov 15 olle 523               || libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_INACTIVE))
3547 16 Oct 15 olle 524           {
3610 20 Nov 15 olle 525             activeFilter = false;
3547 16 Oct 15 olle 526           }
3610 20 Nov 15 olle 527           if (libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_USED_ACTIVE)
3610 20 Nov 15 olle 528               || libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_USED_INACTIVE)
3547 16 Oct 15 olle 529               || libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_USED))
3547 16 Oct 15 olle 530           {
3547 16 Oct 15 olle 531             usedFilter = true;
3547 16 Oct 15 olle 532           }
3547 16 Oct 15 olle 533           else if (libPrepKitFilter.equals(LIB_PREP_KIT_FILTER_UNUSED))
3547 16 Oct 15 olle 534           {
3547 16 Oct 15 olle 535             usedFilter = false;
3547 16 Oct 15 olle 536           }
3547 16 Oct 15 olle 537         }
3547 16 Oct 15 olle 538 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" libPrepKitFilter = " + libPrepKitFilter);
3547 16 Oct 15 olle 539
3610 20 Nov 15 olle 540         dc = sc.newDbControl();
3573 05 Nov 15 olle 541         Integer minUnusedFpaPlateLocs = null;
3610 20 Nov 15 olle 542         JSONObject jsonConsumables = fetchJsonKitData(dc, libPrepKitNameFilter, activeFilter, usedFilter, minUnusedFpaPlateLocs);
3547 16 Oct 15 olle 543
3573 05 Nov 15 olle 544         json.put("consumables", jsonConsumables);
3573 05 Nov 15 olle 545       }
3573 05 Nov 15 olle 546       else if ("GetLibPrepKitsAsPlates".equals(cmd))
3573 05 Nov 15 olle 547       {
3573 05 Nov 15 olle 548         Integer numItems = Values.getInt(req.getParameter("numItems"));
3573 05 Nov 15 olle 549         Integer minUnusedFpaPlateLocs = numItems;
3573 05 Nov 15 olle 550         
3610 20 Nov 15 olle 551         // Get JSONData for all active kits, used or unused
3573 05 Nov 15 olle 552         String libPrepKitNameFilter = null;
3610 20 Nov 15 olle 553         Boolean activeFilter = true;
3573 05 Nov 15 olle 554         Boolean usedFilter = null;
3573 05 Nov 15 olle 555         
3610 20 Nov 15 olle 556         dc = sc.newDbControl();
3610 20 Nov 15 olle 557         JSONObject jsonConsumables = fetchJsonKitData(dc, libPrepKitNameFilter, activeFilter, usedFilter, minUnusedFpaPlateLocs);
3547 16 Oct 15 olle 558
3573 05 Nov 15 olle 559         JSONArray jsonPlates = new JSONArray();
3573 05 Nov 15 olle 560         if (jsonConsumables != null)
3573 05 Nov 15 olle 561         {
3621 25 Nov 15 olle 562           JSONArray jsonKitId = (JSONArray) jsonConsumables.get("KitId");
3573 05 Nov 15 olle 563           JSONArray jsonKitName = (JSONArray) jsonConsumables.get("KitName");
3573 05 Nov 15 olle 564           JSONArray jsonExpirationDate = (JSONArray) jsonConsumables.get("ExpirationDate");
3610 20 Nov 15 olle 565           JSONArray jsonActive = (JSONArray) jsonConsumables.get("Active");
3573 05 Nov 15 olle 566           JSONArray jsonTimesUsed = (JSONArray) jsonConsumables.get("TimesUsed");
3573 05 Nov 15 olle 567           JSONArray jsonUnusedFpaPlateLocs = (JSONArray) jsonConsumables.get("UnusedFpaPlateLocs");
3573 05 Nov 15 olle 568           JSONArray jsonComment = (JSONArray) jsonConsumables.get("Comment");
3547 16 Oct 15 olle 569
3573 05 Nov 15 olle 570           int numKits = jsonKitName.size();
3573 05 Nov 15 olle 571 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" numKits = " + numKits);
3547 16 Oct 15 olle 572
3573 05 Nov 15 olle 573           boolean valid = true;
3573 05 Nov 15 olle 574           json.put("valid", valid);
3547 16 Oct 15 olle 575
3573 05 Nov 15 olle 576           for (int i = 0; i < numKits; i++)
3547 16 Oct 15 olle 577           {
3573 05 Nov 15 olle 578 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i));
3573 05 Nov 15 olle 579             // Store unused FPA plate locations as JSONArray of well names
3573 05 Nov 15 olle 580             JSONArray jsonUnusedFpaWells = new JSONArray();
3573 05 Nov 15 olle 581             JSONArray jsonEffUnusedFpaWells = new JSONArray();
3573 05 Nov 15 olle 582             JSONArray jsonReservedFpaWells = new JSONArray();
3573 05 Nov 15 olle 583             List<String> unusedFpaWellsList = new ArrayList<String>();
3573 05 Nov 15 olle 584             boolean [][] unusedFpaWellsMatrix = new boolean[8][6];
3573 05 Nov 15 olle 585             boolean [][] effUnusedFpaWellsMatrix = new boolean[8][6];
3573 05 Nov 15 olle 586             boolean [][] reservedFpaWellsMatrix = new boolean[8][6];
3573 05 Nov 15 olle 587             List<Integer> numEffUnusedFpaWellsInColumnList = new ArrayList<Integer>();
3573 05 Nov 15 olle 588             JSONArray jsonNumEffUnusedFpaWellsInColumn = new JSONArray();
3573 05 Nov 15 olle 589             int numEffUnusedWells = 0;
3573 05 Nov 15 olle 590             for (int col = 0; col < 6; col++)
3547 16 Oct 15 olle 591             {
3573 05 Nov 15 olle 592               for (int row = 0; row < 8; row++)
3547 16 Oct 15 olle 593               {
3573 05 Nov 15 olle 594                 unusedFpaWellsMatrix[row][col] = false;
3573 05 Nov 15 olle 595                 effUnusedFpaWellsMatrix[row][col] = false;
3573 05 Nov 15 olle 596                 reservedFpaWellsMatrix[row][col] = false;
3573 05 Nov 15 olle 597               }
3573 05 Nov 15 olle 598             }
3573 05 Nov 15 olle 599             String unusedFpaPlateLocsStr = (String) jsonUnusedFpaPlateLocs.get(i);
3573 05 Nov 15 olle 600             String[] unusedWells = unusedFpaPlateLocsStr.split(",", -1);
3573 05 Nov 15 olle 601             // Find unused wells
3573 05 Nov 15 olle 602             for (int j = 0; j < unusedWells.length; j++)
3573 05 Nov 15 olle 603             {
3573 05 Nov 15 olle 604               String unusedWellStr = unusedWells[j];
3573 05 Nov 15 olle 605               String rowLetter = unusedWellStr.substring(0,1);
3573 05 Nov 15 olle 606               String colNoAsString = unusedWellStr.substring(1);
3573 05 Nov 15 olle 607               int row = rowLetterToIndexHashMap.get(rowLetter);
3573 05 Nov 15 olle 608               int col = Integer.parseInt(colNoAsString) - 1;
3573 05 Nov 15 olle 609               unusedFpaWellsMatrix[row][col] = true;
3573 05 Nov 15 olle 610             }
3573 05 Nov 15 olle 611             // Find effective unused wells after removing isolated "islands" in columns
3573 05 Nov 15 olle 612             for (int col = 0; col < 6; col++)
3573 05 Nov 15 olle 613             {
3573 05 Nov 15 olle 614               int firstRow = -1;
3573 05 Nov 15 olle 615               int lastRow = -1;
3573 05 Nov 15 olle 616               for (int row = 0; row < 8; row++)
3573 05 Nov 15 olle 617               {
3573 05 Nov 15 olle 618                 boolean unusedWell = unusedFpaWellsMatrix[row][col];
3573 05 Nov 15 olle 619                 if (unusedWell)
3547 16 Oct 15 olle 620                 {
3573 05 Nov 15 olle 621                   // Well is unused
3573 05 Nov 15 olle 622                   if (firstRow < 0)
3573 05 Nov 15 olle 623                   {
3573 05 Nov 15 olle 624                     firstRow = row;
3573 05 Nov 15 olle 625                   }
3573 05 Nov 15 olle 626                   // Update last row index continuously
3573 05 Nov 15 olle 627                   lastRow = row;
3547 16 Oct 15 olle 628                 }
3573 05 Nov 15 olle 629                 else
3547 16 Oct 15 olle 630                 {
3573 05 Nov 15 olle 631                   // Well is not unused
3573 05 Nov 15 olle 632                   if (firstRow >= 0)
3573 05 Nov 15 olle 633                   {
3573 05 Nov 15 olle 634                     // Part of column with unused wells does not reach bottom, reset variables
3573 05 Nov 15 olle 635                     firstRow = -1;
3573 05 Nov 15 olle 636                     lastRow = -1;
3573 05 Nov 15 olle 637                   }
3547 16 Oct 15 olle 638                 }
3573 05 Nov 15 olle 639               }
3573 05 Nov 15 olle 640               // Store info on effective unused wells for column
3573 05 Nov 15 olle 641               int numEffUnusedWellsInColumn = 0;
3573 05 Nov 15 olle 642               if (firstRow >= 0 && lastRow >=0)
3573 05 Nov 15 olle 643               {
3573 05 Nov 15 olle 644                 for (int row = firstRow; row <= lastRow; row++)
3547 16 Oct 15 olle 645                 {
3573 05 Nov 15 olle 646                   effUnusedFpaWellsMatrix[row][col] = true;
3573 05 Nov 15 olle 647                   numEffUnusedWellsInColumn++;
3547 16 Oct 15 olle 648                 }
3547 16 Oct 15 olle 649               }
3573 05 Nov 15 olle 650               numEffUnusedFpaWellsInColumnList.add(numEffUnusedWellsInColumn);
3573 05 Nov 15 olle 651               jsonNumEffUnusedFpaWellsInColumn.add(numEffUnusedWellsInColumn);
3573 05 Nov 15 olle 652               numEffUnusedWells += numEffUnusedWellsInColumn;
3573 05 Nov 15 olle 653             }
3573 05 Nov 15 olle 654 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " minUnusedFpaPlateLocs = " + minUnusedFpaPlateLocs);
3573 05 Nov 15 olle 655             // Reserve effective unused wells for desired number of new FPA aliquots
3643 03 Dec 15 olle 656             int numReservedWells = 0;
3573 05 Nov 15 olle 657             if (minUnusedFpaPlateLocs > 0)
3547 16 Oct 15 olle 658             {
3573 05 Nov 15 olle 659               boolean reservedWellsOk = false;
3573 05 Nov 15 olle 660               for (int col = 0; col < 6 && !reservedWellsOk; col++)
3547 16 Oct 15 olle 661               {
3573 05 Nov 15 olle 662                 int numEffUnusedWellsInColumn = (Integer) numEffUnusedFpaWellsInColumnList.get(col);
3573 05 Nov 15 olle 663 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn);
3573 05 Nov 15 olle 664                 if (numEffUnusedWellsInColumn < 8)
3547 16 Oct 15 olle 665                 {
3573 05 Nov 15 olle 666                   // Check if number of aliquots fits in current non-full column
3573 05 Nov 15 olle 667                   if (numEffUnusedWellsInColumn >= minUnusedFpaPlateLocs)
3547 16 Oct 15 olle 668                   {
3573 05 Nov 15 olle 669                     // Reserve wells so as to end with bottom well in column
3573 05 Nov 15 olle 670                     int startRow = 8 - minUnusedFpaPlateLocs;
3573 05 Nov 15 olle 671                     for (int row = startRow; row < 8; row++)
3573 05 Nov 15 olle 672                     {
3573 05 Nov 15 olle 673                       reservedFpaWellsMatrix[row][col] = true;
3643 03 Dec 15 olle 674                       numReservedWells++;
3573 05 Nov 15 olle 675                     }
3573 05 Nov 15 olle 676                     reservedWellsOk = true;
3547 16 Oct 15 olle 677                   }
3577 06 Nov 15 olle 678                   // Check if number of aliquots fits in current non-full column plus a number of filled columns to the right
3577 06 Nov 15 olle 679                   else if (numEffUnusedWellsInColumn >= (minUnusedFpaPlateLocs % 8))
3547 16 Oct 15 olle 680                   {
3573 05 Nov 15 olle 681                     // Check if the required number of full right columns exists
3573 05 Nov 15 olle 682                     boolean columnsExist = true;
3573 05 Nov 15 olle 683                     int numExtraColumnsNeeded = minUnusedFpaPlateLocs/8;
3573 05 Nov 15 olle 684 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn + " numExtraColumnsNeeded = " + numExtraColumnsNeeded);
3573 05 Nov 15 olle 685                     if ((col + numExtraColumnsNeeded) < 6)
3573 05 Nov 15 olle 686                     {
3573 05 Nov 15 olle 687                       for (int testCol = col + 1; testCol <= col + numExtraColumnsNeeded; testCol++)
3573 05 Nov 15 olle 688                       {
3573 05 Nov 15 olle 689                         int numEffUnusedWellsInTestColumn = (Integer) numEffUnusedFpaWellsInColumnList.get(testCol);
3573 05 Nov 15 olle 690                         if (numEffUnusedWellsInTestColumn < 8)
3573 05 Nov 15 olle 691                         {
3573 05 Nov 15 olle 692                           columnsExist = false;
3573 05 Nov 15 olle 693                         }
3573 05 Nov 15 olle 694                       }
3573 05 Nov 15 olle 695                     }
3573 05 Nov 15 olle 696                     else
3573 05 Nov 15 olle 697                     {
3573 05 Nov 15 olle 698                       columnsExist = false;
3573 05 Nov 15 olle 699                     }
3573 05 Nov 15 olle 700                     if (columnsExist)
3573 05 Nov 15 olle 701                     {
3577 06 Nov 15 olle 702                       // Reserve wells so as to end with bottom well in column
3577 06 Nov 15 olle 703                       int startRow = 8 - (minUnusedFpaPlateLocs % 8);
3573 05 Nov 15 olle 704                       for (int row = startRow; row < 8; row++)
3573 05 Nov 15 olle 705                       {
3573 05 Nov 15 olle 706                         reservedFpaWellsMatrix[row][col] = true;
3643 03 Dec 15 olle 707                         numReservedWells++;
3573 05 Nov 15 olle 708                       }
3573 05 Nov 15 olle 709                       // Reserve all available wells in needed columns to the right
3573 05 Nov 15 olle 710                       for (int testCol = col + 1; testCol <= col + numExtraColumnsNeeded; testCol++)
3573 05 Nov 15 olle 711                       {
3573 05 Nov 15 olle 712                         for (int row = 0; row < 8; row++)
3573 05 Nov 15 olle 713                         {
3573 05 Nov 15 olle 714                           reservedFpaWellsMatrix[row][testCol] = true;
3643 03 Dec 15 olle 715                           numReservedWells++;
3573 05 Nov 15 olle 716                         }                                          
3573 05 Nov 15 olle 717                       }
3573 05 Nov 15 olle 718                       reservedWellsOk = true;
3573 05 Nov 15 olle 719                     }
3547 16 Oct 15 olle 720                   }
3547 16 Oct 15 olle 721                 }
3573 05 Nov 15 olle 722                 // Check if current column is full and number of aliquots fits in current column plus a number of filled columns to the right
3573 05 Nov 15 olle 723                 else
3547 16 Oct 15 olle 724                 {
3573 05 Nov 15 olle 725                   // Check if the required number of full right columns exists
3573 05 Nov 15 olle 726                   boolean columnsExist = true;
3573 05 Nov 15 olle 727                   int numColumnsNeeded = minUnusedFpaPlateLocs/8;
3573 05 Nov 15 olle 728                   if ((minUnusedFpaPlateLocs % 8) > 0)
3547 16 Oct 15 olle 729                   {
3573 05 Nov 15 olle 730                     numColumnsNeeded++;
3547 16 Oct 15 olle 731                   }
3573 05 Nov 15 olle 732 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn + " numColumnsNeeded = " + numColumnsNeeded);
3573 05 Nov 15 olle 733                   if ((col + numColumnsNeeded - 1) < 6)
3573 05 Nov 15 olle 734                   {
3573 05 Nov 15 olle 735                     for (int testCol = col; testCol < col + numColumnsNeeded; testCol++)
3573 05 Nov 15 olle 736                     {
3573 05 Nov 15 olle 737                       int numEffUnusedWellsInTestColumn = (Integer) numEffUnusedFpaWellsInColumnList.get(testCol);
3573 05 Nov 15 olle 738                       if (numEffUnusedWellsInTestColumn < 8)
3573 05 Nov 15 olle 739                       {
3573 05 Nov 15 olle 740                         columnsExist = false;
3573 05 Nov 15 olle 741                       }
3573 05 Nov 15 olle 742                     }
3573 05 Nov 15 olle 743                   }
3573 05 Nov 15 olle 744                   else
3573 05 Nov 15 olle 745                   {
3573 05 Nov 15 olle 746                     columnsExist = false;
3573 05 Nov 15 olle 747                   }
3573 05 Nov 15 olle 748                   if (columnsExist)
3573 05 Nov 15 olle 749                   {
3643 03 Dec 15 olle 750                     // Reserve available wells in current column and columns to the right
3573 05 Nov 15 olle 751                     int neededWells = minUnusedFpaPlateLocs;
3573 05 Nov 15 olle 752 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn + " neededWells = " + neededWells);
3573 05 Nov 15 olle 753                     for (int testCol = col; testCol < col + numColumnsNeeded; testCol++)
3573 05 Nov 15 olle 754                     {
3573 05 Nov 15 olle 755                       for (int row = 0; row < 8; row++)
3573 05 Nov 15 olle 756                       {
3573 05 Nov 15 olle 757                         if (neededWells > 0)
3573 05 Nov 15 olle 758                         {
3573 05 Nov 15 olle 759                           reservedFpaWellsMatrix[row][testCol] = true;
3643 03 Dec 15 olle 760                           numReservedWells++;
3573 05 Nov 15 olle 761                           neededWells--;
3573 05 Nov 15 olle 762                         }
3573 05 Nov 15 olle 763                       }                                          
3573 05 Nov 15 olle 764                     }
3573 05 Nov 15 olle 765                     reservedWellsOk = true;
3573 05 Nov 15 olle 766                   }                  
3547 16 Oct 15 olle 767                 }
3573 05 Nov 15 olle 768               }
3573 05 Nov 15 olle 769             }
3573 05 Nov 15 olle 770             // Store info on effective unused wells for kit
3573 05 Nov 15 olle 771             for (int col = 0; col < 6; col++)
3573 05 Nov 15 olle 772             {
3573 05 Nov 15 olle 773               int colNo = col + 1;
3573 05 Nov 15 olle 774               for (int row = 0; row < 8; row++)
3573 05 Nov 15 olle 775               {
3573 05 Nov 15 olle 776                 String rowLetter = indexToRowLetterHashMap.get(row);
3573 05 Nov 15 olle 777                 String wellStr = rowLetter + colNo;
3573 05 Nov 15 olle 778 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " row = " + row + " col = " + col + " wellStr = " + wellStr);
3573 05 Nov 15 olle 779                 if (unusedFpaWellsMatrix[row][col])
3547 16 Oct 15 olle 780                 {
3573 05 Nov 15 olle 781                   jsonUnusedFpaWells.add(wellStr);
3547 16 Oct 15 olle 782                 }
3573 05 Nov 15 olle 783                 if (effUnusedFpaWellsMatrix[row][col])
3573 05 Nov 15 olle 784                 {
3573 05 Nov 15 olle 785                   jsonEffUnusedFpaWells.add(wellStr);
3573 05 Nov 15 olle 786                 }
3573 05 Nov 15 olle 787                 if (reservedFpaWellsMatrix[row][col])
3573 05 Nov 15 olle 788                 {
3573 05 Nov 15 olle 789                   jsonReservedFpaWells.add(wellStr);
3573 05 Nov 15 olle 790                 }
3547 16 Oct 15 olle 791               }
3573 05 Nov 15 olle 792             }
3547 16 Oct 15 olle 793
3573 05 Nov 15 olle 794             // Create new dummy plate representing kit
3573 05 Nov 15 olle 795             PlateGeometry geometry = BioplateType.DNA.getPlateGeometry(dc);
3573 05 Nov 15 olle 796             BioPlateType plateType = BioplateType.DNA.load(dc);
3547 16 Oct 15 olle 797
3573 05 Nov 15 olle 798             ReactionPlate kitPlate = ReactionPlate.getNew(dc, BioplateType.DNA);
3573 05 Nov 15 olle 799           
3573 05 Nov 15 olle 800             BioPlate bioPlate = kitPlate.getBioPlate();
3573 05 Nov 15 olle 801 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" bioPlate = " + bioPlate);
3573 05 Nov 15 olle 802             int rows = bioPlate.getRows();
3573 05 Nov 15 olle 803             int cols = bioPlate.getColumns();
3573 05 Nov 15 olle 804 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" rows = " + rows + " cols = " + cols);
3547 16 Oct 15 olle 805
3573 05 Nov 15 olle 806             JSONArray jsonWells = new JSONArray();
3573 05 Nov 15 olle 807             for (int r = 0; r < rows; ++r)
3547 16 Oct 15 olle 808             {
3573 05 Nov 15 olle 809               for (int c = 0; c < cols; ++c)
3573 05 Nov 15 olle 810               {
3573 05 Nov 15 olle 811                 BioWell well = bioPlate.getBioWell(r, c);
3573 05 Nov 15 olle 812 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" r = " + r + " c = " + c + " well = " + well);
3573 05 Nov 15 olle 813                 JSONObject jsonWell = new JSONObject();
3573 05 Nov 15 olle 814                 //jsonWell.put("id", well.getId());
3573 05 Nov 15 olle 815                 jsonWell.put("row", well.getRow());
3573 05 Nov 15 olle 816                 jsonWell.put("column", well.getColumn());
3573 05 Nov 15 olle 817 /*            
3573 05 Nov 15 olle 818                 if (loadBioMaterial)
3573 05 Nov 15 olle 819                 {
3573 05 Nov 15 olle 820                   MeasuredBioMaterial bm = well.getBioMaterial();
3573 05 Nov 15 olle 821                   if (bm != null)
3573 05 Nov 15 olle 822                   {
3573 05 Nov 15 olle 823                     JSONObject jsonBm = new JSONObject();
3573 05 Nov 15 olle 824                     jsonBm.put("id", bm.getId());
3573 05 Nov 15 olle 825                     jsonBm.put("name", bm.getName());
3573 05 Nov 15 olle 826                     jsonBm.put("comment", bm.getDescription());
3573 05 Nov 15 olle 827                     jsonWell.put("bioMaterial", jsonBm);
3573 05 Nov 15 olle 828                   }
3573 05 Nov 15 olle 829                 }
3573 05 Nov 15 olle 830 */          
3573 05 Nov 15 olle 831                 jsonWells.add(jsonWell);
3573 05 Nov 15 olle 832               }
3547 16 Oct 15 olle 833             }
3643 03 Dec 15 olle 834             if (numReservedWells >= minUnusedFpaPlateLocs)
3643 03 Dec 15 olle 835             {
3643 03 Dec 15 olle 836               JSONObject jsonPlate = kitPlate.asJSONObject();
3643 03 Dec 15 olle 837               // Add data for wells
3643 03 Dec 15 olle 838               jsonPlate.put("bioWells", jsonWells);
3643 03 Dec 15 olle 839               // Add extra kit data
3643 03 Dec 15 olle 840               jsonPlate.put("kitId", (Integer) jsonKitId.get(i));
3643 03 Dec 15 olle 841               jsonPlate.put("kitName", (String) jsonKitName.get(i));
3643 03 Dec 15 olle 842               jsonPlate.put("expirationDate", (String) jsonExpirationDate.get(i));
3643 03 Dec 15 olle 843               jsonPlate.put("timesUsed", jsonTimesUsed.get(i));
3643 03 Dec 15 olle 844               jsonPlate.put("numEffUnusedWells", numEffUnusedWells);
3643 03 Dec 15 olle 845               jsonPlate.put("unusedWells", jsonUnusedFpaWells);
3643 03 Dec 15 olle 846               jsonPlate.put("effUnusedWells", jsonEffUnusedFpaWells);
3643 03 Dec 15 olle 847               jsonPlate.put("reservedWells", jsonReservedFpaWells);
3643 03 Dec 15 olle 848               jsonPlate.put("numEffUnusedWellsInColumn", jsonNumEffUnusedFpaWellsInColumn);
3573 05 Nov 15 olle 849 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" jsonPlate = " + jsonPlate);
3573 05 Nov 15 olle 850 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " jsonReservedFpaWells = " + jsonReservedFpaWells);
3643 03 Dec 15 olle 851               //
3643 03 Dec 15 olle 852               jsonPlates.add(jsonPlate);
3643 03 Dec 15 olle 853             }
3573 05 Nov 15 olle 854           }
3547 16 Oct 15 olle 855         }
3573 05 Nov 15 olle 856         json.put("plates", jsonPlates);
3547 16 Oct 15 olle 857       }
4206 04 Nov 16 olle 858       else if ("GetLibPrepKitAsPlate".equals(cmd))
4206 04 Nov 16 olle 859       {
4206 04 Nov 16 olle 860         Integer index = Values.getInteger(req.getParameter("index"), null);
4206 04 Nov 16 olle 861         String libPrepKitNameFilter = Values.getString(req.getParameter("libPrepKitName"));
4206 04 Nov 16 olle 862         Integer minUnusedFpaPlateLocs = 0;
4206 04 Nov 16 olle 863         
4206 04 Nov 16 olle 864         // Get JSONData for kit, active or inactive, used or unused
4206 04 Nov 16 olle 865         Boolean activeFilter = null;
4206 04 Nov 16 olle 866         Boolean usedFilter = null;
4206 04 Nov 16 olle 867         
4206 04 Nov 16 olle 868         dc = sc.newDbControl();
4206 04 Nov 16 olle 869         JSONObject jsonConsumables = fetchJsonKitData(dc, libPrepKitNameFilter, activeFilter, usedFilter, minUnusedFpaPlateLocs);
4206 04 Nov 16 olle 870
4206 04 Nov 16 olle 871         JSONObject jsonPlate = new JSONObject();
4206 04 Nov 16 olle 872         if (jsonConsumables != null)
4206 04 Nov 16 olle 873         {
4206 04 Nov 16 olle 874           JSONArray jsonKitId = (JSONArray) jsonConsumables.get("KitId");
4206 04 Nov 16 olle 875           JSONArray jsonKitName = (JSONArray) jsonConsumables.get("KitName");
4206 04 Nov 16 olle 876           JSONArray jsonExpirationDate = (JSONArray) jsonConsumables.get("ExpirationDate");
4206 04 Nov 16 olle 877           JSONArray jsonActive = (JSONArray) jsonConsumables.get("Active");
4206 04 Nov 16 olle 878           JSONArray jsonTimesUsed = (JSONArray) jsonConsumables.get("TimesUsed");
4206 04 Nov 16 olle 879           JSONArray jsonUnusedFpaPlateLocs = (JSONArray) jsonConsumables.get("UnusedFpaPlateLocs");
4206 04 Nov 16 olle 880           JSONArray jsonComment = (JSONArray) jsonConsumables.get("Comment");
4206 04 Nov 16 olle 881
4206 04 Nov 16 olle 882           int numKits = jsonKitName.size();
4206 04 Nov 16 olle 883
4206 04 Nov 16 olle 884           boolean valid = true;
4206 04 Nov 16 olle 885           json.put("valid", valid);
4206 04 Nov 16 olle 886
4206 04 Nov 16 olle 887           if (numKits > 0)
4206 04 Nov 16 olle 888           {
4206 04 Nov 16 olle 889             // Get first kit in list (normally it consists of only one kit)
4206 04 Nov 16 olle 890             int i = 0;
4206 04 Nov 16 olle 891 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i));
4206 04 Nov 16 olle 892             // Store unused FPA plate locations as JSONArray of well names
4206 04 Nov 16 olle 893             JSONArray jsonUnusedFpaWells = new JSONArray();
4206 04 Nov 16 olle 894             JSONArray jsonEffUnusedFpaWells = new JSONArray();
4206 04 Nov 16 olle 895             JSONArray jsonReservedFpaWells = new JSONArray();
4206 04 Nov 16 olle 896             List<String> unusedFpaWellsList = new ArrayList<String>();
4206 04 Nov 16 olle 897             boolean [][] unusedFpaWellsMatrix = new boolean[8][6];
4206 04 Nov 16 olle 898             boolean [][] effUnusedFpaWellsMatrix = new boolean[8][6];
4206 04 Nov 16 olle 899             boolean [][] reservedFpaWellsMatrix = new boolean[8][6];
4206 04 Nov 16 olle 900             List<Integer> numEffUnusedFpaWellsInColumnList = new ArrayList<Integer>();
4206 04 Nov 16 olle 901             JSONArray jsonNumEffUnusedFpaWellsInColumn = new JSONArray();
4206 04 Nov 16 olle 902             int numEffUnusedWells = 0;
4206 04 Nov 16 olle 903             for (int col = 0; col < 6; col++)
4206 04 Nov 16 olle 904             {
4206 04 Nov 16 olle 905               for (int row = 0; row < 8; row++)
4206 04 Nov 16 olle 906               {
4206 04 Nov 16 olle 907                 unusedFpaWellsMatrix[row][col] = false;
4206 04 Nov 16 olle 908                 effUnusedFpaWellsMatrix[row][col] = false;
4206 04 Nov 16 olle 909                 reservedFpaWellsMatrix[row][col] = false;
4206 04 Nov 16 olle 910               }
4206 04 Nov 16 olle 911             }
4206 04 Nov 16 olle 912             String unusedFpaPlateLocsStr = (String) jsonUnusedFpaPlateLocs.get(i);
4206 04 Nov 16 olle 913             String[] unusedWells = unusedFpaPlateLocsStr.split(",", -1);
4206 04 Nov 16 olle 914             // Find unused wells
4206 04 Nov 16 olle 915             for (int j = 0; j < unusedWells.length; j++)
4206 04 Nov 16 olle 916             {
4206 04 Nov 16 olle 917               String unusedWellStr = unusedWells[j];
4206 04 Nov 16 olle 918               String rowLetter = unusedWellStr.substring(0,1);
4206 04 Nov 16 olle 919               String colNoAsString = unusedWellStr.substring(1);
4206 04 Nov 16 olle 920               int row = rowLetterToIndexHashMap.get(rowLetter);
4206 04 Nov 16 olle 921               int col = Integer.parseInt(colNoAsString) - 1;
4206 04 Nov 16 olle 922               unusedFpaWellsMatrix[row][col] = true;
4206 04 Nov 16 olle 923             }
4206 04 Nov 16 olle 924             // Find effective unused wells after removing isolated "islands" in columns
4206 04 Nov 16 olle 925             for (int col = 0; col < 6; col++)
4206 04 Nov 16 olle 926             {
4206 04 Nov 16 olle 927               int firstRow = -1;
4206 04 Nov 16 olle 928               int lastRow = -1;
4206 04 Nov 16 olle 929               for (int row = 0; row < 8; row++)
4206 04 Nov 16 olle 930               {
4206 04 Nov 16 olle 931                 boolean unusedWell = unusedFpaWellsMatrix[row][col];
4206 04 Nov 16 olle 932                 if (unusedWell)
4206 04 Nov 16 olle 933                 {
4206 04 Nov 16 olle 934                   // Well is unused
4206 04 Nov 16 olle 935                   if (firstRow < 0)
4206 04 Nov 16 olle 936                   {
4206 04 Nov 16 olle 937                     firstRow = row;
4206 04 Nov 16 olle 938                   }
4206 04 Nov 16 olle 939                   // Update last row index continuously
4206 04 Nov 16 olle 940                   lastRow = row;
4206 04 Nov 16 olle 941                 }
4206 04 Nov 16 olle 942                 else
4206 04 Nov 16 olle 943                 {
4206 04 Nov 16 olle 944                   // Well is not unused
4206 04 Nov 16 olle 945                   if (firstRow >= 0)
4206 04 Nov 16 olle 946                   {
4206 04 Nov 16 olle 947                     // Part of column with unused wells does not reach bottom, reset variables
4206 04 Nov 16 olle 948                     firstRow = -1;
4206 04 Nov 16 olle 949                     lastRow = -1;
4206 04 Nov 16 olle 950                   }
4206 04 Nov 16 olle 951                 }
4206 04 Nov 16 olle 952               }
4206 04 Nov 16 olle 953               // Store info on effective unused wells for column
4206 04 Nov 16 olle 954               int numEffUnusedWellsInColumn = 0;
4206 04 Nov 16 olle 955               if (firstRow >= 0 && lastRow >=0)
4206 04 Nov 16 olle 956               {
4206 04 Nov 16 olle 957                 for (int row = firstRow; row <= lastRow; row++)
4206 04 Nov 16 olle 958                 {
4206 04 Nov 16 olle 959                   effUnusedFpaWellsMatrix[row][col] = true;
4206 04 Nov 16 olle 960                   numEffUnusedWellsInColumn++;
4206 04 Nov 16 olle 961                 }
4206 04 Nov 16 olle 962               }
4206 04 Nov 16 olle 963               numEffUnusedFpaWellsInColumnList.add(numEffUnusedWellsInColumn);
4206 04 Nov 16 olle 964               jsonNumEffUnusedFpaWellsInColumn.add(numEffUnusedWellsInColumn);
4206 04 Nov 16 olle 965               numEffUnusedWells += numEffUnusedWellsInColumn;
4206 04 Nov 16 olle 966             }
4206 04 Nov 16 olle 967 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " minUnusedFpaPlateLocs = " + minUnusedFpaPlateLocs);
4206 04 Nov 16 olle 968             // Reserve effective unused wells for desired number of new FPA aliquots
4206 04 Nov 16 olle 969             int numReservedWells = 0;
4206 04 Nov 16 olle 970             if (minUnusedFpaPlateLocs > 0)
4206 04 Nov 16 olle 971             {
4206 04 Nov 16 olle 972               boolean reservedWellsOk = false;
4206 04 Nov 16 olle 973               for (int col = 0; col < 6 && !reservedWellsOk; col++)
4206 04 Nov 16 olle 974               {
4206 04 Nov 16 olle 975                 int numEffUnusedWellsInColumn = (Integer) numEffUnusedFpaWellsInColumnList.get(col);
4206 04 Nov 16 olle 976 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn);
4206 04 Nov 16 olle 977                 if (numEffUnusedWellsInColumn < 8)
4206 04 Nov 16 olle 978                 {
4206 04 Nov 16 olle 979                   // Check if number of aliquots fits in current non-full column
4206 04 Nov 16 olle 980                   if (numEffUnusedWellsInColumn >= minUnusedFpaPlateLocs)
4206 04 Nov 16 olle 981                   {
4206 04 Nov 16 olle 982                     // Reserve wells so as to end with bottom well in column
4206 04 Nov 16 olle 983                     int startRow = 8 - minUnusedFpaPlateLocs;
4206 04 Nov 16 olle 984                     for (int row = startRow; row < 8; row++)
4206 04 Nov 16 olle 985                     {
4206 04 Nov 16 olle 986                       reservedFpaWellsMatrix[row][col] = true;
4206 04 Nov 16 olle 987                       numReservedWells++;
4206 04 Nov 16 olle 988                     }
4206 04 Nov 16 olle 989                     reservedWellsOk = true;
4206 04 Nov 16 olle 990                   }
4206 04 Nov 16 olle 991                   // Check if number of aliquots fits in current non-full column plus a number of filled columns to the right
4206 04 Nov 16 olle 992                   else if (numEffUnusedWellsInColumn >= (minUnusedFpaPlateLocs % 8))
4206 04 Nov 16 olle 993                   {
4206 04 Nov 16 olle 994                     // Check if the required number of full right columns exists
4206 04 Nov 16 olle 995                     boolean columnsExist = true;
4206 04 Nov 16 olle 996                     int numExtraColumnsNeeded = minUnusedFpaPlateLocs/8;
4206 04 Nov 16 olle 997 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn + " numExtraColumnsNeeded = " + numExtraColumnsNeeded);
4206 04 Nov 16 olle 998                     if ((col + numExtraColumnsNeeded) < 6)
4206 04 Nov 16 olle 999                     {
4206 04 Nov 16 olle 1000                       for (int testCol = col + 1; testCol <= col + numExtraColumnsNeeded; testCol++)
4206 04 Nov 16 olle 1001                       {
4206 04 Nov 16 olle 1002                         int numEffUnusedWellsInTestColumn = (Integer) numEffUnusedFpaWellsInColumnList.get(testCol);
4206 04 Nov 16 olle 1003                         if (numEffUnusedWellsInTestColumn < 8)
4206 04 Nov 16 olle 1004                         {
4206 04 Nov 16 olle 1005                           columnsExist = false;
4206 04 Nov 16 olle 1006                         }
4206 04 Nov 16 olle 1007                       }
4206 04 Nov 16 olle 1008                     }
4206 04 Nov 16 olle 1009                     else
4206 04 Nov 16 olle 1010                     {
4206 04 Nov 16 olle 1011                       columnsExist = false;
4206 04 Nov 16 olle 1012                     }
4206 04 Nov 16 olle 1013                     if (columnsExist)
4206 04 Nov 16 olle 1014                     {
4206 04 Nov 16 olle 1015                       // Reserve wells so as to end with bottom well in column
4206 04 Nov 16 olle 1016                       int startRow = 8 - (minUnusedFpaPlateLocs % 8);
4206 04 Nov 16 olle 1017                       for (int row = startRow; row < 8; row++)
4206 04 Nov 16 olle 1018                       {
4206 04 Nov 16 olle 1019                         reservedFpaWellsMatrix[row][col] = true;
4206 04 Nov 16 olle 1020                         numReservedWells++;
4206 04 Nov 16 olle 1021                       }
4206 04 Nov 16 olle 1022                       // Reserve all available wells in needed columns to the right
4206 04 Nov 16 olle 1023                       for (int testCol = col + 1; testCol <= col + numExtraColumnsNeeded; testCol++)
4206 04 Nov 16 olle 1024                       {
4206 04 Nov 16 olle 1025                         for (int row = 0; row < 8; row++)
4206 04 Nov 16 olle 1026                         {
4206 04 Nov 16 olle 1027                           reservedFpaWellsMatrix[row][testCol] = true;
4206 04 Nov 16 olle 1028                           numReservedWells++;
4206 04 Nov 16 olle 1029                         }                                          
4206 04 Nov 16 olle 1030                       }
4206 04 Nov 16 olle 1031                       reservedWellsOk = true;
4206 04 Nov 16 olle 1032                     }
4206 04 Nov 16 olle 1033                   }
4206 04 Nov 16 olle 1034                 }
4206 04 Nov 16 olle 1035                 // Check if current column is full and number of aliquots fits in current column plus a number of filled columns to the right
4206 04 Nov 16 olle 1036                 else
4206 04 Nov 16 olle 1037                 {
4206 04 Nov 16 olle 1038                   // Check if the required number of full right columns exists
4206 04 Nov 16 olle 1039                   boolean columnsExist = true;
4206 04 Nov 16 olle 1040                   int numColumnsNeeded = minUnusedFpaPlateLocs/8;
4206 04 Nov 16 olle 1041                   if ((minUnusedFpaPlateLocs % 8) > 0)
4206 04 Nov 16 olle 1042                   {
4206 04 Nov 16 olle 1043                     numColumnsNeeded++;
4206 04 Nov 16 olle 1044                   }
4206 04 Nov 16 olle 1045 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn + " numColumnsNeeded = " + numColumnsNeeded);
4206 04 Nov 16 olle 1046                   if ((col + numColumnsNeeded - 1) < 6)
4206 04 Nov 16 olle 1047                   {
4206 04 Nov 16 olle 1048                     for (int testCol = col; testCol < col + numColumnsNeeded; testCol++)
4206 04 Nov 16 olle 1049                     {
4206 04 Nov 16 olle 1050                       int numEffUnusedWellsInTestColumn = (Integer) numEffUnusedFpaWellsInColumnList.get(testCol);
4206 04 Nov 16 olle 1051                       if (numEffUnusedWellsInTestColumn < 8)
4206 04 Nov 16 olle 1052                       {
4206 04 Nov 16 olle 1053                         columnsExist = false;
4206 04 Nov 16 olle 1054                       }
4206 04 Nov 16 olle 1055                     }
4206 04 Nov 16 olle 1056                   }
4206 04 Nov 16 olle 1057                   else
4206 04 Nov 16 olle 1058                   {
4206 04 Nov 16 olle 1059                     columnsExist = false;
4206 04 Nov 16 olle 1060                   }
4206 04 Nov 16 olle 1061                   if (columnsExist)
4206 04 Nov 16 olle 1062                   {
4206 04 Nov 16 olle 1063                     // Reserve available wells in current column and columns to the right
4206 04 Nov 16 olle 1064                     int neededWells = minUnusedFpaPlateLocs;
4206 04 Nov 16 olle 1065 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " col = " + col + " numEffUnusedWellsInColumn = " + numEffUnusedWellsInColumn + " neededWells = " + neededWells);
4206 04 Nov 16 olle 1066                     for (int testCol = col; testCol < col + numColumnsNeeded; testCol++)
4206 04 Nov 16 olle 1067                     {
4206 04 Nov 16 olle 1068                       for (int row = 0; row < 8; row++)
4206 04 Nov 16 olle 1069                       {
4206 04 Nov 16 olle 1070                         if (neededWells > 0)
4206 04 Nov 16 olle 1071                         {
4206 04 Nov 16 olle 1072                           reservedFpaWellsMatrix[row][testCol] = true;
4206 04 Nov 16 olle 1073                           numReservedWells++;
4206 04 Nov 16 olle 1074                           neededWells--;
4206 04 Nov 16 olle 1075                         }
4206 04 Nov 16 olle 1076                       }                                          
4206 04 Nov 16 olle 1077                     }
4206 04 Nov 16 olle 1078                     reservedWellsOk = true;
4206 04 Nov 16 olle 1079                   }                  
4206 04 Nov 16 olle 1080                 }
4206 04 Nov 16 olle 1081               }
4206 04 Nov 16 olle 1082             }
4206 04 Nov 16 olle 1083             // Store info on effective unused wells for kit
4206 04 Nov 16 olle 1084             for (int col = 0; col < 6; col++)
4206 04 Nov 16 olle 1085             {
4206 04 Nov 16 olle 1086               int colNo = col + 1;
4206 04 Nov 16 olle 1087               for (int row = 0; row < 8; row++)
4206 04 Nov 16 olle 1088               {
4206 04 Nov 16 olle 1089                 String rowLetter = indexToRowLetterHashMap.get(row);
4206 04 Nov 16 olle 1090                 String wellStr = rowLetter + colNo;
4206 04 Nov 16 olle 1091 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " row = " + row + " col = " + col + " wellStr = " + wellStr);
4206 04 Nov 16 olle 1092                 if (unusedFpaWellsMatrix[row][col])
4206 04 Nov 16 olle 1093                 {
4206 04 Nov 16 olle 1094                   jsonUnusedFpaWells.add(wellStr);
4206 04 Nov 16 olle 1095                 }
4206 04 Nov 16 olle 1096                 if (effUnusedFpaWellsMatrix[row][col])
4206 04 Nov 16 olle 1097                 {
4206 04 Nov 16 olle 1098                   jsonEffUnusedFpaWells.add(wellStr);
4206 04 Nov 16 olle 1099                 }
4206 04 Nov 16 olle 1100                 if (reservedFpaWellsMatrix[row][col])
4206 04 Nov 16 olle 1101                 {
4206 04 Nov 16 olle 1102                   jsonReservedFpaWells.add(wellStr);
4206 04 Nov 16 olle 1103                 }
4206 04 Nov 16 olle 1104               }
4206 04 Nov 16 olle 1105             }
4206 04 Nov 16 olle 1106
4206 04 Nov 16 olle 1107             // Create new dummy plate representing kit
4206 04 Nov 16 olle 1108             PlateGeometry geometry = BioplateType.DNA.getPlateGeometry(dc);
4206 04 Nov 16 olle 1109             BioPlateType plateType = BioplateType.DNA.load(dc);
4206 04 Nov 16 olle 1110
4206 04 Nov 16 olle 1111             ReactionPlate kitPlate = ReactionPlate.getNew(dc, BioplateType.DNA);
4206 04 Nov 16 olle 1112           
4206 04 Nov 16 olle 1113             BioPlate bioPlate = kitPlate.getBioPlate();
4206 04 Nov 16 olle 1114 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" bioPlate = " + bioPlate);
4206 04 Nov 16 olle 1115             int rows = bioPlate.getRows();
4206 04 Nov 16 olle 1116             int cols = bioPlate.getColumns();
4206 04 Nov 16 olle 1117 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" rows = " + rows + " cols = " + cols);
4206 04 Nov 16 olle 1118
4206 04 Nov 16 olle 1119             JSONArray jsonWells = new JSONArray();
4206 04 Nov 16 olle 1120             for (int r = 0; r < rows; ++r)
4206 04 Nov 16 olle 1121             {
4206 04 Nov 16 olle 1122               for (int c = 0; c < cols; ++c)
4206 04 Nov 16 olle 1123               {
4206 04 Nov 16 olle 1124                 BioWell well = bioPlate.getBioWell(r, c);
4206 04 Nov 16 olle 1125 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" r = " + r + " c = " + c + " well = " + well);
4206 04 Nov 16 olle 1126                 JSONObject jsonWell = new JSONObject();
4206 04 Nov 16 olle 1127                 //jsonWell.put("id", well.getId());
4206 04 Nov 16 olle 1128                 jsonWell.put("row", well.getRow());
4206 04 Nov 16 olle 1129                 jsonWell.put("column", well.getColumn());
4206 04 Nov 16 olle 1130 /*            
4206 04 Nov 16 olle 1131                 if (loadBioMaterial)
4206 04 Nov 16 olle 1132                 {
4206 04 Nov 16 olle 1133                   MeasuredBioMaterial bm = well.getBioMaterial();
4206 04 Nov 16 olle 1134                   if (bm != null)
4206 04 Nov 16 olle 1135                   {
4206 04 Nov 16 olle 1136                     JSONObject jsonBm = new JSONObject();
4206 04 Nov 16 olle 1137                     jsonBm.put("id", bm.getId());
4206 04 Nov 16 olle 1138                     jsonBm.put("name", bm.getName());
4206 04 Nov 16 olle 1139                     jsonBm.put("comment", bm.getDescription());
4206 04 Nov 16 olle 1140                     jsonWell.put("bioMaterial", jsonBm);
4206 04 Nov 16 olle 1141                   }
4206 04 Nov 16 olle 1142                 }
4206 04 Nov 16 olle 1143 */          
4206 04 Nov 16 olle 1144                 jsonWells.add(jsonWell);
4206 04 Nov 16 olle 1145               }
4206 04 Nov 16 olle 1146             }
4206 04 Nov 16 olle 1147             if (numReservedWells >= minUnusedFpaPlateLocs)
4206 04 Nov 16 olle 1148             {
4206 04 Nov 16 olle 1149               jsonPlate = kitPlate.asJSONObject();
4206 04 Nov 16 olle 1150               // Add data for wells
4206 04 Nov 16 olle 1151               jsonPlate.put("bioWells", jsonWells);
4206 04 Nov 16 olle 1152               // Add extra kit data
4206 04 Nov 16 olle 1153               jsonPlate.put("kitId", (Integer) jsonKitId.get(i));
4206 04 Nov 16 olle 1154               jsonPlate.put("kitName", (String) jsonKitName.get(i));
4206 04 Nov 16 olle 1155               jsonPlate.put("expirationDate", (String) jsonExpirationDate.get(i));
4206 04 Nov 16 olle 1156               jsonPlate.put("timesUsed", jsonTimesUsed.get(i));
4206 04 Nov 16 olle 1157               jsonPlate.put("numEffUnusedWells", numEffUnusedWells);
4206 04 Nov 16 olle 1158               jsonPlate.put("unusedWells", jsonUnusedFpaWells);
4206 04 Nov 16 olle 1159               jsonPlate.put("effUnusedWells", jsonEffUnusedFpaWells);
4206 04 Nov 16 olle 1160               jsonPlate.put("reservedWells", jsonReservedFpaWells);
4206 04 Nov 16 olle 1161               jsonPlate.put("numEffUnusedWellsInColumn", jsonNumEffUnusedFpaWellsInColumn);
4206 04 Nov 16 olle 1162 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" jsonPlate = " + jsonPlate);
4206 04 Nov 16 olle 1163 //System.out.println(new Date() + " LibPrepServlet::doGet(): cmd = \"" + cmd + "\" kitName = " + jsonKitName.get(i) + " jsonReservedFpaWells = " + jsonReservedFpaWells);
4206 04 Nov 16 olle 1164               //
4206 04 Nov 16 olle 1165             }
4206 04 Nov 16 olle 1166           }
4206 04 Nov 16 olle 1167         }
4206 04 Nov 16 olle 1168         json.put("index", index);
4206 04 Nov 16 olle 1169         json.put("plate", jsonPlate);
4206 04 Nov 16 olle 1170       }
3676 18 Dec 15 olle 1171       else if ("GetNextAutoGeneratedKitName".equals(cmd))
3676 18 Dec 15 olle 1172       {
3676 18 Dec 15 olle 1173         /*
3676 18 Dec 15 olle 1174            Eg. LP_kit_001, LP_kit_002 and so on
3676 18 Dec 15 olle 1175          */
3676 18 Dec 15 olle 1176         // Find all kits of the given 'kit subtype'
4190 28 Oct 16 olle 1177         String kitNamePrefix = Meludi.fetchLibraryPreparationKitItemPrefix(sc.getActiveProjectId());
4230 10 Nov 16 olle 1178         Integer kitNameNumDigits = Meludi.fetchLibraryPreparationKitItemNumDigits(sc.getActiveProjectId());
4230 10 Nov 16 olle 1179         int numDigitsInName = MeludiConfiguration.LIBPREP_KIT_ITEM_NUM_DIGITS_DEFAULT;
4230 10 Nov 16 olle 1180         if (kitNameNumDigits != null)
3676 18 Dec 15 olle 1181         {
4230 10 Nov 16 olle 1182           numDigitsInName = kitNameNumDigits;
3676 18 Dec 15 olle 1183         }
3676 18 Dec 15 olle 1184
3676 18 Dec 15 olle 1185         dc = sc.newDbControl();
3676 18 Dec 15 olle 1186         ItemQuery<Kit> query = Kit.getQuery();
3676 18 Dec 15 olle 1187         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3676 18 Dec 15 olle 1188         Subtype.LIBRARY_PREPARATION_KIT.addFilter(dc, query);
3676 18 Dec 15 olle 1189         query.restrict(Restrictions.rlike(Hql.property("name"), Expressions.string("^" + kitNamePrefix + "[0-9]+$")));
3676 18 Dec 15 olle 1190         query.order(Orders.desc(Hql.property("name")));
3676 18 Dec 15 olle 1191         query.setMaxResults(1);
3676 18 Dec 15 olle 1192         List<Kit> kits = query.list(dc);
3676 18 Dec 15 olle 1193
3676 18 Dec 15 olle 1194         int startIndex = 1;
3676 18 Dec 15 olle 1195         if (kits.size() > 0)
3676 18 Dec 15 olle 1196         {
3676 18 Dec 15 olle 1197           String kitName = kits.get(0).getName().substring(kitNamePrefix.length());
3676 18 Dec 15 olle 1198           startIndex = Integer.parseInt(kitName) + 1;
3676 18 Dec 15 olle 1199         }
3676 18 Dec 15 olle 1200         String kitName = kitNamePrefix + MD5.leftPad(Integer.toString(startIndex), '0', numDigitsInName);
3676 18 Dec 15 olle 1201
3676 18 Dec 15 olle 1202         json.put("name", kitName);
5272 30 Jan 19 olle 1203         json.put("kitNamePrefix", kitNamePrefix);
5272 30 Jan 19 olle 1204         json.put("numDigitsInName", numDigitsInName);
3676 18 Dec 15 olle 1205       }
3449 28 Jul 15 olle 1206 /*
3449 28 Jul 15 olle 1207       else if ("GetConfigurationsForCaliperRunParametersExporter".equals(cmd))
3449 28 Jul 15 olle 1208       {
3449 28 Jul 15 olle 1209 */
3449 28 Jul 15 olle 1210         /*
3449 28 Jul 15 olle 1211           Get all configurations for the CaliperRunParametersExporter plugin
3449 28 Jul 15 olle 1212         */
3449 28 Jul 15 olle 1213 /*
3449 28 Jul 15 olle 1214         dc = sc.newDbControl();
3449 28 Jul 15 olle 1215         PluginDefinition plugin = PluginDefinition.getByClassName(dc, CaliperLibPrepParametersExporter.class.getName());
3449 28 Jul 15 olle 1216         ItemQuery<PluginConfiguration> query = plugin.getPluginConfigurations();
3449 28 Jul 15 olle 1217         query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3449 28 Jul 15 olle 1218         query.setItemPermission(Permission.USE);
3449 28 Jul 15 olle 1219         query.order(Orders.asc(Hql.property("name")));
3449 28 Jul 15 olle 1220         
3449 28 Jul 15 olle 1221         JSONArray jsonConfigurations = new JSONArray();
3449 28 Jul 15 olle 1222         for (PluginConfiguration p : query.list(dc))
3449 28 Jul 15 olle 1223         {
3449 28 Jul 15 olle 1224           JSONObject jsonConfiguration = new JSONObject();
3449 28 Jul 15 olle 1225           jsonConfiguration.put("id", p.getId());
3449 28 Jul 15 olle 1226           jsonConfiguration.put("name", p.getName());
3449 28 Jul 15 olle 1227           jsonConfigurations.add(jsonConfiguration);
3449 28 Jul 15 olle 1228         }
3449 28 Jul 15 olle 1229         
3449 28 Jul 15 olle 1230         json.put("configurations", jsonConfigurations);
3449 28 Jul 15 olle 1231       }
3449 28 Jul 15 olle 1232 */
3449 28 Jul 15 olle 1233 /*
3449 28 Jul 15 olle 1234       else if ("ExportSampleNamesForCaliper".equals(cmd))
3449 28 Jul 15 olle 1235       {
3449 28 Jul 15 olle 1236 */
3449 28 Jul 15 olle 1237         /*
3449 28 Jul 15 olle 1238           Generate CSV file with sample names for the Caliper software
3449 28 Jul 15 olle 1239         */
3449 28 Jul 15 olle 1240 /*
3449 28 Jul 15 olle 1241         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
3449 28 Jul 15 olle 1242         boolean remapTo384 = Values.getBoolean(req.getParameter("remapTo384"));
3449 28 Jul 15 olle 1243         dc = sc.newDbControl();
3449 28 Jul 15 olle 1244         BioPlate plate = BioPlate.getById(dc, bioPlateId);
3449 28 Jul 15 olle 1245         
3449 28 Jul 15 olle 1246         ValueConverter<BioWell, String> wellMapper = remapTo384 ? 
3449 28 Jul 15 olle 1247             new WellCoordinateRemap96To384() : new WellCoordinateConverter();
3449 28 Jul 15 olle 1248             
3449 28 Jul 15 olle 1249         resp.setHeader("Content-Disposition", "attachment; filename=" + Path.makeSafeFilename(plate.getName(), "") + ".csv");
3449 28 Jul 15 olle 1250         resp.setContentType("text/csv");
3449 28 Jul 15 olle 1251         resp.setCharacterEncoding("UTF-8");
3449 28 Jul 15 olle 1252         Writer out = resp.getWriter();
3449 28 Jul 15 olle 1253         CaliperSampleNameExporter exporter = new CaliperSampleNameExporter();
3449 28 Jul 15 olle 1254         exporter.exportSampleNames(dc, plate, wellMapper, out);
3449 28 Jul 15 olle 1255   
3449 28 Jul 15 olle 1256         out.flush();
3449 28 Jul 15 olle 1257         out.close();
3449 28 Jul 15 olle 1258       }
3449 28 Jul 15 olle 1259 */
3449 28 Jul 15 olle 1260 /*
3449 28 Jul 15 olle 1261       else if ("ExportSampleNamesForQubit".equals(cmd))
3449 28 Jul 15 olle 1262       {
3449 28 Jul 15 olle 1263 */
3449 28 Jul 15 olle 1264         /*
3449 28 Jul 15 olle 1265           Generate tab-separated file with sample names and locations
3449 28 Jul 15 olle 1266         */
3449 28 Jul 15 olle 1267 /*
3449 28 Jul 15 olle 1268         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
3449 28 Jul 15 olle 1269         dc = sc.newDbControl();
3449 28 Jul 15 olle 1270         BioPlate plate = BioPlate.getById(dc, bioPlateId);
3449 28 Jul 15 olle 1271         
3449 28 Jul 15 olle 1272         resp.setHeader("Content-Disposition", "attachment; filename=" + Path.makeSafeFilename(plate.getName(), "") + "-qubit.csv");
3449 28 Jul 15 olle 1273         resp.setContentType("text/csv");
3449 28 Jul 15 olle 1274         resp.setCharacterEncoding("UTF-8");
3449 28 Jul 15 olle 1275         Writer out = resp.getWriter();
3449 28 Jul 15 olle 1276         QubitSampleNameExporter exporter = new QubitSampleNameExporter();
3449 28 Jul 15 olle 1277         exporter.exportSampleNames(dc, plate, out);
3449 28 Jul 15 olle 1278   
3449 28 Jul 15 olle 1279         out.flush();
3449 28 Jul 15 olle 1280         out.close();
3449 28 Jul 15 olle 1281       }
3449 28 Jul 15 olle 1282 */
3449 28 Jul 15 olle 1283 /*
3449 28 Jul 15 olle 1284       else if ("ExportCaliperRunParameters".equals(cmd))
3449 28 Jul 15 olle 1285       {
3449 28 Jul 15 olle 1286 */
3449 28 Jul 15 olle 1287         /*
3449 28 Jul 15 olle 1288           Generate run parameter XML file for the Caliper software
3449 28 Jul 15 olle 1289         */
3449 28 Jul 15 olle 1290 /*
3449 28 Jul 15 olle 1291         int bioPlateId = Values.getInt(req.getParameter("bioPlateId"));
3449 28 Jul 15 olle 1292         int configurationId = Values.getInt(req.getParameter("configurationId"));
3449 28 Jul 15 olle 1293         
3449 28 Jul 15 olle 1294         dc = sc.newDbControl();
3449 28 Jul 15 olle 1295         BioPlate plate = BioPlate.getById(dc, bioPlateId);
3449 28 Jul 15 olle 1296         PluginConfiguration config = PluginConfiguration.getById(dc, configurationId);
3449 28 Jul 15 olle 1297         ParameterValues parameters = config.getParameterValues(config.getParameterVersion());
3449 28 Jul 15 olle 1298         boolean remapTo384 = Boolean.TRUE.equals(parameters.getValue("RemapTo384"));
3449 28 Jul 15 olle 1299         ValueConverter<BioWell, String> wellMapper = remapTo384 ? 
3449 28 Jul 15 olle 1300             new WellCoordinateRemap96To384() : new WellCoordinateConverter();        
3449 28 Jul 15 olle 1301         resp.setHeader("Content-Disposition", "attachment; filename=" + Path.makeSafeFilename(plate.getName(), "") + ".xml");
3449 28 Jul 15 olle 1302         resp.setContentType("application/xml");
3449 28 Jul 15 olle 1303         resp.setCharacterEncoding("UTF-8");
3449 28 Jul 15 olle 1304         Writer out = resp.getWriter();
3449 28 Jul 15 olle 1305         
3449 28 Jul 15 olle 1306         CaliperLibPrepParametersExporter exporter = new CaliperLibPrepParametersExporter();
3449 28 Jul 15 olle 1307         exporter.exportRunParameters(dc, plate, wellMapper, parameters, out);
3449 28 Jul 15 olle 1308         
3449 28 Jul 15 olle 1309         out.flush();
3449 28 Jul 15 olle 1310         out.close();
3449 28 Jul 15 olle 1311       }
3449 28 Jul 15 olle 1312 */
3449 28 Jul 15 olle 1313     }
3449 28 Jul 15 olle 1314     catch (Throwable t)
3449 28 Jul 15 olle 1315     {
3449 28 Jul 15 olle 1316       t.printStackTrace();
3449 28 Jul 15 olle 1317       json.clear();
3449 28 Jul 15 olle 1318       json.put("status", "error");
3449 28 Jul 15 olle 1319       json.put("message", t.getMessage());
3449 28 Jul 15 olle 1320       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3449 28 Jul 15 olle 1321     }
3449 28 Jul 15 olle 1322     finally
3449 28 Jul 15 olle 1323     {
3449 28 Jul 15 olle 1324       if (dc != null) dc.close();
3449 28 Jul 15 olle 1325       json.writeJSONString(resp.getWriter());
3449 28 Jul 15 olle 1326     }
3449 28 Jul 15 olle 1327     
3449 28 Jul 15 olle 1328   }
3449 28 Jul 15 olle 1329
3449 28 Jul 15 olle 1330   @SuppressWarnings("unchecked")
3610 20 Nov 15 olle 1331   private JSONObject fetchJsonKitDataForUpdate(DbControl dc, String libPrepKitName, List<String> reservedWellsList)
3610 20 Nov 15 olle 1332   {    
3573 05 Nov 15 olle 1333     String libPrepKitNameFilter = libPrepKitName;
3573 05 Nov 15 olle 1334     if (libPrepKitNameFilter != null && libPrepKitNameFilter.equals(""))
3573 05 Nov 15 olle 1335     {
3573 05 Nov 15 olle 1336       libPrepKitNameFilter = null;
3573 05 Nov 15 olle 1337     }
3610 20 Nov 15 olle 1338     Boolean activeFilter = null;
3573 05 Nov 15 olle 1339     Boolean usedFilter = null;
3573 05 Nov 15 olle 1340 //System.out.println(new Date() + " LibPrepServlet::fetchJsonKitDataForUpdate(): libPrepKitNameFilter = " + libPrepKitNameFilter);
3573 05 Nov 15 olle 1341
3573 05 Nov 15 olle 1342     Integer minUnusedFpaPlateLocs = null;
3610 20 Nov 15 olle 1343     JSONObject jsonConsumables = fetchJsonKitData(dc, libPrepKitNameFilter, activeFilter, usedFilter, minUnusedFpaPlateLocs);
3573 05 Nov 15 olle 1344
3573 05 Nov 15 olle 1345     JSONArray jsonLastUpdatedDate = (JSONArray) jsonConsumables.get("LastUpdatedDate");
3610 20 Nov 15 olle 1346     JSONArray jsonActive = (JSONArray) jsonConsumables.get("Active");
3573 05 Nov 15 olle 1347     JSONArray jsonTimesUsed = (JSONArray) jsonConsumables.get("TimesUsed");
3573 05 Nov 15 olle 1348     JSONArray jsonUnusedFpaPlateLocs = (JSONArray) jsonConsumables.get("UnusedFpaPlateLocs");
3573 05 Nov 15 olle 1349     int numKits = jsonLastUpdatedDate.size();
3573 05 Nov 15 olle 1350     if (numKits > 1)
3573 05 Nov 15 olle 1351     {
3573 05 Nov 15 olle 1352       System.out.println(new Date() + " LibPrepServlet::fetchJsonKitDataForUpdate(): Number of found kits for kit name \"" + libPrepKitName + "\" = " + numKits + " > 1");
3573 05 Nov 15 olle 1353       return null;
3573 05 Nov 15 olle 1354     }
3573 05 Nov 15 olle 1355     // Get current values of selected variables
3626 26 Nov 15 olle 1356     Boolean active = (Boolean) jsonActive.get(0);
3626 26 Nov 15 olle 1357     int timesUsed = (Integer) jsonTimesUsed.get(0);
3573 05 Nov 15 olle 1358     String unusedFpaPlateLocs = (String) jsonUnusedFpaPlateLocs.get(0);
3626 26 Nov 15 olle 1359 //System.out.println(new Date() + " LibPrepServlet::fetchJsonKitDataForUpdate(): libPrepKitNameFilter = " + libPrepKitNameFilter + " active = " + active + " timesUsed = " + timesUsed + " unusedFpaPlateLocs = \"" + unusedFpaPlateLocs + "\"");
3573 05 Nov 15 olle 1360     // Update kit data
3573 05 Nov 15 olle 1361     // Update jsonTimesUsed
3573 05 Nov 15 olle 1362     timesUsed++;
3573 05 Nov 15 olle 1363     jsonTimesUsed.set(0, "" + timesUsed);
3610 20 Nov 15 olle 1364     // Update jsonActive if necessary
3573 05 Nov 15 olle 1365     if (timesUsed >= 6)
3573 05 Nov 15 olle 1366     {
3626 26 Nov 15 olle 1367       active = false;
3626 26 Nov 15 olle 1368       jsonActive.set(0, active);
3573 05 Nov 15 olle 1369     }
3573 05 Nov 15 olle 1370     // Update jsonLastUpdatedDate
3573 05 Nov 15 olle 1371     DateToStringConverter d1 = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd"));
3573 05 Nov 15 olle 1372     Date now = new Date();
3573 05 Nov 15 olle 1373     String lastUpdatedDate = d1.convert(now);
3573 05 Nov 15 olle 1374     jsonLastUpdatedDate.set(0, lastUpdatedDate);
3573 05 Nov 15 olle 1375     // Update jsonUnusedFpaPlateLocs
3573 05 Nov 15 olle 1376     String wellSeparator = ",";
3573 05 Nov 15 olle 1377     String[] unusedFpaPlateWellsArr = unusedFpaPlateLocs.split(wellSeparator);
3573 05 Nov 15 olle 1378     String unusedFpaPlateLocsUpdated = "";
3573 05 Nov 15 olle 1379     for (int i = 0; i < unusedFpaPlateWellsArr.length; i++)
3573 05 Nov 15 olle 1380     {
3573 05 Nov 15 olle 1381       String wellStr = unusedFpaPlateWellsArr[i];
3573 05 Nov 15 olle 1382       // Check if well is reserved for used
3573 05 Nov 15 olle 1383       boolean wellIsReserved = reservedWellsList.contains(wellStr);
3573 05 Nov 15 olle 1384       // Add non-reserved unused wells to comma-separated list of unused wells
3573 05 Nov 15 olle 1385       if (!wellIsReserved)
3573 05 Nov 15 olle 1386       {
3573 05 Nov 15 olle 1387         if (unusedFpaPlateLocsUpdated.length() > 0)
3573 05 Nov 15 olle 1388         {
3573 05 Nov 15 olle 1389           unusedFpaPlateLocsUpdated += wellSeparator;
3573 05 Nov 15 olle 1390         }
3573 05 Nov 15 olle 1391         unusedFpaPlateLocsUpdated += wellStr;
3573 05 Nov 15 olle 1392       }
3573 05 Nov 15 olle 1393     }
3573 05 Nov 15 olle 1394     jsonUnusedFpaPlateLocs.set(0, unusedFpaPlateLocsUpdated);
3610 20 Nov 15 olle 1395 //System.out.println(new Date() + " LibPrepServlet::fetchJsonKitDataForUpdate(): libPrepKitNameFilter = " + libPrepKitNameFilter + " activeStr = " + activeStr + " timesUsed = " + timesUsed + " unusedFpaPlateLocs = \"" + unusedFpaPlateLocs + "\" unusedFpaPlateLocsUpdated = \"" + unusedFpaPlateLocsUpdated + "\"");
3573 05 Nov 15 olle 1396     // Update jsonConsumables
3573 05 Nov 15 olle 1397     jsonConsumables.put("LastUpdatedDate", jsonLastUpdatedDate);
3610 20 Nov 15 olle 1398     jsonConsumables.put("Active", jsonActive);
3573 05 Nov 15 olle 1399     jsonConsumables.put("TimesUsed", jsonTimesUsed);
3573 05 Nov 15 olle 1400     jsonConsumables.put("UnusedFpaPlateLocs", jsonUnusedFpaPlateLocs);
3573 05 Nov 15 olle 1401
3573 05 Nov 15 olle 1402     return jsonConsumables;
3573 05 Nov 15 olle 1403   }
3573 05 Nov 15 olle 1404
3573 05 Nov 15 olle 1405   @SuppressWarnings("unchecked")
3610 20 Nov 15 olle 1406   private JSONObject fetchJsonKitData(DbControl dc, String libPrepKitNameFilter, Boolean activeFilter, Boolean usedFilter, Integer minUnusedFpaPlateLocs)
3573 05 Nov 15 olle 1407   {
3610 20 Nov 15 olle 1408 //System.out.println(new Date() + " LibPrepServlet::fetchJsonKitData(): libPrepKitNameFilter = " + libPrepKitNameFilter + " activeFilter = " + activeFilter + " usedFilter = " + usedFilter + " minUnusedFpaPlateLocs = " + minUnusedFpaPlateLocs);
3621 25 Nov 15 olle 1409     JSONArray jsonKitId = new JSONArray();
3573 05 Nov 15 olle 1410     JSONArray jsonKitName = new JSONArray();
3573 05 Nov 15 olle 1411     JSONArray jsonLibraryPrepBox1 = new JSONArray();
3573 05 Nov 15 olle 1412     JSONArray jsonIndexKit = new JSONArray();
3573 05 Nov 15 olle 1413     JSONArray jsonContentSetBox3 = new JSONArray();
3573 05 Nov 15 olle 1414     JSONArray jsonOligoPoolA_FPA = new JSONArray();
3573 05 Nov 15 olle 1415     JSONArray jsonOligoPoolB_FPB = new JSONArray();
3573 05 Nov 15 olle 1416     JSONArray jsonOligoHybridizationSequencingReagent3 = new JSONArray();
3573 05 Nov 15 olle 1417     JSONArray jsonStringentWash1_SW1 = new JSONArray();
3573 05 Nov 15 olle 1418     JSONArray jsonExtensionLigationMix3 = new JSONArray();
3573 05 Nov 15 olle 1419     JSONArray jsonPcrMasterMix2_PMM2 = new JSONArray();
3573 05 Nov 15 olle 1420     JSONArray jsonUB1 = new JSONArray();
3573 05 Nov 15 olle 1421     JSONArray jsonTDP = new JSONArray();
3573 05 Nov 15 olle 1422     JSONArray jsonFilterplate = new JSONArray();
3573 05 Nov 15 olle 1423     
3573 05 Nov 15 olle 1424     JSONArray jsonIndexPrimerA701 = new JSONArray();
3573 05 Nov 15 olle 1425     JSONArray jsonIndexPrimerA702 = new JSONArray();
3573 05 Nov 15 olle 1426     JSONArray jsonIndexPrimerA703 = new JSONArray();
3573 05 Nov 15 olle 1427     JSONArray jsonIndexPrimerA704 = new JSONArray();
3573 05 Nov 15 olle 1428     JSONArray jsonIndexPrimerA705 = new JSONArray();
3573 05 Nov 15 olle 1429     JSONArray jsonIndexPrimerA706 = new JSONArray();
3573 05 Nov 15 olle 1430     JSONArray jsonIndexPrimerA707 = new JSONArray();
3573 05 Nov 15 olle 1431     JSONArray jsonIndexPrimerA708 = new JSONArray();
3573 05 Nov 15 olle 1432     JSONArray jsonIndexPrimerA709 = new JSONArray();
3573 05 Nov 15 olle 1433     JSONArray jsonIndexPrimerA710 = new JSONArray();
3573 05 Nov 15 olle 1434     JSONArray jsonIndexPrimerA711 = new JSONArray();
3573 05 Nov 15 olle 1435     JSONArray jsonIndexPrimerA712 = new JSONArray();
3573 05 Nov 15 olle 1436     
3573 05 Nov 15 olle 1437     JSONArray jsonIndexPrimerA501 = new JSONArray();
3573 05 Nov 15 olle 1438     JSONArray jsonIndexPrimerA502 = new JSONArray();
3573 05 Nov 15 olle 1439     JSONArray jsonIndexPrimerA503 = new JSONArray();
3573 05 Nov 15 olle 1440     JSONArray jsonIndexPrimerA504 = new JSONArray();
3573 05 Nov 15 olle 1441     JSONArray jsonIndexPrimerA505 = new JSONArray();
3573 05 Nov 15 olle 1442     JSONArray jsonIndexPrimerA506 = new JSONArray();
3573 05 Nov 15 olle 1443     JSONArray jsonIndexPrimerA507 = new JSONArray();
3573 05 Nov 15 olle 1444     JSONArray jsonIndexPrimerA508 = new JSONArray();
3573 05 Nov 15 olle 1445
3573 05 Nov 15 olle 1446     JSONArray jsonRegistrationDate = new JSONArray();
3573 05 Nov 15 olle 1447     JSONArray jsonLastUpdatedDate = new JSONArray();
3573 05 Nov 15 olle 1448     JSONArray jsonExpirationDate = new JSONArray();
3610 20 Nov 15 olle 1449     JSONArray jsonActive = new JSONArray();
3573 05 Nov 15 olle 1450     JSONArray jsonTimesUsed = new JSONArray();
3573 05 Nov 15 olle 1451     JSONArray jsonUnusedFpaPlateLocs = new JSONArray();
3573 05 Nov 15 olle 1452     JSONArray jsonComment = new JSONArray();
3573 05 Nov 15 olle 1453
3610 20 Nov 15 olle 1454     List<String> reagentsDataLineList = new ArrayList<String>();
3610 20 Nov 15 olle 1455     List<String> kitNameList = new ArrayList<String>();
3610 20 Nov 15 olle 1456
3610 20 Nov 15 olle 1457     JSONArray jsonHeaders = fetchKitJsonHeaders();
3610 20 Nov 15 olle 1458
3610 20 Nov 15 olle 1459     int numItemsTot = 0;
3610 20 Nov 15 olle 1460     int numItemsFiltered = 0;
3610 20 Nov 15 olle 1461
3610 20 Nov 15 olle 1462     // Find all kits of the given 'kit subtype'
3610 20 Nov 15 olle 1463     ItemQuery<Kit> query = Kit.getQuery();
3610 20 Nov 15 olle 1464     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3610 20 Nov 15 olle 1465     Subtype.LIBRARY_PREPARATION_KIT.addFilter(dc, query);
3610 20 Nov 15 olle 1466     numItemsTot = query.list(dc).size();
3610 20 Nov 15 olle 1467
3610 20 Nov 15 olle 1468     // Find kits of the given 'kit subtype'
3610 20 Nov 15 olle 1469     query = Kit.getQuery();
3610 20 Nov 15 olle 1470     query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3610 20 Nov 15 olle 1471     Subtype.LIBRARY_PREPARATION_KIT.addFilter(dc, query);
3610 20 Nov 15 olle 1472     if (libPrepKitNameFilter != null && !libPrepKitNameFilter.equals(""))
3573 05 Nov 15 olle 1473     {
3610 20 Nov 15 olle 1474       query.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(libPrepKitNameFilter)));
3610 20 Nov 15 olle 1475     }
3610 20 Nov 15 olle 1476     if (activeFilter != null)
3610 20 Nov 15 olle 1477     {
3610 20 Nov 15 olle 1478       query.restrict(Restrictions.neq(Hql.property("inactive"), Expressions.parameter("inactive", activeFilter, Type.BOOLEAN)));
3610 20 Nov 15 olle 1479     }
3610 20 Nov 15 olle 1480     if (usedFilter != null)
3610 20 Nov 15 olle 1481     {
3610 20 Nov 15 olle 1482       // Join LibPrepKitNumberTimesUsed annotation
3610 20 Nov 15 olle 1483       query.join(Annotations.leftJoin(null, Annotationtype.LIBPREPKIT_NUMBER_TIMES_USED.load(dc), "ntu"));
3610 20 Nov 15 olle 1484       if (usedFilter)
3573 05 Nov 15 olle 1485       {
3610 20 Nov 15 olle 1486         // #Times used > 0
3610 20 Nov 15 olle 1487         query.restrict(Restrictions.gt(Hql.alias("ntu"), Expressions.integer(0)));        
3610 20 Nov 15 olle 1488       }
3610 20 Nov 15 olle 1489       else
3610 20 Nov 15 olle 1490       {
3610 20 Nov 15 olle 1491         // #Times used == 0
3610 20 Nov 15 olle 1492         query.restrict(Restrictions.eq(Hql.alias("ntu"), Expressions.integer(0)));        
3610 20 Nov 15 olle 1493       }
3610 20 Nov 15 olle 1494     }
3610 20 Nov 15 olle 1495     query.order(Orders.asc(Hql.property("name")));
3610 20 Nov 15 olle 1496
3610 20 Nov 15 olle 1497     // Filter on minimum number of unused FPA wells
3610 20 Nov 15 olle 1498     List<Kit> kitsRaw = query.list(dc);
3610 20 Nov 15 olle 1499     List<Kit> kits = new ArrayList<Kit>();
3610 20 Nov 15 olle 1500     if (kitsRaw != null && kitsRaw.size() > 0)
3610 20 Nov 15 olle 1501     {
3610 20 Nov 15 olle 1502       for (int i = 0; i < kitsRaw.size(); i++)
3610 20 Nov 15 olle 1503       {
3610 20 Nov 15 olle 1504         Kit kit = (Kit) kitsRaw.get(i);
3610 20 Nov 15 olle 1505         // Get number of unused FPA plate locations for kit
3610 20 Nov 15 olle 1506         String unusedFpaPlateLocsStr = (String)Annotationtype.LIBPREPKIT_UNUSED_FPA_WELLS.getAnnotationValue(dc, kit);
3610 20 Nov 15 olle 1507         String[] unusedWells = unusedFpaPlateLocsStr.split(",", -1);
3610 20 Nov 15 olle 1508         int numUnusedWells = unusedWells.length;
3610 20 Nov 15 olle 1509         // Check kit filters
3610 20 Nov 15 olle 1510         boolean passed = true;
3610 20 Nov 15 olle 1511         if (minUnusedFpaPlateLocs != null)
3573 05 Nov 15 olle 1512         {
3610 20 Nov 15 olle 1513           if (numUnusedWells < minUnusedFpaPlateLocs)
3610 20 Nov 15 olle 1514           {
3610 20 Nov 15 olle 1515             passed = false;
3610 20 Nov 15 olle 1516           }
3610 20 Nov 15 olle 1517         }
3610 20 Nov 15 olle 1518         if (passed)
3610 20 Nov 15 olle 1519         {
3610 20 Nov 15 olle 1520           kitNameList.add(kit.getName());
3610 20 Nov 15 olle 1521           kits.add(kit);
3610 20 Nov 15 olle 1522         }
3610 20 Nov 15 olle 1523       }
3610 20 Nov 15 olle 1524     }
3573 05 Nov 15 olle 1525
3610 20 Nov 15 olle 1526     numItemsFiltered = kits.size();
3610 20 Nov 15 olle 1527 //System.out.println(new Date() + " LibPrepServlet::fetchJsonKitData(): libPrepKitNameFilter = " + libPrepKitNameFilter + " activeFilter = " + activeFilter + " usedFilter = " + usedFilter + " minUnusedFpaPlateLocs = " + minUnusedFpaPlateLocs + " numItemsFiltered = " + numItemsFiltered);
3610 20 Nov 15 olle 1528     DateToStringConverter d = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd"));
3573 05 Nov 15 olle 1529
3610 20 Nov 15 olle 1530     if (kits != null && kits.size() > 0)
3610 20 Nov 15 olle 1531     {
3610 20 Nov 15 olle 1532       for (int i = 0; i < kits.size(); i++)
3610 20 Nov 15 olle 1533       {
3610 20 Nov 15 olle 1534         Kit kit = (Kit) kits.get(i);
3610 20 Nov 15 olle 1535         
3621 25 Nov 15 olle 1536         jsonKitId.add(kit.getId());
3610 20 Nov 15 olle 1537         jsonKitName.add(kit.getName());
3610 20 Nov 15 olle 1538         jsonLibraryPrepBox1.add(Annotationtype.LIBPREPKIT_LIB_PREP_BOX1.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1539         jsonIndexKit.add(Annotationtype.LIBPREPKIT_LIB_INDEX_KIT.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1540         jsonContentSetBox3.add(Annotationtype.LIBPREPKIT_CONTENT_SET_BOX3.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1541         jsonOligoPoolA_FPA.add(Annotationtype.LIBPREPKIT_OLIGO_POOL_A_FPA.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1542         jsonOligoPoolB_FPB.add(Annotationtype.LIBPREPKIT_OLIGO_POOL_B_FPB.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1543         jsonOligoHybridizationSequencingReagent3.add(Annotationtype.LIBPREPKIT_OLIGO_HYBR_SEQ_REAGENT3_OHS3.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1544         jsonStringentWash1_SW1.add(Annotationtype.LIBPREPKIT_STRINGENT_WASH1_SW1.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1545         jsonExtensionLigationMix3.add(Annotationtype.LIBPREPKIT_EXTENSION_LIGATION_MIX3_ELM3.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1546         jsonPcrMasterMix2_PMM2.add(Annotationtype.LIBPREPKIT_PCR_MASTER_MIX2_PMM2.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1547         jsonUB1.add(Annotationtype.LIBPREPKIT_UB1.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1548         jsonTDP.add(Annotationtype.LIBPREPKIT_TDP.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1549         jsonFilterplate.add(Annotationtype.LIBPREPKIT_FILTER_PLATE.getAnnotationValue(dc, kit));
3573 05 Nov 15 olle 1550
3610 20 Nov 15 olle 1551         jsonIndexPrimerA701.add(Annotationtype.LIBPREPKIT_A701.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1552         jsonIndexPrimerA702.add(Annotationtype.LIBPREPKIT_A702.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1553         jsonIndexPrimerA703.add(Annotationtype.LIBPREPKIT_A703.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1554         jsonIndexPrimerA704.add(Annotationtype.LIBPREPKIT_A704.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1555         jsonIndexPrimerA705.add(Annotationtype.LIBPREPKIT_A705.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1556         jsonIndexPrimerA706.add(Annotationtype.LIBPREPKIT_A706.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1557         jsonIndexPrimerA707.add(Annotationtype.LIBPREPKIT_A707.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1558         jsonIndexPrimerA708.add(Annotationtype.LIBPREPKIT_A708.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1559         jsonIndexPrimerA709.add(Annotationtype.LIBPREPKIT_A709.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1560         jsonIndexPrimerA710.add(Annotationtype.LIBPREPKIT_A710.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1561         jsonIndexPrimerA711.add(Annotationtype.LIBPREPKIT_A711.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1562         jsonIndexPrimerA712.add(Annotationtype.LIBPREPKIT_A712.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1563
3610 20 Nov 15 olle 1564         jsonIndexPrimerA501.add(Annotationtype.LIBPREPKIT_A501.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1565         jsonIndexPrimerA502.add(Annotationtype.LIBPREPKIT_A502.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1566         jsonIndexPrimerA503.add(Annotationtype.LIBPREPKIT_A503.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1567         jsonIndexPrimerA504.add(Annotationtype.LIBPREPKIT_A504.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1568         jsonIndexPrimerA505.add(Annotationtype.LIBPREPKIT_A505.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1569         jsonIndexPrimerA506.add(Annotationtype.LIBPREPKIT_A506.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1570         jsonIndexPrimerA507.add(Annotationtype.LIBPREPKIT_A507.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1571         jsonIndexPrimerA508.add(Annotationtype.LIBPREPKIT_A508.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1572
3610 20 Nov 15 olle 1573         jsonRegistrationDate.add(d.convert(kit.getEntryDate()));
3610 20 Nov 15 olle 1574         jsonLastUpdatedDate.add(d.convert((Date)Annotationtype.LIBPREPKIT_LAST_UPDATED.getAnnotationValue(dc, kit)));
3610 20 Nov 15 olle 1575         jsonExpirationDate.add(d.convert(kit.getExpirationDate()));
3610 20 Nov 15 olle 1576         jsonActive.add(!kit.isInactive());
3610 20 Nov 15 olle 1577         jsonTimesUsed.add(Annotationtype.LIBPREPKIT_NUMBER_TIMES_USED.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1578         jsonUnusedFpaPlateLocs.add(Annotationtype.LIBPREPKIT_UNUSED_FPA_WELLS.getAnnotationValue(dc, kit));
3610 20 Nov 15 olle 1579         jsonComment.add(kit.getDescription());
3610 20 Nov 15 olle 1580       }
3573 05 Nov 15 olle 1581     }
3610 20 Nov 15 olle 1582
3573 05 Nov 15 olle 1583     JSONObject jsonConsumables = new JSONObject();
3573 05 Nov 15 olle 1584
3573 05 Nov 15 olle 1585     jsonConsumables.put("numItemsFiltered", numItemsFiltered);
3573 05 Nov 15 olle 1586     jsonConsumables.put("numItemsTot", numItemsTot);
3573 05 Nov 15 olle 1587
3573 05 Nov 15 olle 1588     // Add JSONArrays
3573 05 Nov 15 olle 1589     jsonConsumables.put("Headers", jsonHeaders);
3573 05 Nov 15 olle 1590
3621 25 Nov 15 olle 1591     jsonConsumables.put("KitId", jsonKitId);
3573 05 Nov 15 olle 1592     jsonConsumables.put("KitName", jsonKitName);
3573 05 Nov 15 olle 1593     jsonConsumables.put("LibraryPrepBox1", jsonLibraryPrepBox1);
3573 05 Nov 15 olle 1594     jsonConsumables.put("IndexKit", jsonIndexKit);
3573 05 Nov 15 olle 1595     jsonConsumables.put("ContentSetBox3", jsonContentSetBox3);
3573 05 Nov 15 olle 1596     jsonConsumables.put("OligoPoolA_FPA", jsonOligoPoolA_FPA);
3573 05 Nov 15 olle 1597     jsonConsumables.put("OligoPoolB_FPB", jsonOligoPoolB_FPB);
3573 05 Nov 15 olle 1598     jsonConsumables.put("OligoHybridizationSequencingReagent3", jsonOligoHybridizationSequencingReagent3);
3573 05 Nov 15 olle 1599     jsonConsumables.put("StringentWash1_SW1", jsonStringentWash1_SW1);
3573 05 Nov 15 olle 1600     jsonConsumables.put("ExtensionLigationMix3", jsonExtensionLigationMix3);
3573 05 Nov 15 olle 1601     jsonConsumables.put("PcrMasterMix2_PMM2", jsonPcrMasterMix2_PMM2);
3573 05 Nov 15 olle 1602     jsonConsumables.put("UB1", jsonUB1);
3573 05 Nov 15 olle 1603     jsonConsumables.put("TDP", jsonTDP);
3573 05 Nov 15 olle 1604     jsonConsumables.put("Filterplate", jsonFilterplate);
3573 05 Nov 15 olle 1605
3573 05 Nov 15 olle 1606     jsonConsumables.put("IndexPrimerA701", jsonIndexPrimerA701);
3573 05 Nov 15 olle 1607     jsonConsumables.put("IndexPrimerA702", jsonIndexPrimerA702);
3573 05 Nov 15 olle 1608     jsonConsumables.put("IndexPrimerA703", jsonIndexPrimerA703);
3573 05 Nov 15 olle 1609     jsonConsumables.put("IndexPrimerA704", jsonIndexPrimerA704);
3573 05 Nov 15 olle 1610     jsonConsumables.put("IndexPrimerA705", jsonIndexPrimerA705);
3573 05 Nov 15 olle 1611     jsonConsumables.put("IndexPrimerA706", jsonIndexPrimerA706);
3573 05 Nov 15 olle 1612     jsonConsumables.put("IndexPrimerA707", jsonIndexPrimerA707);
3573 05 Nov 15 olle 1613     jsonConsumables.put("IndexPrimerA708", jsonIndexPrimerA708);
3573 05 Nov 15 olle 1614     jsonConsumables.put("IndexPrimerA709", jsonIndexPrimerA709);
3573 05 Nov 15 olle 1615     jsonConsumables.put("IndexPrimerA710", jsonIndexPrimerA710);
3573 05 Nov 15 olle 1616     jsonConsumables.put("IndexPrimerA711", jsonIndexPrimerA711);
3573 05 Nov 15 olle 1617     jsonConsumables.put("IndexPrimerA712", jsonIndexPrimerA712);
3573 05 Nov 15 olle 1618
3573 05 Nov 15 olle 1619     jsonConsumables.put("IndexPrimerA501", jsonIndexPrimerA501);
3573 05 Nov 15 olle 1620     jsonConsumables.put("IndexPrimerA502", jsonIndexPrimerA502);
3573 05 Nov 15 olle 1621     jsonConsumables.put("IndexPrimerA503", jsonIndexPrimerA503);
3573 05 Nov 15 olle 1622     jsonConsumables.put("IndexPrimerA504", jsonIndexPrimerA504);
3573 05 Nov 15 olle 1623     jsonConsumables.put("IndexPrimerA505", jsonIndexPrimerA505);
3573 05 Nov 15 olle 1624     jsonConsumables.put("IndexPrimerA506", jsonIndexPrimerA506);
3573 05 Nov 15 olle 1625     jsonConsumables.put("IndexPrimerA507", jsonIndexPrimerA507);
3573 05 Nov 15 olle 1626     jsonConsumables.put("IndexPrimerA508", jsonIndexPrimerA508);
3573 05 Nov 15 olle 1627
3573 05 Nov 15 olle 1628     jsonConsumables.put("RegistrationDate", jsonRegistrationDate);
3573 05 Nov 15 olle 1629     jsonConsumables.put("LastUpdatedDate", jsonLastUpdatedDate);
3573 05 Nov 15 olle 1630     jsonConsumables.put("ExpirationDate", jsonExpirationDate);
3610 20 Nov 15 olle 1631     jsonConsumables.put("Active", jsonActive);
3573 05 Nov 15 olle 1632     jsonConsumables.put("TimesUsed", jsonTimesUsed);
3573 05 Nov 15 olle 1633     jsonConsumables.put("UnusedFpaPlateLocs", jsonUnusedFpaPlateLocs);
3573 05 Nov 15 olle 1634     jsonConsumables.put("Comment", jsonComment);
3610 20 Nov 15 olle 1635 //System.out.println(new Date() + " LibPrepServlet::fetchJsonKitData(): jsonConsumables = " + jsonConsumables);
3573 05 Nov 15 olle 1636
3573 05 Nov 15 olle 1637     return jsonConsumables;
3573 05 Nov 15 olle 1638   }
3573 05 Nov 15 olle 1639
3573 05 Nov 15 olle 1640   @SuppressWarnings("unchecked")
3610 20 Nov 15 olle 1641   private JSONArray fetchKitJsonHeaders()
3610 20 Nov 15 olle 1642   {
3610 20 Nov 15 olle 1643     JSONArray jsonHeaders = new JSONArray();
3610 20 Nov 15 olle 1644
3610 20 Nov 15 olle 1645     // Header line
3610 20 Nov 15 olle 1646     jsonHeaders.add("TruSight Tumor Library Preparation partI");
3610 20 Nov 15 olle 1647     jsonHeaders.add("TruSight Tumor Library Prep Box 1");
3610 20 Nov 15 olle 1648     jsonHeaders.add("TruSight Tumor Index Kit");
3610 20 Nov 15 olle 1649     jsonHeaders.add("TruSight Tumor Content Set Box 3");
3610 20 Nov 15 olle 1650     jsonHeaders.add("TruSight Tumor Oligo Pool A (FPA)");
3610 20 Nov 15 olle 1651     jsonHeaders.add("TruSight Tumor Oligo Pool B (FPB)");
3610 20 Nov 15 olle 1652     jsonHeaders.add("Oligo Hybridization for Sequencing Reagent 3 (OHS3)");
3610 20 Nov 15 olle 1653     jsonHeaders.add("Stringent Wash 1 (SW1)");
3610 20 Nov 15 olle 1654     jsonHeaders.add("Extension Ligation Mix 3 (ELM3)");
3610 20 Nov 15 olle 1655     jsonHeaders.add("PCR Master Mix 2 (PMM2)");
3610 20 Nov 15 olle 1656     jsonHeaders.add("UB1");
3610 20 Nov 15 olle 1657     jsonHeaders.add("TDP");
3610 20 Nov 15 olle 1658     jsonHeaders.add("Filterplate");
3610 20 Nov 15 olle 1659     // Index 1 Primers Lot Number        
3610 20 Nov 15 olle 1660     jsonHeaders.add("A701");
3610 20 Nov 15 olle 1661     jsonHeaders.add("A702");
3610 20 Nov 15 olle 1662     jsonHeaders.add("A703");
3610 20 Nov 15 olle 1663     jsonHeaders.add("A704");
3610 20 Nov 15 olle 1664     jsonHeaders.add("A705");
3610 20 Nov 15 olle 1665     jsonHeaders.add("A706");
3610 20 Nov 15 olle 1666     jsonHeaders.add("A707");
3610 20 Nov 15 olle 1667     jsonHeaders.add("A708");
3610 20 Nov 15 olle 1668     jsonHeaders.add("A709");
3610 20 Nov 15 olle 1669     jsonHeaders.add("A710");
3610 20 Nov 15 olle 1670     jsonHeaders.add("A711");
3610 20 Nov 15 olle 1671     jsonHeaders.add("A712");
3610 20 Nov 15 olle 1672     // Index 2 Primers Lot Number        
3610 20 Nov 15 olle 1673     jsonHeaders.add("A501");
3610 20 Nov 15 olle 1674     jsonHeaders.add("A502");
3610 20 Nov 15 olle 1675     jsonHeaders.add("A503");
3610 20 Nov 15 olle 1676     jsonHeaders.add("A504");
3610 20 Nov 15 olle 1677     jsonHeaders.add("A505");
3610 20 Nov 15 olle 1678     jsonHeaders.add("A506");
3610 20 Nov 15 olle 1679     jsonHeaders.add("A507");
3610 20 Nov 15 olle 1680     jsonHeaders.add("A508");
3610 20 Nov 15 olle 1681     // Extra data
3610 20 Nov 15 olle 1682     jsonHeaders.add("Registration date");
3610 20 Nov 15 olle 1683     jsonHeaders.add("Last updated");
3610 20 Nov 15 olle 1684     jsonHeaders.add("Expiration date");
3610 20 Nov 15 olle 1685     jsonHeaders.add("Active");
3610 20 Nov 15 olle 1686     jsonHeaders.add("#Times used");
3610 20 Nov 15 olle 1687     jsonHeaders.add("Unused FPA plate locations");
3610 20 Nov 15 olle 1688     jsonHeaders.add("Comment");
3610 20 Nov 15 olle 1689     
3610 20 Nov 15 olle 1690     return jsonHeaders;
3610 20 Nov 15 olle 1691   }
3610 20 Nov 15 olle 1692
3610 20 Nov 15 olle 1693   @SuppressWarnings("unchecked")
3449 28 Jul 15 olle 1694   @Override
3449 28 Jul 15 olle 1695   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
3449 28 Jul 15 olle 1696     throws ServletException, IOException 
3449 28 Jul 15 olle 1697   {
3449 28 Jul 15 olle 1698     String ID = req.getParameter("ID");
3449 28 Jul 15 olle 1699     String cmd = req.getParameter("cmd");
3449 28 Jul 15 olle 1700     JsonUtil.setJsonResponseHeaders(resp);
3449 28 Jul 15 olle 1701     
3449 28 Jul 15 olle 1702     JSONObject json = new JSONObject();
3449 28 Jul 15 olle 1703     json.put("status", "ok");
3449 28 Jul 15 olle 1704     
3449 28 Jul 15 olle 1705     JSONArray jsonMessages = new JSONArray();
3449 28 Jul 15 olle 1706   
5468 04 Jun 19 olle 1707     //final SessionControl sc = Application.getSessionControl(ID, req.getRemoteAddr());
5744 20 Nov 19 olle 1708     //final SessionControl sc  = Application.getSessionControl(ID, "", req.getRemoteAddr(), true);
5744 20 Nov 19 olle 1709     final SessionControl sc  = Application.getSessionControl(ID, null, req.getRemoteAddr(), true);
3449 28 Jul 15 olle 1710     DbControl dc = null;
3449 28 Jul 15 olle 1711     try
3449 28 Jul 15 olle 1712     {
3449 28 Jul 15 olle 1713       if ("CreateBarcodedLibraries".equals(cmd))
3449 28 Jul 15 olle 1714       {
3449 28 Jul 15 olle 1715         dc = sc.newDbControl();
3449 28 Jul 15 olle 1716
3449 28 Jul 15 olle 1717         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3449 28 Jul 15 olle 1718
3449 28 Jul 15 olle 1719         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 1720         JSONObject jsonPlate = (JSONObject)jsonReq.get("bioplate");
3449 28 Jul 15 olle 1721         JSONArray jsonWells = (JSONArray)jsonPlate.get("wells");
3449 28 Jul 15 olle 1722
3449 28 Jul 15 olle 1723 /*
3449 28 Jul 15 olle 1724         // Load the CDNA bioplate and store comments and annotations
3449 28 Jul 15 olle 1725         Number cdnaPlateId = (Number)jsonPlate.get("id");
3449 28 Jul 15 olle 1726         BioPlate cDnaPlate = BioPlate.getById(dc, cdnaPlateId.intValue());
3449 28 Jul 15 olle 1727         Annotationtype.POOL_SCHEMA.setAnnotationValue(dc, cDnaPlate, jsonPlate.get("poolSchema"));
3449 28 Jul 15 olle 1728         Annotationtype.BARCODE_VARIANT.setAnnotationValue(dc, cDnaPlate, jsonPlate.get("barcodeVariant"));
3449 28 Jul 15 olle 1729 */
3449 28 Jul 15 olle 1730         // Load the DNA bioplate and store comments and annotations
3449 28 Jul 15 olle 1731         Number dnaPlateId = (Number)jsonPlate.get("id");
3449 28 Jul 15 olle 1732         BioPlate dnaPlate = BioPlate.getById(dc, dnaPlateId.intValue());
3449 28 Jul 15 olle 1733         Annotationtype.POOL_SCHEMA.setAnnotationValue(dc, dnaPlate, jsonPlate.get("poolSchema"));
3449 28 Jul 15 olle 1734         Annotationtype.BARCODE_VARIANT.setAnnotationValue(dc, dnaPlate, jsonPlate.get("barcodeVariant"));
3449 28 Jul 15 olle 1735         
3449 28 Jul 15 olle 1736         // Create a child LibPlate -- copy pooling annotations to it
3449 28 Jul 15 olle 1737 /*
3449 28 Jul 15 olle 1738         String libPlateName = cDnaPlate.getName().replace(BioplateType.CDNA.getPlateNamePrefix(), BioplateType.LIBRARY.getPlateNamePrefix());
3449 28 Jul 15 olle 1739 */
3449 28 Jul 15 olle 1740         String libPlateName = dnaPlate.getName().replace(BioplateType.DNA.getPlateNamePrefix(), BioplateType.LIBRARY.getPlateNamePrefix());
3449 28 Jul 15 olle 1741         BioPlate libPlate = BioPlate.getNew(dc, BioplateType.LIBRARY.getPlateGeometry(dc), BioplateType.LIBRARY.load(dc));
3449 28 Jul 15 olle 1742         libPlate.setName(libPlateName);
3449 28 Jul 15 olle 1743         libPlate.setDescription((String)jsonPlate.get("comments"));
3449 28 Jul 15 olle 1744         Annotationtype.POOL_SCHEMA.setAnnotationValue(dc, libPlate, jsonPlate.get("poolSchema"));
3449 28 Jul 15 olle 1745         Annotationtype.BARCODE_VARIANT.setAnnotationValue(dc, libPlate, jsonPlate.get("barcodeVariant"));
3449 28 Jul 15 olle 1746         dc.saveItem(libPlate);
3449 28 Jul 15 olle 1747         
3449 28 Jul 15 olle 1748         ItemSubtype libType = Subtype.LIBRARY.load(dc);
3449 28 Jul 15 olle 1749         Map<Extract, Integer> childCount = new HashMap<Extract, Integer>();
3449 28 Jul 15 olle 1750         
3449 28 Jul 15 olle 1751         for (int i = 0; i < jsonWells.size(); i++)
3449 28 Jul 15 olle 1752         {
3449 28 Jul 15 olle 1753           JSONObject jsonWell = (JSONObject)jsonWells.get(i);
3449 28 Jul 15 olle 1754 /*
3449 28 Jul 15 olle 1755           JSONObject jsonCDna = (JSONObject)jsonWell.get("cdna");
3449 28 Jul 15 olle 1756           JSONObject jsonBarcode = (JSONObject)jsonCDna.get("barcode");
3449 28 Jul 15 olle 1757 */
3449 28 Jul 15 olle 1758           JSONObject jsonDna = (JSONObject)jsonWell.get("dna");
3449 28 Jul 15 olle 1759           JSONObject jsonBarcode = (JSONObject)jsonDna.get("barcode");
3449 28 Jul 15 olle 1760           
3449 28 Jul 15 olle 1761           Number row = (Number)jsonWell.get("row");
3449 28 Jul 15 olle 1762           Number col = (Number)jsonWell.get("column");
3449 28 Jul 15 olle 1763 /*
3449 28 Jul 15 olle 1764           Number cdnaId = (Number)jsonCDna.get("id");
3449 28 Jul 15 olle 1765 */
3449 28 Jul 15 olle 1766           Number dnaId = (Number)jsonDna.get("id");
3449 28 Jul 15 olle 1767
3449 28 Jul 15 olle 1768 /*
3449 28 Jul 15 olle 1769           // Load CDNA for each well and create a new child Library item
3449 28 Jul 15 olle 1770           Extract cdna = Extract.getById(dc, cdnaId.intValue());
3449 28 Jul 15 olle 1771 */
3449 28 Jul 15 olle 1772           // Load DNA for each well and create a new child Library item
3449 28 Jul 15 olle 1773           Extract dna = Extract.getById(dc, dnaId.intValue());
3449 28 Jul 15 olle 1774           
3449 28 Jul 15 olle 1775           Extract lib = Extract.getNew(dc);
3449 28 Jul 15 olle 1776           lib.setItemSubtype(libType);
3449 28 Jul 15 olle 1777 /*
3449 28 Jul 15 olle 1778           lib.setName(cdna.getName()+".lib");
3449 28 Jul 15 olle 1779 */
3449 28 Jul 15 olle 1780           lib.setName(dna.getName()+".lib");
3449 28 Jul 15 olle 1781           lib.setTag(Tag.getById(dc, ((Number)jsonBarcode.get("id")).intValue()));
3449 28 Jul 15 olle 1782 /*
3449 28 Jul 15 olle 1783           BioMaterialEventSource evtSrc = lib.getCreationEvent().setSource(cdna);
3449 28 Jul 15 olle 1784           lib.setBioWell(libPlate.getBioWell(cdna.getBioWell().getPlateCoordinate()));
3449 28 Jul 15 olle 1785 */
3449 28 Jul 15 olle 1786           BioMaterialEventSource evtSrc = lib.getCreationEvent().setSource(dna);
3449 28 Jul 15 olle 1787           lib.setBioWell(libPlate.getBioWell(dna.getBioWell().getPlateCoordinate()));
3449 28 Jul 15 olle 1788           dc.saveItem(lib);
3449 28 Jul 15 olle 1789         }
3449 28 Jul 15 olle 1790         
3449 28 Jul 15 olle 1791         dc.commit();
3449 28 Jul 15 olle 1792         jsonMessages.add("Created " + libPlate.getName() + " with " + jsonWells.size() + " barcoded libary child items");
3449 28 Jul 15 olle 1793       }
3538 13 Oct 15 olle 1794       else if ("PrepareDownloadReagentLotNoFile".equals(cmd))
3538 13 Oct 15 olle 1795       {
3538 13 Oct 15 olle 1796         dc = sc.newDbControl();
3538 13 Oct 15 olle 1797
3538 13 Oct 15 olle 1798         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3538 13 Oct 15 olle 1799
3538 13 Oct 15 olle 1800         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3538 13 Oct 15 olle 1801
3553 20 Oct 15 olle 1802         String referenceName = req.getParameter("referenceName");
3546 16 Oct 15 olle 1803         String expirationDate = req.getParameter("expirationDate");
3538 13 Oct 15 olle 1804         // Get JSON data on reagent lot numbers from POST data
3538 13 Oct 15 olle 1805         JSONObject reagentInfo = jsonReq;
3538 13 Oct 15 olle 1806         
3538 13 Oct 15 olle 1807         JSONArray jsonHeaders = (JSONArray) reagentInfo.get("Headers");
3538 13 Oct 15 olle 1808
3551 20 Oct 15 olle 1809         JSONArray jsonKitName = (JSONArray) reagentInfo.get("KitName");
3538 13 Oct 15 olle 1810         JSONArray jsonLibraryPrepBox1 = (JSONArray) reagentInfo.get("LibraryPrepBox1");
3538 13 Oct 15 olle 1811         JSONArray jsonIndexKit = (JSONArray) reagentInfo.get("IndexKit");
3538 13 Oct 15 olle 1812         JSONArray jsonContentSetBox3 = (JSONArray) reagentInfo.get("ContentSetBox3");
3538 13 Oct 15 olle 1813         JSONArray jsonOligoPoolA_FPA = (JSONArray) reagentInfo.get("OligoPoolA_FPA");
3538 13 Oct 15 olle 1814         JSONArray jsonOligoPoolB_FPB = (JSONArray) reagentInfo.get("OligoPoolB_FPB");
3538 13 Oct 15 olle 1815         JSONArray jsonOligoHybridizationSequencingReagent3 = (JSONArray) reagentInfo.get("OligoHybridizationSequencingReagent3");
3538 13 Oct 15 olle 1816         JSONArray jsonStringentWash1_SW1 = (JSONArray) reagentInfo.get("StringentWash1_SW1");
3538 13 Oct 15 olle 1817         JSONArray jsonExtensionLigationMix3 = (JSONArray) reagentInfo.get("ExtensionLigationMix3");
3538 13 Oct 15 olle 1818         JSONArray jsonPcrMasterMix2_PMM2 = (JSONArray) reagentInfo.get("PcrMasterMix2_PMM2");
3538 13 Oct 15 olle 1819         JSONArray jsonUB1 = (JSONArray) reagentInfo.get("UB1");
3538 13 Oct 15 olle 1820         JSONArray jsonTDP = (JSONArray) reagentInfo.get("TDP");
3538 13 Oct 15 olle 1821         JSONArray jsonFilterplate = (JSONArray) reagentInfo.get("Filterplate");
3538 13 Oct 15 olle 1822         
3538 13 Oct 15 olle 1823         JSONArray jsonIndexPrimerA701 = (JSONArray) reagentInfo.get("IndexPrimerA701");
3538 13 Oct 15 olle 1824         JSONArray jsonIndexPrimerA702 = (JSONArray) reagentInfo.get("IndexPrimerA702");
3538 13 Oct 15 olle 1825         JSONArray jsonIndexPrimerA703 = (JSONArray) reagentInfo.get("IndexPrimerA703");
3538 13 Oct 15 olle 1826         JSONArray jsonIndexPrimerA704 = (JSONArray) reagentInfo.get("IndexPrimerA704");
3538 13 Oct 15 olle 1827         JSONArray jsonIndexPrimerA705 = (JSONArray) reagentInfo.get("IndexPrimerA705");
3538 13 Oct 15 olle 1828         JSONArray jsonIndexPrimerA706 = (JSONArray) reagentInfo.get("IndexPrimerA706");
3538 13 Oct 15 olle 1829         JSONArray jsonIndexPrimerA707 = (JSONArray) reagentInfo.get("IndexPrimerA707");
3538 13 Oct 15 olle 1830         JSONArray jsonIndexPrimerA708 = (JSONArray) reagentInfo.get("IndexPrimerA708");
3538 13 Oct 15 olle 1831         JSONArray jsonIndexPrimerA709 = (JSONArray) reagentInfo.get("IndexPrimerA709");
3538 13 Oct 15 olle 1832         JSONArray jsonIndexPrimerA710 = (JSONArray) reagentInfo.get("IndexPrimerA710");
3538 13 Oct 15 olle 1833         JSONArray jsonIndexPrimerA711 = (JSONArray) reagentInfo.get("IndexPrimerA711");
3538 13 Oct 15 olle 1834         JSONArray jsonIndexPrimerA712 = (JSONArray) reagentInfo.get("IndexPrimerA712");
3538 13 Oct 15 olle 1835         
3538 13 Oct 15 olle 1836         JSONArray jsonIndexPrimerA501 = (JSONArray) reagentInfo.get("IndexPrimerA501");
3538 13 Oct 15 olle 1837         JSONArray jsonIndexPrimerA502 = (JSONArray) reagentInfo.get("IndexPrimerA502");
3538 13 Oct 15 olle 1838         JSONArray jsonIndexPrimerA503 = (JSONArray) reagentInfo.get("IndexPrimerA503");
3538 13 Oct 15 olle 1839         JSONArray jsonIndexPrimerA504 = (JSONArray) reagentInfo.get("IndexPrimerA504");
3538 13 Oct 15 olle 1840         JSONArray jsonIndexPrimerA505 = (JSONArray) reagentInfo.get("IndexPrimerA505");
3538 13 Oct 15 olle 1841         JSONArray jsonIndexPrimerA506 = (JSONArray) reagentInfo.get("IndexPrimerA506");
3538 13 Oct 15 olle 1842         JSONArray jsonIndexPrimerA507 = (JSONArray) reagentInfo.get("IndexPrimerA507");
3538 13 Oct 15 olle 1843         JSONArray jsonIndexPrimerA508 = (JSONArray) reagentInfo.get("IndexPrimerA508");
3538 13 Oct 15 olle 1844
3551 20 Oct 15 olle 1845         JSONArray jsonExpirationDate = (JSONArray) reagentInfo.get("ExpirationDate");
3551 20 Oct 15 olle 1846
3553 20 Oct 15 olle 1847         // If reference name is file name, remove file extension
3553 20 Oct 15 olle 1848         String sourceRefName = referenceName;
3624 25 Nov 15 olle 1849         int lastDotIndex = referenceName.lastIndexOf(".");
3624 25 Nov 15 olle 1850         if (lastDotIndex >= 0)
3538 13 Oct 15 olle 1851         {
3538 13 Oct 15 olle 1852           // Remove file extension
3624 25 Nov 15 olle 1853           sourceRefName = referenceName.substring(0, lastDotIndex);
3553 20 Oct 15 olle 1854         }        
3553 20 Oct 15 olle 1855         resp.setHeader("Content-Disposition", "attachment; filename=library-preparation-reagents-" + sourceRefName + "-exp-" + expirationDate + ".csv");
3538 13 Oct 15 olle 1856         resp.setContentType("text/plain");
3538 13 Oct 15 olle 1857         resp.setCharacterEncoding("UTF-8");
3538 13 Oct 15 olle 1858
3871 22 Apr 16 olle 1859         // Get static cache
3871 22 Apr 16 olle 1860         StaticCache cache = Application.getStaticCache();
3871 22 Apr 16 olle 1861         // Store temporary data in static cache
3871 22 Apr 16 olle 1862         String tmpFileName = Application.generateRandomId(64);
3871 22 Apr 16 olle 1863         String cacheKey = tmpFileName;
3871 22 Apr 16 olle 1864         int timeout = 1000;
3871 22 Apr 16 olle 1865         OutputStream cacheIS = cache.write(cacheKey, timeout);
3871 22 Apr 16 olle 1866
3871 22 Apr 16 olle 1867         OutputStreamWriter fileWriter = new OutputStreamWriter(cacheIS);
3871 22 Apr 16 olle 1868
3538 13 Oct 15 olle 1869         String separator = ";";
3538 13 Oct 15 olle 1870         fileWriter.write("Consumables" + "\n");
3538 13 Oct 15 olle 1871         fileWriter.write("TruSight Tumor Sample Preparation Kit (Cat no FC-130-2001, 48 samples, Illumina)" + "\n");
3538 13 Oct 15 olle 1872         fileWriter.write("\n");
3538 13 Oct 15 olle 1873         // Header line
3538 13 Oct 15 olle 1874         String headerLine = "TruSight Tumor Library Preparation partI";
3538 13 Oct 15 olle 1875         headerLine += separator + "TruSight Tumor Library Prep Box 1";
3538 13 Oct 15 olle 1876         headerLine += separator + "TruSight Tumor Index Kit";
3538 13 Oct 15 olle 1877         headerLine += separator + "TruSight Tumor Content Set Box 3";
3538 13 Oct 15 olle 1878         headerLine += separator + "TruSight Tumor Oligo Pool A (FPA)";
3538 13 Oct 15 olle 1879         headerLine += separator + "TruSight Tumor Oligo Pool B (FPB)";
3538 13 Oct 15 olle 1880         headerLine += separator + "Oligo Hybridization for Sequencing Reagent 3 (OHS3)";
3538 13 Oct 15 olle 1881         headerLine += separator + "Stringent Wash 1 (SW1)";
3538 13 Oct 15 olle 1882         headerLine += separator + "Extension Ligation Mix 3 (ELM3)";
3538 13 Oct 15 olle 1883         headerLine += separator + "PCR Master Mix 2 (PMM2)";
3538 13 Oct 15 olle 1884         headerLine += separator + "UB1";
3538 13 Oct 15 olle 1885         headerLine += separator + "TDP";
3538 13 Oct 15 olle 1886         headerLine += separator + "Filterplate";
3538 13 Oct 15 olle 1887         // Index 1 Primers Lot Number        
3538 13 Oct 15 olle 1888         headerLine += separator + "A701";
3538 13 Oct 15 olle 1889         headerLine += separator + "A702";
3538 13 Oct 15 olle 1890         headerLine += separator + "A703";
3538 13 Oct 15 olle 1891         headerLine += separator + "A704";
3538 13 Oct 15 olle 1892         headerLine += separator + "A705";
3538 13 Oct 15 olle 1893         headerLine += separator + "A706";
3538 13 Oct 15 olle 1894         headerLine += separator + "A707";
3538 13 Oct 15 olle 1895         headerLine += separator + "A708";
3538 13 Oct 15 olle 1896         headerLine += separator + "A709";
3538 13 Oct 15 olle 1897         headerLine += separator + "A710";
3538 13 Oct 15 olle 1898         headerLine += separator + "A711";
3538 13 Oct 15 olle 1899         headerLine += separator + "A712";
3538 13 Oct 15 olle 1900         // Index 2 Primers Lot Number        
3538 13 Oct 15 olle 1901         headerLine += separator + "A501";
3538 13 Oct 15 olle 1902         headerLine += separator + "A502";
3538 13 Oct 15 olle 1903         headerLine += separator + "A503";
3538 13 Oct 15 olle 1904         headerLine += separator + "A504";
3538 13 Oct 15 olle 1905         headerLine += separator + "A505";
3538 13 Oct 15 olle 1906         headerLine += separator + "A506";
3538 13 Oct 15 olle 1907         headerLine += separator + "A507";
3538 13 Oct 15 olle 1908         headerLine += separator + "A508";
3551 20 Oct 15 olle 1909         // Expiration date
3551 20 Oct 15 olle 1910         headerLine += separator + "Expiration date";
3538 13 Oct 15 olle 1911
3538 13 Oct 15 olle 1912         fileWriter.write(headerLine + "\n");
3538 13 Oct 15 olle 1913
3551 20 Oct 15 olle 1914         int numItems = jsonKitName.size();
3538 13 Oct 15 olle 1915
3538 13 Oct 15 olle 1916         for (int i = 0; i < numItems; i++)
3538 13 Oct 15 olle 1917         {
3551 20 Oct 15 olle 1918           String line = (String) jsonKitName.get(i);
3551 20 Oct 15 olle 1919           line += separator + (String) jsonLibraryPrepBox1.get(i);
3551 20 Oct 15 olle 1920           line += separator + (String) jsonIndexKit.get(i);
3551 20 Oct 15 olle 1921           line += separator + (String) jsonContentSetBox3.get(i);
3551 20 Oct 15 olle 1922           line += separator + (String) jsonOligoPoolA_FPA.get(i);
3551 20 Oct 15 olle 1923           line += separator + (String) jsonOligoPoolB_FPB.get(i);
3551 20 Oct 15 olle 1924           line += separator + (String) jsonOligoHybridizationSequencingReagent3.get(i);  
3551 20 Oct 15 olle 1925           line += separator + (String) jsonStringentWash1_SW1.get(i);
3551 20 Oct 15 olle 1926           line += separator + (String) jsonExtensionLigationMix3.get(i);
3551 20 Oct 15 olle 1927           line += separator + (String) jsonPcrMasterMix2_PMM2.get(i);
3551 20 Oct 15 olle 1928           line += separator + (String) jsonUB1.get(i);
3551 20 Oct 15 olle 1929           line += separator + (String) jsonTDP.get(i);
3551 20 Oct 15 olle 1930           line += separator + (String) jsonFilterplate.get(i);
3551 20 Oct 15 olle 1931           // Index 1 Primers Lot Number
3551 20 Oct 15 olle 1932           line += separator + (String) jsonIndexPrimerA701.get(i);      
3551 20 Oct 15 olle 1933           line += separator + (String) jsonIndexPrimerA702.get(i);
3551 20 Oct 15 olle 1934           line += separator + (String) jsonIndexPrimerA703.get(i);
3551 20 Oct 15 olle 1935           line += separator + (String) jsonIndexPrimerA704.get(i);
3551 20 Oct 15 olle 1936           line += separator + (String) jsonIndexPrimerA705.get(i);
3551 20 Oct 15 olle 1937           line += separator + (String) jsonIndexPrimerA706.get(i);
3551 20 Oct 15 olle 1938           line += separator + (String) jsonIndexPrimerA707.get(i);
3551 20 Oct 15 olle 1939           line += separator + (String) jsonIndexPrimerA708.get(i);
3551 20 Oct 15 olle 1940           line += separator + (String) jsonIndexPrimerA709.get(i);
3551 20 Oct 15 olle 1941           line += separator + (String) jsonIndexPrimerA710.get(i);
3551 20 Oct 15 olle 1942           line += separator + (String) jsonIndexPrimerA711.get(i);
3551 20 Oct 15 olle 1943           line += separator + (String) jsonIndexPrimerA712.get(i);
3551 20 Oct 15 olle 1944           // Index 2 Primers Lot Number
3538 13 Oct 15 olle 1945           line += separator + (String) jsonIndexPrimerA501.get(i);          
3538 13 Oct 15 olle 1946           line += separator + (String) jsonIndexPrimerA502.get(i);          
3538 13 Oct 15 olle 1947           line += separator + (String) jsonIndexPrimerA503.get(i);          
3538 13 Oct 15 olle 1948           line += separator + (String) jsonIndexPrimerA504.get(i);          
3538 13 Oct 15 olle 1949           line += separator + (String) jsonIndexPrimerA505.get(i);          
3538 13 Oct 15 olle 1950           line += separator + (String) jsonIndexPrimerA506.get(i);          
3538 13 Oct 15 olle 1951           line += separator + (String) jsonIndexPrimerA507.get(i);          
3538 13 Oct 15 olle 1952           line += separator + (String) jsonIndexPrimerA508.get(i);
3551 20 Oct 15 olle 1953           // Expiration date
3551 20 Oct 15 olle 1954           line += separator + (String) jsonExpirationDate.get(i);
3538 13 Oct 15 olle 1955
3538 13 Oct 15 olle 1956           fileWriter.write(line + "\n");
3538 13 Oct 15 olle 1957         }
3538 13 Oct 15 olle 1958         fileWriter.flush();
3538 13 Oct 15 olle 1959         fileWriter.close();
3538 13 Oct 15 olle 1960
3871 22 Apr 16 olle 1961         // Return cache key
3538 13 Oct 15 olle 1962         PrintWriter out = resp.getWriter();
3871 22 Apr 16 olle 1963         out.print(cacheKey);        
3538 13 Oct 15 olle 1964         out.flush();
3538 13 Oct 15 olle 1965         out.close();
3538 13 Oct 15 olle 1966         jsonMessages.add("no comment");
3538 13 Oct 15 olle 1967       }
3547 16 Oct 15 olle 1968       else if ("PrepareDownloadLibPrepKitDataFile".equals(cmd))
3547 16 Oct 15 olle 1969       {
3547 16 Oct 15 olle 1970         dc = sc.newDbControl();
3547 16 Oct 15 olle 1971
3547 16 Oct 15 olle 1972         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3547 16 Oct 15 olle 1973
3547 16 Oct 15 olle 1974         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3547 16 Oct 15 olle 1975
3547 16 Oct 15 olle 1976         String kitFilterValue = req.getParameter("kitFilter");
3547 16 Oct 15 olle 1977         String kitFilterName = req.getParameter("kitFilterName");
3547 16 Oct 15 olle 1978         String numItemsFiltered = req.getParameter("numItemsFiltered");
3547 16 Oct 15 olle 1979         String numItemsTot = req.getParameter("numItemsTot");
3547 16 Oct 15 olle 1980         // Get JSON data on reagent lot numbers from POST data
3547 16 Oct 15 olle 1981         JSONObject reagentInfo = jsonReq;
3547 16 Oct 15 olle 1982         
3547 16 Oct 15 olle 1983         JSONArray jsonHeaders = (JSONArray) reagentInfo.get("Headers");
3547 16 Oct 15 olle 1984
3551 20 Oct 15 olle 1985         JSONArray jsonKitName = (JSONArray) reagentInfo.get("KitName");
3547 16 Oct 15 olle 1986         JSONArray jsonLibraryPrepBox1 = (JSONArray) reagentInfo.get("LibraryPrepBox1");
3547 16 Oct 15 olle 1987         JSONArray jsonIndexKit = (JSONArray) reagentInfo.get("IndexKit");
3547 16 Oct 15 olle 1988         JSONArray jsonContentSetBox3 = (JSONArray) reagentInfo.get("ContentSetBox3");
3547 16 Oct 15 olle 1989         JSONArray jsonOligoPoolA_FPA = (JSONArray) reagentInfo.get("OligoPoolA_FPA");
3547 16 Oct 15 olle 1990         JSONArray jsonOligoPoolB_FPB = (JSONArray) reagentInfo.get("OligoPoolB_FPB");
3547 16 Oct 15 olle 1991         JSONArray jsonOligoHybridizationSequencingReagent3 = (JSONArray) reagentInfo.get("OligoHybridizationSequencingReagent3");
3547 16 Oct 15 olle 1992         JSONArray jsonStringentWash1_SW1 = (JSONArray) reagentInfo.get("StringentWash1_SW1");
3547 16 Oct 15 olle 1993         JSONArray jsonExtensionLigationMix3 = (JSONArray) reagentInfo.get("ExtensionLigationMix3");
3547 16 Oct 15 olle 1994         JSONArray jsonPcrMasterMix2_PMM2 = (JSONArray) reagentInfo.get("PcrMasterMix2_PMM2");
3547 16 Oct 15 olle 1995         JSONArray jsonUB1 = (JSONArray) reagentInfo.get("UB1");
3547 16 Oct 15 olle 1996         JSONArray jsonTDP = (JSONArray) reagentInfo.get("TDP");
3547 16 Oct 15 olle 1997         JSONArray jsonFilterplate = (JSONArray) reagentInfo.get("Filterplate");
3547 16 Oct 15 olle 1998         
3547 16 Oct 15 olle 1999         JSONArray jsonIndexPrimerA701 = (JSONArray) reagentInfo.get("IndexPrimerA701");
3547 16 Oct 15 olle 2000         JSONArray jsonIndexPrimerA702 = (JSONArray) reagentInfo.get("IndexPrimerA702");
3547 16 Oct 15 olle 2001         JSONArray jsonIndexPrimerA703 = (JSONArray) reagentInfo.get("IndexPrimerA703");
3547 16 Oct 15 olle 2002         JSONArray jsonIndexPrimerA704 = (JSONArray) reagentInfo.get("IndexPrimerA704");
3547 16 Oct 15 olle 2003         JSONArray jsonIndexPrimerA705 = (JSONArray) reagentInfo.get("IndexPrimerA705");
3547 16 Oct 15 olle 2004         JSONArray jsonIndexPrimerA706 = (JSONArray) reagentInfo.get("IndexPrimerA706");
3547 16 Oct 15 olle 2005         JSONArray jsonIndexPrimerA707 = (JSONArray) reagentInfo.get("IndexPrimerA707");
3547 16 Oct 15 olle 2006         JSONArray jsonIndexPrimerA708 = (JSONArray) reagentInfo.get("IndexPrimerA708");
3547 16 Oct 15 olle 2007         JSONArray jsonIndexPrimerA709 = (JSONArray) reagentInfo.get("IndexPrimerA709");
3547 16 Oct 15 olle 2008         JSONArray jsonIndexPrimerA710 = (JSONArray) reagentInfo.get("IndexPrimerA710");
3547 16 Oct 15 olle 2009         JSONArray jsonIndexPrimerA711 = (JSONArray) reagentInfo.get("IndexPrimerA711");
3547 16 Oct 15 olle 2010         JSONArray jsonIndexPrimerA712 = (JSONArray) reagentInfo.get("IndexPrimerA712");
3547 16 Oct 15 olle 2011         
3547 16 Oct 15 olle 2012         JSONArray jsonIndexPrimerA501 = (JSONArray) reagentInfo.get("IndexPrimerA501");
3547 16 Oct 15 olle 2013         JSONArray jsonIndexPrimerA502 = (JSONArray) reagentInfo.get("IndexPrimerA502");
3547 16 Oct 15 olle 2014         JSONArray jsonIndexPrimerA503 = (JSONArray) reagentInfo.get("IndexPrimerA503");
3547 16 Oct 15 olle 2015         JSONArray jsonIndexPrimerA504 = (JSONArray) reagentInfo.get("IndexPrimerA504");
3547 16 Oct 15 olle 2016         JSONArray jsonIndexPrimerA505 = (JSONArray) reagentInfo.get("IndexPrimerA505");
3547 16 Oct 15 olle 2017         JSONArray jsonIndexPrimerA506 = (JSONArray) reagentInfo.get("IndexPrimerA506");
3547 16 Oct 15 olle 2018         JSONArray jsonIndexPrimerA507 = (JSONArray) reagentInfo.get("IndexPrimerA507");
3547 16 Oct 15 olle 2019         JSONArray jsonIndexPrimerA508 = (JSONArray) reagentInfo.get("IndexPrimerA508");
3547 16 Oct 15 olle 2020
3547 16 Oct 15 olle 2021         JSONArray jsonRegistrationDate = (JSONArray) reagentInfo.get("RegistrationDate");
3547 16 Oct 15 olle 2022         JSONArray jsonLastUpdatedDate = (JSONArray) reagentInfo.get("LastUpdatedDate");
3547 16 Oct 15 olle 2023         JSONArray jsonExpirationDate = (JSONArray) reagentInfo.get("ExpirationDate");
3610 20 Nov 15 olle 2024         JSONArray jsonActive = (JSONArray) reagentInfo.get("Active");
3547 16 Oct 15 olle 2025         JSONArray jsonTimesUsed = (JSONArray) reagentInfo.get("TimesUsed");
3547 16 Oct 15 olle 2026         JSONArray jsonUnusedFpaPlateLocs = (JSONArray) reagentInfo.get("UnusedFpaPlateLocs");
3547 16 Oct 15 olle 2027         JSONArray jsonComment = (JSONArray) reagentInfo.get("Comment");
3547 16 Oct 15 olle 2028
3547 16 Oct 15 olle 2029         DateToStringConverter d1 = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd_HHmm"));
3547 16 Oct 15 olle 2030         DateToStringConverter d2 = new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd HH:mm"));
3547 16 Oct 15 olle 2031         Date now = new Date();
3547 16 Oct 15 olle 2032         String dateStr1 = d1.convert(now);
3547 16 Oct 15 olle 2033         String dateStr2 = d2.convert(now);
3547 16 Oct 15 olle 2034
3547 16 Oct 15 olle 2035         resp.setHeader("Content-Disposition", "attachment; filename=library-preparation-kits-" + kitFilterValue + "-" + dateStr1 + ".csv");
3547 16 Oct 15 olle 2036         resp.setContentType("text/plain");
3547 16 Oct 15 olle 2037         resp.setCharacterEncoding("UTF-8");
3547 16 Oct 15 olle 2038
3871 22 Apr 16 olle 2039         // Get static cache
3871 22 Apr 16 olle 2040         StaticCache cache = Application.getStaticCache();
3871 22 Apr 16 olle 2041         // Store temporary data in static cache
3871 22 Apr 16 olle 2042         String tmpFileName = Application.generateRandomId(64);
3871 22 Apr 16 olle 2043         String cacheKey = tmpFileName;
3871 22 Apr 16 olle 2044         int timeout = 1000;
3871 22 Apr 16 olle 2045         OutputStream cacheIS = cache.write(cacheKey, timeout);
3871 22 Apr 16 olle 2046         
3871 22 Apr 16 olle 2047         OutputStreamWriter fileWriter = new OutputStreamWriter(cacheIS);
3871 22 Apr 16 olle 2048
3547 16 Oct 15 olle 2049         String separator = ";";
3547 16 Oct 15 olle 2050         fileWriter.write("Library preparation kit data " + dateStr2 + "\n");
3547 16 Oct 15 olle 2051         fileWriter.write("Library preparation kit filter: " + kitFilterName + " (" + numItemsFiltered + " of " + numItemsTot + ")" + "\n");
3547 16 Oct 15 olle 2052         //fileWriter.write("TruSight Tumor Sample Preparation Kit (Cat no FC-130-2001, 48 samples, Illumina)" + "\n");
3547 16 Oct 15 olle 2053         fileWriter.write("\n");
3547 16 Oct 15 olle 2054         // Header line
3547 16 Oct 15 olle 2055         String headerLine = "TruSight Tumor Library Preparation partI";
3547 16 Oct 15 olle 2056         headerLine += separator + "TruSight Tumor Library Prep Box 1";
3547 16 Oct 15 olle 2057         headerLine += separator + "TruSight Tumor Index Kit";
3547 16 Oct 15 olle 2058         headerLine += separator + "TruSight Tumor Content Set Box 3";
3547 16 Oct 15 olle 2059         headerLine += separator + "TruSight Tumor Oligo Pool A (FPA)";
3547 16 Oct 15 olle 2060         headerLine += separator + "TruSight Tumor Oligo Pool B (FPB)";
3547 16 Oct 15 olle 2061         headerLine += separator + "Oligo Hybridization for Sequencing Reagent 3 (OHS3)";
3547 16 Oct 15 olle 2062         headerLine += separator + "Stringent Wash 1 (SW1)";
3547 16 Oct 15 olle 2063         headerLine += separator + "Extension Ligation Mix 3 (ELM3)";
3547 16 Oct 15 olle 2064         headerLine += separator + "PCR Master Mix 2 (PMM2)";
3547 16 Oct 15 olle 2065         headerLine += separator + "UB1";
3547 16 Oct 15 olle 2066         headerLine += separator + "TDP";
3547 16 Oct 15 olle 2067         headerLine += separator + "Filterplate";
3547 16 Oct 15 olle 2068         // Index 1 Primers Lot Number        
3547 16 Oct 15 olle 2069         headerLine += separator + "A701";
3547 16 Oct 15 olle 2070         headerLine += separator + "A702";
3547 16 Oct 15 olle 2071         headerLine += separator + "A703";
3547 16 Oct 15 olle 2072         headerLine += separator + "A704";
3547 16 Oct 15 olle 2073         headerLine += separator + "A705";
3547 16 Oct 15 olle 2074         headerLine += separator + "A706";
3547 16 Oct 15 olle 2075         headerLine += separator + "A707";
3547 16 Oct 15 olle 2076         headerLine += separator + "A708";
3547 16 Oct 15 olle 2077         headerLine += separator + "A709";
3547 16 Oct 15 olle 2078         headerLine += separator + "A710";
3547 16 Oct 15 olle 2079         headerLine += separator + "A711";
3547 16 Oct 15 olle 2080         headerLine += separator + "A712";
3547 16 Oct 15 olle 2081         // Index 2 Primers Lot Number        
3547 16 Oct 15 olle 2082         headerLine += separator + "A501";
3547 16 Oct 15 olle 2083         headerLine += separator + "A502";
3547 16 Oct 15 olle 2084         headerLine += separator + "A503";
3547 16 Oct 15 olle 2085         headerLine += separator + "A504";
3547 16 Oct 15 olle 2086         headerLine += separator + "A505";
3547 16 Oct 15 olle 2087         headerLine += separator + "A506";
3547 16 Oct 15 olle 2088         headerLine += separator + "A507";
3547 16 Oct 15 olle 2089         headerLine += separator + "A508";
3547 16 Oct 15 olle 2090         // Extra data
3547 16 Oct 15 olle 2091         headerLine += separator + "Registration date";
3547 16 Oct 15 olle 2092         headerLine += separator + "Last updated";
3547 16 Oct 15 olle 2093         headerLine += separator + "Expiration date";
3610 20 Nov 15 olle 2094         headerLine += separator + "Active";
3547 16 Oct 15 olle 2095         headerLine += separator + "#Times used";
3547 16 Oct 15 olle 2096         headerLine += separator + "Unused FPA plate locations";
3547 16 Oct 15 olle 2097         headerLine += separator + "Comment";
3547 16 Oct 15 olle 2098
3547 16 Oct 15 olle 2099         fileWriter.write(headerLine + "\n");
3547 16 Oct 15 olle 2100
3551 20 Oct 15 olle 2101         int numItems = jsonKitName.size();
3547 16 Oct 15 olle 2102
3547 16 Oct 15 olle 2103         for (int i = 0; i < numItems; i++)
3547 16 Oct 15 olle 2104         {
3610 20 Nov 15 olle 2105           // Convert special variable values to strings
3610 20 Nov 15 olle 2106           String activeStr = "true";
3610 20 Nov 15 olle 2107           if (!(Boolean)jsonActive.get(i))
3610 20 Nov 15 olle 2108           {
3610 20 Nov 15 olle 2109             activeStr = "false";
3610 20 Nov 15 olle 2110           }
3610 20 Nov 15 olle 2111           String timesUsedStr = "" + jsonTimesUsed.get(i);
3610 20 Nov 15 olle 2112
3551 20 Oct 15 olle 2113           String line = (String) jsonKitName.get(i);          
3547 16 Oct 15 olle 2114           line += separator + (String) jsonLibraryPrepBox1.get(i);          
3547 16 Oct 15 olle 2115           line += separator + (String) jsonIndexKit.get(i);          
3547 16 Oct 15 olle 2116           line += separator + (String) jsonContentSetBox3.get(i);          
3547 16 Oct 15 olle 2117           line += separator + (String) jsonOligoPoolA_FPA.get(i);          
3547 16 Oct 15 olle 2118           line += separator + (String) jsonOligoPoolB_FPB.get(i);          
3547 16 Oct 15 olle 2119           line += separator + (String) jsonOligoHybridizationSequencingReagent3.get(i);          
3547 16 Oct 15 olle 2120           line += separator + (String) jsonStringentWash1_SW1.get(i);          
3547 16 Oct 15 olle 2121           line += separator + (String) jsonExtensionLigationMix3.get(i);          
3547 16 Oct 15 olle 2122           line += separator + (String) jsonPcrMasterMix2_PMM2.get(i);          
3547 16 Oct 15 olle 2123           line += separator + (String) jsonUB1.get(i);          
3547 16 Oct 15 olle 2124           line += separator + (String) jsonTDP.get(i);          
3547 16 Oct 15 olle 2125           line += separator + (String) jsonFilterplate.get(i);          
3547 16 Oct 15 olle 2126           // Index 1 Primers Lot Number        
3547 16 Oct 15 olle 2127           line += separator + (String) jsonIndexPrimerA701.get(i);          
3547 16 Oct 15 olle 2128           line += separator + (String) jsonIndexPrimerA702.get(i);          
3547 16 Oct 15 olle 2129           line += separator + (String) jsonIndexPrimerA703.get(i);          
3547 16 Oct 15 olle 2130           line += separator + (String) jsonIndexPrimerA704.get(i);          
3547 16 Oct 15 olle 2131           line += separator + (String) jsonIndexPrimerA705.get(i);          
3547 16 Oct 15 olle 2132           line += separator + (String) jsonIndexPrimerA706.get(i);          
3547 16 Oct 15 olle 2133           line += separator + (String) jsonIndexPrimerA707.get(i);          
3547 16 Oct 15 olle 2134           line += separator + (String) jsonIndexPrimerA708.get(i);          
3547 16 Oct 15 olle 2135           line += separator + (String) jsonIndexPrimerA709.get(i);          
3547 16 Oct 15 olle 2136           line += separator + (String) jsonIndexPrimerA710.get(i);          
3547 16 Oct 15 olle 2137           line += separator + (String) jsonIndexPrimerA711.get(i);          
3547 16 Oct 15 olle 2138           line += separator + (String) jsonIndexPrimerA712.get(i);          
3547 16 Oct 15 olle 2139           // Index 2 Primers Lot Number        
3547 16 Oct 15 olle 2140           line += separator + (String) jsonIndexPrimerA501.get(i);          
3547 16 Oct 15 olle 2141           line += separator + (String) jsonIndexPrimerA502.get(i);          
3547 16 Oct 15 olle 2142           line += separator + (String) jsonIndexPrimerA503.get(i);          
3547 16 Oct 15 olle 2143           line += separator + (String) jsonIndexPrimerA504.get(i);          
3547 16 Oct 15 olle 2144           line += separator + (String) jsonIndexPrimerA505.get(i);          
3547 16 Oct 15 olle 2145           line += separator + (String) jsonIndexPrimerA506.get(i);          
3547 16 Oct 15 olle 2146           line += separator + (String) jsonIndexPrimerA507.get(i);          
3547 16 Oct 15 olle 2147           line += separator + (String) jsonIndexPrimerA508.get(i);
3547 16 Oct 15 olle 2148           // Extra data
3547 16 Oct 15 olle 2149           line += separator + (String) jsonRegistrationDate.get(i);
3547 16 Oct 15 olle 2150           line += separator + (String) jsonLastUpdatedDate.get(i);
3547 16 Oct 15 olle 2151           line += separator + (String) jsonExpirationDate.get(i);
3610 20 Nov 15 olle 2152           line += separator + activeStr;
3610 20 Nov 15 olle 2153           line += separator + timesUsedStr;
3547 16 Oct 15 olle 2154           line += separator + (String) jsonUnusedFpaPlateLocs.get(i);
3547 16 Oct 15 olle 2155           line += separator + (String) jsonComment.get(i);
3547 16 Oct 15 olle 2156
3547 16 Oct 15 olle 2157           fileWriter.write(line + "\n");
3547 16 Oct 15 olle 2158         }
3547 16 Oct 15 olle 2159         fileWriter.flush();
3547 16 Oct 15 olle 2160         fileWriter.close();
3547 16 Oct 15 olle 2161
3871 22 Apr 16 olle 2162         // Return cache key
3547 16 Oct 15 olle 2163         PrintWriter out = resp.getWriter();
3871 22 Apr 16 olle 2164         out.print(cacheKey);        
3547 16 Oct 15 olle 2165         out.flush();
3547 16 Oct 15 olle 2166         out.close();
3547 16 Oct 15 olle 2167         jsonMessages.add("no comment");
3547 16 Oct 15 olle 2168       }
3624 25 Nov 15 olle 2169       else if ("PrepareDownloadLibPrepFile".equals(cmd))
3624 25 Nov 15 olle 2170       {
3624 25 Nov 15 olle 2171         dc = sc.newDbControl();
3624 25 Nov 15 olle 2172
3624 25 Nov 15 olle 2173         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3624 25 Nov 15 olle 2174
3624 25 Nov 15 olle 2175         String referenceName = req.getParameter("referenceName");
3624 25 Nov 15 olle 2176
3624 25 Nov 15 olle 2177         // Reference name is plate name, remove extensions and convert to lower case
3624 25 Nov 15 olle 2178         String sourceRefName = referenceName;
3624 25 Nov 15 olle 2179         int dotIndex = referenceName.indexOf(".");
3624 25 Nov 15 olle 2180         if (dotIndex >= 0)
3624 25 Nov 15 olle 2181         {
3624 25 Nov 15 olle 2182           // Remove extension
3624 25 Nov 15 olle 2183           sourceRefName = referenceName.substring(0, dotIndex);
3624 25 Nov 15 olle 2184         }
3624 25 Nov 15 olle 2185         sourceRefName = sourceRefName.toLowerCase();
3624 25 Nov 15 olle 2186         String filename = sourceRefName + ".amp28" + ".plt";
3624 25 Nov 15 olle 2187         resp.setHeader("Content-Disposition", "attachment; filename=" + filename);
3624 25 Nov 15 olle 2188         resp.setContentType("text/plain");
3624 25 Nov 15 olle 2189         resp.setCharacterEncoding("UTF-8");
3624 25 Nov 15 olle 2190
3871 22 Apr 16 olle 2191         // Get static cache
3871 22 Apr 16 olle 2192         StaticCache cache = Application.getStaticCache();
3871 22 Apr 16 olle 2193         // Store temporary data in static cache
3871 22 Apr 16 olle 2194         String tmpFileName = Application.generateRandomId(64);
3871 22 Apr 16 olle 2195         String cacheKey = tmpFileName;
3871 22 Apr 16 olle 2196         int timeout = 1000;
3871 22 Apr 16 olle 2197         OutputStream cacheIS = cache.write(cacheKey, timeout);
3871 22 Apr 16 olle 2198         
3871 22 Apr 16 olle 2199         OutputStreamWriter fileWriter = new OutputStreamWriter(cacheIS);
3871 22 Apr 16 olle 2200
3624 25 Nov 15 olle 2201         String separator = "\t";
3624 25 Nov 15 olle 2202         String endline = "\r\n";
3624 25 Nov 15 olle 2203         // Header data
3624 25 Nov 15 olle 2204         int version = 2;
3624 25 Nov 15 olle 2205         String assay = "TruSight Tumor";
3624 25 Nov 15 olle 2206         int indexReads = 2;
3624 25 Nov 15 olle 2207         int indexCycles = 8;
3624 25 Nov 15 olle 2208         // Header lines
3624 25 Nov 15 olle 2209         fileWriter.write("Version" + separator + version + endline);
3624 25 Nov 15 olle 2210         fileWriter.write("ID" + separator + sourceRefName + endline);
3624 25 Nov 15 olle 2211         fileWriter.write("Assay" + separator + assay + endline);
3624 25 Nov 15 olle 2212         fileWriter.write("IndexReads" + separator + indexReads + endline);
3624 25 Nov 15 olle 2213         fileWriter.write("IndexCycles" + separator + indexCycles + endline);
3624 25 Nov 15 olle 2214
3624 25 Nov 15 olle 2215         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3624 25 Nov 15 olle 2216         JSONObject jsonPlate = (JSONObject)jsonReq.get("bioplate");
3624 25 Nov 15 olle 2217         
3624 25 Nov 15 olle 2218         String plateName = (String)jsonPlate.get("name");
3624 25 Nov 15 olle 2219         JSONArray jsonWells = (JSONArray)jsonPlate.get("wells");
3624 25 Nov 15 olle 2220
3624 25 Nov 15 olle 2221         // JSONArray jsonWells only contains date for wells with DNA extracts
3624 25 Nov 15 olle 2222         HashMap<String,String> wellStringDnaNameHashMap = new HashMap<String,String>();
3624 25 Nov 15 olle 2223         for (int i = 0; i < jsonWells.size(); i++)
3624 25 Nov 15 olle 2224         {
3624 25 Nov 15 olle 2225           JSONObject jsonWell = (JSONObject)jsonWells.get(i);
3624 25 Nov 15 olle 2226           JSONObject jsonDna = (JSONObject)jsonWell.get("dna");
3624 25 Nov 15 olle 2227           
3624 25 Nov 15 olle 2228           Number rowNum = (Number)jsonWell.get("row");
3624 25 Nov 15 olle 2229           Number colNum = (Number)jsonWell.get("column");          
3624 25 Nov 15 olle 2230           Number dnaId = (Number)jsonDna.get("id");
3624 25 Nov 15 olle 2231
3624 25 Nov 15 olle 2232           // Get well location string = Row letter + two-character column number (1-based, with optional initial zero)
3624 25 Nov 15 olle 2233           int row = rowNum.intValue();
3624 25 Nov 15 olle 2234           int col = colNum.intValue();
3624 25 Nov 15 olle 2235           String rowLetter = indexToRowLetterHashMap.get(row);
3624 25 Nov 15 olle 2236           String optionalInitialZero = "";
3624 25 Nov 15 olle 2237           if (col < 9)
3624 25 Nov 15 olle 2238           {
3624 25 Nov 15 olle 2239             optionalInitialZero = "0";
3624 25 Nov 15 olle 2240           }
3624 25 Nov 15 olle 2241           String wellStr = rowLetter + optionalInitialZero + (col + 1);
3624 25 Nov 15 olle 2242
3624 25 Nov 15 olle 2243           if (dnaId != null)
3624 25 Nov 15 olle 2244           {
3624 25 Nov 15 olle 2245             Extract dna = Extract.getById(dc, dnaId.intValue());
3624 25 Nov 15 olle 2246             String dnaName = dna.getName();
3624 25 Nov 15 olle 2247             // Only use part before first optional dot "."
3624 25 Nov 15 olle 2248             int firstDotIndex = dnaName.indexOf(".");
3624 25 Nov 15 olle 2249             if (firstDotIndex >= 0)
3624 25 Nov 15 olle 2250             {
3624 25 Nov 15 olle 2251               // Remove extension
3624 25 Nov 15 olle 2252               dnaName = dnaName.substring(0, firstDotIndex);
3624 25 Nov 15 olle 2253             }
3624 25 Nov 15 olle 2254             // Convert to upper-case letters
3624 25 Nov 15 olle 2255             dnaName = dnaName.toUpperCase();
3624 25 Nov 15 olle 2256             // Add DNA name to hash map with well string as key
3624 25 Nov 15 olle 2257             wellStringDnaNameHashMap.put(wellStr, dnaName);
3624 25 Nov 15 olle 2258           }
3624 25 Nov 15 olle 2259         }
3624 25 Nov 15 olle 2260
3624 25 Nov 15 olle 2261         // List wells row by row, starting from top
3624 25 Nov 15 olle 2262         for (int row = 0; row < 8; row++)
3624 25 Nov 15 olle 2263         {
3624 25 Nov 15 olle 2264           for (int col = 0; col < 12; col++)
3624 25 Nov 15 olle 2265           {
3624 25 Nov 15 olle 2266             // Get well location string = Row letter + two-character column number (1-based, with optional initial zero)
3624 25 Nov 15 olle 2267             String rowLetter = indexToRowLetterHashMap.get(row);
3624 25 Nov 15 olle 2268             String optionalInitialZero = "";
3624 25 Nov 15 olle 2269             if (col < 9)
3624 25 Nov 15 olle 2270             {
3624 25 Nov 15 olle 2271               optionalInitialZero = "0";
3624 25 Nov 15 olle 2272             }
3624 25 Nov 15 olle 2273             String wellStr = rowLetter + optionalInitialZero + (col + 1);
3624 25 Nov 15 olle 2274
3624 25 Nov 15 olle 2275             // Get aliquot suffix "FPA" or "FPB", depending on which side of the plate the well is located
3624 25 Nov 15 olle 2276             String suffix = "FPA";
3624 25 Nov 15 olle 2277             if (col > 5)
3624 25 Nov 15 olle 2278             {
3624 25 Nov 15 olle 2279               suffix = "FPB";
3624 25 Nov 15 olle 2280             }
3624 25 Nov 15 olle 2281             String dnaName = "";
3624 25 Nov 15 olle 2282             String aliquotName = "";
3624 25 Nov 15 olle 2283             String index1Primer = "";
3624 25 Nov 15 olle 2284             String index2Primer = "";
3624 25 Nov 15 olle 2285             String manifestName = "";
3624 25 Nov 15 olle 2286             if (wellStringDnaNameHashMap.containsKey(wellStr))
3624 25 Nov 15 olle 2287             {
3624 25 Nov 15 olle 2288               dnaName = (String) wellStringDnaNameHashMap.get(wellStr);
3624 25 Nov 15 olle 2289               aliquotName = dnaName + suffix;
3624 25 Nov 15 olle 2290               // Get index 1 primer
3624 25 Nov 15 olle 2291               index1Primer = "A70";
3624 25 Nov 15 olle 2292               if (col > 8)
3624 25 Nov 15 olle 2293               {
3624 25 Nov 15 olle 2294                 index1Primer = "A7";
3624 25 Nov 15 olle 2295               }
3624 25 Nov 15 olle 2296               index1Primer += (col + 1);
3624 25 Nov 15 olle 2297               // Get index 2 primer
3624 25 Nov 15 olle 2298               index2Primer = "A50" + (row + 1);
3624 25 Nov 15 olle 2299               // Get manifest name
3624 25 Nov 15 olle 2300               manifestName = "TruSightTumor-" + suffix + "-Manifest_RevB";
3624 25 Nov 15 olle 2301             }
3624 25 Nov 15 olle 2302
3624 25 Nov 15 olle 2303             // Write info line for well
3624 25 Nov 15 olle 2304             String wellLine = wellStr + separator + aliquotName + separator + index1Primer + separator + index2Primer + separator + manifestName + separator + separator + separator + dnaName + endline;
3624 25 Nov 15 olle 2305             fileWriter.write(wellLine);
3624 25 Nov 15 olle 2306           }
3624 25 Nov 15 olle 2307         }
3624 25 Nov 15 olle 2308         fileWriter.write("[AssaySettings]" + endline);
3624 25 Nov 15 olle 2309         fileWriter.flush();
3624 25 Nov 15 olle 2310         fileWriter.close();
3624 25 Nov 15 olle 2311
3871 22 Apr 16 olle 2312         jsonMessages.add("no comment");
3871 22 Apr 16 olle 2313         // Return cache key
3624 25 Nov 15 olle 2314         PrintWriter out = resp.getWriter();
3871 22 Apr 16 olle 2315         out.print(cacheKey);        
3624 25 Nov 15 olle 2316         out.flush();
3624 25 Nov 15 olle 2317         out.close();
3624 25 Nov 15 olle 2318         jsonMessages.add("no comment");
3624 25 Nov 15 olle 2319       }
3627 26 Nov 15 olle 2320       else if ("RegisterKits".equals(cmd))
3538 13 Oct 15 olle 2321       {
3546 16 Oct 15 olle 2322 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" Start");
3538 13 Oct 15 olle 2323         dc = sc.newDbControl();
3538 13 Oct 15 olle 2324
3538 13 Oct 15 olle 2325         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3538 13 Oct 15 olle 2326
3560 27 Oct 15 olle 2327         boolean overwrite = Values.getBoolean(req.getParameter("overwrite"), false);
3573 05 Nov 15 olle 2328         boolean update = Values.getBoolean(req.getParameter("update"), false);
3573 05 Nov 15 olle 2329 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" overwrite = " + overwrite + " update = " + update);
3573 05 Nov 15 olle 2330         String referenceName = req.getParameter("referenceName");
3560 27 Oct 15 olle 2331
3546 16 Oct 15 olle 2332         DateToStringConverter d = new DateToStringConverter(new SimpleDateFormat("yyyyMMdd"));
3610 20 Nov 15 olle 2333         StringToDateConverter stringToDate = new StringToDateConverter(new SimpleDateFormat("yyyyMMdd"));
3546 16 Oct 15 olle 2334         Date now = new Date();
3546 16 Oct 15 olle 2335         String registrationDate = d.convert(now);
3610 20 Nov 15 olle 2336         Date lastUpdatedDate = now;
3546 16 Oct 15 olle 2337         
3538 13 Oct 15 olle 2338         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3538 13 Oct 15 olle 2339
3538 13 Oct 15 olle 2340         // Get JSON data on reagent lot numbers from POST data
3538 13 Oct 15 olle 2341         JSONObject reagentInfo = jsonReq;
3626 26 Nov 15 olle 2342         // Check if any messages should be transferred
3626 26 Nov 15 olle 2343         JSONArray jsonTransferredMessages = (JSONArray) reagentInfo.get("transferredMessages");
3626 26 Nov 15 olle 2344         if (jsonTransferredMessages != null)
3626 26 Nov 15 olle 2345         {
3626 26 Nov 15 olle 2346           for (int i = 0; i < jsonTransferredMessages.size(); i++)
3626 26 Nov 15 olle 2347           {
3626 26 Nov 15 olle 2348             String message = (String) jsonTransferredMessages.get(i);
3626 26 Nov 15 olle 2349             jsonMessages.add(message);
3626 26 Nov 15 olle 2350           }
3626 26 Nov 15 olle 2351         }
3573 05 Nov 15 olle 2352         //String referenceName = (String) reagentInfo.get("referenceName");
3610 20 Nov 15 olle 2353         Date expirationDate = (Date) stringToDate.convert((String)reagentInfo.get("expirationDate"));
3626 26 Nov 15 olle 2354 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" referenceName = \"" + referenceName + "\" expirationDate = \"" + expirationDate + "\"");
3610 20 Nov 15 olle 2355         boolean active = true;
3560 27 Oct 15 olle 2356         int numTimesUsed = 0;
3560 27 Oct 15 olle 2357         String unusedFpaPlateLocs = "A1,B1,C1,D1,E1,F1,G1,H1";
3560 27 Oct 15 olle 2358         unusedFpaPlateLocs += "," + "A2,B2,C2,D2,E2,F2,G2,H2";
3560 27 Oct 15 olle 2359         unusedFpaPlateLocs += "," + "A3,B3,C3,D3,E3,F3,G3,H3";
3560 27 Oct 15 olle 2360         unusedFpaPlateLocs += "," + "A4,B4,C4,D4,E4,F4,G4,H4";
3560 27 Oct 15 olle 2361         unusedFpaPlateLocs += "," + "A5,B5,C5,D5,E5,F5,G5,H5";
3560 27 Oct 15 olle 2362         unusedFpaPlateLocs += "," + "A6,B6,C6,D6,E6,F6,G6,H6";
3560 27 Oct 15 olle 2363         String kitComment = "";
3573 05 Nov 15 olle 2364
3573 05 Nov 15 olle 2365         if (update)
3573 05 Nov 15 olle 2366         {
3573 05 Nov 15 olle 2367           // Get list of reserved wells
3573 05 Nov 15 olle 2368           JSONObject jsonPlate = (JSONObject)jsonReq.get("bioplate");
3573 05 Nov 15 olle 2369           JSONArray jsonWells = (JSONArray)jsonPlate.get("wells");
3573 05 Nov 15 olle 2370           List<String> reservedWellsList = new ArrayList<String>();
3573 05 Nov 15 olle 2371           for (int i = 0; i < jsonWells.size(); i++)
3573 05 Nov 15 olle 2372           {
3573 05 Nov 15 olle 2373             JSONObject jsonWell = (JSONObject)jsonWells.get(i);
3573 05 Nov 15 olle 2374             JSONObject jsonDna = (JSONObject)jsonWell.get("dna");
3573 05 Nov 15 olle 2375             JSONObject jsonBarcode = (JSONObject)jsonDna.get("barcode");
3573 05 Nov 15 olle 2376             
3573 05 Nov 15 olle 2377             Number rowNum = (Number)jsonWell.get("row");
3573 05 Nov 15 olle 2378             Number colNum = (Number)jsonWell.get("column");
3573 05 Nov 15 olle 2379             int row = rowNum.intValue();
3573 05 Nov 15 olle 2380             int col = colNum.intValue();
3573 05 Nov 15 olle 2381             String wellStr = indexToRowLetterHashMap.get(row) + (col + 1);
3573 05 Nov 15 olle 2382             reservedWellsList.add(wellStr);
3573 05 Nov 15 olle 2383           }
3573 05 Nov 15 olle 2384 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" referenceName = \"" + referenceName + "\" jsonWells.size() = " + jsonWells.size() + " reservedWellsList = " + reservedWellsList);
3573 05 Nov 15 olle 2385
3573 05 Nov 15 olle 2386           // Get jsonConsumables JSONObject for single updated kit
3610 20 Nov 15 olle 2387           reagentInfo = fetchJsonKitDataForUpdate(dc, referenceName, reservedWellsList);
3573 05 Nov 15 olle 2388         }
3538 13 Oct 15 olle 2389         JSONArray jsonHeaders = (JSONArray) reagentInfo.get("Headers");
3538 13 Oct 15 olle 2390
3551 20 Oct 15 olle 2391         JSONArray jsonKitName = (JSONArray) reagentInfo.get("KitName");
3538 13 Oct 15 olle 2392         JSONArray jsonLibraryPrepBox1 = (JSONArray) reagentInfo.get("LibraryPrepBox1");
3538 13 Oct 15 olle 2393         JSONArray jsonIndexKit = (JSONArray) reagentInfo.get("IndexKit");
3538 13 Oct 15 olle 2394         JSONArray jsonContentSetBox3 = (JSONArray) reagentInfo.get("ContentSetBox3");
3538 13 Oct 15 olle 2395         JSONArray jsonOligoPoolA_FPA = (JSONArray) reagentInfo.get("OligoPoolA_FPA");
3538 13 Oct 15 olle 2396         JSONArray jsonOligoPoolB_FPB = (JSONArray) reagentInfo.get("OligoPoolB_FPB");
3538 13 Oct 15 olle 2397         JSONArray jsonOligoHybridizationSequencingReagent3 = (JSONArray) reagentInfo.get("OligoHybridizationSequencingReagent3");
3538 13 Oct 15 olle 2398         JSONArray jsonStringentWash1_SW1 = (JSONArray) reagentInfo.get("StringentWash1_SW1");
3538 13 Oct 15 olle 2399         JSONArray jsonExtensionLigationMix3 = (JSONArray) reagentInfo.get("ExtensionLigationMix3");
3538 13 Oct 15 olle 2400         JSONArray jsonPcrMasterMix2_PMM2 = (JSONArray) reagentInfo.get("PcrMasterMix2_PMM2");
3538 13 Oct 15 olle 2401         JSONArray jsonUB1 = (JSONArray) reagentInfo.get("UB1");
3538 13 Oct 15 olle 2402         JSONArray jsonTDP = (JSONArray) reagentInfo.get("TDP");
3538 13 Oct 15 olle 2403         JSONArray jsonFilterplate = (JSONArray) reagentInfo.get("Filterplate");
3538 13 Oct 15 olle 2404         
3538 13 Oct 15 olle 2405         JSONArray jsonIndexPrimerA701 = (JSONArray) reagentInfo.get("IndexPrimerA701");
3538 13 Oct 15 olle 2406         JSONArray jsonIndexPrimerA702 = (JSONArray) reagentInfo.get("IndexPrimerA702");
3538 13 Oct 15 olle 2407         JSONArray jsonIndexPrimerA703 = (JSONArray) reagentInfo.get("IndexPrimerA703");
3538 13 Oct 15 olle 2408         JSONArray jsonIndexPrimerA704 = (JSONArray) reagentInfo.get("IndexPrimerA704");
3538 13 Oct 15 olle 2409         JSONArray jsonIndexPrimerA705 = (JSONArray) reagentInfo.get("IndexPrimerA705");
3538 13 Oct 15 olle 2410         JSONArray jsonIndexPrimerA706 = (JSONArray) reagentInfo.get("IndexPrimerA706");
3538 13 Oct 15 olle 2411         JSONArray jsonIndexPrimerA707 = (JSONArray) reagentInfo.get("IndexPrimerA707");
3538 13 Oct 15 olle 2412         JSONArray jsonIndexPrimerA708 = (JSONArray) reagentInfo.get("IndexPrimerA708");
3538 13 Oct 15 olle 2413         JSONArray jsonIndexPrimerA709 = (JSONArray) reagentInfo.get("IndexPrimerA709");
3538 13 Oct 15 olle 2414         JSONArray jsonIndexPrimerA710 = (JSONArray) reagentInfo.get("IndexPrimerA710");
3538 13 Oct 15 olle 2415         JSONArray jsonIndexPrimerA711 = (JSONArray) reagentInfo.get("IndexPrimerA711");
3538 13 Oct 15 olle 2416         JSONArray jsonIndexPrimerA712 = (JSONArray) reagentInfo.get("IndexPrimerA712");
3538 13 Oct 15 olle 2417         
3538 13 Oct 15 olle 2418         JSONArray jsonIndexPrimerA501 = (JSONArray) reagentInfo.get("IndexPrimerA501");
3538 13 Oct 15 olle 2419         JSONArray jsonIndexPrimerA502 = (JSONArray) reagentInfo.get("IndexPrimerA502");
3538 13 Oct 15 olle 2420         JSONArray jsonIndexPrimerA503 = (JSONArray) reagentInfo.get("IndexPrimerA503");
3538 13 Oct 15 olle 2421         JSONArray jsonIndexPrimerA504 = (JSONArray) reagentInfo.get("IndexPrimerA504");
3538 13 Oct 15 olle 2422         JSONArray jsonIndexPrimerA505 = (JSONArray) reagentInfo.get("IndexPrimerA505");
3538 13 Oct 15 olle 2423         JSONArray jsonIndexPrimerA506 = (JSONArray) reagentInfo.get("IndexPrimerA506");
3538 13 Oct 15 olle 2424         JSONArray jsonIndexPrimerA507 = (JSONArray) reagentInfo.get("IndexPrimerA507");
3538 13 Oct 15 olle 2425         JSONArray jsonIndexPrimerA508 = (JSONArray) reagentInfo.get("IndexPrimerA508");
3538 13 Oct 15 olle 2426
3560 27 Oct 15 olle 2427         JSONArray jsonRegistrationDate = (JSONArray) reagentInfo.get("RegistrationDate");
3560 27 Oct 15 olle 2428         JSONArray jsonLastUpdatedDate = (JSONArray) reagentInfo.get("LastUpdatedDate");
3560 27 Oct 15 olle 2429         JSONArray jsonExpirationDate = (JSONArray) reagentInfo.get("ExpirationDate");
3610 20 Nov 15 olle 2430         JSONArray jsonActive = (JSONArray) reagentInfo.get("Active");
3560 27 Oct 15 olle 2431         JSONArray jsonTimesUsed = (JSONArray) reagentInfo.get("TimesUsed");
3560 27 Oct 15 olle 2432         JSONArray jsonUnusedFpaPlateLocs = (JSONArray) reagentInfo.get("UnusedFpaPlateLocs");
3560 27 Oct 15 olle 2433         JSONArray jsonComment = (JSONArray) reagentInfo.get("Comment");
3560 27 Oct 15 olle 2434
3560 27 Oct 15 olle 2435         List<String> newKitNameList = new ArrayList<String>();
3560 27 Oct 15 olle 2436         for (int i = 0; i < jsonKitName.size(); i++)
3560 27 Oct 15 olle 2437         {
3560 27 Oct 15 olle 2438           String kitName = (String) jsonKitName.get(i);
3560 27 Oct 15 olle 2439           newKitNameList.add(kitName);
3560 27 Oct 15 olle 2440         }
3560 27 Oct 15 olle 2441 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" newKitNameList = " + newKitNameList);
3538 13 Oct 15 olle 2442         List<String> reagentsDataLineList = new ArrayList<String>();
3538 13 Oct 15 olle 2443         List<String> kitNameList = new ArrayList<String>();
3538 13 Oct 15 olle 2444         List<String> addedKitNamesList = new ArrayList<String>();
3610 20 Nov 15 olle 2445
3610 20 Nov 15 olle 2446         // Check if kit name already is registered
3610 20 Nov 15 olle 2447         ItemSubtype kitSubtype = Subtype.LIBRARY_PREPARATION_KIT.load(dc);
3610 20 Nov 15 olle 2448
3610 20 Nov 15 olle 2449         for (int i = 0; i < newKitNameList.size(); i++)
3538 13 Oct 15 olle 2450         {
3610 20 Nov 15 olle 2451           String kitName = (String) newKitNameList.get(i);
3610 20 Nov 15 olle 2452           // Find kits of the given kit subtype with specified 'name'
3610 20 Nov 15 olle 2453           Kit kit = null;
3610 20 Nov 15 olle 2454           ItemQuery<Kit> query = Kit.getQuery();
3610 20 Nov 15 olle 2455           query.setIncludes(Meludi.INCLUDE_IN_CURRENT_PROJECT);
3610 20 Nov 15 olle 2456           query.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(kitName)));
3610 20 Nov 15 olle 2457           Subtype.LIBRARY_PREPARATION_KIT.addFilter(dc, query);
3610 20 Nov 15 olle 2458           List<Kit> kits = query.list(dc);
3610 20 Nov 15 olle 2459           if (kits != null && kits.size() > 0)
3538 13 Oct 15 olle 2460           {
3647 04 Dec 15 olle 2461             // Previous kit with current name exists
3647 04 Dec 15 olle 2462             if (overwrite)
3610 20 Nov 15 olle 2463             {
3647 04 Dec 15 olle 2464               kit = (Kit) kits.get(0);
3647 04 Dec 15 olle 2465               if (kits.size() > 1)
3647 04 Dec 15 olle 2466               {
3647 04 Dec 15 olle 2467                 System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" Number of stored kits with name \"" + kitName + "\" = " + kits.size());            
3647 04 Dec 15 olle 2468               }
3610 20 Nov 15 olle 2469             }
3538 13 Oct 15 olle 2470           }
3610 20 Nov 15 olle 2471           else
3538 13 Oct 15 olle 2472           {
3647 04 Dec 15 olle 2473             // No previous kit with current name exists
3610 20 Nov 15 olle 2474             kit = Kit.getNew(dc);
3610 20 Nov 15 olle 2475             kit.setItemSubtype(kitSubtype);
3610 20 Nov 15 olle 2476             kit.setName(kitName);
3610 20 Nov 15 olle 2477             dc.saveItem(kit);
3538 13 Oct 15 olle 2478           }
3610 20 Nov 15 olle 2479 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" kitName = \"" + kitName + "\"  kit = " + kit);
3647 04 Dec 15 olle 2480           if (kit != null)
3647 04 Dec 15 olle 2481           {
3647 04 Dec 15 olle 2482             Annotationtype.LIBPREPKIT_LIB_PREP_BOX1.setAnnotationValue(dc, kit, (String) jsonLibraryPrepBox1.get(i));
3647 04 Dec 15 olle 2483             Annotationtype.LIBPREPKIT_LIB_INDEX_KIT.setAnnotationValue(dc, kit, (String) jsonIndexKit.get(i));
3647 04 Dec 15 olle 2484             Annotationtype.LIBPREPKIT_CONTENT_SET_BOX3.setAnnotationValue(dc, kit, (String) jsonContentSetBox3.get(i));
3647 04 Dec 15 olle 2485             Annotationtype.LIBPREPKIT_OLIGO_POOL_A_FPA.setAnnotationValue(dc, kit, (String) jsonOligoPoolA_FPA.get(i));
3647 04 Dec 15 olle 2486             Annotationtype.LIBPREPKIT_OLIGO_POOL_B_FPB.setAnnotationValue(dc, kit, (String) jsonOligoPoolB_FPB.get(i));
3647 04 Dec 15 olle 2487             Annotationtype.LIBPREPKIT_OLIGO_HYBR_SEQ_REAGENT3_OHS3.setAnnotationValue(dc, kit, (String) jsonOligoHybridizationSequencingReagent3.get(i));
3647 04 Dec 15 olle 2488             Annotationtype.LIBPREPKIT_STRINGENT_WASH1_SW1.setAnnotationValue(dc, kit, (String) jsonStringentWash1_SW1.get(i));
3647 04 Dec 15 olle 2489             Annotationtype.LIBPREPKIT_EXTENSION_LIGATION_MIX3_ELM3.setAnnotationValue(dc, kit, (String) jsonExtensionLigationMix3.get(i));
3647 04 Dec 15 olle 2490             Annotationtype.LIBPREPKIT_PCR_MASTER_MIX2_PMM2.setAnnotationValue(dc, kit, (String) jsonPcrMasterMix2_PMM2.get(i));
3647 04 Dec 15 olle 2491             Annotationtype.LIBPREPKIT_UB1.setAnnotationValue(dc, kit, (String) jsonUB1.get(i));
3647 04 Dec 15 olle 2492             Annotationtype.LIBPREPKIT_TDP.setAnnotationValue(dc, kit, (String) jsonTDP.get(i));
3647 04 Dec 15 olle 2493             Annotationtype.LIBPREPKIT_FILTER_PLATE.setAnnotationValue(dc, kit, (String) jsonFilterplate.get(i));
3647 04 Dec 15 olle 2494             // Index 1 Primers Lot Number        
3647 04 Dec 15 olle 2495             Annotationtype.LIBPREPKIT_A701.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA701.get(i));
3647 04 Dec 15 olle 2496             Annotationtype.LIBPREPKIT_A702.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA702.get(i));
3647 04 Dec 15 olle 2497             Annotationtype.LIBPREPKIT_A703.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA703.get(i));
3647 04 Dec 15 olle 2498             Annotationtype.LIBPREPKIT_A704.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA704.get(i));
3647 04 Dec 15 olle 2499             Annotationtype.LIBPREPKIT_A705.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA705.get(i));
3647 04 Dec 15 olle 2500             Annotationtype.LIBPREPKIT_A706.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA706.get(i));
3647 04 Dec 15 olle 2501             Annotationtype.LIBPREPKIT_A707.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA707.get(i));
3647 04 Dec 15 olle 2502             Annotationtype.LIBPREPKIT_A708.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA708.get(i));
3647 04 Dec 15 olle 2503             Annotationtype.LIBPREPKIT_A709.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA709.get(i));
3647 04 Dec 15 olle 2504             Annotationtype.LIBPREPKIT_A710.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA710.get(i));
3647 04 Dec 15 olle 2505             Annotationtype.LIBPREPKIT_A711.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA711.get(i));
3647 04 Dec 15 olle 2506             Annotationtype.LIBPREPKIT_A712.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA712.get(i));
3647 04 Dec 15 olle 2507             // Index 2 Primers Lot Number        
3647 04 Dec 15 olle 2508             Annotationtype.LIBPREPKIT_A501.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA501.get(i));
3647 04 Dec 15 olle 2509             Annotationtype.LIBPREPKIT_A502.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA502.get(i));
3647 04 Dec 15 olle 2510             Annotationtype.LIBPREPKIT_A503.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA503.get(i));
3647 04 Dec 15 olle 2511             Annotationtype.LIBPREPKIT_A504.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA504.get(i));
3647 04 Dec 15 olle 2512             Annotationtype.LIBPREPKIT_A505.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA505.get(i));
3647 04 Dec 15 olle 2513             Annotationtype.LIBPREPKIT_A506.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA506.get(i));
3647 04 Dec 15 olle 2514             Annotationtype.LIBPREPKIT_A507.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA507.get(i));
3647 04 Dec 15 olle 2515             Annotationtype.LIBPREPKIT_A508.setAnnotationValue(dc, kit, (String) jsonIndexPrimerA508.get(i));
3610 20 Nov 15 olle 2516
3647 04 Dec 15 olle 2517             // New data
3647 04 Dec 15 olle 2518             if (overwrite)
3647 04 Dec 15 olle 2519             {
3647 04 Dec 15 olle 2520               // Get values from supplied reagent data
3647 04 Dec 15 olle 2521               registrationDate = (String) jsonRegistrationDate.get(i);
3573 05 Nov 15 olle 2522 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" i = " + i + " kitName = " + kitName + " registrationDate = " + registrationDate);
3647 04 Dec 15 olle 2523               // Last updated date is not copied, but set to current date
3647 04 Dec 15 olle 2524               // Expiration date
3647 04 Dec 15 olle 2525               expirationDate = (Date) stringToDate.convert((String)jsonExpirationDate.get(i));
3573 05 Nov 15 olle 2526 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" i = " + i + " kitName = " + kitName + " expirationDate = " + expirationDate);
3647 04 Dec 15 olle 2527               // Active
3647 04 Dec 15 olle 2528               active = (Boolean) jsonActive.get(i);
3610 20 Nov 15 olle 2529 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" i = " + i + " kitName = " + kitName + " active = " + active);
3647 04 Dec 15 olle 2530               // Number of times used
3573 05 Nov 15 olle 2531 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" i = " + i + " kitName = " + kitName + " jsonTimesUsed.get(i) = " + jsonTimesUsed.get(i));
3647 04 Dec 15 olle 2532               numTimesUsed = Integer.parseInt((String) jsonTimesUsed.get(i));
3573 05 Nov 15 olle 2533 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" i = " + i + " kitName = " + kitName + " numTimesUsed = " + numTimesUsed);
3647 04 Dec 15 olle 2534               // List of unused plate locations for FPA (6 x 8)
3647 04 Dec 15 olle 2535               unusedFpaPlateLocs = (String) jsonUnusedFpaPlateLocs.get(i);
3573 05 Nov 15 olle 2536 //System.out.println(new Date() + " LibPrepServlet::doPost(): cmd = \"" + cmd + "\" i = " + i + " kitName = " + kitName + " unusedFpaPlateLocs = " + unusedFpaPlateLocs);
3647 04 Dec 15 olle 2537               // Comment
3647 04 Dec 15 olle 2538               kitComment = (String) jsonComment.get(i);
3647 04 Dec 15 olle 2539             }
3647 04 Dec 15 olle 2540             // Last updated date
3647 04 Dec 15 olle 2541             Annotationtype.LIBPREPKIT_LAST_UPDATED.setAnnotationValue(dc, kit, lastUpdatedDate);
3647 04 Dec 15 olle 2542             // Expiration date
3647 04 Dec 15 olle 2543             kit.setExpirationDate(expirationDate);
3647 04 Dec 15 olle 2544             // Active
3647 04 Dec 15 olle 2545             kit.setInactive(!active);
3647 04 Dec 15 olle 2546             // Number of times used
3647 04 Dec 15 olle 2547             Annotationtype.LIBPREPKIT_NUMBER_TIMES_USED.setAnnotationValue(dc, kit, numTimesUsed);
3647 04 Dec 15 olle 2548             // List of unused plate locations for FPA aliquots (6 x 8)
3647 04 Dec 15 olle 2549             Annotationtype.LIBPREPKIT_UNUSED_FPA_WELLS.setAnnotationValue(dc, kit, unusedFpaPlateLocs);
3538 13 Oct 15 olle 2550             // Comment
3647 04 Dec 15 olle 2551             kit.setDescription(kitComment);
3647 04 Dec 15 olle 2552             // Add new kit to list
3647 04 Dec 15 olle 2553             kitNameList.add(kitName);
3647 04 Dec 15 olle 2554             addedKitNamesList.add(kitName);
3538 13 Oct 15 olle 2555           }
3538 13 Oct 15 olle 2556         }
3610 20 Nov 15 olle 2557         dc.commit();
3538 13 Oct 15 olle 2558
3560 27 Oct 15 olle 2559         String kitOperationStr = "added";
3560 27 Oct 15 olle 2560         if (overwrite)
3560 27 Oct 15 olle 2561         {
3560 27 Oct 15 olle 2562           kitOperationStr = "updated";
3560 27 Oct 15 olle 2563         }
3538 13 Oct 15 olle 2564         for (int i = 0; i < addedKitNamesList.size(); i++)
3538 13 Oct 15 olle 2565         {
3538 13 Oct 15 olle 2566           String kitName = (String) addedKitNamesList.get(i);
3560 27 Oct 15 olle 2567           jsonMessages.add("Data for kit " + kitName + " " + kitOperationStr + ".");
3538 13 Oct 15 olle 2568         }
3538 13 Oct 15 olle 2569         if (addedKitNamesList.size() == 0)
3538 13 Oct 15 olle 2570         {
3560 27 Oct 15 olle 2571           jsonMessages.add("No kit data " + kitOperationStr + ".");
3538 13 Oct 15 olle 2572         }
3573 05 Nov 15 olle 2573         // Forward request JSON data to be used in library plate creation
3573 05 Nov 15 olle 2574         json.put("submitInfo", jsonReq);
3538 13 Oct 15 olle 2575       }
3538 13 Oct 15 olle 2576 /*
3449 28 Jul 15 olle 2577       else if ("RegisterNeoPrepLibraries".equals(cmd))
3449 28 Jul 15 olle 2578       {
3449 28 Jul 15 olle 2579         dc = sc.newDbControl();
3449 28 Jul 15 olle 2580
3449 28 Jul 15 olle 2581         MeludiRole.checkPermission(dc, "'" + cmd + "' wizard", MeludiRole.LIBRARY_PREP, MeludiRole.ADMINISTRATOR);
3449 28 Jul 15 olle 2582
3449 28 Jul 15 olle 2583         JSONObject jsonReq = (JSONObject)new JSONParser().parse(req.getReader());
3449 28 Jul 15 olle 2584         Number bioPlateId = (Number)jsonReq.get("bioplate");
3449 28 Jul 15 olle 2585         Number libProtocolId = (Number)jsonReq.get("libProtocol");
3449 28 Jul 15 olle 2586         boolean failed = Boolean.TRUE.equals(jsonReq.get("failed"));
3449 28 Jul 15 olle 2587         
3449 28 Jul 15 olle 2588         BioPlate neoPrepPlate = BioPlate.getById(dc, bioPlateId.intValue());
3449 28 Jul 15 olle 2589         ReactionPlate neoPrepReactionPlate = ReactionPlate.getById(dc, bioPlateId.intValue(), BioplateType.NEOPREP);
3449 28 Jul 15 olle 2590
3449 28 Jul 15 olle 2591         // Set date and protocol for the event
3449 28 Jul 15 olle 2592         Date libDate = new Date();
3449 28 Jul 15 olle 2593         neoPrepPlate.setEventDate(libDate);
3449 28 Jul 15 olle 2594         
3449 28 Jul 15 olle 2595         Protocol libProtocol = libProtocolId == null ? null : Protocol.getById(dc, libProtocolId.intValue());
3449 28 Jul 15 olle 2596         neoPrepPlate.setDescription((String)jsonReq.get("libComments"));
3449 28 Jul 15 olle 2597         
3449 28 Jul 15 olle 2598         // Find the creation event for the NeoPrep plate
3449 28 Jul 15 olle 2599         List<BioPlateEvent> events = neoPrepReactionPlate.findEvents(dc, BioPlateEventType.CREATE_BIOMATERIAL, "destination");
3449 28 Jul 15 olle 2600         if (events.size() > 1)
3449 28 Jul 15 olle 2601         {
3449 28 Jul 15 olle 2602           throw new InvalidDataException("Found > 1 creation event for NeoPrep plate: " + neoPrepPlate.getName());
3449 28 Jul 15 olle 2603         }
3449 28 Jul 15 olle 2604         // ...it may not exist if a plate has been created with a batch import...
3449 28 Jul 15 olle 2605         BioPlateEvent neoPrepPlateCreationEvent = events.size() == 0 ? null : events.get(0);
3449 28 Jul 15 olle 2606         if (neoPrepPlateCreationEvent != null)
3449 28 Jul 15 olle 2607         {
3449 28 Jul 15 olle 2608           // This automatically propagated to all Library items on the plate
3449 28 Jul 15 olle 2609           neoPrepPlateCreationEvent.setEventDate(libDate);
3449 28 Jul 15 olle 2610           neoPrepPlateCreationEvent.setProtocol(libProtocol);
3449 28 Jul 15 olle 2611         }
3449 28 Jul 15 olle 2612
3449 28 Jul 15 olle 2613         // Dates and operators 
3449 28 Jul 15 olle 2614         String dilutionDate = (String)jsonReq.get("dilutionDate");
3449 28 Jul 15 olle 2615         if (dilutionDate != null)
3449 28 Jul 15 olle 2616         {
3449 28 Jul 15 olle 2617           Annotationtype.DILUTION_DATE.setAnnotationValue(dc, neoPrepPlate, Meludi.CONVERTER_STRING_TO_DATE.convert(dilutionDate));
3449 28 Jul 15 olle 2618           Annotationtype.DILUTION_OPERATOR.setAnnotationValue(dc, neoPrepPlate, Values.getStringOrNull((String)jsonReq.get("dilutionOperator")));
3449 28 Jul 15 olle 2619         }
3449 28 Jul 15 olle 2620         
3449 28 Jul 15 olle 2621         Annotationtype.NEOPREP_DATE.setAnnotationValue(dc, neoPrepPlate, Meludi.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("neoPrepDate")));
3449 28 Jul 15 olle 2622         Annotationtype.NEOPREP_OPERATOR.setAnnotationValue(dc, neoPrepPlate, Values.getStringOrNull((String)jsonReq.get("neoPrepOperator")));
3449 28 Jul 15 olle 2623         
3449 28 Jul 15 olle 2624         if (failed)
3449 28 Jul 15 olle 2625         {
3449 28 Jul 15 olle 2626           neoPrepPlate.setDestroyed(true);
3449 28 Jul 15 olle 2627           Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, neoPrepPlate, ReactionPlate.PROCESS_FAILED);
3449 28 Jul 15 olle 2628           
3449 28 Jul 15 olle 2629           // Flag the parent RNA items
3449 28 Jul 15 olle 2630           ItemList flaggedList = BiomaterialList.FLAGGED_RNA.load(dc);
3449 28 Jul 15 olle 2631           int numFlagged = 0;
3449 28 Jul 15 olle 2632           
3449 28 Jul 15 olle 2633           for (BioWell well : neoPrepPlate.getBioWells().list(dc))
3449 28 Jul 15 olle 2634           {
3449 28 Jul 15 olle 2635             if (!well.isEmpty())
3449 28 Jul 15 olle 2636             {
3449 28 Jul 15 olle 2637               Extract lib = (Extract)well.getBioMaterial();
3449 28 Jul 15 olle 2638               Library l = Library.get(lib);
3449 28 Jul 15 olle 2639               
3449 28 Jul 15 olle 2640               if (neoPrepPlateCreationEvent == null)
3449 28 Jul 15 olle 2641               {
3449 28 Jul 15 olle 2642                 // If no plate event existed, we must set protocol and date on all Library items
3449 28 Jul 15 olle 2643                 BioMaterialEvent evt = lib.getCreationEvent();
3449 28 Jul 15 olle 2644                 evt.setEventDate(libDate);
3449 28 Jul 15 olle 2645                 evt.setProtocol(libProtocol);
3449 28 Jul 15 olle 2646               }
3449 28 Jul 15 olle 2647               
3449 28 Jul 15 olle 2648               if (!Rna.isSpecialRna(lib.getName()))
3449 28 Jul 15 olle 2649               {
3449 28 Jul 15 olle 2650                 Rna r = l.getRna(dc, false);
3449 28 Jul 15 olle 2651                 Extract rna = r.getExtract();
3449 28 Jul 15 olle 2652                 Annotationtype.FLAG.setAnnotationValue(dc, rna, Rna.FLAG_LIB_PLATE_FAILED);
3449 28 Jul 15 olle 2653                 flaggedList.add(rna);
3449 28 Jul 15 olle 2654                 numFlagged++;
3449 28 Jul 15 olle 2655               }
3449 28 Jul 15 olle 2656             }
3449 28 Jul 15 olle 2657           }
3449 28 Jul 15 olle 2658           
3449 28 Jul 15 olle 2659           if (numFlagged > 0)
3449 28 Jul 15 olle 2660           {
3449 28 Jul 15 olle 2661             jsonMessages.add(numFlagged + " RNA items flagged for re-processing");
3449 28 Jul 15 olle 2662           }
3449 28 Jul 15 olle 2663           
3449 28 Jul 15 olle 2664           jsonMessages.add("Plate '" + neoPrepPlate.getName() + "' registered with status: " + (ReactionPlate.PROCESS_FAILED));
3449 28 Jul 15 olle 2665           
3449 28 Jul 15 olle 2666         }
3449 28 Jul 15 olle 2667         else
3449 28 Jul 15 olle 2668         {
3449 28 Jul 15 olle 2669           // Volumes, molarity and size are pre-determined in NeoPrep
3449 28 Jul 15 olle 2670           // So far, we assume that all libs are ok and up to the spec.
3449 28 Jul 15 olle 2671           Float libVolume = ((Number)jsonReq.get("libVolume")).floatValue(); // Âµl
3449 28 Jul 15 olle 2672           Float libMolarity = ((Number)jsonReq.get("libMolarity")).floatValue(); // nM
3449 28 Jul 15 olle 2673           Float libSize = ((Number)jsonReq.get("libSize")).floatValue(); // Base-pairs
3449 28 Jul 15 olle 2674           
3449 28 Jul 15 olle 2675           // Calculate library concentration
3449 28 Jul 15 olle 2676           Float molFactor = 1000f * 1000f / 649f;
3449 28 Jul 15 olle 2677           Float libConc = libMolarity * libSize / molFactor; // ng/µl
3449 28 Jul 15 olle 2678           
3449 28 Jul 15 olle 2679           int numExtracts = 0;
3449 28 Jul 15 olle 2680           for (BioWell well : neoPrepPlate.getBioWells().list(dc))
3449 28 Jul 15 olle 2681           {
3449 28 Jul 15 olle 2682             if (well.isEmpty()) continue;
3449 28 Jul 15 olle 2683             
3449 28 Jul 15 olle 2684             Extract lib = (Extract)well.getBioMaterial();
3449 28 Jul 15 olle 2685             if (neoPrepPlateCreationEvent == null)
3449 28 Jul 15 olle 2686             {
3449 28 Jul 15 olle 2687               // If no plate event existed, we must set protocol and date on all Library items
3449 28 Jul 15 olle 2688               BioMaterialEvent evt = lib.getCreationEvent();
3449 28 Jul 15 olle 2689               evt.setEventDate(libDate);
3449 28 Jul 15 olle 2690               evt.setProtocol(libProtocol);
3449 28 Jul 15 olle 2691             }
3449 28 Jul 15 olle 2692
3449 28 Jul 15 olle 2693             lib.setOriginalQuantity(libConc * libVolume / 1000);
3449 28 Jul 15 olle 2694             Annotationtype.LIBRARY_MOLARITY_EST.setAnnotationValue(dc, lib, libMolarity);
3449 28 Jul 15 olle 2695             Annotationtype.LIBRARY_SIZE_EST.setAnnotationValue(dc, lib, libSize);
3449 28 Jul 15 olle 2696             Annotationtype.LIBRARY_CONC_EST.setAnnotationValue(dc, lib, libConc);
3449 28 Jul 15 olle 2697   
3449 28 Jul 15 olle 2698             numExtracts++;
3449 28 Jul 15 olle 2699           }
3449 28 Jul 15 olle 2700           
3449 28 Jul 15 olle 2701           // Register the plate as completely processed
3449 28 Jul 15 olle 2702           neoPrepPlate.setDestroyed(false);
3449 28 Jul 15 olle 2703           Annotationtype.PLATE_PROCESS_RESULT.setAnnotationValue(dc, neoPrepPlate, ReactionPlate.PROCESS_SUCCESSFUL);
3449 28 Jul 15 olle 2704           
3449 28 Jul 15 olle 2705           jsonMessages.add("Plate '" + neoPrepPlate.getName() + "' registered with status: " + (ReactionPlate.PROCESS_SUCCESSFUL));
3449 28 Jul 15 olle 2706         }
3449 28 Jul 15 olle 2707         dc.commit();
3449 28 Jul 15 olle 2708       }
3449 28 Jul 15 olle 2709 */
3449 28 Jul 15 olle 2710       json.put("messages", jsonMessages);
3449 28 Jul 15 olle 2711       CounterService.getInstance().setForceCount();
3449 28 Jul 15 olle 2712     }
3449 28 Jul 15 olle 2713     catch (Throwable t)
3449 28 Jul 15 olle 2714     {
3449 28 Jul 15 olle 2715       t.printStackTrace();
3449 28 Jul 15 olle 2716       json.clear();
3449 28 Jul 15 olle 2717       json.put("status", "error");
3449 28 Jul 15 olle 2718       json.put("message", t.getMessage());
3449 28 Jul 15 olle 2719       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
3449 28 Jul 15 olle 2720     }
3449 28 Jul 15 olle 2721     finally
3449 28 Jul 15 olle 2722     {
3449 28 Jul 15 olle 2723       if (dc != null) dc.close();
3449 28 Jul 15 olle 2724       json.writeJSONString(resp.getWriter());
3449 28 Jul 15 olle 2725     }
3449 28 Jul 15 olle 2726     
3449 28 Jul 15 olle 2727   }
3449 28 Jul 15 olle 2728   
3449 28 Jul 15 olle 2729   // Parse the Qubit file and return a map extract name -> concentration
3449 28 Jul 15 olle 2730 /*
3449 28 Jul 15 olle 2731   private boolean addQubitFileLookups(CaliperPlateImporter cpi, File qubitCsv, AnnotationType originalConcType, AnnotationType speedVacConcType)
3449 28 Jul 15 olle 2732   {
3449 28 Jul 15 olle 2733     int lineNo = 0;
3449 28 Jul 15 olle 2734     try
3449 28 Jul 15 olle 2735     {
3449 28 Jul 15 olle 2736       // Parse the Qubit file and store concentration before and after speedvac in two maps
3449 28 Jul 15 olle 2737       FlatFileParser ffp = new FlatFileParser();
3449 28 Jul 15 olle 2738       ffp.setDataHeaderRegexp(Pattern.compile(".*Well.*Library name.*Qubit ng/ml.*After SpeedVac.*"));
3449 28 Jul 15 olle 2739       ffp.setIgnoreRegexp(Pattern.compile("^(\\\"?#.*|)\\s*")); // Ignore all lines starting with (#), ("#), or containing only whitespace
3449 28 Jul 15 olle 2740       ffp.setDataSplitterRegexp(Pattern.compile("\t|,")); // Split on tab or comma
3449 28 Jul 15 olle 2741       ffp.setInputStream(qubitCsv.getDownloadStream(0), "UTF-8");
3449 28 Jul 15 olle 2742       ffp.setTrimQuotes(true);
3449 28 Jul 15 olle 2743       if (ffp.parseHeaders() != LineType.DATA_HEADER)
3449 28 Jul 15 olle 2744       {
3449 28 Jul 15 olle 2745         throw new InvalidDataException("Could not find header line starting with 'Well{tab}Library name{tab}Qubit ng/ml...'");
3449 28 Jul 15 olle 2746       }
3449 28 Jul 15 olle 2747   
3449 28 Jul 15 olle 2748       Mapper nameMapper = ffp.getMapper("\\Library name\\");
3449 28 Jul 15 olle 2749       Mapper originalConcMapper = ffp.getMapper("\\Qubit ng/ml\\");
3449 28 Jul 15 olle 2750       Mapper speedVacConcMapper = ffp.getMapper("\\After SpeedVac\\");
3449 28 Jul 15 olle 2751   
3449 28 Jul 15 olle 2752       Map<String, Float> originalConc = new HashMap<String, Float>();
3449 28 Jul 15 olle 2753       Map<String, Float> speedVacConc = new HashMap<String, Float>();
3449 28 Jul 15 olle 2754       Map<String, String> errors = new HashMap<String, String>();
3449 28 Jul 15 olle 2755       while (ffp.hasMoreData())
3449 28 Jul 15 olle 2756       {
3449 28 Jul 15 olle 2757         lineNo = ffp.getParsedLines();
3449 28 Jul 15 olle 2758         FlatFileParser.Data data = ffp.nextData();
3449 28 Jul 15 olle 2759         String libName = nameMapper.getValue(data);
3449 28 Jul 15 olle 2760         Float c = checkQubitConcentration(originalConcMapper.getFloat(data));
3449 28 Jul 15 olle 2761         if (c == null)
3449 28 Jul 15 olle 2762         {
3449 28 Jul 15 olle 2763           errors.put(libName, "File '" + qubitCsv.getName() + "' line " + lineNo + ": Missing concentration for extract '" + libName + "'");
3449 28 Jul 15 olle 2764         }
3449 28 Jul 15 olle 2765         originalConc.put(libName, c);
3449 28 Jul 15 olle 2766         speedVacConc.put(libName, checkQubitConcentration(speedVacConcMapper.getFloat(data)));
3449 28 Jul 15 olle 2767       }
3449 28 Jul 15 olle 2768       
3449 28 Jul 15 olle 2769       // An original concentration is required, but SpeedVac concentration is optional
3449 28 Jul 15 olle 2770       LookupConverter<String, Float> originalConcLookup = new LookupConverter<String, Float>(originalConc, errors, 
3449 28 Jul 15 olle 2771           "File '" + qubitCsv.getName() + "': Could not find any data for library '{KEY}'");
3449 28 Jul 15 olle 2772       LookupConverter<String, Float> speedVacConcLookup = new LookupConverter<String, Float>(speedVacConc, null, null);
3449 28 Jul 15 olle 2773   
3449 28 Jul 15 olle 2774       cpi.addExternalLookup(originalConcLookup, originalConcType);
3449 28 Jul 15 olle 2775       cpi.addExternalLookup(speedVacConcLookup, speedVacConcType);
3449 28 Jul 15 olle 2776     }
3449 28 Jul 15 olle 2777     catch (Exception ex)
3449 28 Jul 15 olle 2778     {
3449 28 Jul 15 olle 2779       cpi.addErrorMessage("File '" + qubitCsv.getName() + "' line " + lineNo + ": " + ex.getMessage());
3449 28 Jul 15 olle 2780     }
3449 28 Jul 15 olle 2781     return !cpi.hasError();
3449 28 Jul 15 olle 2782   }
3449 28 Jul 15 olle 2783 */
3449 28 Jul 15 olle 2784   
3449 28 Jul 15 olle 2785   private Float checkQubitConcentration(Float value)
3449 28 Jul 15 olle 2786   {
3449 28 Jul 15 olle 2787     return value == null ? null : value / 1000;
3449 28 Jul 15 olle 2788   }
3449 28 Jul 15 olle 2789   
3449 28 Jul 15 olle 2790   private Float calculateWeightedSize(Float libSize, Float libMolarity, Float adptSize, Float adptMolarity)
3449 28 Jul 15 olle 2791   {
3449 28 Jul 15 olle 2792     Float totalMolarity = libMolarity + adptMolarity;
3449 28 Jul 15 olle 2793     Float weightedSize = libSize;
3449 28 Jul 15 olle 2794     if (totalMolarity > 0)
3449 28 Jul 15 olle 2795     {
3449 28 Jul 15 olle 2796       weightedSize = (libSize * libMolarity + adptSize * adptMolarity) / totalMolarity;
3449 28 Jul 15 olle 2797     }
3449 28 Jul 15 olle 2798     return weightedSize;
3449 28 Jul 15 olle 2799   }
3449 28 Jul 15 olle 2800   
3449 28 Jul 15 olle 2801   private Integer calculateAdapterFraction(Float libMolarity, Float adptMolarity)
3449 28 Jul 15 olle 2802   {
3449 28 Jul 15 olle 2803     Float totalMolarity = libMolarity + adptMolarity;
3449 28 Jul 15 olle 2804     return totalMolarity > 0 ? (int)(100 * adptMolarity / totalMolarity) : null;
3449 28 Jul 15 olle 2805   }
3449 28 Jul 15 olle 2806   
3449 28 Jul 15 olle 2807   public static class TimesTenValueConverter
3449 28 Jul 15 olle 2808     implements ValueConverter<String, Float>
3449 28 Jul 15 olle 2809   {
3449 28 Jul 15 olle 2810
3449 28 Jul 15 olle 2811     private final ValueConverter<String, String> parent;
3449 28 Jul 15 olle 2812     
3449 28 Jul 15 olle 2813     public TimesTenValueConverter(ValueConverter<String, String> parent) 
3449 28 Jul 15 olle 2814     {
3449 28 Jul 15 olle 2815       this.parent = parent;
3449 28 Jul 15 olle 2816     }
3449 28 Jul 15 olle 2817     
3449 28 Jul 15 olle 2818     @Override
3449 28 Jul 15 olle 2819     public Float convert(String value) 
3449 28 Jul 15 olle 2820     {
3449 28 Jul 15 olle 2821       Float f = (Float)Type.FLOAT.parseString(parent.convert(value));
3449 28 Jul 15 olle 2822       if (f != null) f = f * 10;
3449 28 Jul 15 olle 2823       return f;
3449 28 Jul 15 olle 2824     }
3449 28 Jul 15 olle 2825     
3449 28 Jul 15 olle 2826   }
3449 28 Jul 15 olle 2827 }