
package de.ivu.elect.presentation.kandidatenportal;

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

import de.ivu.elect.business.kandidatenportal.boundary.WvpBenutzerBoundaryCreate;
import de.ivu.elect.business.kandidatenportal.boundary.WvpBenutzerFactory;
import de.ivu.elect.business.kandidatenportal.entity.WvpBenutzer;
import de.ivu.elect.business.kandidatenportal.entity.WvpBenutzerMeta;
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(AbstractWvpBenutzerCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractWvpBenutzerCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.WVP_BENUTZER_CREATE)
public abstract class AbstractWvpBenutzerCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<WvpBenutzer> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wvpBenutzerCreateView";

  public static final String VIEW_ID = "/kandidatenportal/wvp-benutzer-create.xhtml";

  public static final String PARENT_VIEW_ID = "/kandidatenportal/wvp-benutzer-search.xhtml";

  @Inject
  protected transient WvpBenutzerBoundaryCreate create;

  protected WvpBenutzer entity;

  @Inject
  protected transient WvpBenutzerMeta entityMeta;

  
  @Inject
  protected transient WvpBenutzerFactory factory;

  public AbstractWvpBenutzerCreateNoScopeView() {
    super();
  }

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

  

  public WvpBenutzer getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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