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

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.wertelisten.boundary.AbstractWertelistenSearchParameters;
import de.ivu.elect.business.wertelisten.boundary.WertelistenChangeEvent;
import de.ivu.elect.business.wertelisten.boundary.WertelistenConversationStore;
import de.ivu.elect.business.wertelisten.boundary.WertelistenExtendedDAO;
import de.ivu.elect.business.wertelisten.boundary.WertelistenSearch;
import de.ivu.elect.business.wertelisten.boundary.WertelistenSearchParameters;
import de.ivu.elect.business.wahl.boundary.WahlSelectChangeEvent;
import de.ivu.elect.business.wahl.boundary.WahlterminSelectChangeEvent;
import de.ivu.elect.business.wertelisten.entity.Wertelisten;
import de.ivu.elect.business.wertelisten.entity.WertelistenMeta;
import de.ivu.elect.jee.entity.EagerLoadingCollector;
import de.ivu.elect.presentation.AbstractElectSearchView;
import de.ivu.elect.presentation.View;

@ViewAccessScoped
@Named(AbstractWertelistenSearchView.COMPONENT_NAME)
@View(viewId = AbstractWertelistenSearchView.VIEW_ID, useCase = UseCasePermission.WERTELISTEN_SEARCH)
public abstract class AbstractWertelistenSearchView extends AbstractElectSearchView<Wertelisten> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wertelistenSearchView";
  
  public static final String VIEW_ID = "/wertelisten/wertelisten-search.xhtml";

  @Inject
  private transient RequestHandlerAbstractWertelistenSearchView requestHandler;

  @Inject
  private transient WertelistenExtendedDAO extendedDAO;

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

  @Inject
  protected transient WertelistenSearch search;

  @Inject
  @ConversationGroup(AbstractWertelistenSearchParameters.class)
  protected transient WertelistenSearchParameters searchParameters;

  @Inject
  protected transient WertelistenMeta entityMeta;

  public AbstractWertelistenSearchView() {
    super();
  }

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

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

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

    return null;
  }

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

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

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

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

    return super.getSearchResult();
  }

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

  public void onWertelistenChangeEvent(@Observes(notifyObserver = Reception.IF_EXISTS) WertelistenChangeEvent 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
  //  Das Suchergebnis soll nur einmal pro Request geladen werden, daher gibt es dieses "Konstrukt" aus "SearchView" und "RequestHandlerAbstract"
  public static class RequestHandlerAbstractWertelistenSearchView {

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

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

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