
package de.ivu.elect.presentation.kandidatur;

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

import de.ivu.elect.business.kandidatur.boundary.DirektkandidaturBoundaryCreate;
import de.ivu.elect.business.kandidatur.boundary.DirektkandidaturFactory;
import de.ivu.elect.business.kandidatur.entity.Direktkandidatur;
import de.ivu.elect.business.kandidatur.entity.DirektkandidaturMeta;
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(AbstractDirektkandidaturCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractDirektkandidaturCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.DIREKTKANDIDATUR_CREATE)
public abstract class AbstractDirektkandidaturCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Direktkandidatur> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "direktkandidaturCreateView";

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

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

  @Inject
  protected transient DirektkandidaturBoundaryCreate create;

  protected Direktkandidatur entity;

  @Inject
  protected transient DirektkandidaturMeta entityMeta;

  
  @Inject
  protected transient DirektkandidaturFactory factory;

  public AbstractDirektkandidaturCreateNoScopeView() {
    super();
  }

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

  

  public Direktkandidatur getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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