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

Code
Comments
Other
Rev Date Author Line
1403 11 Oct 11 martin 1 package net.sf.basedb.reggie.servlet;
1403 11 Oct 11 martin 2
1403 11 Oct 11 martin 3 import java.io.IOException;
3167 05 Mar 15 nicklas 4 import java.util.ArrayList;
1406 17 Oct 11 martin 5 import java.util.Date;
1403 11 Oct 11 martin 6 import java.util.List;
1403 11 Oct 11 martin 7
1403 11 Oct 11 martin 8 import javax.servlet.ServletException;
1403 11 Oct 11 martin 9 import javax.servlet.http.HttpServlet;
1403 11 Oct 11 martin 10 import javax.servlet.http.HttpServletRequest;
1403 11 Oct 11 martin 11 import javax.servlet.http.HttpServletResponse;
1403 11 Oct 11 martin 12
1403 11 Oct 11 martin 13 import net.sf.basedb.core.AnnotationType;
1406 17 Oct 11 martin 14 import net.sf.basedb.core.BioMaterialEvent;
1406 17 Oct 11 martin 15 import net.sf.basedb.core.BioPlate;
1406 17 Oct 11 martin 16 import net.sf.basedb.core.BioWell;
1403 11 Oct 11 martin 17 import net.sf.basedb.core.DbControl;
1406 17 Oct 11 martin 18 import net.sf.basedb.core.Extract;
1403 11 Oct 11 martin 19 import net.sf.basedb.core.InvalidDataException;
1406 17 Oct 11 martin 20 import net.sf.basedb.core.ItemQuery;
4896 09 Jul 18 nicklas 21 import net.sf.basedb.core.ItemSubtype;
1406 17 Oct 11 martin 22 import net.sf.basedb.core.Sample;
1403 11 Oct 11 martin 23 import net.sf.basedb.core.SessionControl;
1406 17 Oct 11 martin 24 import net.sf.basedb.core.query.Expressions;
1406 17 Oct 11 martin 25 import net.sf.basedb.core.query.Hql;
1406 17 Oct 11 martin 26 import net.sf.basedb.core.query.Restrictions;
2598 22 Aug 14 nicklas 27 import net.sf.basedb.reggie.JsonUtil;
1983 21 May 13 nicklas 28 import net.sf.basedb.reggie.Reggie;
5384 26 Apr 19 nicklas 29 import net.sf.basedb.reggie.activity.ActivityDef;
3059 19 Dec 14 nicklas 30 import net.sf.basedb.reggie.counter.CounterService;
1610 23 Apr 12 nicklas 31 import net.sf.basedb.reggie.dao.Annotationtype;
5791 16 Dec 19 nicklas 32 import net.sf.basedb.reggie.dao.DoNotUse;
2161 09 Dec 13 nicklas 33 import net.sf.basedb.reggie.dao.ReggieRole;
1403 11 Oct 11 martin 34 import net.sf.basedb.reggie.dao.SpecimenTube;
1463 14 Nov 11 martin 35 import net.sf.basedb.reggie.dao.Subtype;
1406 17 Oct 11 martin 36 import net.sf.basedb.util.Values;
1403 11 Oct 11 martin 37 import net.sf.basedb.util.error.ThrowableUtil;
5371 16 Apr 19 nicklas 38 import net.sf.basedb.util.excel.XlsxTableWriter;
5371 16 Apr 19 nicklas 39 import net.sf.basedb.util.excel.XlsxToCsvUtil;
5299 14 Feb 19 nicklas 40 import net.sf.basedb.util.export.TableWriter;
1403 11 Oct 11 martin 41
1403 11 Oct 11 martin 42 import org.json.simple.JSONArray;
1403 11 Oct 11 martin 43 import org.json.simple.JSONObject;
1403 11 Oct 11 martin 44
1403 11 Oct 11 martin 45 public class PartitionRegistrationServlet 
1403 11 Oct 11 martin 46   extends HttpServlet
1403 11 Oct 11 martin 47 {  
1403 11 Oct 11 martin 48   private static final long serialVersionUID = 4343512283090513225L;
1403 11 Oct 11 martin 49   
1403 11 Oct 11 martin 50   public PartitionRegistrationServlet()
1403 11 Oct 11 martin 51   {}
1403 11 Oct 11 martin 52   
1403 11 Oct 11 martin 53   @Override
1403 11 Oct 11 martin 54   protected void doGet(HttpServletRequest req, HttpServletResponse resp)
1403 11 Oct 11 martin 55     throws ServletException, IOException 
1403 11 Oct 11 martin 56   {
1403 11 Oct 11 martin 57     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 58     JsonUtil.setJsonResponseHeaders(resp);
1403 11 Oct 11 martin 59     
1406 17 Oct 11 martin 60     JSONObject json = new JSONObject();    
1403 11 Oct 11 martin 61     json.put("status", "ok");
1403 11 Oct 11 martin 62     
3975 26 May 16 nicklas 63     final SessionControl sc = Reggie.getSessionControl(req);
1449 02 Nov 11 martin 64     DbControl dc = null;    
1449 02 Nov 11 martin 65             
1403 11 Oct 11 martin 66     try
1403 11 Oct 11 martin 67     {
1403 11 Oct 11 martin 68       if ("GetSpecimenTubes".equals(cmd))
1403 11 Oct 11 martin 69       {
6330 14 Jun 21 nicklas 70         dc = sc.newDbControl(":Partition wizard");
1403 11 Oct 11 martin 71         JSONArray jsonTubes = new JSONArray();
3145 25 Feb 15 nicklas 72         List<SpecimenTube> unPartitionedTubes = SpecimenTube.findUnPartitionedTubes(dc, true);        
1419 28 Oct 11 martin 73         for (SpecimenTube tube : unPartitionedTubes)
1821 06 Feb 13 nicklas 74         {
1821 06 Feb 13 nicklas 75           tube.loadBioPlateLocation();
6721 03 May 22 nicklas 76           tube.loadAnnotations(dc, "label", Annotationtype.TUBE_LABEL, null);
3738 11 Feb 16 nicklas 77           tube.loadAnnotations(dc, "YellowLabel", Annotationtype.YELLOW_LABEL, null);
3145 25 Feb 15 nicklas 78           tube.loadAnnotations(dc, "AutoProcessing", Annotationtype.AUTO_PROCESSING, null);
3145 25 Feb 15 nicklas 79           tube.loadAnnotations(dc, "OperatorPartitionComment", Annotationtype.OPERATOR_PARTITION_COMMENT, null);
3145 25 Feb 15 nicklas 80           tube.loadAnnotations(dc, "NofPieces", Annotationtype.NOF_PIECES, null);
3145 25 Feb 15 nicklas 81           tube.setAnnotation("remainingQuantity", tube.getSample().getRemainingQuantity());
5335 28 Mar 19 nicklas 82           tube.setAnnotation("originalQuantity", tube.getSample().getOriginalQuantity());
1419 28 Oct 11 martin 83           jsonTubes.add(tube.asJSONObject());
1403 11 Oct 11 martin 84         }
2740 06 Oct 14 nicklas 85         json.put("specimenTubes", jsonTubes);
6721 03 May 22 nicklas 86         json.put("TubeLabelAnnotationType", Annotationtype.TUBE_LABEL.get(dc).getId());
1403 11 Oct 11 martin 87       }
1449 02 Nov 11 martin 88       else if ("GetLabelFile".equals(cmd))
1449 02 Nov 11 martin 89       {
5338 29 Mar 19 nicklas 90         json = null; // No JSON output
6330 14 Jun 21 nicklas 91         dc = sc.newDbControl(":Partition wizard");
3167 05 Mar 15 nicklas 92         List<String> firstPart = new ArrayList<String>();
3167 05 Mar 15 nicklas 93         List<String> secondPart = new ArrayList<String>();
1449 02 Nov 11 martin 94         
3146 25 Feb 15 nicklas 95         Integer[] ids = Values.getInt(req.getParameter("specimen").split(","));
5304 15 Feb 19 nicklas 96         String format = Values.getString(req.getParameter("format"), "csv");
5299 14 Feb 19 nicklas 97         
5813 22 Jan 20 nicklas 98         boolean includeHisLabels = Values.getBoolean(req.getParameter("includeHisLabels"));
3146 25 Feb 15 nicklas 99         boolean includeAllHisLabels = Values.getBoolean(req.getParameter("includeAllHisLabels"));
3146 25 Feb 15 nicklas 100         for (Integer specimenId : ids)
1449 02 Nov 11 martin 101         {
3146 25 Feb 15 nicklas 102           SpecimenTube sp = SpecimenTube.getById(dc, specimenId);
5335 28 Mar 19 nicklas 103           Float originalQuantity = sp.getItem().getOriginalQuantity();
5335 28 Mar 19 nicklas 104           boolean firstExtraction = originalQuantity == null;
3146 25 Feb 15 nicklas 105           
3146 25 Feb 15 nicklas 106           String spName = sp.getName();
3146 25 Feb 15 nicklas 107           String lysateName = sp.getNextLysateName(dc);
3146 25 Feb 15 nicklas 108           String apName = lysateName.replace(".l", ".ap");
3146 25 Feb 15 nicklas 109           
5343 02 Apr 19 nicklas 110           if (firstExtraction) firstPart.add(spName);
5813 22 Jan 20 nicklas 111           if (firstExtraction && includeHisLabels || !firstExtraction && includeAllHisLabels)
3146 25 Feb 15 nicklas 112           {
5343 02 Apr 19 nicklas 113             String hisName = sp.getNextHistologyName(dc);
5343 02 Apr 19 nicklas 114             firstPart.add(hisName);
1449 02 Nov 11 martin 115           }
3167 05 Mar 15 nicklas 116           firstPart.add(apName);
1449 02 Nov 11 martin 117           
3167 05 Mar 15 nicklas 118           secondPart.add(lysateName);
3167 05 Mar 15 nicklas 119           secondPart.add(lysateName+".r");
3167 05 Mar 15 nicklas 120           secondPart.add(lysateName+".r.n");
3167 05 Mar 15 nicklas 121           secondPart.add(lysateName+".d");
3167 05 Mar 15 nicklas 122           secondPart.add(lysateName+".ft");
3167 05 Mar 15 nicklas 123           secondPart.add("--");
3146 25 Feb 15 nicklas 124         }
3167 05 Mar 15 nicklas 125         
3167 05 Mar 15 nicklas 126         if (firstPart.size() % 2 != 0)
3167 05 Mar 15 nicklas 127         {
3167 05 Mar 15 nicklas 128           firstPart.add("--");
3167 05 Mar 15 nicklas 129         }
3167 05 Mar 15 nicklas 130         
3146 25 Feb 15 nicklas 131         // Send labels in a file to the client.
5299 14 Feb 19 nicklas 132         TableWriter tw = null;
5299 14 Feb 19 nicklas 133         XlsxTableWriter xls = null;
5299 14 Feb 19 nicklas 134         if ("xlsx".equals(format))
5299 14 Feb 19 nicklas 135         {
5299 14 Feb 19 nicklas 136           resp.setHeader("Content-Disposition", "attachment; filename=Partition_labels.xlsx");
5371 16 Apr 19 nicklas 137           resp.setContentType(XlsxToCsvUtil.XLSX_MIME_TYPE);
5299 14 Feb 19 nicklas 138           xls = new XlsxTableWriter("PartitionLabels");
5299 14 Feb 19 nicklas 139           tw = xls;
5299 14 Feb 19 nicklas 140         }
5299 14 Feb 19 nicklas 141         else
5299 14 Feb 19 nicklas 142         {
5299 14 Feb 19 nicklas 143           resp.setHeader("Content-Disposition", "attachment; filename=Partition_labels.csv");
5299 14 Feb 19 nicklas 144           resp.setContentType("text/plain");
5299 14 Feb 19 nicklas 145           resp.setCharacterEncoding("UTF-8");
5299 14 Feb 19 nicklas 146           tw = new TableWriter(resp.getWriter());
5299 14 Feb 19 nicklas 147           // The LABEL header is only required in the CSV format
5299 14 Feb 19 nicklas 148           tw.tablePrintData("LABEL");
5299 14 Feb 19 nicklas 149         }
5299 14 Feb 19 nicklas 150         
5299 14 Feb 19 nicklas 151         for (String lbl : firstPart)
5299 14 Feb 19 nicklas 152         {
5299 14 Feb 19 nicklas 153           tw.tablePrintData(lbl);
5299 14 Feb 19 nicklas 154         }
5299 14 Feb 19 nicklas 155         for (String lbl : secondPart)
5299 14 Feb 19 nicklas 156         {
5299 14 Feb 19 nicklas 157           tw.tablePrintData(lbl);
5299 14 Feb 19 nicklas 158         }
5304 15 Feb 19 nicklas 159         tw.flush();
5299 14 Feb 19 nicklas 160         
5299 14 Feb 19 nicklas 161         if (xls != null) 
5299 14 Feb 19 nicklas 162         {
5299 14 Feb 19 nicklas 163           xls.getSheet().setColumnWidth(0, 18*256);
5299 14 Feb 19 nicklas 164           xls.saveTo(resp.getOutputStream());
5299 14 Feb 19 nicklas 165           xls.close();
5299 14 Feb 19 nicklas 166         }
1449 02 Nov 11 martin 167       }  
1403 11 Oct 11 martin 168     }
1403 11 Oct 11 martin 169     catch (Throwable t)
1403 11 Oct 11 martin 170     {
1403 11 Oct 11 martin 171       t.printStackTrace();
5338 29 Mar 19 nicklas 172       if (json != null)
5338 29 Mar 19 nicklas 173       {
5338 29 Mar 19 nicklas 174         json.clear();
5338 29 Mar 19 nicklas 175         json.put("status", "error");
5338 29 Mar 19 nicklas 176         json.put("message", t.getMessage());
5338 29 Mar 19 nicklas 177         json.put("stacktrace", ThrowableUtil.stackTraceToString(t));
5338 29 Mar 19 nicklas 178       }
5338 29 Mar 19 nicklas 179       else
5338 29 Mar 19 nicklas 180       {
5338 29 Mar 19 nicklas 181         resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, t.getMessage());
5338 29 Mar 19 nicklas 182       }
1403 11 Oct 11 martin 183     }
1403 11 Oct 11 martin 184     finally
1403 11 Oct 11 martin 185     {
1403 11 Oct 11 martin 186       if (dc!=null) dc.close();
5338 29 Mar 19 nicklas 187       if (json != null)
5338 29 Mar 19 nicklas 188       {
5338 29 Mar 19 nicklas 189         json.writeJSONString(resp.getWriter());
5338 29 Mar 19 nicklas 190       }
1403 11 Oct 11 martin 191     }
1403 11 Oct 11 martin 192   }
1403 11 Oct 11 martin 193   
1403 11 Oct 11 martin 194   @Override
1403 11 Oct 11 martin 195   protected void doPost(HttpServletRequest req, HttpServletResponse resp)
1403 11 Oct 11 martin 196     throws ServletException, IOException 
1403 11 Oct 11 martin 197   {
1406 17 Oct 11 martin 198     String cmd = req.getParameter("cmd");
2598 22 Aug 14 nicklas 199     JsonUtil.setJsonResponseHeaders(resp);
1403 11 Oct 11 martin 200     
1406 17 Oct 11 martin 201     JSONObject json = new JSONObject();
1406 17 Oct 11 martin 202     JSONArray jsonMessages = new JSONArray();
1406 17 Oct 11 martin 203     json.put("status", "ok");
1406 17 Oct 11 martin 204     
3975 26 May 16 nicklas 205     final SessionControl sc = Reggie.getSessionControl(req);
1406 17 Oct 11 martin 206     DbControl dc = null;
1406 17 Oct 11 martin 207       
1406 17 Oct 11 martin 208     try
1406 17 Oct 11 martin 209     {
1406 17 Oct 11 martin 210       if ("CreatePartitions".equals(cmd))
1406 17 Oct 11 martin 211       {
6330 14 Jun 21 nicklas 212         dc = sc.newDbControl(":Partition wizard");
2161 09 Dec 13 nicklas 213
2161 09 Dec 13 nicklas 214         ReggieRole.checkPermission(dc, "'" + cmd + "' wizard", ReggieRole.SAMPLE_PREP, ReggieRole.ADMINISTRATOR);
2161 09 Dec 13 nicklas 215
3752 17 Feb 16 nicklas 216         JSONObject jsonReq = JsonUtil.parseRequest(req);
2740 06 Oct 14 nicklas 217         JSONArray jsonSpecimens = (JSONArray)jsonReq.get("specimens");
1532 07 Feb 12 nicklas 218         JSONArray jsonPartitioned = new JSONArray();
1406 17 Oct 11 martin 219         
1610 23 Apr 12 nicklas 220         AnnotationType partitionCommentType = Annotationtype.OPERATOR_PARTITION_COMMENT.load(dc);
2740 06 Oct 14 nicklas 221         Date partitionDate = Reggie.CONVERTER_STRING_TO_DATE.convert((String)jsonReq.get("partitionDate"));
1406 17 Oct 11 martin 222         
4896 09 Jul 18 nicklas 223         ItemSubtype lysateType = Subtype.LYSATE.load(dc);
4896 09 Jul 18 nicklas 224         ItemSubtype histologyType = Subtype.HISTOLOGY.load(dc);
4896 09 Jul 18 nicklas 225         
1406 17 Oct 11 martin 226         if (jsonSpecimens != null && jsonSpecimens.size()>0)
1406 17 Oct 11 martin 227         {
1406 17 Oct 11 martin 228           for (int i=0; i<jsonSpecimens.size(); i++)
1406 17 Oct 11 martin 229           {
3150 26 Feb 15 nicklas 230             float sumWeights = 0;
1406 17 Oct 11 martin 231             JSONObject jsonSpecimen = (JSONObject)jsonSpecimens.get(i);
1406 17 Oct 11 martin 232             Number specimenId = (Number)jsonSpecimen.get("id");
3150 26 Feb 15 nicklas 233             SpecimenTube tube = SpecimenTube.getById(dc, specimenId.intValue());
3150 26 Feb 15 nicklas 234             Sample specimen = tube.getItem();
1406 17 Oct 11 martin 235             
3150 26 Feb 15 nicklas 236             boolean reprocess = "ReProcess".equals(Annotationtype.AUTO_PROCESSING.getAnnotationValue(dc, specimen));
5335 28 Mar 19 nicklas 237             boolean firstExtraction = specimen.getOriginalQuantity() == null;
3150 26 Feb 15 nicklas 238             
3150 26 Feb 15 nicklas 239             if (reprocess)
3150 26 Feb 15 nicklas 240             {
3150 26 Feb 15 nicklas 241               // Assign new biowell
3150 26 Feb 15 nicklas 242               JSONObject jsonNewWell = (JSONObject)jsonSpecimen.get("newBioWell");
3150 26 Feb 15 nicklas 243               Number wellId = (Number)jsonNewWell.get("id");
3150 26 Feb 15 nicklas 244               specimen.setBioWell(BioWell.getById(dc, wellId.intValue()));
3150 26 Feb 15 nicklas 245               // Reset AutoProcessing annotation
3150 26 Feb 15 nicklas 246               Annotationtype.AUTO_PROCESSING.setAnnotationValue(dc, specimen, null);
3150 26 Feb 15 nicklas 247             }
5335 28 Mar 19 nicklas 248             
5335 28 Mar 19 nicklas 249             if (firstExtraction)
3150 26 Feb 15 nicklas 250             {
3150 26 Feb 15 nicklas 251               // Only register number of pieces and partition comments for the first partition
3150 26 Feb 15 nicklas 252               Number nofPieces = (Number)jsonSpecimen.get("nofPieces");
3150 26 Feb 15 nicklas 253               String operatorPartitionComment = Values.getStringOrNull((String)jsonSpecimen.get("operatorPartitionComment"));
3150 26 Feb 15 nicklas 254               Annotationtype.NOF_PIECES.setAnnotationValue(dc, specimen, nofPieces);            
3150 26 Feb 15 nicklas 255               Annotationtype.OPERATOR_PARTITION_COMMENT.setAnnotationValue(dc, specimen, operatorPartitionComment);            
3150 26 Feb 15 nicklas 256             }
3150 26 Feb 15 nicklas 257             
2740 06 Oct 14 nicklas 258             Number spWeight = (Number)jsonSpecimen.get("spWeight");
2890 03 Nov 14 nicklas 259             if (spWeight != null) spWeight = spWeight.floatValue() * 1000;
2740 06 Oct 14 nicklas 260             Number apWeight = (Number)jsonSpecimen.get("apWeight");
2890 03 Nov 14 nicklas 261             if (apWeight != null) apWeight = apWeight.floatValue() * 1000;
2740 06 Oct 14 nicklas 262             Number hisWeight = (Number)jsonSpecimen.get("hisWeight");
2890 03 Nov 14 nicklas 263             if (hisWeight != null) hisWeight = hisWeight.floatValue() * 1000;
2740 06 Oct 14 nicklas 264             if (spWeight != null) 
2740 06 Oct 14 nicklas 265             {
3150 26 Feb 15 nicklas 266               sumWeights = sumWeights + spWeight.floatValue();
2740 06 Oct 14 nicklas 267             }
1406 17 Oct 11 martin 268             
1406 17 Oct 11 martin 269             // Create lys-extract
1406 17 Oct 11 martin 270             if (apWeight != null)
1406 17 Oct 11 martin 271             {
3150 26 Feb 15 nicklas 272               sumWeights = sumWeights + apWeight.floatValue();
2740 06 Oct 14 nicklas 273               if (apWeight.floatValue() > 0)
1406 17 Oct 11 martin 274               {
2740 06 Oct 14 nicklas 275                 Extract lysExtract = specimen.newExtract(apWeight.floatValue());
5791 16 Dec 19 nicklas 276                 DoNotUse.copyDoNotUseAnnotations(dc, specimen, lysExtract, false);
4896 09 Jul 18 nicklas 277                 lysExtract.setItemSubtype(lysateType);
4896 09 Jul 18 nicklas 278                 lysExtract.setName(tube.getNextLysateName(dc));
1406 17 Oct 11 martin 279                 dc.saveItem(lysExtract);
1406 17 Oct 11 martin 280                 
1457 10 Nov 11 martin 281                 lysExtract.getCreationEvent().setEventDate(null);
1610 23 Apr 12 nicklas 282                 Annotationtype.PARTITION_DATE.setAnnotationValue(dc, lysExtract, partitionDate);
4457 20 Apr 17 nicklas 283                 Annotationtype.MULTIPLE_PIECES.setAnnotationValue(dc, lysExtract, jsonSpecimen.get("multPieces"));
4457 20 Apr 17 nicklas 284
1406 17 Oct 11 martin 285                 // Set box position
3150 26 Feb 15 nicklas 286                 BioWell specimenWell = specimen.getBioWell();
3150 26 Feb 15 nicklas 287                 String specimenBoxName = specimenWell.getPlate().getName();
4896 09 Jul 18 nicklas 288                 
3150 26 Feb 15 nicklas 289                 String lysBoxName = "Lys"+specimenBoxName.substring(2);
3150 26 Feb 15 nicklas 290                 ItemQuery<BioPlate> lysBoxQuery = BioPlate.getQuery();
3150 26 Feb 15 nicklas 291                 lysBoxQuery.restrict(Restrictions.eq(Hql.property("name"), Expressions.string(lysBoxName)));
3150 26 Feb 15 nicklas 292                 List<BioPlate> lysBoxes = lysBoxQuery.list(dc);
3150 26 Feb 15 nicklas 293                 if (lysBoxes.size()<1)
1406 17 Oct 11 martin 294                 {
3150 26 Feb 15 nicklas 295                   throw new InvalidDataException("No lys-box was found with name: "+lysBoxName+". " +
3150 26 Feb 15 nicklas 296                       "This wizard can not be used until this is fixed.");
1409 18 Oct 11 martin 297                 }
3150 26 Feb 15 nicklas 298                 else if (lysBoxes.size() > 1)
3150 26 Feb 15 nicklas 299                 {
3150 26 Feb 15 nicklas 300                   throw new InvalidDataException("More then one lys-box were found with name: "+lysBoxName+". " +
3150 26 Feb 15 nicklas 301                       "This wizard can not be used until this is fixed.");
3150 26 Feb 15 nicklas 302                 }
3150 26 Feb 15 nicklas 303                 
3150 26 Feb 15 nicklas 304                 BioPlate lysBox = lysBoxes.get(0);
3150 26 Feb 15 nicklas 305                 if (!lysBox.getBioWell(specimenWell.getRow(), specimenWell.getColumn()).isEmpty())
3150 26 Feb 15 nicklas 306                 {
3150 26 Feb 15 nicklas 307                   throw new InvalidDataException("Position [" + specimenWell.getRow() + "," + specimenWell.getColumn() + "]" +
3150 26 Feb 15 nicklas 308                       " is not empty in box: " + lysBoxName);
3150 26 Feb 15 nicklas 309                 }
3150 26 Feb 15 nicklas 310                 lysExtract.setBioWell(lysBox.getBioWell(specimenWell.getRow(), specimenWell.getColumn()));                  
1406 17 Oct 11 martin 311               }
1406 17 Oct 11 martin 312             }
1406 17 Oct 11 martin 313             
1406 17 Oct 11 martin 314             // Create his-sample
1406 17 Oct 11 martin 315             if (hisWeight != null)
1406 17 Oct 11 martin 316             {
3150 26 Feb 15 nicklas 317               sumWeights = sumWeights + hisWeight.floatValue();
2740 06 Oct 14 nicklas 318               if (hisWeight.floatValue() > 0)
1406 17 Oct 11 martin 319               {
3150 26 Feb 15 nicklas 320                 Sample hisSample = Sample.getNew(dc);
4896 09 Jul 18 nicklas 321                 hisSample.setItemSubtype(histologyType);
4896 09 Jul 18 nicklas 322                 hisSample.setName(tube.getNextHistologyName(dc));
1406 17 Oct 11 martin 323                 dc.saveItem(hisSample);
2740 06 Oct 14 nicklas 324                 hisSample.setOriginalQuantity(hisWeight.floatValue());
1406 17 Oct 11 martin 325                 BioMaterialEvent creationEvent = hisSample.getCreationEvent();
2740 06 Oct 14 nicklas 326                 creationEvent.addSource(specimen).setUsedQuantity(hisWeight.floatValue());
5791 16 Dec 19 nicklas 327                 DoNotUse.copyDoNotUseAnnotations(dc, specimen, hisSample, false);
1406 17 Oct 11 martin 328                 
1610 23 Apr 12 nicklas 329                 Annotationtype.PARTITION_DATE.setAnnotationValue(dc, hisSample, partitionDate);
1406 17 Oct 11 martin 330               }
3150 26 Feb 15 nicklas 331             }
1406 17 Oct 11 martin 332             
5335 28 Mar 19 nicklas 333             if (!firstExtraction)
3150 26 Feb 15 nicklas 334             {
3150 26 Feb 15 nicklas 335               float weightDiff = specimen.getRemainingQuantity() - sumWeights;
3150 26 Feb 15 nicklas 336               if (Math.abs(weightDiff) > 1)
3150 26 Feb 15 nicklas 337               {
3150 26 Feb 15 nicklas 338                 // Create 'other' event to register difference
3150 26 Feb 15 nicklas 339                 BioMaterialEvent correctionEvent = specimen.newEvent();
3150 26 Feb 15 nicklas 340                 correctionEvent.setEventDate(partitionDate);
3150 26 Feb 15 nicklas 341                 correctionEvent.setUsedQuantity(weightDiff);
3150 26 Feb 15 nicklas 342                 correctionEvent.setComment("Correcting remaining quanitity after new weight measurements due to re-extraction.");
3150 26 Feb 15 nicklas 343                 dc.saveItem(correctionEvent);
3150 26 Feb 15 nicklas 344               }
3150 26 Feb 15 nicklas 345             }
3150 26 Feb 15 nicklas 346             else
3150 26 Feb 15 nicklas 347             {
3150 26 Feb 15 nicklas 348               specimen.setOriginalQuantity(sumWeights);
3150 26 Feb 15 nicklas 349             }
3150 26 Feb 15 nicklas 350             
1532 07 Feb 12 nicklas 351             jsonMessages.add("Specimen tube " + specimen.getName() + " partitioned successfully");
1532 07 Feb 12 nicklas 352             jsonPartitioned.add(specimen.getId());
1406 17 Oct 11 martin 353           }
1406 17 Oct 11 martin 354         }
5377 23 Apr 19 nicklas 355         if (jsonPartitioned.size() > 0)
5377 23 Apr 19 nicklas 356         {
5421 13 May 19 nicklas 357           ActivityDef.PARTITIONED_SPECIMEN.merge(dc, jsonPartitioned.size());
5377 23 Apr 19 nicklas 358         }
1406 17 Oct 11 martin 359         dc.commit();
1532 07 Feb 12 nicklas 360         json.put("partitioned", jsonPartitioned);
1449 02 Nov 11 martin 361       }  
1406 17 Oct 11 martin 362       json.put("messages", jsonMessages);
3059 19 Dec 14 nicklas 363       CounterService.getInstance().setForceCount();
1406 17 Oct 11 martin 364     }
1406 17 Oct 11 martin 365     catch (Throwable t)
1406 17 Oct 11 martin 366     {
1406 17 Oct 11 martin 367       t.printStackTrace();
1406 17 Oct 11 martin 368       json.clear();
1406 17 Oct 11 martin 369       json.put("status", "error");
1406 17 Oct 11 martin 370       json.put("message", t.getMessage());
1406 17 Oct 11 martin 371       json.put("stacktrace", ThrowableUtil.stackTraceToString(t));      
1406 17 Oct 11 martin 372     }
1406 17 Oct 11 martin 373     finally
1406 17 Oct 11 martin 374     {
1406 17 Oct 11 martin 375       if (dc != null) dc.close();
1406 17 Oct 11 martin 376       json.writeJSONString(resp.getWriter());
1406 17 Oct 11 martin 377     }
1403 11 Oct 11 martin 378   }
1403 11 Oct 11 martin 379 }