
package de.ivu.elect.presentation.konfiguration;

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

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

import de.ivu.elect.business.konfiguration.boundary.KonfigurationConversationStore;
import de.ivu.elect.business.konfiguration.boundary.KonfigurationExtendedDAO;
import de.ivu.elect.business.konfiguration.boundary.KonfigurationEdit;
import de.ivu.elect.business.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationMeta;
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(AbstractKonfigurationEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractKonfigurationEditNoScopeView.VIEW_ID, useCase = UseCasePermission.KONFIGURATION_EDIT)
public abstract class AbstractKonfigurationEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<Konfiguration>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "konfigurationEditView";

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

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

  @Inject
  protected transient KonfigurationExtendedDAO extendedDAO;

  @Inject
  protected transient KonfigurationEdit edit;

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

  @Inject
  protected transient KonfigurationMeta entityMeta;

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

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

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

  public void setEntity(Konfiguration 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.KONFIGURATION_DELETE)
  @Override
  public String delete() {
    return delete(getEntity());
  }

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

    addMessageDeleteSuccess();

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



  @Redirect
  @UseCaseSecure(UseCasePermission.KONFIGURATION_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.KONFIGURATION_DELETE);
  }

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.KONFIGURATION_EDIT);
  }
  
  @Redirect
  public String edit(Konfiguration 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 KonfigurationEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<Konfiguration,   de.ivu.elect.business.konfiguration.entity.KonfigurationPK  > {

    private static final long serialVersionUID = 1L;
  }
}
