
package de.ivu.elect.presentation.email;

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

import de.ivu.elect.business.email.boundary.EmailVorgangBoundaryCreate;
import de.ivu.elect.business.email.entity.EmailVorgang;
import de.ivu.elect.business.email.entity.EmailVorgangMeta;
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(AbstractEmailVorgangCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractEmailVorgangCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.EMAIL_VORGANG_CREATE)
public abstract class AbstractEmailVorgangCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<EmailVorgang> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailVorgangCreateView";

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

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

  @Inject
  protected transient EmailVorgangBoundaryCreate create;

  protected EmailVorgang entity;

  @Inject
  protected transient EmailVorgangMeta entityMeta;

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

  

  public EmailVorgang getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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