
package de.ivu.elect.presentation.veranstaltung;

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

import de.ivu.elect.business.veranstaltung.boundary.VeranstaltungBoundaryCreate;
import de.ivu.elect.business.veranstaltung.boundary.VeranstaltungFactory;
import de.ivu.elect.business.veranstaltung.entity.Veranstaltung;
import de.ivu.elect.business.veranstaltung.entity.VeranstaltungMeta;
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(AbstractVeranstaltungCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractVeranstaltungCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.VERANSTALTUNG_CREATE)
public abstract class AbstractVeranstaltungCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Veranstaltung> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "veranstaltungCreateView";

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

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

  @Inject
  protected transient VeranstaltungBoundaryCreate create;

  protected Veranstaltung entity;

  @Inject
  protected transient VeranstaltungMeta entityMeta;

  
  @Inject
  protected transient VeranstaltungFactory factory;

  public AbstractVeranstaltungCreateNoScopeView() {
    super();
  }

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

  

  public Veranstaltung getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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