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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wahlterminConversationSelect";

  @Inject
  protected transient WahlterminExtendedDAO entityDAO;

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

  public WahlterminConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.wahl.entity.WahlterminPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected Wahltermin attach( de.ivu.elect.business.wahl.entity.WahlterminPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
