IntegratedStaticDataEntityBase.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tradecloud.domain.common;


import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tradecloud.common.base.HibernateUtils;
import com.tradecloud.common.base.StaticDataEntityBase;
import com.tradecloud.common.externalreference.ExternalReference;
import com.tradecloud.common.externalreference.IntegratedSystem;

import javax.persistence.*;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlTransient;
import java.util.ArrayList;
import java.util.List;

/**
 * Wrapper class implemented purely because of a nasty Hibernate bug in
 * Hibernate 3.
 * <p>
 * https://hibernate.atlassian.net/browse/HHH-2763
 * <p>
 * I couldn't find any other way around it.
 * <p>
 * Blogged about it here:
 * https://connect.devstream.net/pages/viewpage.action?pageId=11174611
 * <p>
 * Apologies if you're the person that has to fix this.
 * <p>
 * See IntegratedPersistenceBase.java for how I WANTED to implemente it.
 */
@MappedSuperclass
@XmlTransient
@XmlAccessorType(XmlAccessType.FIELD)
public class IntegratedStaticDataEntityBase extends StaticDataEntityBase {

    private static final long serialVersionUID = 5469119007870172275L;

    @OneToOne(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
    @JsonIgnore
    private ExternalReferenceWrapper externalReferenceWrapper;

    public IntegratedStaticDataEntityBase() {
    }

    public IntegratedStaticDataEntityBase(String code) {
        super(code);
    }

    public IntegratedStaticDataEntityBase(String code, String name) {
        super(code, name);
    }

    public ExternalReferenceWrapper getExternalReferenceWrapper() {
        return externalReferenceWrapper;
    }

    public void setExternalReferenceWrapper(ExternalReferenceWrapper externalReferenceWrapper) {
        this.externalReferenceWrapper = externalReferenceWrapper;
    }

    @JsonIgnore
    public List<ExternalReference> getExternalReferenceList() {
        if (externalReferenceWrapper == null) {
            externalReferenceWrapper = new ExternalReferenceWrapper();
        }
        return new ArrayList<ExternalReference>(getExternalReferenceWrapper().getExternalReferences());
    }

    @Transient
    @JsonIgnore
    public String getExternalReferenceValue(IntegratedSystem integratedSystem) {
        if (externalReferenceWrapper != null) {
            HibernateUtils.initializeAndUnproxy(externalReferenceWrapper.getExternalReferences());
            for (ExternalReference externalReference : externalReferenceWrapper.getExternalReferences()) {
                if (externalReference.getIntegratedSystem().equals(integratedSystem)) {
                    return externalReference.getReferenceValue();
                }
            }
        }
        return null;
    }

    public void addExternalReference(ExternalReference externalReference) {
        if (externalReferenceWrapper == null) {
            externalReferenceWrapper = new ExternalReferenceWrapper();
        }
        externalReferenceWrapper.getExternalReferences().add(externalReference);
    }

}