
package de.ivu.elect.presentation.wertelisten;

import javax.inject.Inject;
import javax.inject.Named;

import org.apache.deltaspike.core.api.scope.ConversationGroup;

import de.ivu.elect.business.wertelisten.boundary.WertelistenConversationStore;
import de.ivu.elect.business.wertelisten.boundary.WertelistenExtendedDAO;
import de.ivu.elect.business.wertelisten.boundary.WertelistenEdit;
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(AbstractWertelistenEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractWertelistenEditNoScopeView.VIEW_ID, useCase = UseCasePermission.WERTELISTEN_EDIT)
public abstract class AbstractWertelistenEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<Wertelisten>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wertelistenEditView";

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

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

  @Inject
  protected transient WertelistenExtendedDAO extendedDAO;

  @Inject
  protected transient WertelistenEdit edit;

  @Inject
  @ConversationGroup(WertelistenConversationStore.class)
  protected transient WertelistenConversationStore conversationStore;

  @Inject
  protected transient WertelistenMeta entityMeta;

  @Inject
  // ELECTDD-204 Auf doppelte Bearbeitung von Entitäten Hinweisen
  protected transient WertelistenEditViewScopeStore entityViewStore;

  @de.ivu.elect.jee.faces.PreConditionCheck
  public String checkPreCondition() {
    if (getEntity() == null) {
      return getParentViewId();
    }
    return null;
  }

  @Override
  public Wertelisten getEntity() {
    if (conversationStore.getEntityPK() != null) {
      return extendedDAO.findByPrimaryKey(conversationStore.getEntityPK());
    }
    return null;
  }

  public void setEntity(Wertelisten entity) {
    resetModel(); // DynaForm-Model zurücksetzen, damit die aktuelle Entität verwendte wird...
    if (entity == null) {
      conversationStore.setEntityPK(null);
    } else {
      conversationStore.setEntityPK(entity.getPk());
    }
  }

  public boolean isEntitySet() {
    return conversationStore.getEntityPK() != null;
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.WERTELISTEN_DELETE)
  @Override
  public String delete() {
    return delete(getEntity());
  }

  protected String delete(Wertelisten entity) {
    edit.delete(entity);

    addMessageDeleteSuccess();

    resetStateAfterAction();
    return getParentViewId(); // definierter Parent, benutze deleteAndGoBack um zum vorherigen View zurückzukehren
  }



  @Redirect
  @UseCaseSecure(UseCasePermission.WERTELISTEN_DELETE)
  public String deleteAndGoBack() {
    delete();
    return goBack(getParentViewId());
  }

  protected void addMessageDeleteSuccess() {
    addInfoMessage("entity.edit.action.delete.success.message", entityMeta.getEinzahlMessage());
  }

  public boolean isBtnDeleteRendered() {
    return hasPermission(UseCasePermission.WERTELISTEN_DELETE);
  }

  @Redirect
  @Override
  public String cancel() {
    resetStateAfterAction();

    return goBack(getParentViewId());
  }

  @Override
  public boolean isBtnCancelRendered() {
    return true;
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.WERTELISTEN_EDIT)
  @Override
  public String save() {
    Wertelisten entity = getEntity();

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

  @Redirect
  @Override
  public String reset() {
    super.reset();
    Wertelisten entity = getEntity();
    resetStateAfterAction();
    setEntity(entity);
    return getViewId();
  }

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.WERTELISTEN_EDIT);
  }
  
  @Redirect
  public String edit(Wertelisten entity) {
    conversationStore.setEntityPK(entity.getPk());
    return getViewId();
  }

  protected String getViewId() {
    return VIEW_ID;
  }

  protected String getParentViewId() {
    return PARENT_VIEW_ID;
  }

  protected void resetStateAfterAction() {
    conversationStore.close();
  }

  @de.ivu.elect.jee.faces.PreConditionCheck
  public void checkDoppelteBearbeitung() {
     entityViewStore.setEntity(getEntity());
  }

  @org.apache.deltaspike.core.api.scope.ViewAccessScoped
  public static class WertelistenEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<Wertelisten,   de.ivu.elect.business.wertelisten.entity.WertelistenPK  > {

    private static final long serialVersionUID = 1L;
  }
}
