All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.mntviews.jreport.JRTemplate Maven / Gradle / Ivy

There is a newer version: 1.2.2
Show newest version
package com.mntviews.jreport;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.mntviews.jreport.exception.JRReportException;
import com.mntviews.jreport.exception.JRTemplateException;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperReport;

import java.io.InputStream;
import java.sql.Connection;

@JsonDeserialize(builder = JRTemplate.Builder.class)
public class JRTemplate {
    @JsonProperty("source")
    private final JRTemplateSource templateSource;

    private JRTemplate(Builder builder) {
        if (null == builder.templateSource)
            throw new JRTemplateException("templateSource is null");
        this.templateSource = builder.templateSource;

    }

    public static Builder custom() {
        return new JRTemplate.Builder();
    }

    public InputStream load(JRTemplateCommon jrTemplateCommon) {
        InputStream loadTemplateSource = templateSource.load(jrTemplateCommon);
        if (null == loadTemplateSource)
            throw new JRTemplateException("loadTemplateSource is null");

        return loadTemplateSource;
    }

    public JasperReport compile(JRTemplateCommon jrTemplateCommon) {
        try {
            return JasperCompileManager.compileReport(load(jrTemplateCommon));
        } catch (JRException e) {
            throw new JRReportException(e);
        }
    }


    @JsonPOJOBuilder
    public static final class Builder {
        private JRTemplateSource templateSource;

        @JsonProperty("source")
        public Builder withTemplateSource(JRTemplateSource templateSource) {
            this.templateSource=templateSource;
            return this;
        }
        public JRTemplate build() {
            return new JRTemplate(this);
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy