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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "listeConversationSelect";

  @Inject
  protected transient ListeExtendedDAO entityDAO;

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

  public ListeConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.wahlvorschlag.entity.WahlvorschlagPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected Liste attach( de.ivu.elect.business.wahlvorschlag.entity.WahlvorschlagPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
