
package de.ivu.elect.presentation.mandant;

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

import de.ivu.elect.business.mandant.boundary.MandantBoundaryCreate;
import de.ivu.elect.business.mandant.entity.Mandant;
import de.ivu.elect.business.mandant.entity.MandantMeta;
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(AbstractMandantCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractMandantCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.MANDANT_CREATE)
public abstract class AbstractMandantCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Mandant> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "mandantCreateView";

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

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

  @Inject
  protected transient MandantBoundaryCreate create;

  protected Mandant entity;

  @Inject
  protected transient MandantMeta entityMeta;

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

  

  public Mandant getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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