Rule.java

package com.tradecloud.domain.model.organisationalunit;

import com.tradecloud.common.base.PersistenceBase;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;

import javax.persistence.*;
import javax.xml.bind.annotation.*;
import java.util.ArrayList;
import java.util.Collection;

/**
 * Configuration rule used by an organisational unit. Example: ruleName -
 * has_rate_source rate_source - STANDARD_RATES lookup_date_type - DEAL_DATE
 */
@Entity(name = "organisationrule")
@Table(name = "organisationrule")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
public class Rule extends PersistenceBase {

    private static final long serialVersionUID = 1L;

    @XmlAttribute
    String code;

    @XmlAttribute
    String description;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Fetch(FetchMode.SUBSELECT)
    @JoinColumn(name = "rule_id")
    @XmlElementWrapper(name = "Attributes")
    @XmlElement(name = "Attribute")
    Collection<RuleAttribute> attributes = new ArrayList<RuleAttribute>();
    ;

    public Rule() {
        super();
    }

    public Rule(String code, String description) {
        super();
        this.code = code;
        this.description = description;
    }

    public Collection<RuleAttribute> getAttributes() {
        return attributes;
    }

    public void setAttributes(Collection<RuleAttribute> attributes) {
        this.attributes = attributes;
    }

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getDescription() {
        return description;
    }

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

    @Override
    public String toString() {
        return "Rule [code=" + code + ", description=" + description + ", attributes=" + attributes + "]";
    }

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = super.hashCode();
        result = prime * result + ((code == null) ? 0 : code.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (!super.equals(obj))
            return false;
        if (getClass() != obj.getClass())
            return false;
        Rule other = (Rule) obj;
        if (code == null) {
            if (other.code != null)
                return false;
        } else if (!code.equals(other.code))
            return false;
        return true;
    }

    public void addAttribute(RuleAttribute ruleAttribute) {
        attributes.add(ruleAttribute);
    }

    /**
     * Gets an attribute from a rule.
     */
    public RuleAttribute getAttributeWithCode(String code) {
        for (RuleAttribute attribute : this.getAttributes()) {
            if (attribute.getCode().equals(code)) {
                return attribute;
            }
        }
        return null;
    }

}