
package de.ivu.elect.presentation.fremdkandidatur;

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

import de.ivu.elect.business.fremdkandidatur.boundary.FremdkandidaturBoundaryCreate;
import de.ivu.elect.business.fremdkandidatur.boundary.FremdkandidaturFactory;
import de.ivu.elect.business.fremdkandidatur.entity.Fremdkandidatur;
import de.ivu.elect.business.fremdkandidatur.entity.FremdkandidaturMeta;
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(AbstractFremdkandidaturCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractFremdkandidaturCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.FREMDKANDIDATUR_CREATE)
public abstract class AbstractFremdkandidaturCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Fremdkandidatur> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "fremdkandidaturCreateView";

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

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

  @Inject
  protected transient FremdkandidaturBoundaryCreate create;

  protected Fremdkandidatur entity;

  @Inject
  protected transient FremdkandidaturMeta entityMeta;

  
  @Inject
  protected transient FremdkandidaturFactory factory;

  public AbstractFremdkandidaturCreateNoScopeView() {
    super();
  }

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

  

  public Fremdkandidatur getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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