
package de.ivu.elect.presentation.personenpruefung;

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

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

import de.ivu.elect.business.personenpruefung.boundary.PersonPruefungKonfigurationConversationStore;
import de.ivu.elect.business.personenpruefung.boundary.PersonPruefungKonfigurationExtendedDAO;
import de.ivu.elect.business.personenpruefung.boundary.PersonPruefungKonfigurationEdit;
import de.ivu.elect.business.personenpruefung.entity.PersonPruefungKonfiguration;
import de.ivu.elect.business.personenpruefung.entity.PersonPruefungKonfigurationMeta;
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(AbstractPersonPruefungKonfigurationEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractPersonPruefungKonfigurationEditNoScopeView.VIEW_ID, useCase = UseCasePermission.PERSON_PRUEFUNG_KONFIGURATION_EDIT)
public abstract class AbstractPersonPruefungKonfigurationEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<PersonPruefungKonfiguration>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "personPruefungKonfigurationEditView";

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

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

  @Inject
  protected transient PersonPruefungKonfigurationExtendedDAO extendedDAO;

  @Inject
  protected transient PersonPruefungKonfigurationEdit edit;

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

  @Inject
  protected transient PersonPruefungKonfigurationMeta entityMeta;

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

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

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

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

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

    addMessageDeleteSuccess();

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



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

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.PERSON_PRUEFUNG_KONFIGURATION_EDIT);
  }
  
  @Redirect
  public String edit(PersonPruefungKonfiguration 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 PersonPruefungKonfigurationEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<PersonPruefungKonfiguration,   de.ivu.elect.business.personenpruefung.entity.PersonPruefungKonfigurationPK  > {

    private static final long serialVersionUID = 1L;
  }
}
