Changeset 3525
- Timestamp:
- Oct 5, 2015, 8:52:36 AM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
extensions/net.sf.basedb.reggie/trunk/src/net/sf/basedb/reggie/pdf/PilotReportWorker.java
r3506 r3525 5 5 import java.io.IOException; 6 6 import java.io.OutputStream; 7 import java.text.DecimalFormat; 8 import java.text.DecimalFormatSymbols; 9 import java.text.NumberFormat; 7 10 import java.text.SimpleDateFormat; 8 11 import java.util.Date; 12 import java.util.HashMap; 13 import java.util.List; 9 14 import java.util.Map; 10 15 … … 12 17 import com.itextpdf.text.Element; 13 18 import com.itextpdf.text.pdf.PdfContentByte; 19 import com.itextpdf.text.pdf.StringUtils; 14 20 15 21 import net.sf.basedb.core.BioMaterial; 16 22 import net.sf.basedb.core.BioSource; 17 23 import net.sf.basedb.core.DbControl; 24 import net.sf.basedb.core.Extract; 18 25 import net.sf.basedb.core.Sample; 26 import net.sf.basedb.core.StringUtil; 19 27 import net.sf.basedb.reggie.Reggie; 20 28 import net.sf.basedb.reggie.Site; … … 22 30 import net.sf.basedb.reggie.dao.Annotationtype; 23 31 import net.sf.basedb.reggie.dao.Rawbioassay; 32 import net.sf.basedb.reggie.dao.Rna; 33 import net.sf.basedb.reggie.dao.RnaQc; 24 34 import net.sf.basedb.reggie.dao.Subtype; 25 35 import net.sf.basedb.reggie.logo.Logo; 26 36 import net.sf.basedb.reggie.r.PilotReport; 27 37 import net.sf.basedb.reggie.r.RResult; 38 import net.sf.basedb.util.MD5; 39 import net.sf.basedb.util.NumberFormatUtil; 40 import net.sf.basedb.util.Values; 41 import net.sf.basedb.util.formatter.NumberFormatter; 28 42 29 43 /** … … 48 62 new DateToStringConverter(new SimpleDateFormat("yyyy-MM-dd")); 49 63 50 64 private static final float MARGIN_RIGHT = 579; 65 private static final float MARGIN_LEFT = 14; 66 67 /* PLOT POSITIONS */ 68 // Left X of plots 69 private static final float PLOT_X = 355; //309; 51 70 // Bottom Y of first plot 52 private static final float PLOT_START_Y = 588; 53 71 private static final float PLOT_START_Y = 474.5f; //588; 54 72 // Distance between plots 55 private static final float PLOT_DELTA_Y = 124; 56 57 // Left X of left plot set 58 private static final float PLOT_X1 = 15; 59 60 // Left X of right plot set 61 private static final float PLOT_X2 = 309; 62 73 private static final float PLOT_DELTA_Y = 89.5f; //124; 63 74 // Size of plots 64 private static final float PLOT_WIDTH = 270; 65 private static final float PLOT_HEIGHT = 97; 66 67 // Top right corner of site logo 68 private static final float LOGO_X = 576; 69 private static final float LOGO_Y = 824; 70 // Max size of logo 71 private static final float LOGO_HEIGHT = 72; 72 private static final float LOGO_WIDTH = 180; 73 74 75 private static final float DATA_X = 298; 76 private static final float DATA_START_Y = 786.8f; 77 private static final float DATA_DELTA_Y = 15.2f; 78 75 private static final float PLOT_WIDTH = 160; //270; 76 private static final float PLOT_HEIGHT = 80; //97; 77 78 /* TEXT POSITIONS */ 79 // Left X of text columns 80 private static final float TEXT_X1 = 99; 81 private static final float TEXT_X2 = 191; 82 private static final float TEXT_X3 = 283; 83 private static final float TEXT_X4 = 404; 84 private static final float TEXT_X5 = 495; 85 86 // Bottom Y of text rows 87 private static final float TEXT_Y1 = 801; 88 private static final float TEXT_Y2 = 773; 89 private static final float TEXT_Y3 = 746; 90 private static final float TEXT_Y4 = 718; 91 private static final float TEXT_Y5 = 690; 92 79 93 private PilotReport script; 80 94 private String config; 81 95 private String pdfTemplatePath; 96 private Map<String, String> translations; 97 private NumberFormat oneDecimal; 98 private NumberFormat twoDecimals; 82 99 83 100 PilotReportWorker(PdfReportTemplate template, String config) … … 101 118 pdfTemplatePath = Reggie.getConfig().getConfig(config+"/template", null, script.getScriptDir() + "/template.pdf"); 102 119 Reggie.checkFile(pdfTemplatePath, false); 120 oneDecimal = createNumberFormat(1); 121 twoDecimals = createNumberFormat(2); 103 122 } 104 123 PilotReport.Result result = script.run(dc, raw); … … 116 135 Rawbioassay raw = result.raw; 117 136 118 Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.PATIENT, Subtype.SPECIMEN); 137 Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.PATIENT, Subtype.SPECIMEN, Subtype.CASE, Subtype.RNA); 138 Extract rna = (Extract)parents.get(Subtype.RNA); 119 139 Sample specimen = (Sample)parents.get(Subtype.SPECIMEN); 140 Sample theCase = (Sample)parents.get(Subtype.CASE); 120 141 BioSource patient = (BioSource)parents.get(Subtype.PATIENT); 142 Site site = Site.findByCaseName(raw.getName()); 121 143 122 144 PdfUtil pdfUtil = null; … … 136 158 if (f2.exists()) 137 159 { 138 pdfUtil.importPdf(new FileInputStream(f2), PLOT_X 2, y, 1.0f, 1.0f);160 pdfUtil.importPdf(new FileInputStream(f2), PLOT_X, y, 1.0f, 1.0f); 139 161 y -= PLOT_DELTA_Y; 140 162 } 141 163 } 142 164 143 // Include site logo 144 Site site = Site.findByCaseName(raw.getName()); 145 if (site.getLogo() != null) 165 166 // Patient and sample information 167 // Row 2 168 if (patient != null) pdfUtil.addText(patient.getName(), 12, Element.ALIGN_LEFT, TEXT_X1, TEXT_Y2); 169 if (theCase != null) pdfUtil.addText(theCase.getName(), 12, Element.ALIGN_LEFT, TEXT_X2, TEXT_Y2); 170 if (specimen != null) pdfUtil.addText(specimen.getName(), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y2); 171 172 // Row 3 173 String consent = (String)Annotationtype.CONSENT.getAnnotationValue(dc, theCase); 174 Date consentDate = (Date)Annotationtype.CONSENT_DATE.getAnnotationValue(dc, theCase); 175 pdfUtil.addText(translate(consent), 12, Element.ALIGN_LEFT, TEXT_X1, TEXT_Y3); 176 pdfUtil.addText(DATE_FORMAT.convert(consentDate), 12, Element.ALIGN_LEFT, TEXT_X2, TEXT_Y3); 177 if (rna != null) 146 178 { 147 Logo logo = site.getLogo(); 148 pdfUtil.importImage(logo.getLogo(), LOGO_X, LOGO_Y, LOGO_WIDTH, LOGO_HEIGHT, Element.ALIGN_RIGHT+Element.ALIGN_TOP); 179 List<RnaQc> rnaqc = RnaQc.findByRna(dc, Rna.get(rna)); 180 RnaQc lastRnaQc = RnaQc.findLast(dc, null, rnaqc); 181 if (lastRnaQc != null) 182 { 183 Float rqs = (Float)Annotationtype.CA_RQS.getAnnotationValue(dc, lastRnaQc.getItem()); 184 Float rin = (Float)Annotationtype.BA_RIN.getAnnotationValue(dc, lastRnaQc.getItem()); 185 if (rqs != null || rin != null) 186 { 187 pdfUtil.addText(oneDecimal.format((double)(rqs != null ? rqs : rin)), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y3); 188 } 189 } 149 190 } 150 191 151 // TODO -- remove when we have a real pilot report 152 // Write white rectangle to cover the "Genrapport" text 153 // and then write "Pilotrapport" on top of it 154 PdfContentByte canvas = pdfUtil.getCanvas(); 155 canvas.saveState(); 156 canvas.setColorFill(BaseColor.WHITE); 157 canvas.rectangle(DATA_X - 100, DATA_START_Y+DATA_DELTA_Y, 200, DATA_DELTA_Y*2); 158 canvas.fillStroke(); 159 canvas.restoreState(); 160 pdfUtil.addText("Pilotrapport", 24, Element.ALIGN_CENTER, DATA_X, DATA_START_Y + 1.5f*DATA_DELTA_Y); 161 // ------ 162 163 // Patient and sample information 164 pdfUtil.addText(DATE_FORMAT.convert(new Date()), 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y); 165 if (site != Site.UNKNOWN) 166 { 167 pdfUtil.addText(site.getName(), 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y - DATA_DELTA_Y); 168 } 169 if (patient != null) 170 { 171 pdfUtil.addText(patient.getName(), 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y - 2 * DATA_DELTA_Y); 172 } 173 pdfUtil.addText(specimen != null ? specimen.getName() : raw.getName().split("\\.(?!\\d)")[0], 12, Element.ALIGN_LEFT, DATA_X, DATA_START_Y - 3 * DATA_DELTA_Y); 192 // Row 4 193 pdfUtil.addText(DATE_FORMAT.convert((Date)Annotationtype.SAMPLING_DATETIME.getAnnotationValue(dc, specimen)), 12, Element.ALIGN_LEFT, TEXT_X1, TEXT_Y4); 194 pdfUtil.addText(DATE_FORMAT.convert((Date)Annotationtype.ARRIVAL_DATE.getAnnotationValue(dc, specimen)), 12, Element.ALIGN_LEFT, TEXT_X2, TEXT_Y4); 195 pdfUtil.addText(DATE_FORMAT.convert(new Date()), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y4); 196 197 // Row 5 198 String laterality = (String)Annotationtype.LATERALITY.getAnnotationValue(dc, specimen); 199 pdfUtil.addText(translate(laterality), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y5); 200 if (site != Site.UNKNOWN) pdfUtil.addText(site.getName(), 12, Element.ALIGN_LEFT, TEXT_X5, TEXT_Y5); 174 201 175 202 // Extra info at bottom 176 pdfUtil.addText(raw.getName(), 8, Element.ALIGN_RIGHT, PLOT_X2+PLOT_WIDTH, 10);203 pdfUtil.addText(raw.getName(), 8, Element.ALIGN_RIGHT, MARGIN_RIGHT, 10); 177 204 } 178 205 finally … … 193 220 Map<Subtype, BioMaterial> parents = raw.findParentBioMaterial(dc, Subtype.SPECIMEN, Subtype.PATIENT); 194 221 BioSource patient = (BioSource)parents.get(Subtype.PATIENT); 195 Sample speciemen = (Sample)parents.get(Subtype.SPECIMEN); 196 197 198 float X = PilotReportWorker.PLOT_X1 + PilotReportWorker.PLOT_WIDTH / 2; 199 float Y = PilotReportWorker.PLOT_START_Y + PilotReportWorker.PLOT_DELTA_Y / 2; 200 PdfContentByte canvas = pdf.getCanvas(); 201 canvas.saveState(); 202 canvas.setColorFill(new BaseColor(255, 255, 204)); 203 canvas.rectangle(PilotReportWorker.PLOT_X1, PilotReportWorker.PLOT_START_Y, PilotReportWorker.PLOT_WIDTH, PilotReportWorker.PLOT_HEIGHT); 204 canvas.fillStroke(); 205 canvas.restoreState(); 222 Sample specimen = (Sample)parents.get(Subtype.SPECIMEN); 223 224 pdf.addText(DATE_FORMAT.convert(new Date()), 8, Element.ALIGN_LEFT, MARGIN_LEFT, 10); 206 225 207 226 if (patient != null) 208 227 { 209 pdf.addText(patient.getName(), 12, Element.ALIGN_CENTER, X, Y); 210 pdf.addText((String)Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, patient)+" "+Annotationtype.FAMILY_NAME.getAnnotationValue(dc, patient), 12, Element.ALIGN_CENTER, X, Y-12); 211 pdf.addText((String)Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient), 12, Element.ALIGN_CENTER, X, Y-24); 228 pdf.addText((String)Annotationtype.PERSONAL_NUMBER.getAnnotationValue(dc, patient), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y1); 229 pdf.addText((String)Annotationtype.FAMILY_NAME.getAnnotationValue(dc, patient), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y2); 230 pdf.addText((String)Annotationtype.ALL_FIRST_NAMES.getAnnotationValue(dc, patient), 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y3); 231 pdf.addText((String)Annotationtype.PAD.getAnnotationValue(dc, specimen), 12, Element.ALIGN_LEFT, TEXT_X3, TEXT_Y5); 212 232 } 213 233 else 214 234 { 215 pdf.addText("No patient information", 12, Element.ALIGN_CENTER, X, Y); 216 } 217 } 218 235 pdf.addText("No patient information", 12, Element.ALIGN_LEFT, TEXT_X4, TEXT_Y1); 236 } 237 } 238 239 private String translate(String word) 240 { 241 if (word == null) return null; 242 if (translations == null) 243 { 244 translations = new HashMap<String, String>(); 245 translations.put("left", "Vänster"); 246 translations.put("right", "Höger"); 247 translations.put("yes", "Ja"); 248 translations.put("no", "Nej"); 249 } 250 251 String translatedWord = translations.get(word.toLowerCase()); 252 return translatedWord == null ? word : translatedWord; 253 } 254 255 private NumberFormat createNumberFormat(int numDecimals) 256 { 257 DecimalFormatSymbols sym = new DecimalFormatSymbols(); 258 sym.setDecimalSeparator(','); 259 String format = "0"; 260 if (numDecimals > 0) format += "." + MD5.leftPad("", '0', numDecimals); 261 DecimalFormat df = new DecimalFormat(format, sym); 262 return df; 263 } 219 264 220 265 }
Note: See TracChangeset
for help on using the changeset viewer.