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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "personenKonfliktConversationSelect";

  @Inject
  protected transient PersonenKonfliktExtendedDAO entityDAO;

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

  public PersonenKonfliktConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.personenpruefung.entity.PersonenKonfliktPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected PersonenKonflikt attach( de.ivu.elect.business.personenpruefung.entity.PersonenKonfliktPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
