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

com.mntviews.mnt.bridge.file.service.impl.BridgePluginImpl Maven / Gradle / Ivy

Go to download

Worker-queue implementation for uploading files to the databases using native mechanisms like sqlldr,pg_dump ...

There is a newer version: 1.0.1
Show newest version
package com.mntviews.mnt.bridge.file.service.impl;

import com.mntviews.Util;
import com.mntviews.mnt.bridge.file.service.BridgePlugin;
import com.mntviews.mnt.bridge.file.service.exception.BridgeFileException;
import com.mntviews.bridge.model.BufData;
import com.mntviews.bridge.model.ConnectionData;
import com.mntviews.bridge.model.ProcessData;
import com.mntviews.bridge.service.*;
import com.mntviews.bridge.service.exception.TaskServiceException;
import com.mntviews.bridge.service.impl.BridgeExtensionImpl;
import com.mntviews.model.UploadData;
import com.mntviews.model.UploadType;
import com.mntviews.upload.UploadContext;
import com.mntviews.upload.UploadDataBaseType;
import com.mntviews.upload.UploadService;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.sql.Connection;
import java.util.*;

import static com.mntviews.Util.nvl;

@Slf4j
public class BridgePluginImpl extends BridgeExtensionImpl implements BridgePlugin, BridgeProcessing {


    public static final String TYPE_TAG = "FILE";

    private final String tableName;

    private final Boolean isRename;

    private final Boolean isDelAfterUpload;

    private final String script;


    public String getTableName() {
        return tableName;
    }

    public Boolean getIsRename() {
        return isRename;
    }

    public Boolean getIsDelAfterUpload() {
        return isDelAfterUpload;
    }


    public String getScript() {
        return script;
    }

    private BridgePluginImpl(Builder builder) {
        super(builder);

        if (builder.tableName == null)
            throw new TaskServiceException("tableName is null");
        else
            this.tableName = builder.tableName;

        this.isRename = builder.isRename != null && builder.isRename;
        this.isDelAfterUpload = builder.isDelAfterUpload != null && builder.isDelAfterUpload;
        this.script = builder.script;
    }


    public void init() {
        Map paramsL = new HashMap<>();
        paramsL.put("ORDER", "LIFO");
        paramsL.put("SKIP", 1);
        paramsL.put("ATTEMPT", -1);
        paramsL.putAll(this.params);


        for (Map.Entry entry : dbMap.entrySet()) {
            BridgeContext bridgeContext = BridgeContext.custom(groupTag, metaTag
                            , new ConnectionData(entry.getValue().getUrl()
                                    , entry.getValue().getUserName()
                                    , entry.getValue().getPassword()))
                    .withSchemaName(BridgeUtil.nvl(schemaName, entry.getValue().getSchemaName()))
                    .withBeforeProcessing(this)
                    .withParam(paramsL)
                    .withDataBaseType(DataBaseType.findByJdbcUrl(entry.getValue().getUrl()))
                    .build();
            if (isMigrate)
                bridgeContext.migrate(true);
            bridgeContext.init();
            bridgeContextMap.put(entry.getKey(), bridgeContext);
        }

        checkBridgeContextMap();
    }


    @Override
    public void process(Connection connection, ProcessData processData, BridgeContext bridgeContext) {
        Optional bufDataOpt = bridgeContext.findBufDataById(processData.getBufId(), connection);
        BufData bufData = bufDataOpt.orElseThrow(() -> new BridgeFileException("Buf not found"));
        Thread.currentThread().setName(bufData.getFId() + "{" + bufData.getFTag() + "}");
        //   log.trace("dbTag=" + dbMap.getDb().get(bufData.getFMsg()));
        //ConnectionData connectionData = dbMap.get(bufData.getFMsg());

        try {
            UploadData uploadData = objectMapper.readValue(bufData.getFPayload(), UploadData.class);

            ConnectionData connectionData = dbMap.get(uploadData.getDbTag());
            if (connectionData == null) {
                throw new BridgeFileException("connectionData property not found {dbTag=" + uploadData.getDbTag() + "}");
            }

            if (!uploadData.getIsSkip()) {
                log.debug("bufData.fPayload={}", bufData.getFPayload());
                String script = nvl(uploadData.getScript(), this.script);
                if (script == null)
                    throw new BridgeFileException("script property is null");
                String decodedScript = new String(Base64.getDecoder().decode(script), StandardCharsets.UTF_8);
                script = decodedScript
                        .replace(UploadService.TABLE_NAME_TEMPLATE, nvl(uploadData.getTableName(), tableName))
                        .replace(UploadService.FILE_ID_TEMPLATE, String.valueOf(bufData.getId()));

                log.trace("script=" + script);

                UploadContext uploadContext = UploadContext.custom().withDataBaseType(UploadDataBaseType.valueOf(bridgeContext.getDataBaseType().name()))
                        .withUrl(nvl(connectionData.findParam(BridgePlugin.SCRIPT_URL_PARAM, String.class), connectionData.getUrl()))
                        .withUserName(connectionData.getUserName())
                        .withPassword(connectionData.getPassword())
                        .withUploadType(uploadData.getUploadType())
                        .withScript(script).build();

                Path path = Paths.get(uploadData.getFileData().getPath() + uploadData.getFileData().getFileName());
                if (!path.toFile().exists())
                    throw new BridgeFileException("File " + path + " not found");

                if (nvl(uploadData.getIsRename(), isRename)) {
                    File newFile = new File(uploadData.getFileData().getPath() + UUID.randomUUID() + ".dat");

                    try {
                        Files.move(path, newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
                    } catch (IOException e) {
                        throw new BridgeFileException(e.getMessage());
                    }

                    path = newFile.toPath();
                }
                uploadContext.upload(path.toFile());

                if (isDelAfterUpload)
                    Files.deleteIfExists(path);
            } else
                log.debug("Skipped {fileName=" + uploadData.getFileData().getFileName() + "}");
        } catch (Exception e) {
            log.error(e.getMessage());
            throw new BridgeFileException(e);
        }


    }

    public static class Builder> extends BridgeExtensionImpl.Builder {

        private String tableName;

        private Boolean isRename;

        private Boolean isDelAfterUpload;

        private String script;

        public T withTableName(String tableName) {
            this.tableName = tableName;
            return self();
        }

        public T withIsRename(Boolean isRename) {
            this.isRename = isRename;
            return self();
        }

        public T withIsDelAfterUpload(Boolean isDelAfterUpload) {
            this.isDelAfterUpload = isDelAfterUpload;
            return self();
        }

        public T withScript(String script) {
            this.script = script;
            return self();
        }


        @Override
        public BridgePluginImpl build() {
            BridgePluginImpl bridgePlugin = new BridgePluginImpl(this);
            bridgePlugin.init();
            return bridgePlugin;
        }
    }

    @Override
    public String getTypeTag() {
        return TYPE_TAG;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy