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

org.bidib.wizard.common.model.settings.ConnectionConfiguration Maven / Gradle / Ivy

package org.bidib.wizard.common.model.settings;

import java.util.List;

import javax.validation.constraints.NotNull;

import org.apache.commons.collections4.IterableUtils;
import org.apache.commons.collections4.Predicate;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.bidib.wizard.api.model.common.PreferencesPortType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.jgoodies.binding.beans.Model;

@Validated
@JsonIgnoreProperties(value = { "propertyChangeListeners", "vetoableChangeListeners", "providerKey" })
public class ConnectionConfiguration extends Model {

    private static final long serialVersionUID = 1L;

    private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionConfiguration.class);

    public static final String PROPERTY_NAME = "name";

    public static final String PROPERTY_CONNECTION_URI = "connectionUri";

    public static final String PROPERTY_CONNECTION_PROVIDER = "connectionProvider";

    @NotNull
    private String id;

    private String name;

    private String connectionUri;

    private String connectionProvider;

    @JsonIgnore
    private String providerKey;

    public String getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        String oldValue = this.name;
        this.name = name;

        firePropertyChange(PROPERTY_NAME, oldValue, name);
    }

    public String getConnectionUri() {
        return connectionUri;
    }

    public void setConnectionUri(String connectionUri) {
        LOGGER.info("Set the connectionUri: {}", connectionUri);
        String oldValue = this.connectionUri;
        this.connectionUri = connectionUri;

        firePropertyChange(PROPERTY_CONNECTION_URI, oldValue, connectionUri);
    }

    /**
     * @return the connection provider
     */
    public String getConnectionProvider() {
        return connectionProvider;
    }

    /**
     * @param connectionProvider
     *            the connectionProvider to set
     */
    public void setConnectionProvider(String connectionProvider) {
        String oldValue = this.connectionProvider;
        this.connectionProvider = connectionProvider;

        firePropertyChange(PROPERTY_CONNECTION_PROVIDER, oldValue, connectionProvider);
    }

    public String getProviderKey() {
        return providerKey;
    }

    public void setProviderKey(String providerKey) {
        this.providerKey = providerKey;
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    public ConnectionConfiguration withId(String id) {
        this.setId(id);
        return this;
    }

    public ConnectionConfiguration withName(String name) {
        this.setName(name);
        return this;
    }

    public ConnectionConfiguration withConnectionUri(String connectionUri) {
        this.setConnectionUri(connectionUri);
        return this;
    }

    public ConnectionConfiguration withConnectionProvider(String connectionProvider) {
        this.setConnectionProvider(connectionProvider);
        return this;
    }

    public static PreferencesPortType toPreferencesPortType(
        List connectionConfigurations, String connectionId) {

        // must get the correct connection from the globalSettings
        ConnectionConfiguration connectionConfiguration =
            IterableUtils.find(connectionConfigurations, new Predicate() {

                @Override
                public boolean evaluate(ConnectionConfiguration config) {
                    return connectionId.equals(config.getId());
                }
            });

        if (connectionConfiguration != null) {
            String portTypeValue =
                connectionConfiguration.getConnectionProvider() + ":" + connectionConfiguration.getConnectionUri();
            LOGGER.info("Use the provided portTypeValue: {}", portTypeValue);
            PreferencesPortType portType = PreferencesPortType.getValue(portTypeValue);
            return portType;
        }
        return null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy