public void setup() throws Exception { setupProperties(); ReportDefinition rd = createReportDefinition(); ReportDesign design = h.createRowPerPatientXlsOverviewReportDesign( rd, "ChemotherapyDailyExpectedPatientList.xls", "ChemotherapyDailyPatientList.xls_", null); Properties props = new Properties(); props.put("repeatingSections", "sheet:1,row:7,dataset:dataSet"); props.put("sortWeight", "5000"); design.setProperties(props); h.saveReportDesign(design); ReportDesign designTwo = h.createRowPerPatientXlsOverviewReportDesign( rd, "ChemotherapyDailyTreatmentSummary.xls", "ChemotherapyDailyTreatmentSummary.xls_", null); Properties propsTwo = new Properties(); propsTwo.put("repeatingSections", "sheet:1,row:7,dataset:dataSet"); props.put("sortWeight", "5000"); designTwo.setProperties(propsTwo); h.saveReportDesign(designTwo); }
public void delete() { ReportService rs = Context.getService(ReportService.class); for (ReportDesign rd : rs.getAllReportDesigns(false)) { if ("ChemotherapyDailyPatientList.xls_".equals(rd.getName()) || "ChemotherapyDailyTreatmentSummary.xls_".equals(rd.getName())) { rs.purgeReportDesign(rd); } } h.purgeReportDefinition("ONC-Chemotherapy Daily Expected Patient List"); }
/** Tests that the Report and associated ReportDesigns are successfully saved */ @Test public void shouldSetupReport() throws Exception { List<ReportDefinition> l = getReportDefinitionService().getDefinitions(getReportName(), true); Assert.assertEquals(0, l.size()); ReportDefinition reportDefinition = setupReport(); Assert.assertEquals(getReportName(), reportDefinition.getName()); ReportService rs = Context.getService(ReportService.class); List<ReportDesign> designs = rs.getReportDesigns(reportDefinition, null, false); Assert.assertEquals(getReportDesignNames().size(), designs.size()); int numMatches = 0; for (ReportDesign rd : designs) { if (getReportDesignNames().contains(rd.getName())) { numMatches++; } } Assert.assertEquals(getReportDesignNames().size(), numMatches); }
@Override public ReportDesign getReportDesign() { ReportDesign design = new ReportDesign(); design.setName("MOH 361A Register Design"); design.setReportDefinition(this.getReportDefinition()); design.setRendererType(ExcelTemplateRenderer.class); Properties props = new Properties(); props.put("repeatingSections", "sheet:2,row:4,dataset:allPatients"); design.setProperties(props); ReportDesignResource resource = new ReportDesignResource(); resource.setName("template.xls"); InputStream is = OpenmrsClassLoader.getInstance() .getResourceAsStream("templates/MOH361AReportTemplate_0_2.xls"); if (is == null) throw new APIException("Could not find report template."); try { resource.setContents(IOUtils.toByteArray(is)); } catch (IOException ex) { throw new APIException("Could not create report design for MOH 361A Register.", ex); } IOUtils.closeQuietly(is); design.addResource(resource); return design; }
/** * @return a Map of String to String that can be used to find repeating sections This converts a * user design property in the format: sheet:3,dataset:allPatients | * sheet:1,row:6-8,dataset:allPatients | sheet:2,column:4,dataset:malePatients into a Map * which can be quickly accessed as each row / column combination is accessed during * processing */ protected Map<String, String> getRepeatingSections(ReportDesign design) { Map<String, String> m = new HashMap<String, String>(); String propertyValue = design.getPropertyValue("repeatingSections", null); if (propertyValue != null) { for (String sectionConfig : propertyValue.split("\\|")) { try { Integer sheetNum = null; Integer rowNum = null; Integer columnNum = null; Integer spanNum = null; String dataSetName = null; for (String sectionComponent : sectionConfig.split(",")) { String[] keyValue = sectionComponent.split("\\:"); String key = keyValue[0].trim().toLowerCase(); String[] valueSplit = keyValue[1].trim().split("\\-"); String lowerBound = valueSplit[0].trim(); String upperBound = valueSplit.length == 1 ? lowerBound : valueSplit[1].trim(); if ("sheet".equals(key)) { sheetNum = Integer.parseInt(lowerBound); } else if ("row".equals(key)) { rowNum = Integer.parseInt(lowerBound); spanNum = Integer.parseInt(upperBound) - rowNum + 1; } else if ("column".equals(key)) { columnNum = Integer.parseInt(lowerBound); spanNum = Integer.parseInt(upperBound) - columnNum + 1; } else if ("dataset".equals(key)) { dataSetName = lowerBound; } } String key = "repeatSheet" + sheetNum + (rowNum != null ? "Row" + rowNum : "") + (columnNum != null ? "Column" + columnNum : ""); String value = dataSetName + (spanNum != null ? "," + spanNum : ""); m.put(key, value); } catch (Exception e) { log.warn( "Error in configuration of repeating sections of ExcelTemplateRenderer. Please check your configuration.", e); } } } return m; }
@RequestMapping(method = RequestMethod.POST) public void process( final @RequestParam(required = false, value = "cohort") Integer cohortId, final @RequestParam(required = true, value = "template") MultipartFile template, final HttpServletResponse response) throws IOException, EvaluationException { EvaluationContext context = new EvaluationContext(); context.addParameterValue("currentDate", new Date()); if (cohortId != null) { context.setBaseCohort(Context.getCohortService().getCohort(cohortId)); } PatientDataSetDefinition definition = new PatientDataSetDefinition(); definition.addColumn( "id", new PatientIdDataDefinition(), StringUtils.EMPTY, new ObjectFormatter()); ListConverter listConverter = new ListConverter(); listConverter.setMaxNumberOfItems(1); PatientIdentifierDataDefinition preferredIdentifier = new PatientIdentifierDataDefinition(); preferredIdentifier.addType(Context.getPatientService().getPatientIdentifierType(1)); definition.addColumn("identifier", preferredIdentifier, StringUtils.EMPTY, listConverter); definition.addColumn( "name", new PreferredNameDataDefinition(), StringUtils.EMPTY, new ObjectFormatter("{familyName}, {givenName}")); AgeDataDefinition ageOnDate = new AgeDataDefinition(); ageOnDate.addParameter(new Parameter("effectiveDate", "effective date", Date.class)); definition.addColumn("age", ageOnDate, "effectiveDate=${currentDate}", new AgeConverter()); definition.addColumn( "birthdate", new BirthdateDataDefinition(), StringUtils.EMPTY, new BirthdateConverter("dd-MMM-yyyy")); definition.addColumn( "gender", new GenderDataDefinition(), StringUtils.EMPTY, new ObjectFormatter()); ReportDefinition reportDefinition = new ReportDefinition(); reportDefinition.setName("Test Report"); reportDefinition.addDataSetDefinition("PatientDataSetDefinition", definition, null); final ReportDesign design = new ReportDesign(); design.setName("Test Report Design With Excel Template Renderer"); design.setReportDefinition(reportDefinition); design.setRendererType(ExcelTemplateRenderer.class); Properties properties = new Properties(); properties.put("repeatingSections", "sheet:1,dataset:PatientDataSetDefinition"); design.setProperties(properties); ReportDesignResource resource = new ReportDesignResource(); resource.setName("excel-template.xls"); InputStream inputStream = template.getInputStream(); resource.setContents(IOUtils.toByteArray(inputStream)); IOUtils.closeQuietly(inputStream); design.addResource(resource); ExcelTemplateRenderer renderer = new ExcelTemplateRenderer() { public ReportDesign getDesign(String argument) { return design; } }; ReportDefinitionService rs = Context.getService(ReportDefinitionService.class); ReportData data = rs.evaluate(reportDefinition, context); CsvReportRenderer csvReportRenderer = new CsvReportRenderer(); csvReportRenderer.render(data, "output:csv", System.out); File file = File.createTempFile("excel", "summary"); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(file)); renderer.render(data, "output:xls", bufferedOutputStream); bufferedOutputStream.close(); response.setHeader("Content-Disposition", "attachment; filename=patient-summary.xls"); response.setContentType("application/vnd.ms-excel"); response.setContentLength((int) file.length()); FileCopyUtils.copy(new FileInputStream(file), response.getOutputStream()); if (file.delete()) log.info("Temporary file deleted!"); }