
package de.ivu.elect.presentation.wahl;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.wahl.boundary.WahlBoundaryCreate;
import de.ivu.elect.business.wahl.boundary.WahlFactory;
import de.ivu.elect.business.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.WahlMeta;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.business.security.UseCaseSecure;
import de.ivu.elect.presentation.View;
import de.ivu.elect.jee.presentation.redirect.Redirect;

@Named(AbstractWahlCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractWahlCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.WAHL_CREATE)
public abstract class AbstractWahlCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Wahl> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wahlCreateView";

  public static final String VIEW_ID = "/wahl/wahl-create.xhtml";

  public static final String PARENT_VIEW_ID = "/wahl/wahl-search.xhtml";

  @Inject
  protected transient WahlBoundaryCreate create;

  protected Wahl entity;

  @Inject
  protected transient WahlMeta entityMeta;

  
  @Inject
  protected transient WahlFactory factory;

  public AbstractWahlCreateNoScopeView() {
    super();
  }

  @PostConstruct
  public void init() {
    setEntity(factory.create());
  }

  

  public Wahl getEntity() {
    return entity;
  }

  protected void setEntity(Wahl entity) {
    this.entity = entity;
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.WAHL_CREATE)
  public String save() {
    callSave(getEntity());

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

  protected void callSave(Wahl entity) {
    create.save(entity);
  }

  protected void addMessageSaveSuccess(Wahl entity) {
    addInfoMessage("entity.create.action.save.success.message", entityMeta.getEinzahlMessage());
  }

  public boolean isBtnSaveRendered() {
    return hasPermission(UseCasePermission.WAHL_CREATE);
  }

  @Redirect
  public String reset() {
    resetStateAfterAction();

    init();

    return getViewId();
  }

  public boolean isBtnResetRendered() {
    return true;
  }

  @Redirect
  public String cancel() {
    resetStateAfterAction();

    return goBack(getParentViewId());
  }

  public boolean isBtnCancelRendered() {
    return true;
  }

  protected String getViewId() {
    return VIEW_ID;
  }

  protected String getParentViewId() {
    return PARENT_VIEW_ID;
  }

  protected void resetStateAfterAction() {
    // Do nothing.
  }
}
