From b7f40f2f9c66626c4f1fd7780b55aa668fcff997 Mon Sep 17 00:00:00 2001 From: "Katarina Valalikova (katkav)" Date: Thu, 26 Feb 2015 15:27:25 +0100 Subject: [PATCH] small clean up - report handler --- .../report/impl/ReportCreateTaskHandler.java | 808 ++++++++---------- .../report/impl/ReportManagerImpl.java | 2 +- .../midpoint/report/BasicReportTest.java | 4 +- .../evolveum/midpoint/report/TestReport.java | 4 +- 4 files changed, 347 insertions(+), 471 deletions(-) diff --git a/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportCreateTaskHandler.java b/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportCreateTaskHandler.java index c680bb154a4..29f7004c3e0 100644 --- a/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportCreateTaskHandler.java +++ b/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportCreateTaskHandler.java @@ -1,26 +1,24 @@ -/* - * Copyright (c) 2010-2013 Evolveum - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - package com.evolveum.midpoint.report.impl; +import java.util.List; + +import org.springframework.stereotype.Component; + +import com.evolveum.midpoint.task.api.Task; +import com.evolveum.midpoint.task.api.TaskHandler; +import com.evolveum.midpoint.task.api.TaskRunResult; + + + import java.io.ByteArrayInputStream; import java.io.File; +import java.io.IOException; import java.io.InputStream; +import java.io.OutputStream; +import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Collection; +import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -28,50 +26,49 @@ import javax.annotation.PostConstruct; import net.sf.jasperreports.engine.JRException; -import net.sf.jasperreports.engine.JRExporterParameter; import net.sf.jasperreports.engine.JRTemplate; +import net.sf.jasperreports.engine.JasperCompileManager; import net.sf.jasperreports.engine.JasperExportManager; import net.sf.jasperreports.engine.JasperFillManager; import net.sf.jasperreports.engine.JasperPrint; import net.sf.jasperreports.engine.JasperReport; -import net.sf.jasperreports.engine.export.JExcelApiExporter; -import net.sf.jasperreports.engine.export.JRCsvExporter; -import net.sf.jasperreports.engine.export.JRRtfExporter; +import net.sf.jasperreports.engine.design.JRDesignExpression; +import net.sf.jasperreports.engine.design.JRDesignParameter; +import net.sf.jasperreports.engine.design.JRDesignReportTemplate; +import net.sf.jasperreports.engine.design.JasperDesign; import net.sf.jasperreports.engine.export.JRXhtmlExporter; import net.sf.jasperreports.engine.export.JRXlsExporter; -import net.sf.jasperreports.engine.export.JRXlsExporterParameter; import net.sf.jasperreports.engine.export.oasis.JROdsExporter; import net.sf.jasperreports.engine.export.oasis.JROdtExporter; import net.sf.jasperreports.engine.export.ooxml.JRDocxExporter; import net.sf.jasperreports.engine.export.ooxml.JRPptxExporter; import net.sf.jasperreports.engine.export.ooxml.JRXlsxExporter; +import net.sf.jasperreports.engine.xml.JRXmlLoader; import net.sf.jasperreports.engine.xml.JRXmlTemplateLoader; +import net.sf.jasperreports.engine.xml.JRXmlTemplateWriter; +import net.sf.jasperreports.engine.xml.JRXmlWriter; +import net.sf.jasperreports.export.Exporter; +import net.sf.jasperreports.export.ExporterInput; +import net.sf.jasperreports.export.ExporterOutput; +import net.sf.jasperreports.export.OutputStreamExporterOutput; +import net.sf.jasperreports.export.SimpleExporterInput; +import net.sf.jasperreports.export.SimpleOutputStreamExporterOutput; +import net.sf.jasperreports.export.SimpleWriterExporterOutput; +import net.sf.jasperreports.export.WriterExporterOutput; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.FilenameUtils; -import org.hibernate.SessionFactory; -import org.springframework.beans.BeansException; +import org.apache.commons.lang.StringUtils; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.stereotype.Component; import com.evolveum.midpoint.audit.api.AuditService; import com.evolveum.midpoint.model.api.ModelService; -import com.evolveum.midpoint.model.api.expr.MidpointFunctions; import com.evolveum.midpoint.model.common.expression.ExpressionFactory; -import com.evolveum.midpoint.model.common.expression.functions.FunctionLibrary; -import com.evolveum.midpoint.prism.Containerable; -import com.evolveum.midpoint.prism.PrismContainer; import com.evolveum.midpoint.prism.PrismContext; import com.evolveum.midpoint.prism.PrismObject; import com.evolveum.midpoint.prism.delta.ObjectDelta; -import com.evolveum.midpoint.prism.schema.PrismSchema; -import com.evolveum.midpoint.report.api.ReportManager; -import com.evolveum.midpoint.schema.result.OperationConstants; import com.evolveum.midpoint.schema.result.OperationResult; -import com.evolveum.midpoint.schema.result.OperationResultStatus; import com.evolveum.midpoint.schema.util.MiscSchemaUtil; import com.evolveum.midpoint.schema.util.ObjectResolver; import com.evolveum.midpoint.task.api.Task; @@ -86,37 +83,26 @@ import com.evolveum.midpoint.util.logging.LoggingUtils; import com.evolveum.midpoint.util.logging.Trace; import com.evolveum.midpoint.util.logging.TraceManager; +import com.evolveum.midpoint.xml.ns._public.common.common_3.ExportType; import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType; import com.evolveum.midpoint.xml.ns._public.common.common_3.ReportOutputType; import com.evolveum.midpoint.xml.ns._public.common.common_3.ReportType; import com.evolveum.midpoint.xml.ns._public.common.common_3.SubreportType; import com.evolveum.prism.xml.ns._public.types_3.PolyStringType; - -/** - * @author lazyman - */ @Component -public class ReportCreateTaskHandler implements TaskHandler, ApplicationContextAware { - - public static final String REPORT_CREATE_TASK_URI = "http://midpoint.evolveum.com/xml/ns/public/report/create/handler-1"; +public class ReportCreateTaskHandler implements TaskHandler{ - private static final Trace LOGGER = TraceManager.getTrace(ReportCreateTaskHandler.class); - - - private static final String CLASS_NAME_WITH_DOT = ReportCreateTaskHandler.class - .getName() + "."; - - private static final String CREATE_REPORT = CLASS_NAME_WITH_DOT + "createReport"; - - private static final String CREATE_REPORT_OUTPUT_TYPE = CLASS_NAME_WITH_DOT + "createReportOutputType"; + public static final String REPORT_CREATE_TASK_URI = "http://midpoint.evolveum.com/xml/ns/public/report/create/handler-3"; + private static final Trace LOGGER = TraceManager.getTrace(ReportCreateTaskHandler.class); + private static String PARAMETER_TEMPLATE_STYLES = "baseTemplateStyles"; private static String PARAMETER_REPORT_OID = "reportOid"; private static String PARAMETER_OPERATION_RESULT = "operationResult"; - private static String PARAMETER_TEMPLATE_STYLES = "baseTemplateStyles"; + private static String MIDPOINT_HOME = System.getProperty("midpoint.home"); + private static String EXPORT_DIR = MIDPOINT_HOME + "export/"; - @Autowired private TaskManager taskManager; @@ -126,452 +112,342 @@ public class ReportCreateTaskHandler implements TaskHandler, ApplicationContextA @Autowired private PrismContext prismContext; - @Autowired - private ReportManager reportManager; - - @Autowired - private SessionFactory sessionFactory; - - @Autowired(required=true) - private ExpressionFactory expressionFactory; - - @Autowired(required=true) - private ObjectResolver objectResolver; - - @Autowired(required=true) - private MidpointFunctions midpointFunctions; - - @Autowired(required=true) - private AuditService auditService; - - private ApplicationContext context; - - -// @PostConstruct -// private void initialize() { -// if (LOGGER.isTraceEnabled()) { -// LOGGER.trace("Registering with taskManager as a handler for " + REPORT_CREATE_TASK_URI); -// } -// taskManager.registerHandler(REPORT_CREATE_TASK_URI, this); -// } + + @Autowired(required = true) + private ObjectResolver objectResolver; + + @Autowired(required=true) + private ExpressionFactory expressionFactory; + + @Autowired(required=true) + private AuditService auditService; - @Override - public void setApplicationContext(ApplicationContext context) throws BeansException { - this.context = context; - } - - private Map getSubreportParameters(SubreportType subreportType, OperationResult subResult) - { - Map reportParams = new HashMap(); - try - { - ReportType reportType = ReportUtils.getReport(subreportType.getReportRef().getOid(), subResult, modelService); - - PrismSchema reportSchema = ReportUtils.getParametersSchema(reportType, prismContext); - PrismContainer parameterConfiguration = ReportUtils.getParametersContainer(reportType, reportSchema); + + @PostConstruct + private void initialize() { + if (LOGGER.isTraceEnabled()) { + LOGGER.trace("Registering with taskManager as a handler for " + REPORT_CREATE_TASK_URI); + } + taskManager.registerHandler(REPORT_CREATE_TASK_URI, this); + } - Map parameters = prepareReportParameters(reportType, subResult); - reportParams.putAll(parameters); - - JasperReport jasperReport = ReportUtils.getJasperReport(reportType, parameterConfiguration, reportSchema); - reportParams.put(subreportType.getName(), jasperReport); - - Map subReportParams = processSubreportParameters(reportType, subResult); - reportParams.putAll(subReportParams); - - - } catch (Exception ex) { - LOGGER.error("Error read subreport parameter {} :", ex); - } - - return reportParams; - } - - - - public Map getReportParameters(ReportType reportType, PrismContext prismContext, OperationResult parentResult) throws Exception - { - Map parameters = new HashMap(); - try - { - - - } catch (Exception ex) { - throw ex; - } - - - return parameters; - } - @Override - public TaskRunResult run(Task task) { - //here a jasper magic should be done which creates PDF file (or something else) in midpoint directory - //also as a result ReportOutputType should be created and stored to DB. - - LOGGER.trace("ReportCreateTaskHandler.run starting"); - - OperationResult opResult = new OperationResult(OperationConstants.CREATE_REPORT_FILE); - opResult.setStatus(OperationResultStatus.IN_PROGRESS); + + + @Override + public TaskRunResult run(Task task) { + // TODO Auto-generated method stub + OperationResult parentResult = task.getResult(); + OperationResult result = parentResult.createSubresult(ReportCreateTaskHandler.class.getSimpleName() + ".run"); TaskRunResult runResult = new TaskRunResult(); - runResult.setOperationResult(opResult); + runResult.setOperationResult(result); - recordProgress(task, 0, opResult); + recordProgress(task, 0, result); long progress = task.getProgress(); - - Map params = new HashMap(); - JasperReport jasperReport; - try - { - OperationResult subResult = opResult.createSubresult(CREATE_REPORT); - - ReportType reportType = ReportUtils.getReport(task.getObjectOid(), subResult, modelService); - - PrismSchema reportSchema = ReportUtils.getParametersSchema(reportType, prismContext); - PrismContainer parameterConfiguration = ReportUtils.getParametersContainer(reportType, reportSchema); - - // Compile template - jasperReport = ReportUtils.getJasperReport(reportType, parameterConfiguration, reportSchema); - LOGGER.trace("compile jasper design, create jasper report : {}", jasperReport); - - - Map parameters = prepareReportParameters(reportType, opResult); -// ReportUtils.getReportParameters(modelService, taskManager, prismContext, reportType, parameterConfiguration, reportSchema, subResult); - params.putAll(parameters); - LOGGER.trace("create report params : {}", parameters); - - OperationResult subreportResult = opResult.createSubresult("get report subreport"); - Map subreportParameters = processSubreportParameters(reportType, subreportResult); - subreportResult.computeStatus(); - - //params.put("subreportParameters", subreportParameters); - params.putAll(subreportParameters); - ReportFunctions reportFunctions = new ReportFunctions(prismContext, modelService, taskManager, auditService); - params.put(MidPointQueryExecutorFactory.PARAMETER_REPORT_FUNCTIONS, reportFunctions); - - LOGGER.trace("All Report parameters : {}", params); - JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params); + + try { + ReportType parentReport = objectResolver.resolve(task.getObjectRef(), ReportType.class, null, "resolving report", result); + + JasperReport jasperReport = ReportUtils.loadJasperReport(parentReport); + + LOGGER.trace("compile jasper design, create jasper report : {}", jasperReport); + Map parameters = completeReport(parentReport, result); + + LOGGER.trace("All Report parameters : {}", parameters); + JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, parameters); LOGGER.trace("fill report : {}", jasperPrint); - String reportFilePath = generateReport(reportType, jasperPrint); + String reportFilePath = generateReport(parentReport, jasperPrint); LOGGER.trace("generate report : {}", reportFilePath); - saveReportOutputType(reportFilePath, reportType, task, subResult); + saveReportOutputType(reportFilePath, parentReport, task, result); LOGGER.trace("create report output type : {}", reportFilePath); - subResult.computeStatus(); - - } catch (Exception ex) { - LOGGER.error("CreateReport: {}", ex.getMessage(), ex); - opResult.recordFatalError(ex.getMessage(), ex); + result.computeStatus(); + + + } catch (Exception ex) { + LOGGER.error("CreateReport: {}", ex.getMessage(), ex); + result.recordFatalError(ex.getMessage(), ex); runResult.setRunResultStatus(TaskRunResultStatus.PERMANENT_ERROR); runResult.setProgress(progress); return runResult; - } - - opResult.computeStatus(); + } + // This "run" is finished. But the task goes on ... runResult.setRunResultStatus(TaskRunResultStatus.FINISHED); runResult.setProgress(progress); LOGGER.trace("CreateReportTaskHandler.run stopping"); return runResult; - } - - private Map prepareReportParameters(ReportType reportType, OperationResult parentResult){ - Map params = new HashMap(); - if (reportType.getTemplateStyle() != null) - { - byte[] reportTemplateStyleBase64 = reportType.getTemplateStyle(); - byte[] reportTemplateStyle = Base64.decodeBase64(reportTemplateStyleBase64); - try{ - LOGGER.trace("Style template string {}", new String(reportTemplateStyle)); - InputStream inputStreamJRTX = new ByteArrayInputStream(reportTemplateStyle); - JRTemplate templateStyle = JRXmlTemplateLoader.load(inputStreamJRTX); - params.put(PARAMETER_TEMPLATE_STYLES, templateStyle); - LOGGER.trace("Style template parameter {}", templateStyle); - - } catch (Exception ex) { - LOGGER.error("Error create style template parameter {}", ex.getMessage()); - throw new SystemException(ex); - } - - } + } + + private Map completeReport(ReportType parentReport, OperationResult result) throws SchemaException, ObjectNotFoundException{ + return completeReport(parentReport, null, null, result); + } + + private Map completeReport(ReportType parentReport, JasperReport subReport, String subReportName, OperationResult result) throws SchemaException, ObjectNotFoundException{ + Map params = new HashMap(); + + if (subReport != null && StringUtils.isNotBlank(subReportName)){ + params.put(subReportName, subReport); + } - // for our special datasource - params.put(PARAMETER_REPORT_OID, reportType.getOid()); - params.put(PARAMETER_OPERATION_RESULT, parentResult); - params.put(MidPointQueryExecutorFactory.PARAMETER_MIDPOINT_CONNECTION, modelService); - params.put(MidPointQueryExecutorFactory.PARAMETER_PRISM_CONTEXT, prismContext); - params.put(MidPointQueryExecutorFactory.PARAMETER_TASK_MANAGER, taskManager); - params.put(MidPointQueryExecutorFactory.PARAMETER_EXPRESSION_FACTORY, expressionFactory); - params.put(MidPointQueryExecutorFactory.PARAMETER_AUDIT_SERVICE, auditService); + Map parameters = prepareReportParameters(parentReport, result); + params.putAll(parameters); + LOGGER.trace("create report params : {}", parameters); + Map subreportParameters = processSubreportParameters(parentReport, result); + params.putAll(subreportParameters); return params; - } - - private Map processSubreportParameters(ReportType reportType, OperationResult subreportResult){ - Map subreportParameters = new HashMap(); - for(SubreportType subreport : reportType.getSubreport()) - { - Map subreportParam = getSubreportParameters(subreport, subreportResult); - LOGGER.trace("create subreport params : {}", subreportParam); - subreportParameters.putAll(subreportParam); + } + + + +// private JasperReport loadJasperReport(ReportType reportType) throws SchemaException{ +// +// if (reportType.getTemplate() == null) { +// throw new IllegalStateException("Could not create report. No jasper template defined."); +// } +// try { +// byte[] reportTemplate = Base64.decodeBase64(reportType.getTemplate()); +// +// InputStream inputStreamJRXML = new ByteArrayInputStream(reportTemplate); +// JasperDesign jasperDesign = JRXmlLoader.load(inputStreamJRXML); +// LOGGER.trace("load jasper design : {}", jasperDesign); +// +// if (reportType.getTemplateStyle() != null){ +// JRDesignReportTemplate templateStyle = new JRDesignReportTemplate(new JRDesignExpression("$P{" + PARAMETER_TEMPLATE_STYLES + "}")); +// jasperDesign.addTemplate(templateStyle); +// JRDesignParameter parameter = new JRDesignParameter(); +// parameter.setName(PARAMETER_TEMPLATE_STYLES); +// parameter.setValueClass(JRTemplate.class); +// parameter.setForPrompting(false); +// jasperDesign.addParameter(parameter); +// } +// JasperReport jasperReport = JasperCompileManager.compileReport(jasperDesign); +// return jasperReport; +// } catch (JRException ex){ +// LOGGER.error("Couldn't create jasper report design {}", ex.getMessage()); +// throw new SchemaException(ex.getMessage(), ex.getCause()); +// } +// +// +// } + + + private Map prepareReportParameters(ReportType reportType, OperationResult parentResult){ + Map params = new HashMap(); + if (reportType.getTemplateStyle() != null) + { + byte[] reportTemplateStyleBase64 = reportType.getTemplateStyle(); + byte[] reportTemplateStyle = Base64.decodeBase64(reportTemplateStyleBase64); + try{ + LOGGER.trace("Style template string {}", new String(reportTemplateStyle)); + InputStream inputStreamJRTX = new ByteArrayInputStream(reportTemplateStyle); + JRTemplate templateStyle = JRXmlTemplateLoader.load(inputStreamJRTX); + params.put(PARAMETER_TEMPLATE_STYLES, templateStyle); + LOGGER.trace("Style template parameter {}", templateStyle); + + } catch (Exception ex) { + LOGGER.error("Error create style template parameter {}", ex.getMessage()); + throw new SystemException(ex); + } - } - return subreportParameters; - } - - @Override - public Long heartbeat(Task task) { - return null; - } + } + + // for our special datasource + params.put(PARAMETER_REPORT_OID, reportType.getOid()); + params.put(PARAMETER_OPERATION_RESULT, parentResult); + params.put(MidPointQueryExecutorFactory.PARAMETER_MIDPOINT_CONNECTION, modelService); + params.put(MidPointQueryExecutorFactory.PARAMETER_PRISM_CONTEXT, prismContext); + params.put(MidPointQueryExecutorFactory.PARAMETER_TASK_MANAGER, taskManager); + params.put(MidPointQueryExecutorFactory.PARAMETER_EXPRESSION_FACTORY, expressionFactory); + params.put(MidPointQueryExecutorFactory.PARAMETER_AUDIT_SERVICE, auditService); + ReportFunctions reportFunctions = new ReportFunctions(prismContext, modelService, taskManager, auditService); + params.put(MidPointQueryExecutorFactory.PARAMETER_REPORT_FUNCTIONS, reportFunctions); + + return params; + } + + private Map processSubreportParameters(ReportType reportType, OperationResult subreportResult) throws SchemaException, ObjectNotFoundException{ + Map subreportParameters = new HashMap(); + for(SubreportType subreport : reportType.getSubreport()) + { + Map subreportParam = getSubreportParameters(subreport, subreportResult); + LOGGER.trace("create subreport params : {}", subreportParam); + subreportParameters.putAll(subreportParam); + + } + return subreportParameters; + } + + private Map getSubreportParameters(SubreportType subreportType, OperationResult subResult) + throws SchemaException, ObjectNotFoundException { + Map reportParams = new HashMap(); + ReportType reportType = objectResolver.resolve(subreportType.getReportRef(), ReportType.class, null, + "resolve subreport", subResult); - @Override - public void refreshStatus(Task task) { - } + Map parameters = prepareReportParameters(reportType, subResult); + reportParams.putAll(parameters); - @Override - public List getCategoryNames() { - return null; - } - - @Override - public String getCategoryName(Task task) { - return TaskCategory.REPORT; - } - - + JasperReport jasperReport = ReportUtils.loadJasperReport(reportType); + reportParams.put(subreportType.getName(), jasperReport); - // generate report - export - private String generateReport(ReportType reportType, JasperPrint jasperPrint) throws JRException - { - String output = ReportUtils.getReportOutputFilePath(reportType); - switch (reportType.getExport()) - { - case PDF : pdf(jasperPrint, output); - break; - case CSV : { - csv(jasperPrint, output); - //pdf(jasperPrint, output.substring(0, output.lastIndexOf(".")) + ".pdf"); - } - - break; - case XML : xml(jasperPrint, output); - break; - case XML_EMBED : xmlEmbed(jasperPrint, output); - break; - case HTML : html(jasperPrint, output); - break; - case RTF : rtf(jasperPrint, output); - break; - case XLS : xls(jasperPrint, output); - break; - case ODT : odt(jasperPrint, output); - break; - case ODS : ods(jasperPrint, output); - break; - case DOCX : docx(jasperPrint, output); - break; - case XLSX : xlsx(jasperPrint, output); - break; - case PPTX : pptx(jasperPrint, output); - break; - case XHTML : xhtml(jasperPrint, output); - break; - case JXL : jxl(jasperPrint, output); - break; - default: - break; - } - return output; - } - - //export report - private void pdf(JasperPrint jasperPrint, String output) throws JRException - { - JasperExportManager.exportReportToPdfFile(jasperPrint, output); - } - - private static void csv(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRCsvExporter exporter = new JRCsvExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - - exporter.exportReport(); - } - - private void xml(JasperPrint jasperPrint, String output) throws JRException - { - JasperExportManager.exportReportToXmlFile(jasperPrint, output, false); - } - - private void xmlEmbed(JasperPrint jasperPrint, String output) throws JRException - { - JasperExportManager.exportReportToXmlFile(jasperPrint, output, true); - } - - private void html(JasperPrint jasperPrint, String output) throws JRException - { - JasperExportManager.exportReportToHtmlFile(jasperPrint, output); - } - - private void rtf(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRRtfExporter exporter = new JRRtfExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - - exporter.exportReport(); - } - - private void xls(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRXlsExporter exporter = new JRXlsExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); - - exporter.exportReport(); - } - - private void odt(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JROdtExporter exporter = new JROdtExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - - exporter.exportReport(); - } - - private void ods(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JROdsExporter exporter = new JROdsExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); - - exporter.exportReport(); - } - - private void docx(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRDocxExporter exporter = new JRDocxExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - - exporter.exportReport(); - } - - private void xlsx(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRXlsxExporter exporter = new JRXlsxExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); - - exporter.exportReport(); - } - - private void pptx(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRPptxExporter exporter = new JRPptxExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); + Map subReportParams = processSubreportParameters(reportType, subResult); + reportParams.putAll(subReportParams); - exporter.exportReport(); - } - - private void xhtml(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JRXhtmlExporter exporter = new JRXhtmlExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); + return reportParams; + } - exporter.exportReport(); - - } - - private void jxl(JasperPrint jasperPrint, String output) throws JRException - { - File destFile = new File(output); - - JExcelApiExporter exporter = new JExcelApiExporter(); - - exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); - exporter.setParameter(JRExporterParameter.OUTPUT_FILE_NAME, destFile.toString()); - exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE); + private void recordProgress(Task task, long progress, OperationResult opResult) { + try { + task.setProgressImmediate(progress, opResult); + } catch (ObjectNotFoundException e) { // these exceptions are of so little probability and harmless, so we just log them and do not report higher + LoggingUtils.logException(LOGGER, "Couldn't record progress to task {}, probably because the task does not exist anymore", e, task); + } catch (SchemaException e) { + LoggingUtils.logException(LOGGER, "Couldn't record progress to task {}, due to unexpected schema exception", e, task); + } + } + + private String generateReport(ReportType reportType, JasperPrint jasperPrint) throws JRException + { + String destinationFileName = getDestinationFileName(reportType); + switch (reportType.getExport()) + { + case PDF : + JasperExportManager.exportReportToPdfFile(jasperPrint, destinationFileName); + break; + case XML : + JasperExportManager.exportReportToXmlFile(jasperPrint, destinationFileName, true); + break; + case XML_EMBED : + JasperExportManager.exportReportToXmlFile(jasperPrint, destinationFileName, true); + break; + case HTML : + JasperExportManager.exportReportToHtmlFile(jasperPrint, destinationFileName); + break; + case CSV : + case RTF : + case XLS : + case ODT : + case ODS : + case DOCX : + case XLSX : + case PPTX : + case XHTML : + case JXL : + ExporterInput input = new SimpleExporterInput(jasperPrint); + ExporterOutput output = new SimpleOutputStreamExporterOutput(destinationFileName); + + Exporter exporter = createExporter(reportType.getExport()); + if (exporter == null){ + break; + } + exporter.setExporterInput(input); + exporter.setExporterOutput(output); + exporter.exportReport(); + break; + default: + break; + } + return destinationFileName; + } + + private Exporter createExporter(ExportType type) { + switch (type) { + case CSV: + case RTF: + case XLS: + return new JRXlsExporter(); + case ODT: + return new JROdtExporter(); + case ODS: + return new JROdsExporter(); + case DOCX: + return new JRDocxExporter(); + case XLSX: + return new JRXlsxExporter(); + case PPTX: + return new JRPptxExporter(); + case XHTML: + return new JRXhtmlExporter(); + default: + return null; + } + } - exporter.exportReport(); - } + public static String getDateTime() { + Date createDate = new Date(System.currentTimeMillis()); + SimpleDateFormat formatDate = new SimpleDateFormat("dd-MM-yyyy hh-mm-ss"); + return formatDate.format(createDate); + } + private static String getDestinationFileName(ReportType reportType){ + File exportFolder = new File(EXPORT_DIR); + if (!exportFolder.exists() || !exportFolder.isDirectory()) { + exportFolder.mkdir(); + } + + String output = EXPORT_DIR + reportType.getName().getOrig() + " " + getDateTime(); + + if (reportType.getExport() == ExportType.XML_EMBED){ + return output + "_embed.xml"; + } + + return output + "." +reportType.getExport().value(); + + } + + private void saveReportOutputType(String filePath, ReportType reportType, Task task, OperationResult parentResult) throws Exception + { + + String fileName = FilenameUtils.getBaseName(filePath); + String reportOutputName = fileName + " - " + reportType.getExport().value() ; + + ReportOutputType reportOutputType = new ReportOutputType(); + prismContext.adopt(reportOutputType); + + reportOutputType.setFilePath(filePath); + reportOutputType.setReportRef(MiscSchemaUtil.createObjectReference(reportType.getOid(), ReportType.COMPLEX_TYPE)); + reportOutputType.setName(new PolyStringType(reportOutputName)); + reportOutputType.setDescription(reportType.getDescription() + " - " + reportType.getExport().value()); + + ObjectDelta objectDelta = null; + Collection> deltas = new ArrayList>(); + OperationResult subResult = null; + + objectDelta = ObjectDelta.createAddDelta((PrismObject) reportOutputType.asPrismObject()); + deltas.add(objectDelta); + subResult = parentResult.createSubresult(ReportCreateTaskHandler.class.getName()+ "createRepourtOutput"); + + modelService.executeChanges(deltas, null, task, subResult); + + subResult.computeStatus(); + } + + + @Override + public Long heartbeat(Task task) { + // TODO Auto-generated method stub + return null; + } - + @Override + public void refreshStatus(Task task) { + // TODO Auto-generated method stub + + } - private void recordProgress(Task task, long progress, OperationResult opResult) { - try { - task.setProgressImmediate(progress, opResult); - } catch (ObjectNotFoundException e) { // these exceptions are of so little probability and harmless, so we just log them and do not report higher - LoggingUtils.logException(LOGGER, "Couldn't record progress to task {}, probably because the task does not exist anymore", e, task); - } catch (SchemaException e) { - LoggingUtils.logException(LOGGER, "Couldn't record progress to task {}, due to unexpected schema exception", e, task); - } - } - - - private void saveReportOutputType(String filePath, ReportType reportType, Task task, OperationResult parentResult) throws Exception - { - - String fileName = FilenameUtils.getBaseName(filePath); - String reportOutputName = fileName + " - " + reportType.getExport().value() ; - - ReportOutputType reportOutputType = new ReportOutputType(); - prismContext.adopt(reportOutputType); - - reportOutputType.setFilePath(filePath); - reportOutputType.setReportRef(MiscSchemaUtil.createObjectReference(reportType.getOid(), ReportType.COMPLEX_TYPE)); - reportOutputType.setName(new PolyStringType(reportOutputName)); - reportOutputType.setDescription(reportType.getDescription() + " - " + reportType.getExport().value()); - - ObjectDelta objectDelta = null; - Collection> deltas = new ArrayList>(); - OperationResult subResult = null; - - objectDelta = ObjectDelta.createAddDelta((PrismObject) reportOutputType.asPrismObject()); - deltas.add(objectDelta); - subResult = parentResult.createSubresult(CREATE_REPORT_OUTPUT_TYPE); - - modelService.executeChanges(deltas, null, task, subResult); - - subResult.computeStatus(); - } + @Override + public String getCategoryName(Task task) { + return TaskCategory.REPORT; + } + @Override + public List getCategoryNames() { + // TODO Auto-generated method stub + return null; + } + + } - diff --git a/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportManagerImpl.java b/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportManagerImpl.java index 1b3f4ba33c7..723ebaa2679 100644 --- a/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportManagerImpl.java +++ b/model/report-impl/src/main/java/com/evolveum/midpoint/report/impl/ReportManagerImpl.java @@ -151,7 +151,7 @@ private boolean isRaw(Collection> options) @Override public void runReport(PrismObject object, Task task, OperationResult parentResult) { - task.setHandlerUri(ReportCreateTaskHandlerNew.REPORT_CREATE_TASK_URI); + task.setHandlerUri(ReportCreateTaskHandler.REPORT_CREATE_TASK_URI); task.setObjectRef(object.getOid(), ReportType.COMPLEX_TYPE); task.setThreadStopAction(ThreadStopActionType.CLOSE); diff --git a/model/report-impl/src/test/java/com/evolveum/midpoint/report/BasicReportTest.java b/model/report-impl/src/test/java/com/evolveum/midpoint/report/BasicReportTest.java index 25283e5697e..19b3a165ab5 100644 --- a/model/report-impl/src/test/java/com/evolveum/midpoint/report/BasicReportTest.java +++ b/model/report-impl/src/test/java/com/evolveum/midpoint/report/BasicReportTest.java @@ -76,7 +76,7 @@ import com.evolveum.midpoint.prism.schema.SchemaRegistry; import com.evolveum.midpoint.prism.xml.XmlTypeConverter; import com.evolveum.midpoint.report.api.ReportManager; -import com.evolveum.midpoint.report.impl.ReportCreateTaskHandler; +import com.evolveum.midpoint.report.impl.ReportCreateTaskHandlerOld; import com.evolveum.midpoint.report.impl.ReportUtils; import com.evolveum.midpoint.schema.GetOperationOptions; import com.evolveum.midpoint.schema.SelectorOptions; @@ -218,7 +218,7 @@ public class BasicReportTest extends AbstractModelIntegrationTest { private ReportManager reportManager; @Autowired - private ReportCreateTaskHandler reportHandler; + private ReportCreateTaskHandlerOld reportHandler; @Autowired private SessionFactory sessionFactory; diff --git a/model/report-impl/src/test/java/com/evolveum/midpoint/report/TestReport.java b/model/report-impl/src/test/java/com/evolveum/midpoint/report/TestReport.java index a423741b748..cf370464df2 100644 --- a/model/report-impl/src/test/java/com/evolveum/midpoint/report/TestReport.java +++ b/model/report-impl/src/test/java/com/evolveum/midpoint/report/TestReport.java @@ -26,7 +26,7 @@ import com.evolveum.midpoint.prism.PrismObject; import com.evolveum.midpoint.prism.polystring.PolyString; import com.evolveum.midpoint.report.impl.MidPointQueryExecutorFactory; -import com.evolveum.midpoint.report.impl.ReportCreateTaskHandler; +import com.evolveum.midpoint.report.impl.ReportCreateTaskHandlerOld; import com.evolveum.midpoint.schema.result.OperationResult; import com.evolveum.midpoint.task.api.Task; import com.evolveum.midpoint.task.api.TaskManager; @@ -80,7 +80,7 @@ public class TestReport extends AbstractModelIntegrationTest{ protected PrismObject resourceDummy; @Autowired(required = true) - ReportCreateTaskHandler reportTaskHandler; + ReportCreateTaskHandlerOld reportTaskHandler; @Override protected void startResources() throws Exception {