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

com.denimgroup.threadfix.data.entities.Waf Maven / Gradle / Ivy

////////////////////////////////////////////////////////////////////////
//
//     Copyright (c) 2009-2016 Denim Group, Ltd.
//
//     The contents of this file are subject to the Mozilla Public 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.mozilla.org/MPL/
//
//     Software distributed under the License is distributed on an "AS IS"
//     basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
//     License for the specific language governing rights and limitations
//     under the License.
//
//     The Original Code is ThreadFix.
//
//     The Initial Developer of the Original Code is Denim Group, Ltd.
//     Portions created by Denim Group, Ltd. are Copyright (C)
//     Denim Group, Ltd. All Rights Reserved.
//
//     Contributor(s): Denim Group, Ltd.
//
////////////////////////////////////////////////////////////////////////
package com.denimgroup.threadfix.data.entities;

import com.denimgroup.threadfix.views.AllViews;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonView;
import org.hibernate.validator.constraints.NotEmpty;

import javax.persistence.*;
import javax.validation.constraints.Size;
import java.util.ArrayList;
import java.util.List;

import static com.denimgroup.threadfix.CollectionUtils.list;

@Entity
@Table(name = "Waf")
public class Waf extends AuditableEntity {

	private static final long serialVersionUID = 2937339816996157154L;

	public static final int NAME_LENGTH = 50;
	
	@NotEmpty(message = "{errors.required}")
	@Size(max = NAME_LENGTH, message = "{errors.maxlength} " + NAME_LENGTH + ".")
	private String name;
	private WafType wafType;
	private Integer currentId;
	private WafRuleDirective lastWafRuleDirective;

	private List applicationList;
	private List wafRuleList;
	
	boolean canDelete = false;

	@Column(length = NAME_LENGTH, nullable = false)
    @JsonView(Object.class) // This means it will be included in all ObjectWriters with Views.
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	@Column
	@JsonView(AllViews.RestViewWafs2_1.class)
	public Integer getCurrentId() {
		return currentId;
	}

	public void setCurrentId(Integer currentId) {
		this.currentId = currentId;
	}

	@ManyToOne
	@JoinColumn(name = "wafTypeId", foreignKey = @ForeignKey(name="FK152BC98050448"))
	@JsonView({ AllViews.TableRow.class, AllViews.RestViewWafs2_1.class })
	public WafType getWafType() {
		return wafType;
	}

	public void setWafType(WafType wafType) {
		this.wafType = wafType;
	}

    @Transient
	@JsonView({ AllViews.RestViewWaf2_1.class, AllViews.RestViewWafs2_1.class })
    private String getWafTypeName() {
        return getWafType() == null ? null : getWafType().getName();
    }

	@ManyToOne
	@JoinColumn(name = "wafRuleDirectiveId", foreignKey = @ForeignKey(name="FK152BC91C1AEC8"))
	@JsonView(AllViews.RestViewWafs2_1.class)
	public WafRuleDirective getLastWafRuleDirective() {
		return lastWafRuleDirective;
	}

	public void setLastWafRuleDirective(WafRuleDirective lastWafRuleDirective) {
		this.lastWafRuleDirective = lastWafRuleDirective;
	}

	@OneToMany(mappedBy = "waf")
    @JsonIgnore
	public List getApplications() {
        return applicationList;
	}

    @JsonView({ AllViews.RestViewWaf2_1.class, AllViews.RestViewWafs2_1.class })
    @Transient
    @JsonProperty("applications")
    public List getActiveApplications() {
        List list = list();

        if (applicationList != null) {
            for (Application application : applicationList) {
                if (application.isActive()) {
                    list.add(application);
                }
            }
        }

        return list;
    }

	public void setApplications(List applicationList) {
		this.applicationList = applicationList;
	}

	@OneToMany(mappedBy = "waf")
	@JsonView({ AllViews.TableRow.class, AllViews.RestViewWafs2_1.class })
	public List getWafRules() {
		return wafRuleList;
	}

	public void setWafRules(List wafRuleList) {
		this.wafRuleList = wafRuleList;
	}
	
	@Transient
	@JsonView({ AllViews.TableRow.class, AllViews.RestViewWafs2_1.class })
	public boolean getCanDelete() {
		boolean hasActiveApplication = false;
		if (getApplications() != null) {
			for (Application application : getApplications()) {
				if (application.isActive()) {
					hasActiveApplication = true;
					break;
				}
			}
		}
		return !hasActiveApplication;
	}

    public void addWafRules(List wafRuleList) {
        if (getWafRules()==null)
            setWafRules(new ArrayList());
        for (WafRule rule: wafRuleList) {
            if (!getWafRules().contains(rule))
                getWafRules().add(rule);
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy