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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "internetWidgetConversationSelect";

  @Inject
  protected transient InternetWidgetExtendedDAO entityDAO;

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

  public InternetWidgetConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.internet.entity.InternetWidgetPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected InternetWidget attach( de.ivu.elect.business.internet.entity.InternetWidgetPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
