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

com.edmunds.tools.databricks.maven.model.EnvironmentDTO Maven / Gradle / Ivy

There is a newer version: 1.13.1
Show newest version
/*
 *  Copyright 2018 Edmunds.com, Inc.
 *
 *      Licensed under the Apache License, Version 2.0 (the "License");
 *      you may not use this file except in compliance with the License.
 *      You may obtain a copy of the License at
 *
 *          http://www.apache.org/licenses/LICENSE-2.0
 *
 *      Unless required by applicable law or agreed to in writing, software
 *      distributed under the License is distributed on an "AS IS" BASIS,
 *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *      See the License for the specific language governing permissions and
 *      limitations under the License.
 */

package com.edmunds.tools.databricks.maven.model;

import static org.apache.commons.lang3.StringUtils.defaultString;

import com.edmunds.tools.databricks.maven.util.ObjectMapperUtils;
import java.io.File;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.builder.ReflectionToStringBuilder;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;

/**
 * Simple POJO for keeping Project and Environment properties.
 */
public class EnvironmentDTO {

    private static final String DEPLOY_VERSION = "deploy-version";

    private Properties projectProperties;
    private Properties systemProperties;
    private String groupId;
    private String artifactId;
    private String version;
    //This must be in this file due to reliance on this variable for freemarker
    private String environment;
    private String groupWithoutCompany;
    // The rationale for persisting these properties is because a deployed artifact will already have been deployed
    // to a specific place. You cannot change that after the fact!
    private String databricksRepo;
    private String databricksRepoKey;

    /**
     * Don't use this - it's for jackson deserialization only.
     */
    public EnvironmentDTO() {
    }

    /**
     * EnvironmentDTO constructor.
     *
     * @param project project
     * @param environment environment
     * @param databricksRepo databricksRepo
     * @param databricksRepoKey databricksRepoKey
     * @param prefixToStrip prefixToStrip
     */
    public EnvironmentDTO(MavenProject project, String environment, String databricksRepo, String databricksRepoKey,
        String prefixToStrip) {
        this.groupId = project.getGroupId();
        this.artifactId = project.getArtifactId();
        this.projectProperties = project.getProperties();
        this.systemProperties = System.getProperties();
        this.version = defaultString(systemProperties.getProperty(DEPLOY_VERSION), project.getVersion());
        this.groupWithoutCompany = stripCompanyPackage(prefixToStrip, project.getGroupId());
        this.databricksRepo = databricksRepo;
        this.databricksRepoKey = databricksRepoKey;
        this.environment = environment;
        //TODO NEED TO GET RID OF this once we are ready. This is for backwards compatibility
        projectProperties.setProperty("databricks.repo", databricksRepo);
        projectProperties.setProperty("databricks.repo.key", databricksRepoKey);
    }

    public static String stripCompanyPackage(String prefixToStrip, String path) {
        return path.replaceAll(prefixToStrip, "");
    }

    /**
     * Load EnvironmentDTO from file.
     *
     * @param environmentDTOFile environmentDTO file
     * @return EnvironmentDTO
     * @throws MojoExecutionException exception
     */
    public static EnvironmentDTO loadEnvironmentDTOFromFile(File environmentDTOFile) throws MojoExecutionException {
        if (environmentDTOFile == null) {
            throw new MojoExecutionException("environmentDTOFile must be set!");
        }
        try {
            String environmentDTOJson = FileUtils.readFileToString(environmentDTOFile, StandardCharsets.UTF_8);
            return ObjectMapperUtils.deserialize(environmentDTOJson, EnvironmentDTO.class);
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }

    public Properties getProjectProperties() {
        return projectProperties;
    }

    public Properties getSystemProperties() {
        return systemProperties;
    }

    public String getGroupId() {
        return groupId;
    }

    public String getArtifactId() {
        return artifactId;
    }

    public String getVersion() {
        return version;
    }

    public String getEnvironment() {
        return environment;
    }

    public void setEnvironment(String environment) {
        this.environment = environment;
    }

    public String getGroupWithoutCompany() {
        return groupWithoutCompany;
    }

    public String toString() {
        return ReflectionToStringBuilder.toString(this);
    }

    public String getDatabricksRepo() {
        return databricksRepo;
    }

    public void setDatabricksRepo(String databricksRepo) {
        this.databricksRepo = databricksRepo;
    }

    public String getDatabricksRepoKey() {
        return databricksRepoKey;
    }

    public void setDatabricksRepoKey(String databricksRepoKey) {
        this.databricksRepoKey = databricksRepoKey;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy