<commit_before>/*
 * Copyright (c) 2016, Mirante and/or its affiliates. All rights reserved.
 * Mirante PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package br.net.mirante.singular.form.persistence.entity;

import br.net.mirante.singular.commons.base.SingularException;
import br.net.mirante.singular.form.io.CompressionUtil;
import br.net.mirante.singular.form.type.core.attachment.IAttachmentRef;
import br.net.mirante.singular.support.persistence.entity.BaseEntity;
import br.net.mirante.singular.support.persistence.util.Constants;
import org.apache.commons.io.IOUtils;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Blob;
import java.sql.SQLException;

@MappedSuperclass
@Table(schema = Constants.SCHEMA, name = "TB_ARQUIVO_PETICAO")
public class AbstractAttachmentEntity extends BaseEntity<String> implements IAttachmentRef {

    @Id
    @Column(name = "CO_ARQUIVO_PETICAO")
    private String id;

    @Column(name = "DS_SHA1", nullable = false)
    private String hashSha1;

    @Lob
    @Column(name = "BL_ARQUIVO_PETICAO", nullable = false)
    private Blob rawContent;

    @Column(name = "NU_TAMANHO", nullable = false)
    private long size;

    private transient File f;

    public AbstractAttachmentEntity() {
    }

    public AbstractAttachmentEntity(String id) {
        this.id = id;
    }

    @Override
    public String getCod() {
        return getId();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHashSha1() {
        return hashSha1;
    }

    public void setHashSha1(String hashSha1) {
        this.hashSha1 = hashSha1;
    }

    public Blob getRawContent() {
        return rawContent;
    }

    public void setRawContent(Blob rawContent) {
        this.rawContent = rawContent;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    @Override
    public String getHasSHA1() {
        return hashSha1;
    }

    @Override
    public InputStream newInputStream() {
        try {
            if (f == null) {
                f = File.createTempFile(id, hashSha1);
                f.deleteOnExit();
                try (InputStream in = rawContent.getBinaryStream();
                     BufferedOutputStream fos = new BufferedOutputStream(new FileOutputStream(f))) {
                    IOUtils.copy(in, fos);
                }
            }
            return CompressionUtil.inflateToInputStream(new FileInputStream(f));<commit_msg>[SINGULAR-FORM] bufferizando leitura do banco<commit_after>/*
 * Copyright (c) 2016, Mirante and/or its affiliates. All rights reserved.
 * Mirante PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package br.net.mirante.singular.form.persistence.entity;

import br.net.mirante.singular.commons.base.SingularException;
import br.net.mirante.singular.form.io.CompressionUtil;
import br.net.mirante.singular.form.io.IOUtil;
import br.net.mirante.singular.form.type.core.attachment.IAttachmentRef;
import br.net.mirante.singular.support.persistence.entity.BaseEntity;
import br.net.mirante.singular.support.persistence.util.Constants;
import org.apache.commons.io.IOUtils;

import javax.persistence.Column;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.MappedSuperclass;
import javax.persistence.Table;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Blob;
import java.sql.SQLException;

@MappedSuperclass
@Table(schema = Constants.SCHEMA, name = "TB_ARQUIVO_PETICAO")
public class AbstractAttachmentEntity extends BaseEntity<String> implements IAttachmentRef {

    @Id
    @Column(name = "CO_ARQUIVO_PETICAO")
    private String id;

    @Column(name = "DS_SHA1", nullable = false)
    private String hashSha1;

    @Lob
    @Column(name = "BL_ARQUIVO_PETICAO", nullable = false)
    private Blob rawContent;

    @Column(name = "NU_TAMANHO", nullable = false)
    private long size;

    private transient File f;

    public AbstractAttachmentEntity() {
    }

    public AbstractAttachmentEntity(String id) {
        this.id = id;
    }

    @Override
    public String getCod() {
        return getId();
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getHashSha1() {
        return hashSha1;
    }

    public void setHashSha1(String hashSha1) {
        this.hashSha1 = hashSha1;
    }

    public Blob getRawContent() {
        return rawContent;
    }

    public void setRawContent(Blob rawContent) {
        this.rawContent = rawContent;
    }

    public long getSize() {
        return size;
    }

    public void setSize(long size) {
        this.size = size;
    }

    @Override
    public String getHasSHA1() {
        return hashSha1;
    }

    @Override
    public InputStream newInputStream() {
        try {
            if (f == null) {
                f = File.createTempFile(id, hashSha1);
                f.deleteOnExit();
                try (InputStream in = rawContent.getBinaryStream();
                     OutputStream fos = IOUtil.newBuffredOutputStream(f)) {
                    IOUtils.copy(in, fos);
                }
            }
            return CompressionUtil.inflateToInputStream(new FileInputStream(f));
{'fraction_non_alphanumeric': 0.09670254570305827, 'fraction_numerical': 0.005125576627370579, 'mean_word_length': 2.858932102834542, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3878646', 'n_tokens_mistral': 1824, 'n_tokens_neox': 1776, 'n_words': 389}