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

com.atlan.samples.loaders.models.DatabaseDetails Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
// Generated by delombok at Thu Sep 07 11:44:18 UTC 2023
/* SPDX-License-Identifier: Apache-2.0 */
/* Copyright 2023 Atlan Pte. Ltd. */
package com.atlan.samples.loaders.models;

import com.atlan.Atlan;
import com.atlan.exception.AtlanException;
import com.atlan.exception.NotFoundException;
import com.atlan.model.assets.Database;
import com.atlan.util.AssetBatch;
import java.util.*;

/**
 * Utility class for capturing the full details provided about a database.
 */
public class DatabaseDetails extends AssetDetails {
    @java.lang.SuppressWarnings("all")
    private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(DatabaseDetails.class);
    public static final String COL_DB = "DATABASE NAME";
    private static final List REQUIRED = List.of(ConnectionDetails.COL_CONNECTOR, ConnectionDetails.COL_CONNECTION, COL_DB);
    private static final List REQUIRED_EMPTY = List.of(SchemaDetails.COL_SCHEMA, ContainerDetails.COL_CONTAINER, ColumnDetails.COL_COLUMN);
    private String connectionQualifiedName;
    private String name;

    /**
     * {@inheritDoc}
     */
    @Override
    public String getIdentity() {
        return connectionQualifiedName + "/" + name;
    }

    /**
     * Construct a database's qualifiedName from the row of data and cache of connections.
     *
     * @param connectionCache cache of connections
     * @param row of data
     * @return the qualifiedName for the database on that row of data
     */
    public static String getQualifiedName(Map connectionCache, Map row) {
        String connectionQN = ConnectionDetails.getQualifiedName(connectionCache, row);
        if (connectionQN != null) {
            String dbName = row.get(COL_DB);
            if (dbName != null) {
                return connectionQN + "/" + dbName;
            }
        }
        return null;
    }

    /**
     * Build up details about the database on the provided row.
     *
     * @param connectionCache a cache of connections that have first been resolved across the spreadsheet
     * @param row a row of data from the spreadsheet, as a map from column name to value
     * @param delim delimiter used in cells that can contain multiple values
     * @return the database details for that row
     */
    public static DatabaseDetails getFromRow(Map connectionCache, Map row, String delim) {
        if (getMissingFields(row, REQUIRED).isEmpty()) {
            String connectionQualifiedName = ConnectionDetails.getQualifiedName(connectionCache, row);
            if (getRequiredEmptyFields(row, REQUIRED_EMPTY).isEmpty()) {
                return getFromRow(DatabaseDetails.builder(), row, delim).connectionQualifiedName(connectionQualifiedName).name(row.get(COL_DB)).stub(false).build();
            } else {
                return DatabaseDetails.builder().connectionQualifiedName(connectionQualifiedName).name(row.get(COL_DB)).stub(true).build();
            }
        }
        return null;
    }

    /**
     * Create databases in bulk, if they do not exist, or update them if they do (idempotent).
     *
     * @param databases the set of databases to ensure exist
     * @param batchSize maximum number of databases to create per batch
     * @param updateOnly if true, only attempt to update existing assets, otherwise allow assets to be created as well
     */
    public static void upsert(Map databases, int batchSize, boolean updateOnly) {
        AssetBatch batch = new AssetBatch(Atlan.getDefaultClient(), Database.TYPE_NAME, batchSize);
        Map> toClassify = new HashMap<>();
        long totalResults = databases.size();
        long localCount = 0;
        try {
            for (DatabaseDetails details : databases.values()) {
                String connectionQualifiedName = details.getConnectionQualifiedName();
                String databaseName = details.getName();
                if (updateOnly) {
                    String qualifiedName = Database.generateQualifiedName(databaseName, connectionQualifiedName);
                    try {
                        Database.get(Atlan.getDefaultClient(), qualifiedName, false);
                        Database toUpdate = Database.updater(qualifiedName, databaseName).description(details.getDescription()).certificateStatus(details.getCertificate()).certificateStatusMessage(details.getCertificateStatusMessage()).announcementType(details.getAnnouncementType()).announcementTitle(details.getAnnouncementTitle()).announcementMessage(details.getAnnouncementMessage()).ownerUsers(details.getOwnerUsers()).ownerGroups(details.getOwnerGroups()).build();
                        if (!details.getAtlanTags().isEmpty()) {
                            toClassify.put(toUpdate.getQualifiedName(), details.getAtlanTags());
                        }
                        localCount++;
                        if (batch.add(toUpdate) != null) {
                            log.info(" ... processed {}/{} ({}%)", localCount, totalResults, Math.round(((double) localCount / totalResults) * 100));
                        }
                    } catch (NotFoundException e) {
                        log.warn("Unable to find existing database — skipping: {}", qualifiedName, e);
                    } catch (AtlanException e) {
                        log.error("Unable to lookup whether database exists or not.", e);
                    }
                } else {
                    Database database = Database.creator(databaseName, connectionQualifiedName).description(details.getDescription()).certificateStatus(details.getCertificate()).certificateStatusMessage(details.getCertificateStatusMessage()).announcementType(details.getAnnouncementType()).announcementTitle(details.getAnnouncementTitle()).announcementMessage(details.getAnnouncementMessage()).ownerUsers(details.getOwnerUsers()).ownerGroups(details.getOwnerGroups()).build();
                    if (!details.getAtlanTags().isEmpty()) {
                        toClassify.put(database.getQualifiedName(), details.getAtlanTags());
                    }
                    localCount++;
                    if (batch.add(database) != null) {
                        log.info(" ... processed {}/{} ({}%)", localCount, totalResults, Math.round(((double) localCount / totalResults) * 100));
                    }
                }
            }
            // And don't forget to flush out any that remain
            if (batch.flush() != null) {
                log.info(" ... processed {}/{} ({}%)", localCount, totalResults, Math.round(((double) localCount / totalResults) * 100));
            }
        } catch (AtlanException e) {
            log.error("Unable to bulk-upsert database details.", e);
        }
        // Classifications must be added in a second pass, after the asset exists
        appendAtlanTags(toClassify, Database.TYPE_NAME);
    }


    @java.lang.SuppressWarnings("all")
    public static abstract class DatabaseDetailsBuilder> extends AssetDetails.AssetDetailsBuilder {
        @java.lang.SuppressWarnings("all")
        private String connectionQualifiedName;
        @java.lang.SuppressWarnings("all")
        private String name;

        @java.lang.Override
        @java.lang.SuppressWarnings("all")
        protected abstract B self();

        @java.lang.Override
        @java.lang.SuppressWarnings("all")
        public abstract C build();

        /**
         * @return {@code this}.
         */
        @java.lang.SuppressWarnings("all")
        public B connectionQualifiedName(final String connectionQualifiedName) {
            this.connectionQualifiedName = connectionQualifiedName;
            return self();
        }

        /**
         * @return {@code this}.
         */
        @java.lang.SuppressWarnings("all")
        public B name(final String name) {
            this.name = name;
            return self();
        }

        @java.lang.Override
        @java.lang.SuppressWarnings("all")
        public java.lang.String toString() {
            return "DatabaseDetails.DatabaseDetailsBuilder(super=" + super.toString() + ", connectionQualifiedName=" + this.connectionQualifiedName + ", name=" + this.name + ")";
        }
    }


    @java.lang.SuppressWarnings("all")
    private static final class DatabaseDetailsBuilderImpl extends DatabaseDetails.DatabaseDetailsBuilder {
        @java.lang.SuppressWarnings("all")
        private DatabaseDetailsBuilderImpl() {
        }

        @java.lang.Override
        @java.lang.SuppressWarnings("all")
        protected DatabaseDetails.DatabaseDetailsBuilderImpl self() {
            return this;
        }

        @java.lang.Override
        @java.lang.SuppressWarnings("all")
        public DatabaseDetails build() {
            return new DatabaseDetails(this);
        }
    }

    @java.lang.SuppressWarnings("all")
    protected DatabaseDetails(final DatabaseDetails.DatabaseDetailsBuilder b) {
        super(b);
        this.connectionQualifiedName = b.connectionQualifiedName;
        this.name = b.name;
    }

    @java.lang.SuppressWarnings("all")
    public static DatabaseDetails.DatabaseDetailsBuilder builder() {
        return new DatabaseDetails.DatabaseDetailsBuilderImpl();
    }

    @java.lang.SuppressWarnings("all")
    public String getConnectionQualifiedName() {
        return this.connectionQualifiedName;
    }

    @java.lang.SuppressWarnings("all")
    public String getName() {
        return this.name;
    }

    @java.lang.Override
    @java.lang.SuppressWarnings("all")
    public boolean equals(final java.lang.Object o) {
        if (o == this) return true;
        if (!(o instanceof DatabaseDetails)) return false;
        final DatabaseDetails other = (DatabaseDetails) o;
        if (!other.canEqual((java.lang.Object) this)) return false;
        final java.lang.Object this$connectionQualifiedName = this.getConnectionQualifiedName();
        final java.lang.Object other$connectionQualifiedName = other.getConnectionQualifiedName();
        if (this$connectionQualifiedName == null ? other$connectionQualifiedName != null : !this$connectionQualifiedName.equals(other$connectionQualifiedName)) return false;
        final java.lang.Object this$name = this.getName();
        final java.lang.Object other$name = other.getName();
        if (this$name == null ? other$name != null : !this$name.equals(other$name)) return false;
        return true;
    }

    @java.lang.SuppressWarnings("all")
    protected boolean canEqual(final java.lang.Object other) {
        return other instanceof DatabaseDetails;
    }

    @java.lang.Override
    @java.lang.SuppressWarnings("all")
    public int hashCode() {
        final int PRIME = 59;
        int result = 1;
        final java.lang.Object $connectionQualifiedName = this.getConnectionQualifiedName();
        result = result * PRIME + ($connectionQualifiedName == null ? 43 : $connectionQualifiedName.hashCode());
        final java.lang.Object $name = this.getName();
        result = result * PRIME + ($name == null ? 43 : $name.hashCode());
        return result;
    }

    @java.lang.Override
    @java.lang.SuppressWarnings("all")
    public java.lang.String toString() {
        return "DatabaseDetails(super=" + super.toString() + ", connectionQualifiedName=" + this.getConnectionQualifiedName() + ", name=" + this.getName() + ")";
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy