
package de.ivu.elect.presentation.stimmzettel;

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

import de.ivu.elect.business.stimmzettel.boundary.StimmzettelBoundaryCreate;
import de.ivu.elect.business.stimmzettel.boundary.StimmzettelFactory;
import de.ivu.elect.business.stimmzettel.entity.Stimmzettel;
import de.ivu.elect.business.stimmzettel.entity.StimmzettelMeta;
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(AbstractStimmzettelCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractStimmzettelCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.STIMMZETTEL_CREATE)
public abstract class AbstractStimmzettelCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Stimmzettel> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "stimmzettelCreateView";

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

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

  @Inject
  protected transient StimmzettelBoundaryCreate create;

  protected Stimmzettel entity;

  @Inject
  protected transient StimmzettelMeta entityMeta;

  
  @Inject
  protected transient StimmzettelFactory factory;

  public AbstractStimmzettelCreateNoScopeView() {
    super();
  }

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

  

  public Stimmzettel getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

  public boolean isBtnSaveRendered() {
    return hasPermission(UseCasePermission.STIMMZETTEL_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.
  }
}
