
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.WahlartConversationStore;
import de.ivu.elect.business.wahl.boundary.WahlartExtendedDAO;
import de.ivu.elect.business.wahl.boundary.WahlartEdit;
import de.ivu.elect.business.wahl.entity.Wahlart;
import de.ivu.elect.business.wahl.entity.WahlartMeta;
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(AbstractWahlartEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractWahlartEditNoScopeView.VIEW_ID, useCase = UseCasePermission.WAHLART_EDIT)
public abstract class AbstractWahlartEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<Wahlart>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "wahlartEditView";

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

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

  @Inject
  protected transient WahlartExtendedDAO extendedDAO;

  @Inject
  protected transient WahlartEdit edit;

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

  @Inject
  protected transient WahlartMeta entityMeta;

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

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

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

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

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

    addMessageDeleteSuccess();

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



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

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

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

    private static final long serialVersionUID = 1L;
  }
}
