/*
 * This file is part of Dependency-Track.
 *
 * Dependency-Track is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * Dependency-Track is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * Dependency-Track. If not, see http://www.gnu.org/licenses/.
 */
package org.owasp.dependencytrack.model;

import javax.persistence.*;
import java.math.BigDecimal;

@Entity
@Table(name = "vulnerability")
public class Vulnerability {

    public enum Severity {
        HIGH,
        MEDIUM,
        LOW,
        INFO
    }

    /**
     * The unique identifier of the persisted object.
     */
    @Id
    @Column(name = "id")
    @GeneratedValue
    private Integer id;

    @Column(name = "name")
    private String name;

    @Column(name = "description", columnDefinition = "LONGTEXT")
    private String description;

    @Column(name = "CWE ")
    private String cwe;

    @Column(name = "cvssscore")
    private BigDecimal cvssScore;

    @Column(name = "matchedcpe")
    private String matchedCPE;

    @Column(name = "matchedallpreviouscpe")
    private String matchedAllPreviousCPE;

    public Integer getId() {
        return id;
    }

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

    public String getCwe() {
        return cwe;
    }

    public String getCweId() {
        if (cwe != null) {
            return cwe.substring(4, 7);
        }
        return null;
    }

    public Severity getSeverity() {
        final double dv = cvssScore.doubleValue();
        if (dv >= 7) {
            return Severity.HIGH;
        } else if (dv >= 4) {
            return Severity.MEDIUM;
        } else if (dv > 0) {
            return Severity.LOW;
        } else {
            return Severity.INFO;
        }
    }

    public String getName() {
        return name;
    }

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

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public void setCwe(String cwe) {
        this.cwe = cwe;
    }

    public BigDecimal getCvssScore() {
        return cvssScore;
    }

    public void setCvssScore(BigDecimal cvssScore) {
        this.cvssScore = cvssScore;
    }

    public String getMatchedCPE() {
        return matchedCPE;
    }

    public void setMatchedCPE(String matchedCPE) {
        this.matchedCPE = matchedCPE;
    }

    public String getMatchedAllPreviousCPE() {
        return matchedAllPreviousCPE;
    }

    public void setMatchedAllPreviousCPE(String matchedAllPreviousCPE) {
        this.matchedAllPreviousCPE = matchedAllPreviousCPE;
    }

}
