
package de.ivu.elect.business.mandant.boundary;

import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ConversationGroup;

import de.ivu.elect.business.mandant.entity.Mandant;

@RequestScoped
@Named(MandantConversationSelect.COMPONENT_NAME)
public class MandantConversationSelect implements java.io.Serializable {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "mandantConversationSelect";

  @Inject
  protected transient MandantExtendedDAO entityDAO;

  @Inject
  @ConversationGroup(MandantConversationStore.class)
  protected transient MandantConversationStore conversationStore;

  public MandantConversationSelect() {
    super();
  }

  public boolean isSelected() {
    return getEntityPK() != null;
  }

  public Mandant getEntity() {
    return getEntityInternal();
  }

  private Mandant getEntityInternal() {
    if (getEntityPK() == null) {
      return null;
    }

    return attach(getEntityPK());
  }

  public void setEntity(Mandant entity) {
    setEntityAttached(attach(entity));
  }

  public void setEntity( de.ivu.elect.business.mandant.entity.MandantPK  pk) {
    setEntityAttached(attach(pk));
  }

  protected void setEntityAttached(Mandant entity) {
    if(entity == null) {
      setEntityPK(null);
    } else {
      setEntityPK(entity.getPk());
    }
  }

  public
    de.ivu.elect.business.mandant.entity.MandantPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

  public void setEntityPK(
    de.ivu.elect.business.mandant.entity.MandantPK
   entityPK) {
    conversationStore.setEntityPK(entityPK);
  }

  protected Mandant attach(Mandant entity) {
    if (entity == null){
      return null;
    }

    return attach(entity.getPk());
  }

  protected Mandant attach( de.ivu.elect.business.mandant.entity.MandantPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
