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

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

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "geodataOrtConversationSelect";

  @Inject
  protected transient GeodataOrtExtendedDAO entityDAO;

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

  public GeodataOrtConversationSelect() {
    super();
  }

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

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

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

    return attach(getEntityPK());
  }

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

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

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

  public
    de.ivu.elect.business.geodata.entity.GeodataOrtPK
   getEntityPK() {
    return conversationStore.getEntityPK();
  }

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

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

    return attach(entity.getPk());
  }

  protected GeodataOrt attach( de.ivu.elect.business.geodata.entity.GeodataOrtPK  pk) {
    if (pk == null) {
      return null;
    }

    return entityDAO.findByPrimaryKey(pk);
  }

  public void clearEntity() {
    setEntityPK(null);

    conversationStore.close();
  }
}
