
package de.ivu.elect.presentation.internet;

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

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

import de.ivu.elect.business.internet.boundary.InternetWidgetConversationStore;
import de.ivu.elect.business.internet.boundary.InternetWidgetExtendedDAO;
import de.ivu.elect.business.internet.boundary.InternetWidgetEdit;
import de.ivu.elect.business.internet.entity.InternetWidget;
import de.ivu.elect.business.internet.entity.InternetWidgetMeta;
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(AbstractInternetWidgetEditNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractInternetWidgetEditNoScopeView.VIEW_ID, useCase = UseCasePermission.INTERNET_WIDGET_EDIT)
public abstract class AbstractInternetWidgetEditNoScopeView extends de.ivu.elect.presentation.AbstractElectEditView<InternetWidget>  {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "internetWidgetEditView";

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

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

  @Inject
  protected transient InternetWidgetExtendedDAO extendedDAO;

  @Inject
  protected transient InternetWidgetEdit edit;

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

  @Inject
  protected transient InternetWidgetMeta entityMeta;

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

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

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

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

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

    addMessageDeleteSuccess();

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



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

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

    return goBack(getParentViewId());
  }

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

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

    callSave(entity);

    addMessageSaveSuccess(entity);

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

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

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

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

  @Override
  protected boolean isSavePermissionVorhanden() {
    return hasPermission(UseCasePermission.INTERNET_WIDGET_EDIT);
  }
  
  @Redirect
  public String edit(InternetWidget 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 InternetWidgetEditViewScopeStore extends  de.ivu.elect.presentation.edit.AbstractViewScopeStore<InternetWidget,   de.ivu.elect.business.internet.entity.InternetWidgetPK  > {

    private static final long serialVersionUID = 1L;
  }
}
