
package de.ivu.elect.jee.faces.security;

import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.event.PhaseListener;
import javax.inject.Inject;

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

public class ConditionViewPhaseListener implements PhaseListener {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient ConditionViewValidatorMarker conditionViewValidator;

  @Override
  public void afterPhase(PhaseEvent event) {

    if (event == null || event.getFacesContext() == null || event.getFacesContext().getViewRoot() == null
        || event.getFacesContext().getViewRoot().getViewId() == null) {
      return;
    }

    try {
      conditionViewValidator.checkView(event.getFacesContext().getViewRoot().getViewId());
    } catch (IllegalStateException e) {
      FacesMessageUtil.addErrorMessage(e.getMessage());
    }
  }



  @Override
  public void beforePhase(PhaseEvent event) {
    // nichts zu tun
  }

  @Override
  public PhaseId getPhaseId() {
    return PhaseId.RESTORE_VIEW;
  }

}
