
package de.ivu.elect.jee.presentation.status;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import de.ivu.elect.jee.presentation.view.AbstractView;

public abstract class AbstractStatusView<E extends StatusEntry> extends AbstractView {

  private static final long serialVersionUID = 1L;

  private boolean searchReady;

  private transient WeakReference<List<E>> statusEntries;

  public AbstractStatusView() {
    super();

    this.searchReady = false;
    this.statusEntries = new WeakReference<>(null);
  }

  /**
   * Implementierung mit @Redirect und @UseCaseSecure annotieren.
   */
  public abstract String redirectTo(E statusEntry);

  public List<E> getStatusEntries() {
    if (isSearchReady()) {
      List<E> result = statusEntries.get();
      if (result == null) {
        result = bestimmeStatusEntries();
        statusEntries = new WeakReference<>(result);
      }
      return result;
    }
    return new ArrayList<>();
  }

  protected abstract List<E> bestimmeStatusEntries();

  public void reset() {
    statusEntries = new WeakReference<>(null);
    setSearchNotReady();
  }

  public boolean isRendered() {
    return isSearchReady();
  }

  private void setSearchNotReady() {
    searchReady = false;
  }

  public boolean isSupportsLazyLoading() {
    return false;
  }

  public void setSearchReady() {
    searchReady = true;
  }

  public boolean isSearchReady() {
    return !isSupportsLazyLoading() || searchReady; // immer wahr, wenn lazy-loading nicht unterstützt wird...
  }

  public boolean isAutoRun() {
    return isSupportsLazyLoading() && !isSearchReady();
  }
}
