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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "gebietConversationSelect";

  @Inject
  protected transient GebietExtendedDAO entityDAO;

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

  public GebietConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.gebietsbaum.entity.GebietPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected Gebiet attach( de.ivu.elect.business.gebietsbaum.entity.GebietPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
