
package de.ivu.elect.presentation.gebietsbaum;

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

import de.ivu.elect.business.gebietsbaum.boundary.GebietsartBoundaryCreate;
import de.ivu.elect.business.gebietsbaum.entity.Gebietsart;
import de.ivu.elect.business.gebietsbaum.entity.GebietsartMeta;
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(AbstractGebietsartCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractGebietsartCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.GEBIETSART_CREATE)
public abstract class AbstractGebietsartCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Gebietsart> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "gebietsartCreateView";

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

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

  @Inject
  protected transient GebietsartBoundaryCreate create;

  protected Gebietsart entity;

  @Inject
  protected transient GebietsartMeta entityMeta;

  
  @PostConstruct
  public void init() {
    setEntity(new  Gebietsart());
  }

  

  public Gebietsart getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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