
package de.ivu.elect.presentation.wahlvorschlag;

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

import de.ivu.elect.business.wahlvorschlag.boundary.ReferendumOptionBoundaryCreate;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOption;
import de.ivu.elect.business.wahlvorschlag.entity.ReferendumOptionMeta;
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(AbstractReferendumOptionCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractReferendumOptionCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.REFERENDUM_OPTION_CREATE)
public abstract class AbstractReferendumOptionCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<ReferendumOption> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "referendumOptionCreateView";

  public static final String VIEW_ID = "/wahlvorschlag/referendum-option-create.xhtml";

  public static final String PARENT_VIEW_ID = "/wahlvorschlag/referendum-option-search.xhtml";

  @Inject
  protected transient ReferendumOptionBoundaryCreate create;

  protected ReferendumOption entity;

  @Inject
  protected transient ReferendumOptionMeta entityMeta;

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

  

  public ReferendumOption getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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