
package de.ivu.elect.presentation.system;

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

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

import de.ivu.elect.business.system.boundary.SystemLogConversationStore;
import de.ivu.elect.business.system.boundary.SystemLogExtendedDAO;
import de.ivu.elect.business.system.boundary.SystemLogEdit;
import de.ivu.elect.business.system.entity.SystemLog;
import de.ivu.elect.business.system.entity.SystemLogMeta;
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(AbstractSystemLogEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractSystemLogEditNoScopeView.VIEW_ID, useCase = UseCasePermission.SYSTEM_LOG_EDIT)
public abstract class AbstractSystemLogEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<SystemLog>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "systemLogEditView";

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

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

  @Inject
  protected transient SystemLogExtendedDAO extendedDAO;

  @Inject
  protected transient SystemLogEdit edit;

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

  @Inject
  protected transient SystemLogMeta entityMeta;

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

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

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

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

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

    addMessageDeleteSuccess();

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



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

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.SYSTEM_LOG_EDIT);
  }
  
  @Redirect
  public String edit(SystemLog 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 SystemLogEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<SystemLog,   de.ivu.elect.business.system.entity.SystemLogPK  > {

    private static final long serialVersionUID = 1L;
  }
}
