
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.EmailLayoutBoundaryCreate;
import de.ivu.elect.business.email.entity.EmailLayout;
import de.ivu.elect.business.email.entity.EmailLayoutMeta;
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(AbstractEmailLayoutCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractEmailLayoutCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.EMAIL_LAYOUT_CREATE)
public abstract class AbstractEmailLayoutCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<EmailLayout> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "emailLayoutCreateView";

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

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

  @Inject
  protected transient EmailLayoutBoundaryCreate create;

  protected EmailLayout entity;

  @Inject
  protected transient EmailLayoutMeta entityMeta;

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

  

  public EmailLayout getEntity() {
    return entity;
  }

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

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

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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