LetterOfCreditTemplate.java

package com.tradecloud.domain.letterofcredit;

import com.tradecloud.common.base.PersistenceBase;
import com.tradecloud.domain.party.Bank;
import com.tradecloud.domain.state.BaseStateful;
import com.tradecloud.domain.supplier.OrganisationalUnitSupplier;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Fetch;
import org.hibernate.annotations.FetchMode;
import org.hibernate.annotations.ForeignKey;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.HashSet;
import java.util.Set;

/**
 * https://connect.devstream.net/display/Dev/Letter+of+Credit+Templates.
 */
@Entity
@Component(value = "letterofcredittemplate")
@Table(name = "letterofcredittemplate")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "LetterOfCreditTemplate")
public class LetterOfCreditTemplate extends PersistenceBase implements BaseStateful<LetterOfCreditTemplateState> {

    @NotNull(message = "ID is required")
    @Column(name = "keyx", length = 16)
    private String key;

    @NotNull(message = "Name is required")
    @Column(length = 65)
    private String name;

    private String description;

    @Column(length = 1300)
    private String documents;

    @Column(length = 1300)
    private String conditions;

    private String charges;

    //private boolean active;
    //private boolean internal;

    private boolean draftsRequired;

    private boolean confirm;

    private boolean irrevocable;

    private boolean transferable;

    @NotNull(message = "Owner is required")
    @ManyToOne
    @ForeignKey(name = "fk_owningentity")
    private Bank owningEntity;

    private Integer presentationPeriod;

    @NotNull
    @Enumerated(EnumType.STRING)
    private LetterOfCreditTemplateState state = LetterOfCreditTemplateState.UNFINALISED;

    private String drawnOn;

    @NotNull(message = "Bank charges are required")
    @Enumerated(EnumType.STRING)
    private LetterOfCreditBankCharges bankCharges;

    @NotNull(message = "Notify by is required")
    @Enumerated(EnumType.STRING)
    private LetterOfCreditNotification notifyBy = LetterOfCreditNotification.COU;

    @OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @Fetch(value = FetchMode.SUBSELECT)
    @JoinTable(name = "lctemplate_orgsupplier",
            joinColumns = @JoinColumn(name = "lctemplate_id", unique = false),
            inverseJoinColumns = @JoinColumn(name = "orgsupplier_id", unique = false))
    private Set<OrganisationalUnitSupplier> selectedSuppliers = new HashSet<OrganisationalUnitSupplier>();

    private boolean integrated;

    public LetterOfCreditTemplate() {
        super();
    }

    public LetterOfCreditTemplate(String key, String name, Bank owningEntity, Set<OrganisationalUnitSupplier> selectedSuppliers,
                                  LetterOfCreditNotification notifyBy, String drawnOn, LetterOfCreditBankCharges bankCharges) {

        this.key = key;
        this.name = name;
        this.owningEntity = owningEntity;
        this.selectedSuppliers = selectedSuppliers;
        this.notifyBy = notifyBy;
        this.drawnOn = drawnOn;
        this.bankCharges = bankCharges;
    }

    public LetterOfCreditNotification getNotifyBy() {
        return notifyBy;
    }

    public void setNotifyBy(LetterOfCreditNotification notifyBy) {
        this.notifyBy = notifyBy;
    }

    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 boolean isActive() {
     return active;
     }

     public void setActive(boolean active) {
     this.active = active;
     }
     */
    @Override
    public LetterOfCreditTemplateState getState() {
        return state;
    }

    @Override
    public void setState(LetterOfCreditTemplateState state) {
        this.state = state;
    }

    /*
     public boolean isInternal() {
     return internal;
     }

     public void setInternal(boolean internal) {
     this.internal = internal;
     }
     */
    protected String description() {
        return description;
    }

    public Bank getOwningEntity() {
        return owningEntity;
    }

    public void setOwningEntity(Bank owner) {
        this.owningEntity = owner;
    }

    public String getDocuments() {
        return documents;
    }

    public void setDocuments(String documents) {
        this.documents = documents;
    }

    public String getConditions() {
        return conditions;
    }

    public void setConditions(String conditions) {
        this.conditions = conditions;
    }

    public boolean isDraftsRequired() {
        return draftsRequired;
    }

    public void setDraftsRequired(boolean draftsRequired) {
        this.draftsRequired = draftsRequired;
    }

    public Integer getPresentationPeriod() {
        return presentationPeriod;
    }

    public void setPresentationPeriod(Integer presentationPeriod) {
        this.presentationPeriod = presentationPeriod;
    }

    public String getDrawnOn() {
        return drawnOn;
    }

    public void setDrawnOn(String drawnOn) {
        this.drawnOn = drawnOn;
    }

    public String getCharges() {
        return charges;
    }

    public void setCharges(String charges) {
        this.charges = charges;
    }

    public LetterOfCreditBankCharges getBankCharges() {
        return bankCharges;
    }

    public void setBankCharges(LetterOfCreditBankCharges bankCharges) {
        this.bankCharges = bankCharges;
    }

    public Set<OrganisationalUnitSupplier> getSelectedSuppliers() {
        return selectedSuppliers;
    }

    public void setSelectedSuppliers(Set<OrganisationalUnitSupplier> selectedSuppliers) {
        this.selectedSuppliers = selectedSuppliers;
    }

    public void addSupplier(OrganisationalUnitSupplier supplier) {
        if (selectedSuppliers == null) {
            selectedSuppliers = new HashSet<OrganisationalUnitSupplier>();
        }
        selectedSuppliers.add(supplier);
    }

    public boolean isConfirm() {
        return confirm;
    }

    public void setConfirm(boolean confirm) {
        this.confirm = confirm;
    }

    public boolean isIrrevocable() {
        return irrevocable;
    }

    public void setIrrevocable(boolean irrevocable) {
        this.irrevocable = irrevocable;
    }

    public boolean isTransferable() {
        return transferable;
    }

    public void setTransferable(boolean transferable) {
        this.transferable = transferable;
    }

    @Override
    public String toString() {
        return StringUtils.isNotBlank(name) ? name : super.toString();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder()
                .appendSuper(super.hashCode())
                .append(name)
                .toHashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof LetterOfCreditTemplate)) {
            return false;
        }
        LetterOfCreditTemplate other = (LetterOfCreditTemplate) obj;
        return new EqualsBuilder()
                .appendSuper(super.equals(obj))
                .append(name, other.name)
                .append(key, other.key)
                .append(owningEntity, other.owningEntity)
                .isEquals();
    }

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    public boolean isDeleted() {
        return this.getState().equals(LetterOfCreditTemplateState.DELETED);
    }

    public boolean isFinalised() {
        return this.getState().equals(LetterOfCreditTemplateState.FINALISED);
    }

    public boolean isUnfinalised() {
        return this.getState().equals(LetterOfCreditTemplateState.UNFINALISED);
    }

    public boolean isIntegrated() {
        return integrated;
    }

    public void setIntegrated(boolean integrated) {
        this.integrated = integrated;
    }
}