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

org.nuiton.topia.service.script.TopiaSqlScriptGeneratorServiceImpl Maven / Gradle / Ivy

package org.nuiton.topia.service.script;

/*-
 * #%L
 * ObServe Toolkit :: ToPIA Script service
 * %%
 * Copyright (C) 2017 - 2018 IRD, 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 com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.nuiton.topia.persistence.TopiaApplicationContext;
import org.nuiton.topia.persistence.TopiaException;
import org.nuiton.topia.persistence.script.TopiaBlobsContainer;
import org.nuiton.topia.persistence.script.TopiaSqlScript;
import org.nuiton.topia.service.script.executor.SqlRequestExecutor;
import org.nuiton.topia.service.script.executor.SqlRequestExecutorProvider;
import org.nuiton.topia.service.script.request.SqlRequest;

import java.nio.file.Path;
import java.util.Map;
import java.util.Optional;
import java.util.Set;

/**
 * Created by tchemit on 13/05/2018.
 *
 * @author Tony Chemit - [email protected]
 */
public class TopiaSqlScriptGeneratorServiceImpl implements TopiaSqlScriptGeneratorService {

    private TopiaSqlScriptGeneratorServiceConfiguration configuration;
    private TopiaApplicationContext topiaApplicationContext;

    @Override
    public void initTopiaService(TopiaApplicationContext topiaApplicationContext, Map serviceConfiguration) {

        this.topiaApplicationContext = topiaApplicationContext;
        this.configuration = new TopiaSqlScriptGeneratorServiceConfiguration();

        String readFetchSizeStr = serviceConfiguration.get(TopiaSqlScriptGeneratorServiceConfiguration.PROPERTY_READ_FETCH_SIZE);
        int readFetchSize = readFetchSizeStr == null ? TopiaSqlScriptGeneratorServiceConfiguration.DEFAULT_READ_FETCH_SIZE : Integer.valueOf(readFetchSizeStr);
        configuration.setReadFetchSize(readFetchSize);

        String gzipStr = serviceConfiguration.get(TopiaSqlScriptGeneratorServiceConfiguration.PROPERTY_GZIP);
        boolean gzip = gzipStr == null ? TopiaSqlScriptGeneratorServiceConfiguration.DEFAULT_GZIP : Boolean.valueOf(gzipStr);
        configuration.setGzip(gzip);
    }

    @Override
    public TopiaSqlScriptGeneratorServiceConfiguration getConfiguration() {
        return configuration;
    }

    @Override
    public TopiaSqlScriptGeneratorRequest newRequest(Path target) {
        return new TopiaSqlScriptGeneratorRequest(target).gzip(configuration.isGzip()).readFetchSize(configuration.getReadFetchSize());
    }

    @Override
    public TopiaSqlScript generate(TopiaSqlScriptGeneratorRequest request) {

        ImmutableMap.Builder blobsContainersBuilder = ImmutableMap.builder();

        try (TopiaSqlScriptGeneratorServiceContext context = new TopiaSqlScriptGeneratorServiceContext(request, topiaApplicationContext)) {
            for (SqlRequest sqlRequest : request.getRequests()) {
                consumeRequest(sqlRequest, context);
            }

            Optional> optionalBlobsContainers = context.getBlobsContainersBuilder();
            if (optionalBlobsContainers.isPresent()) {
                Set blobsContainers = optionalBlobsContainers.get();
                for (TopiaBlobsContainer.Builder blobsContainerBuilder : blobsContainers) {
                    String tableName = blobsContainerBuilder.getTableName();
                    blobsContainersBuilder.put(tableName, blobsContainerBuilder.build());
                }
            }

        } catch (RuntimeException e) {
            throw e;
        } catch (Exception e) {
            throw new TopiaException("Can't execute requests", e);
        }

        ImmutableSet blobsContainers = ImmutableSet.copyOf(blobsContainersBuilder.build().values());
        return TopiaSqlScript.of(request.getPath()).addBlobsContainers(blobsContainers);
    }

    @Override
    public void close() {
    }

    private > void consumeRequest(R request, TopiaSqlScriptGeneratorServiceContext context) {
        Optional action = SqlRequestExecutorProvider.get().get(request);
        action.orElseThrow(() -> new NullPointerException("Could not find action for request type: " + request.getClass().getName())).execute(request, context);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy