
package de.ivu.elect.presentation.fremdkandidatur;

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

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

import de.ivu.elect.business.fremdkandidatur.boundary.FremdkandidaturConversationStore;
import de.ivu.elect.business.fremdkandidatur.boundary.FremdkandidaturExtendedDAO;
import de.ivu.elect.business.fremdkandidatur.boundary.FremdkandidaturEdit;
import de.ivu.elect.business.fremdkandidatur.entity.Fremdkandidatur;
import de.ivu.elect.business.fremdkandidatur.entity.FremdkandidaturMeta;
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(AbstractFremdkandidaturEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractFremdkandidaturEditNoScopeView.VIEW_ID, useCase = UseCasePermission.FREMDKANDIDATUR_EDIT)
public abstract class AbstractFremdkandidaturEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<Fremdkandidatur>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "fremdkandidaturEditView";

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

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

  @Inject
  protected transient FremdkandidaturExtendedDAO extendedDAO;

  @Inject
  protected transient FremdkandidaturEdit edit;

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

  @Inject
  protected transient FremdkandidaturMeta entityMeta;

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

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

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

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

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

    addMessageDeleteSuccess();

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



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

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.FREMDKANDIDATUR_EDIT);
  }
  
  @Redirect
  public String edit(Fremdkandidatur 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 FremdkandidaturEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<Fremdkandidatur,  de.ivu.elect.business.person.entity.PersonendatenPK  > {

    private static final long serialVersionUID = 1L;
  }
}
