DocumentGroup.java

package com.tradecloud.domain.dms;

import com.tradecloud.common.base.PersistenceBase;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.LazyCollection;
import org.hibernate.annotations.LazyCollectionOption;
import org.hibernate.annotations.NaturalId;
import org.springframework.stereotype.Component;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.*;
import java.util.*;

@Entity
@Component(value = "documentgroup")
@Table(name = "documentgroup")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "DocumentGroup")
@NamedQueries({@NamedQuery(name = "findByType", query = "from DocumentGroup as d where d.documentGroupType = :documentGroupType")})
public class DocumentGroup extends PersistenceBase implements Comparable<DocumentGroup> {

    private static final long serialVersionUID = 8448926222528885605L;

    @NotNull
    @NaturalId(mutable = true)
    private String key;

    private String createdBy;

    @OneToOne
    private DocumentGroupType documentGroupType;

    @OneToMany(mappedBy = "documentGroup", cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    private Set<Document> documents = new HashSet<Document>();

    @OneToMany(cascade = CascadeType.ALL)
    @LazyCollection(LazyCollectionOption.FALSE)
    @XmlElementWrapper(name = "ElcLineItems")
    @XmlElement(name = "ElcLineItem")
    private List<Property> properties = new ArrayList<Property>();

    @ManyToOne(cascade = CascadeType.ALL)
    private Step currentStep;

    public String getKey() {
        return key;
    }

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

    public String getCreatedBy() {
        return createdBy;
    }

    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }

    public DocumentGroupType getDocumentGroupType() {
        return documentGroupType;
    }

    public void setDocumentGroupType(DocumentGroupType documentGroupType) {
        this.documentGroupType = documentGroupType;
    }

    public Set<Document> getDocuments() {
        return documents;
    }

    public void setDocuments(Set<Document> documents) {
        this.documents = documents;
    }

    public void addDocument(Document document) {
        this.documents.add(document);
    }

    public List<Document> getDocumentList() {
        List list = new ArrayList<>(documents);
        Collections.sort(list);
        return list;
    }

    public void setProperties(List<Property> properties) {
        this.properties = properties;
    }

    public List<Property> getProperties() {
        Collections.sort(properties);
        return properties;
    }

    public void addProperty(Property property) {
        properties.add(property);
    }

    @Override
    public boolean equals(final Object other) {
        if (!(other instanceof DocumentGroup))
            return false;
        DocumentGroup castOther = (DocumentGroup) other;
        return new EqualsBuilder().append(getId(), castOther.getId()).append(getKey(), castOther.getKey())
                .append(getCreated() != null ? getCreated().getTime() : -1, castOther.getCreated() != null ? castOther.getCreated().getTime() : -1)
                .append(getDocumentGroupType(), castOther.getDocumentGroupType()).isEquals();
    }

    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(getId()).append(key).append(created).append(documentGroupType).toHashCode();
    }

    public void removeAllProperties() {
        properties = new ArrayList<Property>();
    }

    public Step getCurrentStep() {
        return currentStep;
    }

    public void setCurrentStep(Step currentStep) {
        this.currentStep = currentStep;
    }

    @Override
    public int compareTo(DocumentGroup o) {
        if (o != null && o.getKey() != null && this.getKey() != null) {
            return this.getKey().toLowerCase().compareTo(o.getKey().toLowerCase());
        }
        return 0;
    }
}