
package de.ivu.elect.presentation.person;

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

import de.ivu.elect.business.person.boundary.BerufBoundaryCreate;
import de.ivu.elect.business.person.entity.Beruf;
import de.ivu.elect.business.person.entity.BerufMeta;
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(AbstractBerufCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractBerufCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.BERUF_CREATE)
public abstract class AbstractBerufCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<Beruf> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "berufCreateView";

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

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

  @Inject
  protected transient BerufBoundaryCreate create;

  protected Beruf entity;

  @Inject
  protected transient BerufMeta entityMeta;

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

  

  public Beruf getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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