
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.Gebietsart;

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "gebietsartConversationSelect";

  @Inject
  protected transient GebietsartExtendedDAO entityDAO;

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

  public GebietsartConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

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

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

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

    return attach(entity.getPk());
  }

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

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
