/*
* Template: src/main/takatuka/templates/EntityPresentationAbstractSearchView.vm
*/
package de.ivu.elect.presentation.geodata;

import java.util.List;

import javax.annotation.PostConstruct;
import javax.enterprise.context.RequestScoped;
import javax.enterprise.event.Observes;
import javax.enterprise.event.Reception;
import javax.enterprise.inject.Instance;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ConversationGroup;
import org.apache.deltaspike.core.api.scope.ViewAccessScoped;

import de.ivu.elect.business.security.UseCasePermission;

import de.ivu.elect.business.geodata.boundary.AbstractGeodataOrtSearchParameters;
import de.ivu.elect.business.geodata.boundary.GeodataOrtChangeEvent;
import de.ivu.elect.business.geodata.boundary.GeodataOrtConversationStore;
import de.ivu.elect.business.geodata.boundary.GeodataOrtExtendedDAO;
import de.ivu.elect.business.geodata.boundary.GeodataOrtSearch;
import de.ivu.elect.business.geodata.boundary.GeodataOrtSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.geodata.entity.GeodataOrt;
import de.ivu.elect.business.geodata.entity.GeodataOrtMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractGeodataOrtSearchView.COMPONENT_NAME)
@View(viewId = AbstractGeodataOrtSearchView.VIEW_ID, useCase = UseCasePermission.GEODATA_ORT_SEARCH)
public abstract class AbstractGeodataOrtSearchView extends AbstractElectSearchView<GeodataOrt> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "geodataOrtSearchView";
  
  public static final String VIEW_ID = "/geodata/geodata-ort-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractGeodataOrtSearchView requestHandler;

  @Inject
  private transient GeodataOrtExtendedDAO extendedDAO;

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

  @Inject
  protected transient GeodataOrtSearch search;

  @Inject
  @ConversationGroup(AbstractGeodataOrtSearchParameters.class)
  protected transient GeodataOrtSearchParameters searchParameters;

  @Inject
  protected transient GeodataOrtMeta entityMeta;

  public AbstractGeodataOrtSearchView() {
    super();
  }

  @Override
  protected String getViewId() {
    return VIEW_ID;
  }

  public boolean isEntitySelected() {
    return conversationStore.getEntityPK() != null;
  }

  public GeodataOrt getSelectedEntity() {
    if (isEntitySelected()) {
      return extendedDAO.findByPrimaryKey(conversationStore.getEntityPK());
    }

    return null;
  }

  public void setSelectedEntity(GeodataOrt entity) {
    if (entity == null) {
      conversationStore.setEntityPK(null);
    } else {
      conversationStore.setEntityPK(entity.getPk());
    }
  }

  @Override
  public GeodataOrtSearchParameters getSearchParameters() {
    return searchParameters;
  }

  public EagerLoadingCollector<GeodataOrt> getEagerLoadingCollector() {
    return EagerLoadingCollector.<GeodataOrt> create();
  }

  @Override
  public List<GeodataOrt> getSearchResult() {
    requestHandler.nothing(); // ELECT-248

    return super.getSearchResult();
  }

  @Override
  protected List<GeodataOrt> performSearch() {
    return search.getSearchResult(getSearchParameters(), getEagerLoadingCollector());
  }

  public void onGeodataOrtChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) GeodataOrtChangeEvent event) {
    resetSearchResult();
  }

  // ELECT-2231    ELECT-2250
  /*
   * searchParameters sind von der zur Bearbeitung ausgewählten Wahl abhängig.
   */
  public void onWahlSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlSelectChangeEvent event) {
    reset();
  }

  /*
   * searchParameters sind vom zur Bearbeitung ausgewählten Wahltermin abhängig.
   */
  public void onWahlterminSelectChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WahlterminSelectChangeEvent event) {
    reset();
  }

  @RequestScoped
  // elect: Das Suchergebnis soll nur einmal pro Request geladen werden, daher gibt es dieses "Konstrukt" aus "SearchView" und "RequestHandlerAbstract"
  public static class RequestHandlerAbstractGeodataOrtSearchView {

    @View
    @Inject
    private transient Instance<AbstractGeodataOrtSearchView> searchViews;

    @PostConstruct
    public void init() {
      // ELECT-3543 Doppelte Suchen vermeiden - Suchergebnis für Request nicht beim POST-löschen
      if (!FacesContext.getCurrentInstance().isPostback()) {
        for (AbstractGeodataOrtSearchView searchView : searchViews) {
          searchView.resetSearchResult();
        }
      }
    }

    public void nothing() {
      // Do nothing.
    }
  }
}
