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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "staatConversationSelect";

  @Inject
  protected transient StaatExtendedDAO entityDAO;

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

  public StaatConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.staat.entity.StaatPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected Staat attach( de.ivu.elect.business.staat.entity.StaatPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
