/*
 * This file is part of dependency-check-core.
 *
 * 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.
 *
 * Copyright (c) 2014 Jeremy Long. All Rights Reserved.
 */
package org.owasp.dependencycheck.data.nexus;

/**
 * Simple bean representing a Maven Artifact.
 *
 * @author colezlaw
 */
public class MavenArtifact {

    /**
     * The base URL for download artifacts from Central.
     */
    private static final String CENTRAL_CONTENT_URL = "http://search.maven.org/remotecontent?filepath=";

    /**
     * The groupId
     */
    private String groupId;

    /**
     * The artifactId
     */
    private String artifactId;

    /**
     * The version
     */
    private String version;

    /**
     * The artifact url. This may change depending on which Nexus server the search took place.
     */
    private String artifactUrl;
    /**
     * The url to download the POM from.
     */
    private String pomUrl;

    /**
     * Creates an empty MavenArtifact.
     */
    public MavenArtifact() {
    }

    /**
     * Creates a MavenArtifact with the given attributes.
     *
     * @param groupId the groupId
     * @param artifactId the artifactId
     * @param version the version
     */
    public MavenArtifact(String groupId, String artifactId, String version) {
        this.groupId = groupId;
        this.artifactId = artifactId;
        this.version = version;
    }

    /**
     * Creates a MavenArtifact with the given attributes.
     *
     * @param groupId the groupId
     * @param artifactId the artifactId
     * @param version the version
     * @param jarAvailable if the jar file is available from central
     * @param pomAvailable if the pom file is available from central
     */
    public MavenArtifact(String groupId, String artifactId, String version, boolean jarAvailable, boolean pomAvailable) {
        this.groupId = groupId;
        this.artifactId = artifactId;
        this.version = version;
        if (jarAvailable) {
            //org/springframework/spring-core/3.2.0.RELEASE/spring-core-3.2.0.RELEASE.pom
            this.artifactUrl = this.CENTRAL_CONTENT_URL + groupId.replace('.', '/') + "/" + artifactId.replace('.', '/') + "/"
                    + version + "/" + artifactId + "-" + version + ".jar";
        }
        if (pomAvailable) {
            //org/springframework/spring-core/3.2.0.RELEASE/spring-core-3.2.0.RELEASE.pom
            this.pomUrl = this.CENTRAL_CONTENT_URL + groupId.replace('.', '/') + "/" + artifactId.replace('.', '/') + "/"
                    + version + "/" + artifactId + "-" + version + ".pom";
        }
    }

    /**
     * Creates a MavenArtifact with the given attributes.
     *
     * @param groupId the groupId
     * @param artifactId the artifactId
     * @param version the version
     * @param url the artifactLink url
     */
    public MavenArtifact(String groupId, String artifactId, String version, String url) {
        this.groupId = groupId;
        this.artifactId = artifactId;
        this.version = version;
        this.artifactUrl = url;
    }

    /**
     * Returns the Artifact coordinates as a String.
     *
     * @return the String representation of the artifact coordinates
     */
    @Override
    public String toString() {
        return String.format("%s:%s:%s", groupId, artifactId, version);
    }

    /**
     * Sets the groupId.
     *
     * @param groupId the groupId
     */
    public void setGroupId(String groupId) {
        this.groupId = groupId;
    }

    /**
     * Gets the groupId.
     *
     * @return the groupId
     */
    public String getGroupId() {
        return groupId;
    }

    /**
     * Sets the artifactId.
     *
     * @param artifactId the artifactId
     */
    public void setArtifactId(String artifactId) {
        this.artifactId = artifactId;
    }

    /**
     * Gets the artifactId.
     *
     * @return the artifactId
     */
    public String getArtifactId() {
        return artifactId;
    }

    /**
     * Sets the version.
     *
     * @param version the version
     */
    public void setVersion(String version) {
        this.version = version;
    }

    /**
     * Gets the version.
     *
     * @return the version
     */
    public String getVersion() {
        return version;
    }

    /**
     * Sets the artifactUrl.
     *
     * @param artifactUrl the artifactUrl
     */
    public void setArtifactUrl(String artifactUrl) {
        this.artifactUrl = artifactUrl;
    }

    /**
     * Gets the artifactUrl.
     *
     * @return the artifactUrl
     */
    public String getArtifactUrl() {
        return artifactUrl;
    }

    /**
     * Get the value of pomUrl.
     *
     * @return the value of pomUrl
     */
    public String getPomUrl() {
        return pomUrl;
    }

    /**
     * Set the value of pomUrl.
     *
     * @param pomUrl new value of pomUrl
     */
    public void setPomUrl(String pomUrl) {
        this.pomUrl = pomUrl;
    }

}

// vim: cc=120:sw=4:ts=4:sts=4
