
package de.ivu.elect.presentation;

import static org.apache.commons.lang3.StringUtils.isBlank;

import java.io.Serializable;

import javax.enterprise.context.RequestScoped;
import javax.enterprise.inject.Instance;
import javax.inject.Inject;

import de.ivu.elect.jee.faces.FacesContextUtil;

@RequestScoped
@Deprecated // handle with care - lädt alle Views...
public class CurrentView implements Serializable {

  private static final long serialVersionUID = 1L;

  @View
  @Inject
  private transient Instance<Object> views;

  public CurrentView() {
    super();
  }

  public Object getCurrentViewInstance() {
    String currentViewId = FacesContextUtil.getCurrentViewId();

    if (isBlank(currentViewId)) {
      return null;
    }

    for (Object view : views) {
      View viewAnnotation = view.getClass().getAnnotation(View.class);

      if (currentViewId.equals(viewAnnotation.viewId())) {
        return view;
      }
    }

    return null;
  }
}
