
package de.ivu.elect.business.dokumente.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.dokumente.entity.DokumentVorlage;

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "dokumentVorlageConversationSelect";

  @Inject
  protected transient DokumentVorlageExtendedDAO entityDAO;

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

  public DokumentVorlageConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.dokumente.entity.DokumentVorlagePK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected DokumentVorlage attach( de.ivu.elect.business.dokumente.entity.DokumentVorlagePK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
