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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "personendatenConversationSelect";

  @Inject
  protected transient PersonendatenExtendedDAO entityDAO;

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

  public PersonendatenConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.person.entity.PersonendatenPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected Personendaten attach( de.ivu.elect.business.person.entity.PersonendatenPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
