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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailVorgangConversationSelect";

  @Inject
  protected transient EmailVorgangExtendedDAO entityDAO;

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

  public EmailVorgangConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.email.entity.EmailVorgangPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected EmailVorgang attach( de.ivu.elect.business.email.entity.EmailVorgangPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
