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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wertelistenConversationSelect";

  @Inject
  protected transient WertelistenExtendedDAO entityDAO;

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

  public WertelistenConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.wertelisten.entity.WertelistenPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected Wertelisten attach( de.ivu.elect.business.wertelisten.entity.WertelistenPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
