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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "dateiImportConversationSelect";

  @Inject
  protected transient DateiImportExtendedDAO entityDAO;

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

  public DateiImportConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.datei.entity.DateiImportPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected DateiImport attach( de.ivu.elect.business.datei.entity.DateiImportPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
