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

com.mntviews.base.module.BridgeModule Maven / Gradle / Ivy

The newest version!
package com.mntviews.base.module;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder;
import com.mntviews.bridge.model.ConnectionData;
import com.mntviews.bridge.service.BridgeContext;
import com.mntviews.bridge.service.BridgeExtension;
import com.mntviews.bridge.service.BridgeExtensionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

@JsonDeserialize(builder = BridgeModule.Builder.class)
public class BridgeModule {

    private final static Logger log = LoggerFactory.getLogger(BridgeModule.class);


    private final BridgeExtension bridgeExtension;


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

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

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

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

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

    @JsonProperty("params")
    private final Map params;

    @JsonProperty("isMigrate")
    private final Boolean isMigrate;

    @JsonProperty("props")
    private final Map props;


    private BridgeModule(Builder builder) {
        this.groupTag = builder.groupTag;
        this.metaTag = builder.metaTag;
        this.schemaName = builder.schemaName;
        this.cron = builder.cron;
        this.params = new HashMap<>();
        this.props = new HashMap<>();
        this.type = builder.type;

        if (builder.params != null)
            this.params.putAll(builder.params);

        if (!builder.props.isEmpty())
            this.props.putAll(builder.props);

        this.isMigrate = builder.isMigrate == null || builder.isMigrate;

        Map connectionDataMap = new HashMap<>();
        BridgeModuleContext.getDbs().forEach((tag, db) -> connectionDataMap.put(tag, new ConnectionData(db.getUrl(), db.getUserName(), db.getPassword(), db.getSchemaName(), db.getParams())));

        try {
            Class cls = BridgeModule.class.getClassLoader().loadClass("com.mntviews.mnt.bridge." + type.toLowerCase() + ".service.BridgePlugin");
            Object buider = cls.getMethod("custom").invoke(null);
            buider = buider.getClass().getMethod("withGroupTag", String.class).invoke(buider, groupTag);
            buider = buider.getClass().getMethod("withMetaTag", String.class).invoke(buider, metaTag);
            buider = buider.getClass().getMethod("withParams", Map.class).invoke(buider, params);
            buider = buider.getClass().getMethod("withIsMigrate", Boolean.class).invoke(buider, isMigrate);
            buider = buider.getClass().getMethod("withSchemaName", String.class).invoke(buider, schemaName);
            buider = buider.getClass().getMethod("withDbMap", Map.class).invoke(buider, connectionDataMap);

            for (Map.Entry entry : this.props.entrySet()) {
                buider = buider.getClass().getMethod("with" + entry.getKey().substring(0, 1).toUpperCase() + entry.getKey().substring(1), entry.getValue().getClass()).invoke(buider, entry.getValue());
            }

            bridgeExtension = (BridgeExtension) buider.getClass().getMethod("build").invoke(buider);
            log.info("Module " + type + " initialized");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public Map getProps() {
        return props;
    }

    public String getType() {
        return type;
    }

    public Map getParams() {
        return params;
    }

    public Boolean getMigrate() {
        return isMigrate;
    }

    public String getTypeTag() {
        return bridgeExtension.getTypeTag();
    }


    public Map getBridgeContextMap() {
        return ((BridgeExtensionService) bridgeExtension).getBridgeContextMap();
    }


    public String getGroupTag() {
        return groupTag;
    }


    public String getMetaTag() {
        return metaTag;
    }


    public String getSchemaName() {
        return schemaName;
    }


    public BridgeExtension getBridgeExtension() {
        return bridgeExtension;
    }


    public String getCron() {
        return cron;
    }

    @JsonPOJOBuilder
    public static class Builder {
        private String groupTag;
        private String metaTag;
        private String schemaName;
        private String cron;
        private Map params;
        private Boolean isMigrate;
        private String type;
        private final Map props = new HashMap<>();


        //  @JsonProperty("props")
        //  public void setProps(String name, Object value) {
        //     this.props.put(name, value);
        //  }
        @JsonProperty("props")
        public Builder withProps(Map props) {
            this.props.putAll(props);
            return this;
        }


        @JsonProperty("type")
        public Builder withType(String type) {
            this.type = type;
            return this;
        }

        @JsonProperty("groupTag")
        public Builder withGroupTag(String groupTag) {
            this.groupTag = groupTag;
            return this;
        }

        @JsonProperty("metaTag")
        public Builder withMetaTag(String metaTag) {
            this.metaTag = metaTag;
            return this;
        }

        @JsonProperty("schemaName")
        public Builder withSchemaName(String schemaName) {
            this.schemaName = schemaName;
            return this;
        }

        @JsonProperty("cron")
        public Builder withCron(String cron) {
            this.cron = cron;
            return this;
        }

        @JsonProperty("isMigrate")
        public Builder withIsMigrate(Boolean isMigrate) {
            this.isMigrate = isMigrate;
            return this;
        }


        @JsonProperty("params")
        public Builder withParams(Map params) {
            this.params = params;
            return this;
        }


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




© 2015 - 2024 Weber Informatics LLC | Privacy Policy