
package de.ivu.elect.presentation.wertelisten;

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

import de.ivu.elect.business.wertelisten.boundary.WertelistenBoundaryCreate;
import de.ivu.elect.business.wertelisten.entity.Wertelisten;
import de.ivu.elect.business.wertelisten.entity.WertelistenMeta;
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(AbstractWertelistenCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractWertelistenCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.WERTELISTEN_CREATE)
public abstract class AbstractWertelistenCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Wertelisten> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wertelistenCreateView";

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

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

  @Inject
  protected transient WertelistenBoundaryCreate create;

  protected Wertelisten entity;

  @Inject
  protected transient WertelistenMeta entityMeta;

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

  

  public Wertelisten getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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