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

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

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

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.JROutputException;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;


@JsonDeserialize(builder = JROutputFile.Builder.class)
public class JROutputFile extends JROutput {

    @JsonProperty("path")
    private final String path;

    @JsonProperty("path")
    public String getPath() {
        return path;
    }

    private JROutputFile(Builder builder) {
        path = builder.path;
    }

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

    private void outputFile(JRFile file) throws IOException {
        Files.createDirectories(Paths.get(path));
        try (OutputStream outputStream = new FileOutputStream(path + "/" + file.getFileName())) {
            file.getData().writeTo(outputStream);
        } catch (Exception e) {
            throw new JROutputException(e);
        } finally {
            if (file.getData() != null)
                file.getData().close();
        }
    }

    @Override
    public void execute(List fileList) {
        for (JRFile file : fileList) {
            try {
                outputFile(file);
            } catch (IOException e) {
                throw new JROutputException(e);
            }
        }
    }

    @JsonPOJOBuilder
    public static class Builder {
        private String path;

        @JsonProperty("path")
        public Builder withPath(String path) {
            this.path = path;
            return this;
        }

        public JROutputFile build() {
            return new JROutputFile(this);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy