gt: 				if(lastGroup!=null && !lastGroup.equals(group)) {
context: /*
   * Spirit, a study/biosample management tool for research.
   * Copyright (C) 2018 Idorsia Pharmaceuticals Ltd., Hegenheimermattweg 91,
   * CH-4123 Allschwil, Switzerland.
   *
   * This program is free software: you can redistribute it and/or modify
   * it under the terms of the GNU General Public License as published by
   * the Free Software Foundation, either version 3 of the License, or
   * (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   * GNU General Public License for more details.
   *
   * You should have received a copy of the GNU General Public License
   * along with this program.  If not, see <http://www.gnu.org/licenses/>
   *
   * @author Joel Freyss
   */
  
  package com.actelion.research.spiritapp.report;
  
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  import java.util.Set;
  import java.util.TreeSet;
  
  import org.apache.poi.ss.usermodel.Sheet;
  import org.apache.poi.ss.util.WorkbookUtil;
  
  import com.actelion.research.spiritapp.ui.SpiritFrame;
  import com.actelion.research.spiritapp.ui.util.POIUtils;
  import com.actelion.research.spiritcore.business.biosample.ActionTreatment;
  import com.actelion.research.spiritcore.business.biosample.Biosample;
  import com.actelion.research.spiritcore.business.result.Result;
  import com.actelion.research.spiritcore.business.result.Test;
  import com.actelion.research.spiritcore.business.study.Group;
  import com.actelion.research.spiritcore.business.study.Phase;
  import com.actelion.research.spiritcore.business.study.StudyAction;
  import com.actelion.research.spiritcore.services.dao.DAOResult;
  import com.actelion.research.spiritcore.services.dao.DAORevision;
  import com.actelion.research.spiritcore.services.dao.DAOSpiritUser;
  import com.actelion.research.spiritcore.services.dao.DAOStudy;
  import com.actelion.research.spiritcore.services.dao.DAOTest;
  import com.actelion.research.spiritcore.util.ListHashMap;
  import com.actelion.research.spiritcore.util.MiscUtils;
  
  public class WeighingPerPhaseReport extends AbstractReport {
  
  	public WeighingPerPhaseReport() {
  		super(ReportCategory.PARTICIPANTS, "Bodyweights (separated per phase)", MiscUtils.convert2Html(
  				"Group\tContainerId\tParticipantId\tNo\tWeight\tIncrease\tTreatment\tFormulation\tDate\n"
  						+ "1. \tCage1\tParticipantId1\t1\t20\t+1%\tVehicle\n"
  						+ "1. \tCage1\tParticipantId2\t2\t18\t+2%\tVehicle\n"
  						+ "Phase1 | Phase2 | Phase 3 | Summary | Increase"));
  	}
  
  	public static ActionTreatment getTreatment(Biosample h) {
  		if(h==null) return null;
  		if(h.getLastAction() instanceof ActionTreatment) {
  			return ((ActionTreatment)h.getLastAction());
  		}
  		return null;
  	}
  
  	@Override
  	protected void populateWorkBook() throws Exception {
  
  		List<Biosample> animals = study.getParticipantsSorted();
  		Map<Biosample, Map<String, Biosample>> bio2history = new HashMap<>();
  
  		//Load Weighings
  		Test weightingTest = DAOTest.getTest(DAOTest.WEIGHING_TESTNAME);
  		if(weightingTest==null) throw new Exception("Error test "+DAOTest.WEIGHING_TESTNAME+" not found");
  		DAOResult.attachOrCreateStudyResultsToTops(study, animals, null, null);
  
  
  		for(Biosample b: animals) {
  			bio2history.put(b, new HashMap<>());
  			List<Biosample> history = DAORevision.getHistory(b);
  			for (Biosample b2 : history) {
  				if(!study.equals(b2.getInheritedStudy())) continue;
  				ActionTreatment t = getTreatment(b2);
  				if(t!=null) {
  					//Add the treatment, if it is the most recent
  					if(!bio2history.get(b).containsKey(t.getPhaseName())) {
  						bio2history.get(b).put(t.getPhaseName(), b2);
  					}
  				}
  			}
  		}
  
  		//Check phases where we have results, tratments
  		Set<Phase> phases = new TreeSet<>();
  		for(Biosample b: animals) {
  			for(Result r: b.getAuxResults(weightingTest.getName(), null)){
  				phases.add(r.getInheritedPhase());
  			}
  		}
  		for(Phase p: study.getPhases()) {
  			for(StudyAction a: study.getStudyActions(p)) {
  				if(a.getNamedTreatment()!=null || a.isMeasureWeight()) {
  					phases.add(p);
  					break;
  				}
  			}
  		}
  
  		for (Phase phase : phases) {
  			//Check if we need to display a compound
  			boolean hasCompound2 = false;
  			for (Biosample animal : animals) {
  				Biosample h = bio2history.get(animal).get(phase.getShortName());
  				ActionTreatment t = getTreatment(h);
  				if(t!=null && t.getEff2().length()>0) {
  					hasCompound2 = true;
  				}
  			}
  
  			//Print headers
  			Phase after = phase.getNextPhase();
  			Sheet sheet = createSheet(wb, "BW "+phase.getShortName());
  
  			createHeadersWithTitle(sheet, study, "BW used for given dose " + (after==null?"at " + phase.getShortName(): "from " + phase.getShortName() + " to " + after.getShortName()));
  
  			//Create Header for Weighing & Doses
  			int line = 3;
  			int col = 0;
  
  			set(sheet, line, col++, "Group", Style.S_TH_CENTER);
  			set(sheet, line, col++, "ContainerId", Style.S_TH_CENTER);
  			set(sheet, line, col++, "ParticipantId", Style.S_TH_CENTER);
  			set(sheet, line, col++, "No", Style.S_TH_CENTER);
  
  			set(sheet, line, col++, "Weight [g]", Style.S_TH_CENTER);
  			set(sheet, line, col++, "Increase [%]", Style.S_TH_CENTER);
  
  			set(sheet, line, col++, "Treatment", Style.S_TH_CENTER);
  
  			set(sheet, line, col++, "Given Dose" + (hasCompound2?"1":""), Style.S_TH_CENTER);
  			if(hasCompound2) {
  				set(sheet, line, col++, "Given Dose2", Style.S_TH_CENTER);
  			}
  
  
  			set(sheet, line, col++, "Formulation", Style.S_TH_CENTER);
  			set(sheet, line, col++, "Comments", Style.S_TH_CENTER);
  
  			set(sheet, line, col++, "Date", Style.S_TH_CENTER);
  			int maxCol = col-1;
  			line++;
  
  			//Add Biosample Weighing
  			Group lastGroup = null;
  			ListHashMap<Group, Integer> group2Lines = new ListHashMap<>();
  			for (Biosample animal : animals) {
  				//Gets Weight + Increase
  				Result weighResult = animal==null? null: animal.getAuxResult(weightingTest, phase);
  				Result prevWeighResult = animal==null || weighResult==null?null: Result.getPrevious(weighResult, animal.getAuxResults(DAOTest.WEIGHING_TESTNAME, null));
  
  
  				Double weight = weighResult==null?null: weighResult.getOutputResultValues().get(0).getDoubleValue();
  				Double increase = null;
  				if(weight!=null && prevWeighResult!=null && prevWeighResult.getOutputResultValues().get(0).getDoubleValue()!=null && prevWeighResult.getOutputResultValues().get(0).getDoubleValue()>0) {
  					Double prev = prevWeighResult.getOutputResultValues().get(0).getDoubleValue();
  					increase = 100*(weight - prev)/prev;
  				}
  
  				//Memorize line to compute averages
  				group2Lines.add(animal.getInheritedGroup(), line);
  
  				//Display data
  				Biosample h = bio2history.get(animal).get(phase.getShortName());
  				ActionTreatment t = getTreatment(h);
  
  				col = 0;
  				set(sheet, line, col++, animal==null? null: animal.getInheritedGroupString(SpiritFrame.getUsername()), Style.S_TD_LEFT);
  				set(sheet, line, col++, animal==null? null: animal.getContainerId(), Style.S_TD_CENTER);
  				set(sheet, line, col++, animal==null? null: animal.getTopParent().getSampleId(), Style.S_TD_CENTER);
  				set(sheet, line, col++, animal==null? null: animal.getSampleName(), Style.S_TD_CENTER);
  
  				set(sheet, line, col++, weight, Style.S_TD_DOUBLE1_BLUE);
  				set(sheet, line, col++, increase , Style.S_TD_DOUBLE1);
  
  				set(sheet, line, col++, t==null? null: t.getTreatmentName(), Style.S_TD_LEFT);
  
  				set(sheet, line, col++, t==null? null: t.getEff1(), Style.S_TD_DOUBLE1);
  
  				if(hasCompound2) {
  					set(sheet, line, col++, t==null? null: t.getEff2(), Style.S_TD_DOUBLE1);
  				}
  
  				set(sheet, line, col++, t==null? null: t.getFormulation(), Style.S_TD_LEFT);
  				set(sheet, line, col++, t==null? null: t.getComments(), Style.S_TD_LEFT);
  
  				set(sheet, line, col++, weighResult==null? (h==null? null: h.getUpdDate()): weighResult.getUpdDate(), Style.S_TD_DATE);
  
  
  				//Separator if we change group
  				Group group = animal.getInheritedGroup();
  