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

org.nuiton.topia.persistence.script.TopiaSqlScript Maven / Gradle / Ivy

There is a newer version: 2.0.7
Show newest version
package org.nuiton.topia.persistence.script;

/*-
 * #%L
 * ToPIA Extension :: Persistence
 * %%
 * Copyright (C) 2018 - 2022 Ultreia.io
 * %%
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program.  If not, see
 * .
 * #L%
 */

import io.ultreia.java4all.http.json.JsonAware;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.Supplier;

/**
 * Created by tchemit on 14/05/2018.
 *
 * @author Tony Chemit - [email protected]
 */
public class TopiaSqlScript implements JsonAware {

    protected Supplier location;
    protected Set blobsContainers;

    public static TopiaSqlScript of(Path location) {
        try {
            return of(location.toUri().toURL());
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static TopiaSqlScript of(URI location) {
        try {
            return of(location.toURL());
        } catch (MalformedURLException e) {
            throw new IllegalArgumentException(e);
        }
    }

    public static TopiaSqlScript of(byte... location) {
        return new TopiaSqlScript(() -> SqlScriptReader.of(location));
    }

    public static TopiaSqlScript of(String location) {
        return new TopiaSqlScript(() -> SqlScriptReader.of(location));
    }

    public static TopiaSqlScript of(URL location) {
        return new TopiaSqlScript(() -> SqlScriptReader.of(location));
    }

    protected TopiaSqlScript(Supplier location) {
        this.location = location;
    }

    public TopiaSqlScript addBlobsContainer(TopiaBlobsContainer blobsContainer) {
        getBlobsContainers().add(blobsContainer);
        return this;
    }

    public TopiaSqlScript addBlobsContainers(Collection blobsContainers) {
        getBlobsContainers().addAll(blobsContainers);
        return this;
    }

    public Set getBlobsContainers() {
        return blobsContainers == null ? blobsContainers = new LinkedHashSet<>() : blobsContainers;
    }

    public SqlScriptReader getLocation() {
        return location.get();
    }

    public void setLocation(Supplier location) {
        this.location = location;
    }

    public void copy(Path target) throws IOException {
        try (SqlScriptWriter scriptWriter = SqlScriptWriter.of(target)) {
            copy(scriptWriter);
        }
    }

    public void copy(OutputStream target) throws IOException {
        try (SqlScriptWriter scriptWriter = SqlScriptWriter.of(target)) {
            copy(scriptWriter);
        }
    }

    public void copy(SqlScriptWriter scriptWriter) throws IOException {
        try (SqlScriptReader scriptReader = location.get()) {
            for (String statement : scriptReader) {
                scriptWriter.writeSql(statement);
            }
        }
    }

    public String content() {
        try (ByteArrayOutputStream target = new ByteArrayOutputStream()) {
            try (BufferedOutputStream buffer = new BufferedOutputStream(target)) {
                copy(buffer);
            }
            return target.toString(StandardCharsets.UTF_8);
        } catch (IOException e) {
            throw new IllegalStateException("Can't copy script content", e);
        }
    }

    public byte[] toByteArray() {
        try (ByteArrayOutputStream target = new ByteArrayOutputStream()) {
            try (BufferedOutputStream buffer = new BufferedOutputStream(target)) {
                copy(buffer);
            }
            return target.toByteArray();
        } catch (IOException e) {
            throw new IllegalStateException("Can't copy script content", e);
        }
    }

    public boolean withBlobs() {
        return blobsContainers != null && blobsContainers.size() > 1;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy