protected BandData createNewBand(String bandName) { BandData newParentBand = new BandData(bandName); List<BandData> childrenList = root.getChildrenByName(bandName); root.getChildrenBands().remove(bandName); newParentBand.addChildren(childrenList); childrenList.forEach(childBand -> childBand.setParentBand(newParentBand)); visitedBands.put(newParentBand, newParentBand.getChildrenList().iterator()); return newParentBand; }
public List<BandData> findBandsRecursively(String name) { BandData firstBand = findBandRecursively(name); if (firstBand == null) { return Collections.emptyList(); } List<BandData> allBands = firstBand.getParentBand() != null ? firstBand.getParentBand().getChildrenByName(name) : Collections.singletonList(firstBand); return allBands; }
protected BandData findBandByPath(String path) { if (rootBand.getName().equals(path)) return rootBand; String[] pathParts = path.split("\\."); BandData currentBand = rootBand; for (String pathPart : pathParts) { if (currentBand == null) return null; currentBand = currentBand.findBandRecursively(pathPart); } return currentBand; }
@Test public void testDocx() throws Exception { BandData root = createRootBand(); root.addReportFieldFormats(Collections.singletonList(new ReportFieldFormatImpl("Band1.col2", "${html}"))); BandData footer = root.getChildByName("Footer"); BandData footerChild = new BandData("FooterChild", footer); footerChild.addData("nestedData", "NESTED_DATA"); footerChild.addData("nestedData.withPoint", "NESTED_DATA_WITH_POINT"); footer.addChild(footerChild); FileOutputStream outputStream = new FileOutputStream("./result/smoke/result.docx"); ReportFormatter formatter = new DefaultFormatterFactory().createFormatter(new FormatterFactoryInput("docx", root, new ReportTemplateImpl("", "./modules/core/test/smoketest/test.docx", "./modules/core/test/smoketest/test.docx", ReportOutputType.docx), outputStream)); formatter.renderDocument(); IOUtils.closeQuietly(outputStream); }
@Test public void testDataLinkage() { Report report = createReport(); BandData rootBand = new BandData(BandData.ROOT_BAND_NAME); rootBand.setData(new HashMap<>()); rootBand.addReportFieldFormats(report.getReportFieldFormats()); rootBand.setFirstLevelBandDefinitionNames(new HashSet<>()); new DataExtractorImpl(new DefaultLoaderFactory().setGroovyDataLoader( new GroovyDataLoader(new DefaultScriptingImpl()))).extractData(report, new HashMap<>(), rootBand); List<BandData> bands = rootBand.getChildrenByName("Band1"); for (BandData band : bands) { if (((Integer)1).equals(band.getParameterValue("link"))) { Assert.assertEquals(1, band.getParameterValue("col1")); Assert.assertEquals(10, band.getParameterValue("col2")); Assert.assertEquals(100, band.getParameterValue("col3")); } else if (((Integer)2).equals(band.getParameterValue("link"))) { Assert.assertEquals(2, band.getParameterValue("col1")); Assert.assertEquals(20, band.getParameterValue("col2")); Assert.assertEquals(200, band.getParameterValue("col3")); } } }
protected Range getLastRenderedBandForThisLevel(BandData band) { List<BandData> sameLevelBands = band.getParentBand().getChildrenByName(band.getName()); for (BandData sameLevelBand : sameLevelBands) { Range range = bandsForRanges.resultForBand(sameLevelBand); if (range != null) { return lastRenderedRangeForBandName.get(band.getName()); } } return null; }
String fullParameterName = band.getName() + "." + bandAndParameter.getParameterName(); Object parameterValue = band.getParameterValue(bandAndParameter.getParameterName());
@SuppressWarnings("unchecked") protected String processQueryTemplate(String query, BandData parentBand, Map<String, Object> reportParams) { try { GStringTemplateEngine engine = new GStringTemplateEngine(); Map bindings = new HashMap(); if (reportParams != null) { bindings.putAll(reportParams); } while (parentBand != null) { if (parentBand.getData() != null) { bindings.put(parentBand.getName(), parentBand.getData()); } parentBand = parentBand.getParentBand(); } return engine.createTemplate(query).make(bindings).toString(); } catch (ClassNotFoundException | IOException e) { throw new DataLoadingException(String.format("An error occurred while loading processing query template [%s]", query), e); } }
protected Row findNextRowForHBand(BandData band, Range templateRange, List<Row> resultSheetRows) { Row firstRow = null; boolean isFirstLevelBand = BandData.ROOT_BAND_NAME.equals(band.getParentBand().getName()); //we suppose that when we render HORIZONTAL first level band, it should not be any right offset if (isFirstLevelBand || (previousRangeBandData != null && !previousRangeBandData.getParentBand().equals(band.getParentBand()))) { setPreviousRangeVerticalOffset(0, null); } Range lastRenderedRange = getLastRenderedBandForThisLevel(band); if (lastRenderedRange != null) {//this band has been already rendered at least once BandData lastRenderedBand = bandsForRanges.bandForResultRange(lastRenderedRange); LastRowBandVisitor bandVisitor = new LastRowBandVisitor(); lastRenderedBand.visit(bandVisitor); if (resultSheetRows.size() > bandVisitor.lastRow) {//get next row firstRow = resultSheetRows.get(bandVisitor.lastRow); } } else if (!isFirstLevelBand) { firstRow = findNextRowForChildBand(band, templateRange, resultSheetRows); } else {//this is the first render firstRow = findNextRowForFirstRender(templateRange, resultSheetRows); } return firstRow; }
protected void insertValue(XText text, XTextRange textRange, BandData band, String parameterName) { checkThreadInterrupted(); String fullParameterName = band.getName() + "." + parameterName; Object paramValue = band.getParameterValue(parameterName); Map<String, ReportFieldFormat> formats = rootBand.getReportFieldFormats(); try { boolean handled = false; if (paramValue != null) { if ((formats != null) && (formats.containsKey(fullParameterName))) { String format = formats.get(fullParameterName).getFormat(); // Handle doctags for (ContentInliner contentInliner : contentInliners) { Matcher matcher = contentInliner.getTagPattern().matcher(format); if (matcher.find()) { contentInliner.inlineToDoc(officeComponent, textRange, text, paramValue, matcher); handled = true; } } } if (!handled) { String valueString = formatValue(paramValue, parameterName, fullParameterName); text.insertString(textRange, valueString, true); } } else { text.insertString(textRange, "", true); } } catch (Exception ex) { throw wrapWithReportingException(String.format("An error occurred while inserting parameter [%s] into text line [%s]", parameterName, text.getString()), ex); } }
protected void addParentBandDataToParameters(BandData parentBand, Map<String, Object> currentParams) { if (parentBand != null) { String parentBandName = parentBand.getName(); for (Map.Entry<String, Object> entry : parentBand.getData().entrySet()) { currentParams.put(parentBandName + "." + entry.getKey(), entry.getValue()); } } } }
@Test public void testXls() throws Exception { BandData root = createRootBand(); BandData date = new BandData("Date", root); BandData dateHeader = new BandData("DateHeader", root); date.addData("date", new Date()); root.addChild(dateHeader); root.addChild(date); List<BandData> band1objects = root.getChildrenByName("Band1"); for (int i = 0; i < band1objects.size(); i++) { BandData bandData = band1objects.get(i); if (i % 2 == 0) { bandData.addData("theStyle", "red"); } } FileOutputStream outputStream = new FileOutputStream("./result/smoke/result.xls"); ReportFormatter formatter = new DefaultFormatterFactory().createFormatter(new FormatterFactoryInput("xls", root, new ReportTemplateImpl("", "./modules/core/test/smoketest/test.xls", "./modules/core/test/smoketest/test.xls", ReportOutputType.xls), outputStream)); formatter.renderDocument(); IOUtils.closeQuietly(outputStream); }
protected List<KeyValueEntity> getEntries(PivotTableDescription configuration) { List<BandData> childrenByName = rootBand.getChildrenByName(configuration.getBandName()); if (childrenByName == null) return Collections.emptyList(); return childrenByName.stream() .filter(band -> band.getData() != null && !band.getData().isEmpty()) .map(band -> { KeyValueEntity entity = new KeyValueEntity(); band.getData().forEach(entity::setValue); return entity; }) .collect(Collectors.toList()); } }
protected BandData loadBandData(Report report, Map<String, Object> handledParams) { BandData rootBand = new BandData(BandData.ROOT_BAND_NAME); rootBand.setData(new HashMap<>(handledParams)); rootBand.addReportFieldFormats(report.getReportFieldFormats()); rootBand.setFirstLevelBandDefinitionNames(new HashSet<>()); dataExtractor.extractData(report, handledParams, rootBand); return rootBand; }
@Test public void testFormats() throws Exception { BandData root = new BandData("Root", null, BandOrientation.HORIZONTAL); HashMap<String, Object> rootData = new HashMap<String, Object>(); root.setData(rootData); BandData band1 = new BandData("Band1", root, BandOrientation.HORIZONTAL); band1.addData("date", new SimpleDateFormat("dd-MM-yyyy").parse("12-04-1961")); root.addChild(band1); FileOutputStream outputStream = new FileOutputStream("./result/integration/result-formats.xls"); ReportFormatter formatter = new DefaultFormatterFactory().createFormatter(new FormatterFactoryInput("xls", root, new ReportTemplateImpl("", "./modules/core/test/integration/test-formats.xls", "./modules/core/test/integration/test-formats.xls", ReportOutputType.xls), outputStream)); formatter.renderDocument(); IOUtils.closeQuietly(outputStream); compareFiles("./modules/core/test/integration/etalon-formats.xls", "./result/integration/result-formats.xls"); }
@Override protected BandData wrapData(ExtractionContext context, Map<String, Object> data) { final BandData bandData = new BandData(context.getBand().getName(), context.getParentBandData(), BandOrientation.VERTICAL); bandData.setData(ObjectUtils.defaultIfNull(data, new HashMap<>())); return bandData; }
/** * Accepts root element. * Goes down one level because root must not have elements. * * @param root of the tree */ public JRBandDataDataSource(BandData root) { this.root = root; currentIterator = root.getChildrenList().iterator(); visitedBands.put(root, currentIterator); currentBand = root; }
public Object getParameter(String name) { return getParameterValue(name); }
protected void fillTable(String name, BandData parentBand, TableManager tableManager, XDispatchHelper xDispatchHelper, int numberOfRowWithAliases) throws com.sun.star.uno.Exception { // Lock clipboard, cause uno uses it to grow tables, relevant for desktops synchronized (clipboardLock) { XTextTable xTextTable = tableManager.getXTextTable(); if (officeIntegration.isDisplayDeviceAvailable()) { clearClipboard(); } List<BandData> childrenByName = parentBand.getChildrenByName(name); for (BandData ignored : childrenByName) { tableManager.copyRow(xDispatchHelper, as(XTextDocument.class, xComponent).getCurrentController(), numberOfRowWithAliases); } int i = numberOfRowWithAliases; for (BandData child : childrenByName) { if (name.equals(child.getName())) { fillRow(child, tableManager, i); i++; } } tableManager.deleteRow(i); } }