protected Map<String, Object> handleParameters(Report report, Map<String, Object> params) { Map<String, Object> handledParams = new HashMap<String, Object>(params); for (ReportParameter reportParameter : report.getReportParameters()) { String paramName = reportParameter.getAlias(); Object parameterValue = handledParams.get(paramName); if (reportParameter instanceof ReportParameterWithDefaultValue) { String parameterDefaultValue = ((ReportParameterWithDefaultValue) reportParameter).getDefaultValue(); if (parameterValue == null && parameterDefaultValue != null) { parameterValue = objectToStringConverter.convertFromString(reportParameter.getParameterClass(), parameterDefaultValue); handledParams.put(paramName, parameterValue); } } if (Boolean.TRUE.equals(reportParameter.getRequired()) && parameterValue == null) { throw new IllegalArgumentException(format("Required report parameter \"%s\" not found", paramName)); } if (!handledParams.containsKey(paramName)) {//make sure map contains all user parameters, even if value == null handledParams.put(paramName, null); } } return handledParams; }
public RunParams(Report report) { this.report = report; this.reportTemplate = report.getReportTemplates().get(ReportTemplate.DEFAULT_TEMPLATE_CODE); }
protected void logReport(String caption, Report report, Map<String, Object> params) { StringBuilder parametersString = new StringBuilder(); for (Map.Entry<String, Object> entry : params.entrySet()) { parametersString.append("\n").append(entry.getKey()).append(":").append(entry.getValue()); } logger.info(format(caption, report.getName(), parametersString)); }
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; }
protected void writeRootBand(Report report, Element root) { ReportBand rootBandDefinition = report.getRootBand(); Element rootBandDefinitionElement = root.addElement("rootBand"); writeBandDefinition(rootBandDefinitionElement, rootBandDefinition); }
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; }
protected void writeRootBand(Report report, Element root) { ReportBand rootBandDefinition = report.getRootBand(); Element rootBandDefinitionElement = root.addElement("rootBand"); writeBandDefinition(rootBandDefinitionElement, rootBandDefinition); }
protected Map<String, Object> handleParameters(Report report, Map<String, Object> params) { Map<String, Object> handledParams = new HashMap<String, Object>(params); for (ReportParameter reportParameter : report.getReportParameters()) { String paramName = reportParameter.getAlias(); Object parameterValue = handledParams.get(paramName); if (reportParameter instanceof ReportParameterWithDefaultValue) { String parameterDefaultValue = ((ReportParameterWithDefaultValue) reportParameter).getDefaultValue(); if (parameterValue == null && parameterDefaultValue != null) { parameterValue = objectToStringConverter.convertFromString(reportParameter.getParameterClass(), parameterDefaultValue); handledParams.put(paramName, parameterValue); } } if (Boolean.TRUE.equals(reportParameter.getRequired()) && parameterValue == null) { throw new IllegalArgumentException(format("Required report parameter \"%s\" not found", paramName)); } if (!handledParams.containsKey(paramName)) {//make sure map contains all user parameters, even if value == null handledParams.put(paramName, null); } } return handledParams; }
public RunParams(Report report) { this.report = report; this.reportTemplate = report.getReportTemplates().get(ReportTemplate.DEFAULT_TEMPLATE_CODE); }
protected void writeValueFormats(Report report, Element root) { Element reportTemplatesElement = root.addElement("formats"); for (ReportFieldFormat reportFieldFormat : report.getReportFieldFormats()) { Element reportTemplateElement = reportTemplatesElement.addElement("format"); reportTemplateElement.addAttribute("name", reportFieldFormat.getName()); reportTemplateElement.addAttribute("format", reportFieldFormat.getFormat()); } }
protected void logReport(String caption, Report report, Map<String, Object> params) { StringBuilder parametersString = new StringBuilder(); for (Map.Entry<String, Object> entry : params.entrySet()) { parametersString.append("\n").append(entry.getKey()).append(":").append(entry.getValue()); } logger.info(format(caption, report.getName(), parametersString)); }
public void extractData(Report report, Map<String, Object> params, BandData rootBand) { List<Map<String, Object>> rootBandData = controllerFactory.defaultController().extractData( contextFactory.context(report.getRootBand(), null, params) ); if (CollectionUtils.isNotEmpty(rootBandData)) { rootBand.getData().putAll(rootBandData.get(0)); } List<ReportBand> firstLevelBands = report.getRootBand().getChildren(); if (firstLevelBands != null) { for (ReportBand definition : firstLevelBands) { List<BandData> bands = createBands(definition, rootBand, params); rootBand.addChildren(bands); rootBand.getFirstLevelBandDefinitionNames().add(definition.getName()); } } }
private static Map<String, Object> parseReportParams(CommandLine cmd, Report report) { if (cmd.hasOption(REPORT_PARAMETER)) { Map<String, Object> params = new HashMap<String, Object>(); Properties optionProperties = cmd.getOptionProperties(REPORT_PARAMETER); for (ReportParameter reportParameter : report.getReportParameters()) { String paramValueStr = optionProperties.getProperty(reportParameter.getAlias()); if (paramValueStr != null) { params.put(reportParameter.getAlias(), converter.convertFromString(reportParameter.getParameterClass(), paramValueStr)); } } return params; } else { return Collections.emptyMap(); } }
/** * Setup necessary template by string code. Throws validation exception if code is null or template not found * @param templateCode - string code of template */ public RunParams templateCode(String templateCode) { if (templateCode == null) { throw new NullPointerException("\"templateCode\" parameter can not be null"); } this.reportTemplate = report.getReportTemplates().get(templateCode); if (reportTemplate == null) { throw new NullPointerException(String.format("Report template not found for code [%s]", templateCode)); } return this; }
protected void writeValueFormats(Report report, Element root) { Element reportTemplatesElement = root.addElement("formats"); for (ReportFieldFormat reportFieldFormat : report.getReportFieldFormats()) { Element reportTemplateElement = reportTemplatesElement.addElement("format"); reportTemplateElement.addAttribute("name", reportFieldFormat.getName()); reportTemplateElement.addAttribute("format", reportFieldFormat.getFormat()); } }
@Override public String buildXml(Report report) { try { Document document = DocumentFactory.getInstance().createDocument(); Element root = document.addElement("report"); root.addAttribute("name", report.getName()); writeTemplates(report, root); writeInputParameters(report, root); writeValueFormats(report, root); writeRootBand(report, root); StringWriter stringWriter = new StringWriter(); new XMLWriter(stringWriter, OutputFormat.createPrettyPrint()).write(document); return stringWriter.toString(); } catch (IOException e) { throw new ReportingException(e); } }
public void extractData(Report report, Map<String, Object> params, BandData rootBand) { List<Map<String, Object>> rootBandData = controllerFactory.defaultController().extractData( contextFactory.context(report.getRootBand(), null, params) ); if (CollectionUtils.isNotEmpty(rootBandData)) { rootBand.getData().putAll(rootBandData.get(0)); } List<ReportBand> firstLevelBands = report.getRootBand().getChildren(); if (firstLevelBands != null) { for (ReportBand definition : firstLevelBands) { List<BandData> bands = createBands(definition, rootBand, params); rootBand.addChildren(bands); rootBand.getFirstLevelBandDefinitionNames().add(definition.getName()); } } }
private static Map<String, Object> parseReportParams(CommandLine cmd, Report report) { if (cmd.hasOption(REPORT_PARAMETER)) { Map<String, Object> params = new HashMap<String, Object>(); Properties optionProperties = cmd.getOptionProperties(REPORT_PARAMETER); for (ReportParameter reportParameter : report.getReportParameters()) { String paramValueStr = optionProperties.getProperty(reportParameter.getAlias()); if (paramValueStr != null) { params.put(reportParameter.getAlias(), converter.convertFromString(reportParameter.getParameterClass(), paramValueStr)); } } return params; } else { return Collections.emptyMap(); } }
/** * Setup necessary template by string code. Throws validation exception if code is null or template not found * @param templateCode - string code of template */ public RunParams templateCode(String templateCode) { if (templateCode == null) { throw new NullPointerException("\"templateCode\" parameter can not be null"); } this.reportTemplate = report.getReportTemplates().get(templateCode); if (reportTemplate == null) { throw new NullPointerException(String.format("Report template not found for code [%s]", templateCode)); } return this; }
@Test public void stressTest() throws IOException, URISyntaxException { int queries = 100; int recordsPerQuery = 10000; Report report = createReport(queries, recordsPerQuery); BandData rootBand = new BandData(BandData.ROOT_BAND_NAME); rootBand.setData(new HashMap<>()); rootBand.addReportFieldFormats(report.getReportFieldFormats()); rootBand.setFirstLevelBandDefinitionNames(new HashSet<>()); long start = System.currentTimeMillis(); try { new DataExtractorImpl(new DefaultLoaderFactory().setGroovyDataLoader( new GroovyDataLoader(new DefaultScriptingImpl()))).extractData(report, new HashMap<>(), rootBand); } finally { System.out.println( String.format("Report processing stress test (%d queries and %d records per query) took %d ms", queries, recordsPerQuery, System.currentTimeMillis() - start) ); } }