Employee.java

package com.tradecloud.domain.party;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tradecloud.domain.model.organisationalunit.Logo;
import com.tradecloud.domain.model.organisationalunit.OrganisationalUnit;
import com.tradecloud.domain.party.base.Person;
import com.tradecloud.domain.place.Country;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.hibernate.annotations.ForeignKey;
import org.springframework.stereotype.Component;

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

/**
 * Employee Role (Buyer, Seller, signer offer of invoices etc etc).
 * <p>
 * If this person works for the organisation that is represented by the primary
 * client, then they should go in this table
 */
@Entity
@Component(value = "employee")
@Table(name = "employee")
@Access(AccessType.FIELD)
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement(name = "Employee")
@NamedQueries({
        @NamedQuery(name = "findByIdWithRolesLoaded", query = "from Employee employee left join fetch employee.employeeRoles where employee.id=:id"),
        @NamedQuery(name = "findByIdWithRolesAndReferencesLoaded",
                query = "from Employee employee left join fetch employee.employeeRoles where employee.id=:id")})
public class Employee extends Person {

    private static final long serialVersionUID = 1L;

    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(name = "employee_employeerole", joinColumns = {@JoinColumn(name = "employee_id")}, inverseJoinColumns = {@JoinColumn(
            name = "employee_role")})
    @ForeignKey(name = "fk_role_employee", inverseName = "fk_employee_role")
    @XmlElementWrapper(name = "EmployeeRoles")
    @XmlElement(name = "EmployeeRole")
    private Set<EmployeeRole> employeeRoles = new HashSet<EmployeeRole>();

    @XmlTransient
    @ManyToMany
    @JoinTable(name = "organisationalunit_employee", joinColumns = {@JoinColumn(name = "employees_id", unique = false)},
            inverseJoinColumns = {@JoinColumn(name = "organisationalunit_id", unique = false)})
    @JsonBackReference
    private Set<OrganisationalUnit> organisationalUnit = new HashSet<>();

    @XmlAttribute
    private String passportNumber;

    private Date passportExpiryDate;

    private Date dateOfBirth;

    @Enumerated(EnumType.STRING)
    private Gender gender;

    @Enumerated(EnumType.STRING)
    private Occupation occupation;

    @Enumerated(EnumType.STRING)
    private ReasonForTravel reasonForTravel;

    @Enumerated(EnumType.STRING)
    private TravelDocumentType travelDocumentType;

    @Enumerated(EnumType.STRING)
    private TravellerType travellerType;

    @OneToOne
    private Country countryOfResidence;

    @OneToOne
    private Country nationality;

    @OneToOne
    private Country travelDocumentCountryOfIssue;

    @XmlTransient
    @JsonIgnore
    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    private Logo electronicSignature;

    /**
     * Constructor.
     *
     * @param firstName
     * @param lastName
     */
    public Employee(String firstName, String lastName) {
        super(firstName, lastName);
    }

    /**
     * Constructor.
     */
    public Employee() {
    }

    public Set<EmployeeRole> getEmployeeRoles() {
        return employeeRoles;
    }

    public void setEmployeeRoles(Set<EmployeeRole> employeeRolesToSet) {
        employeeRoles = employeeRolesToSet;
    }

    /**
     * Just needed for jsf. (doesn't like sets).
     */
    public List<EmployeeRole> getEmployeeRolesList() {
        return new ArrayList<EmployeeRole>(employeeRoles);
    }

    public void setEmployeeRolesList(List<EmployeeRole> employeeRolesList) {
        setEmployeeRoles(new HashSet<EmployeeRole>(employeeRolesList));
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (!(obj instanceof Employee)) {
            return false;
        }
        Person other = (Person) obj;
        return new EqualsBuilder().append(getTitle(), other.getTitle()).append(getFirstName(), other.getFirstName())
                .append(getLastName(), other.getLastName()).append(getEmail(), other.getEmail()).append(getFax(), other.getFax())
                .append(getIdentificationNumber(), other.getIdentificationNumber()).append(getPhone(), other.getPhone())
                .append(getMobilePhone(), other.getMobilePhone()).isEquals();
    }

    public Set<OrganisationalUnit> getOrganisationalUnit() {
        return organisationalUnit;
    }

    public void setOrganisationalUnit(Set<OrganisationalUnit> organisationalUnit) {
        this.organisationalUnit = organisationalUnit;
    }

    public List<String> getOrganisationName() {
        List<String> nameList = new ArrayList<String>();
        for (OrganisationalUnit organisationalUnit2 : organisationalUnit) {
            nameList.add(organisationalUnit2.getName());
        }
        return nameList;
    }

    public void setOrganisationName() {

    }

    public String getPassportNumber() {
        return passportNumber;
    }

    public void setPassportNumber(String passportNumber) {
        this.passportNumber = passportNumber;
    }

    public Date getPassportExpiryDate() {
        return passportExpiryDate;
    }

    public void setPassportExpiryDate(Date passportExpiryDate) {
        this.passportExpiryDate = passportExpiryDate;
    }

    public Date getDateOfBirth() {
        return dateOfBirth;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public Gender getGender() {
        return gender;
    }

    public void setGender(Gender gender) {
        this.gender = gender;
    }

    public static long getSerialVersionUID() {
        return serialVersionUID;
    }

    public Occupation getOccupation() {
        return occupation;
    }

    public void setOccupation(Occupation occupation) {
        this.occupation = occupation;
    }

    public ReasonForTravel getReasonForTravel() {
        return reasonForTravel;
    }

    public void setReasonForTravel(ReasonForTravel reasonForTravel) {
        this.reasonForTravel = reasonForTravel;
    }

    public TravelDocumentType getTravelDocumentType() {
        return travelDocumentType;
    }

    public void setTravelDocumentType(TravelDocumentType travelDocumentType) {
        this.travelDocumentType = travelDocumentType;
    }

    public TravellerType getTravellerType() {
        return travellerType;
    }

    public void setTravellerType(TravellerType travellerType) {
        this.travellerType = travellerType;
    }

    public Country getCountryOfResidence() {
        return countryOfResidence;
    }

    public void setCountryOfResidence(Country countryOfResidence) {
        this.countryOfResidence = countryOfResidence;
    }

    public Country getNationality() {
        return nationality;
    }

    public void setNationality(Country nationality) {
        this.nationality = nationality;
    }

    public Country getTravelDocumentCountryOfIssue() {
        return travelDocumentCountryOfIssue;
    }

    public void setTravelDocumentCountryOfIssue(Country travelDocumentCountryOfIssue) {
        this.travelDocumentCountryOfIssue = travelDocumentCountryOfIssue;
    }

    public Logo getElectronicSignature() {
        return electronicSignature;
    }

    public void setElectronicSignature(Logo electronicSignature) {
        this.electronicSignature = electronicSignature;
    }
}