
package de.ivu.elect.presentation.wahl;

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

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

import de.ivu.elect.business.wahl.boundary.WahlParameterConversationStore;
import de.ivu.elect.business.wahl.boundary.WahlParameterExtendedDAO;
import de.ivu.elect.business.wahl.boundary.WahlParameterEdit;
import de.ivu.elect.business.wahl.entity.WahlParameter;
import de.ivu.elect.business.wahl.entity.WahlParameterMeta;
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(AbstractWahlParameterEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractWahlParameterEditNoScopeView.VIEW_ID, useCase = UseCasePermission.WAHL_PARAMETER_EDIT)
public abstract class AbstractWahlParameterEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<WahlParameter>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wahlParameterEditView";

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

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

  @Inject
  protected transient WahlParameterExtendedDAO extendedDAO;

  @Inject
  protected transient WahlParameterEdit edit;

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

  @Inject
  protected transient WahlParameterMeta entityMeta;

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

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

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

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

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

    addMessageDeleteSuccess();

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



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

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.WAHL_PARAMETER_EDIT);
  }
  
  @Redirect
  public String edit(WahlParameter 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 WahlParameterEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<WahlParameter,   de.ivu.elect.business.wahl.entity.WahlParameterPK  > {

    private static final long serialVersionUID = 1L;
  }
}
