Search in sources :

Example 11 with JRException

use of net.sf.jasperreports.engine.JRException in project jgnash by ccavanaugh.

the class DynamicJasperReportPanel method saveAction.

private void saveAction() {
    Preferences p = Preferences.userNodeForPackage(DynamicJasperReportPanel.class);
    JFileChooser fileChooser = new JFileChooser();
    fileChooser.setMultiSelectionEnabled(false);
    saveContributors.forEach(fileChooser::addChoosableFileFilter);
    // restore the last save format
    if (p.get(LAST_CONTRIBUTOR, null) != null) {
        String last = p.get(LAST_CONTRIBUTOR, null);
        for (JRSaveContributor saveContributor : saveContributors) {
            if (saveContributor.getDescription().equals(last)) {
                fileChooser.setFileFilter(saveContributor);
                break;
            }
        }
    } else if (!saveContributors.isEmpty()) {
        fileChooser.setFileFilter(saveContributors.get(0));
    }
    if (p.get(LAST_DIRECTORY, null) != null) {
        fileChooser.setCurrentDirectory(new File(p.get(LAST_DIRECTORY, null)));
    }
    int retValue = fileChooser.showSaveDialog(this);
    if (retValue == JFileChooser.APPROVE_OPTION) {
        FileFilter fileFilter = fileChooser.getFileFilter();
        File file = fileChooser.getSelectedFile();
        p.put(LAST_DIRECTORY, file.getParent());
        JRSaveContributor contributor = null;
        if (fileFilter instanceof JRSaveContributor) {
            // save format chosen from the list
            contributor = (JRSaveContributor) fileFilter;
        } else {
            for (JRSaveContributor saveContributor : saveContributors) {
                // need to determine the best match
                if (saveContributor.accept(file)) {
                    contributor = saveContributor;
                    break;
                }
            }
            if (contributor == null) {
                JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
            }
        }
        if (contributor != null) {
            p.put(LAST_CONTRIBUTOR, contributor.getDescription());
            try {
                if (contributor instanceof JRSingleSheetXlsSaveContributor) {
                    LOG.info("Formatting for xls file");
                    JasperPrint print = report.createJasperPrint(true);
                    contributor.save(print, file);
                } else if (contributor instanceof JRCsvSaveContributor) {
                    LOG.info("Formatting for csv file");
                    JasperPrint print = report.createJasperPrint(true);
                    contributor.save(print, file);
                } else {
                    contributor.save(jasperPrint, file);
                }
            } catch (final JRException ex) {
                LOG.log(Level.SEVERE, ex.getMessage(), ex);
                JOptionPane.showMessageDialog(this, resourceBundle.getString("error.saving"));
            }
        }
    }
}
Also used : JRSingleSheetXlsSaveContributor(net.sf.jasperreports.view.save.JRSingleSheetXlsSaveContributor) JFileChooser(javax.swing.JFileChooser) JRException(net.sf.jasperreports.engine.JRException) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRSaveContributor(net.sf.jasperreports.view.JRSaveContributor) Preferences(java.util.prefs.Preferences) FileFilter(javax.swing.filechooser.FileFilter) File(java.io.File) JasperPrint(net.sf.jasperreports.engine.JasperPrint) JRCsvSaveContributor(net.sf.jasperreports.view.save.JRCsvSaveContributor)

Example 12 with JRException

use of net.sf.jasperreports.engine.JRException in project jgnash by ccavanaugh.

the class JasperViewerDialogController method handleSaveAction.

@FXML
private void handleSaveAction() {
    Preferences preferences = Preferences.userNodeForPackage(JasperViewerDialogController.class);
    final String lastDir = preferences.get(LAST_DIR, null);
    final FileChooser fileChooser = new FileChooser();
    fileChooser.setTitle(ResourceUtils.getString("Title.SaveFile"));
    if (lastDir != null) {
        fileChooser.setInitialDirectory(new File(lastDir));
    }
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PDF Document", "*.pdf", "*.PDF"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("ODT Document", "*.odt", "*.ODT"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("DOCX Document", "*.docx", "*.DOCX"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("XLSX Document", "*.xlsx", "*.XLSX"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("CSV Document", "*.csv", "*.CSV"));
    fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("RTF Document", "*.rtf", "*.RTF"));
    final File file = fileChooser.showSaveDialog(MainView.getPrimaryStage());
    if (file != null) {
        preferences.put(LAST_DIR, file.getParent());
        switch(FileUtils.getFileExtension(file.getAbsolutePath()).toLowerCase(Locale.ROOT)) {
            case "pdf":
                try {
                    JasperExportManager.exportReportToPdfFile(jasperPrint.get(), file.getAbsolutePath());
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "odt":
                try {
                    final JROdtExporter exporter = new JROdtExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "docx":
                try {
                    final JRDocxExporter exporter = new JRDocxExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "xlsx":
                try {
                    final JRXlsExporter exporter = new JRXlsExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(file));
                    final SimpleXlsReportConfiguration configuration = new SimpleXlsReportConfiguration();
                    configuration.setOnePagePerSheet(false);
                    exporter.setConfiguration(configuration);
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "csv":
                try {
                    final JRCsvExporter exporter = new JRCsvExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleWriterExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            case "rtf":
                try {
                    final JRRtfExporter exporter = new JRRtfExporter();
                    exporter.setExporterInput(new SimpleExporterInput(jasperPrint.get()));
                    exporter.setExporterOutput(new SimpleWriterExporterOutput(file));
                    exporter.exportReport();
                } catch (final JRException e) {
                    StaticUIMethods.displayException(e);
                }
                break;
            default:
        }
    }
}
Also used : SimpleXlsReportConfiguration(net.sf.jasperreports.export.SimpleXlsReportConfiguration) JROdtExporter(net.sf.jasperreports.engine.export.oasis.JROdtExporter) JRException(net.sf.jasperreports.engine.JRException) SimpleOutputStreamExporterOutput(net.sf.jasperreports.export.SimpleOutputStreamExporterOutput) JRCsvExporter(net.sf.jasperreports.engine.export.JRCsvExporter) SimpleExporterInput(net.sf.jasperreports.export.SimpleExporterInput) JRXlsExporter(net.sf.jasperreports.engine.export.JRXlsExporter) SimpleWriterExporterOutput(net.sf.jasperreports.export.SimpleWriterExporterOutput) FileChooser(javafx.stage.FileChooser) Preferences(java.util.prefs.Preferences) JRDocxExporter(net.sf.jasperreports.engine.export.ooxml.JRDocxExporter) File(java.io.File) JRRtfExporter(net.sf.jasperreports.engine.export.JRRtfExporter) FXML(javafx.fxml.FXML) InjectFXML(jgnash.uifx.util.InjectFXML)

Example 13 with JRException

use of net.sf.jasperreports.engine.JRException in project jgnash by ccavanaugh.

the class JasperViewerDialogController method refresh.

private void refresh() {
    final List<Node> children = pagePane.getChildren();
    children.clear();
    for (int i = 0; i < pageCount.get(); i++) {
        try {
            final BufferedImage bufferedImage = (BufferedImage) JasperPrintManager.printPageToImage(jasperPrint.get(), i, (float) zoom);
            final ImageView imageView = new ImageView(SwingFXUtils.toFXImage(bufferedImage, null));
            imageView.setEffect(dropShadow);
            children.add(imageView);
        } catch (final JRException ex) {
            StaticUIMethods.displayException(ex);
        }
    }
    setPageIndex(0);
}
Also used : JRException(net.sf.jasperreports.engine.JRException) Node(javafx.scene.Node) ImageView(javafx.scene.image.ImageView) JasperPrint(net.sf.jasperreports.engine.JasperPrint) BufferedImage(java.awt.image.BufferedImage)

Example 14 with JRException

use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.

the class CustomJRJdtCompiler method getNameEnvironment.

protected INameEnvironment getNameEnvironment(final JRCompilationUnit[] units) {
    final INameEnvironment env = new INameEnvironment() {

        @Override
        public NameEnvironmentAnswer findType(char[][] compoundTypeName) {
            StringBuilder result = new StringBuilder();
            String sep = "";
            for (int i = 0; i < compoundTypeName.length; i++) {
                result.append(sep);
                result.append(compoundTypeName[i]);
                sep = ".";
            }
            return findType(result.toString());
        }

        @Override
        public NameEnvironmentAnswer findType(char[] typeName, char[][] packageName) {
            StringBuilder result = new StringBuilder();
            String sep = "";
            for (int i = 0; i < packageName.length; i++) {
                result.append(sep);
                result.append(packageName[i]);
                sep = ".";
            }
            result.append(sep);
            result.append(typeName);
            return findType(result.toString());
        }

        private int getClassIndex(String className) {
            int classIdx;
            for (classIdx = 0; classIdx < units.length; ++classIdx) {
                if (className.equals(units[classIdx].getName())) {
                    break;
                }
            }
            if (classIdx >= units.length) {
                classIdx = -1;
            }
            return classIdx;
        }

        private NameEnvironmentAnswer findType(String className) {
            try {
                int classIdx = getClassIndex(className);
                if (classIdx >= 0) {
                    ICompilationUnit compilationUnit = new CompilationUnit(units[classIdx].getSourceCode(), className);
                    return new NameEnvironmentAnswer(compilationUnit, null);
                }
                String resourceName = className.replace('.', '/') + ".class";
                InputStream is = getResource(resourceName);
                if (is != null) {
                    try {
                        byte[] classBytes = JRLoader.loadBytes(is);
                        char[] fileName = className.toCharArray();
                        ClassFileReader classFileReader = new ClassFileReader(classBytes, fileName, true);
                        return new NameEnvironmentAnswer(classFileReader, null);
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                        // ignore
                        }
                    }
                }
            } catch (JRException e) {
                LOG.error("Compilation error", e);
            } catch (org.eclipse.jdt.internal.compiler.classfmt.ClassFormatException exc) {
                LOG.error("Compilation error", exc);
            } catch (IllegalArgumentException e) {
                throw new JRRuntimeException(EXCEPTION_MESSAGE_KEY_NAME_ENVIRONMENT_ANSWER_INSTANCE_ERROR, (Object[]) null, e);
            }
            return null;
        }

        private boolean isPackage(String result) {
            int classIdx = getClassIndex(result);
            if (classIdx >= 0) {
                return false;
            }
            String resourceName = result.replace('.', '/') + ".class";
            boolean isPackage = true;
            InputStream is = getResource(resourceName);
            if (is != null) {
                // 1.5 plugin
                try {
                    isPackage = (is.read() > 0);
                } catch (IOException e) {
                // ignore
                } finally {
                    try {
                        is.close();
                    } catch (IOException e) {
                    // ignore
                    }
                }
            }
            return isPackage;
        }

        @Override
        public boolean isPackage(char[][] parentPackageName, char[] packageName) {
            StringBuilder result = new StringBuilder();
            String sep = "";
            if (parentPackageName != null) {
                for (int i = 0; i < parentPackageName.length; i++) {
                    result.append(sep);
                    result.append(parentPackageName[i]);
                    sep = ".";
                }
            }
            if (Character.isUpperCase(packageName[0])) {
                if (!isPackage(result.toString())) {
                    return false;
                }
            }
            result.append(sep);
            result.append(packageName);
            return isPackage(result.toString());
        }

        @Override
        public void cleanup() {
        }
    };
    return env;
}
Also used : JRCompilationUnit(net.sf.jasperreports.engine.design.JRCompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) ICompilationUnit(org.eclipse.jdt.internal.compiler.env.ICompilationUnit) NameEnvironmentAnswer(org.eclipse.jdt.internal.compiler.env.NameEnvironmentAnswer) JRException(net.sf.jasperreports.engine.JRException) InputStream(java.io.InputStream) ClassFileReader(org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader) JRRuntimeException(net.sf.jasperreports.engine.JRRuntimeException) IOException(java.io.IOException) INameEnvironment(org.eclipse.jdt.internal.compiler.env.INameEnvironment)

Example 15 with JRException

use of net.sf.jasperreports.engine.JRException in project opennms by OpenNMS.

the class ReportCompiler method main.

/**
     * The main method.
     *
     * @param args the arguments
     */
public static void main(String[] args) {
    if (args.length > 1 && new File(args[0]).exists()) {
        System.setProperty("opennms.home", args[0]);
    }
    initializeSingleInstanceDatabase();
    // To trigger Spring Initialization
    BeanUtils.getBeanFactory("jasperReportContext");
    File reportsDirectory = new File(ConfigFileConstants.getFilePathString(), "report-templates");
    System.out.printf("Analyzing jasper reports located at %s\n", reportsDirectory);
    for (File report : FileUtils.listFiles(reportsDirectory, new String[] { "jrxml" }, true)) {
        System.out.printf("Compiling report template %s\n", report.getAbsolutePath());
        try {
            JasperCompileManager.compileReportToFile(report.getAbsolutePath());
        } catch (JRException e) {
            System.err.println("Error: cannot compile report because: " + e.getMessage());
            e.printStackTrace();
        }
    }
}
Also used : JRException(net.sf.jasperreports.engine.JRException) File(java.io.File)

Aggregations

JRException (net.sf.jasperreports.engine.JRException)33 JasperReport (net.sf.jasperreports.engine.JasperReport)8 File (java.io.File)7 InputStream (java.io.InputStream)7 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 JasperPrint (net.sf.jasperreports.engine.JasperPrint)6 PrismObject (com.evolveum.midpoint.prism.PrismObject)5 SchemaException (com.evolveum.midpoint.util.exception.SchemaException)5 ArrayList (java.util.ArrayList)4 JRBeanCollectionDataSource (net.sf.jasperreports.engine.data.JRBeanCollectionDataSource)4 Containerable (com.evolveum.midpoint.prism.Containerable)3 PrismContainerValue (com.evolveum.midpoint.prism.PrismContainerValue)3 ByteArrayInputStream (java.io.ByteArrayInputStream)3 Map (java.util.Map)3 JasperDesign (net.sf.jasperreports.engine.design.JasperDesign)3 BusinessServiceException (sic.service.BusinessServiceException)3 ServiceException (sic.service.ServiceException)3 Item (com.evolveum.midpoint.prism.Item)2 PrismContainer (com.evolveum.midpoint.prism.PrismContainer)2