
package de.ivu.elect.presentation.geodata;

import javax.annotation.PostConstruct;
import javax.inject.Inject;
import javax.inject.Named;

import de.ivu.elect.business.geodata.boundary.GeodataOrtBoundaryCreate;
import de.ivu.elect.business.geodata.entity.GeodataOrt;
import de.ivu.elect.business.geodata.entity.GeodataOrtMeta;
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(AbstractGeodataOrtCreateNoScopeView.COMPONENT_NAME)
@View(viewId = AbstractGeodataOrtCreateNoScopeView.VIEW_ID, useCase = UseCasePermission.GEODATA_ORT_CREATE)
public abstract class AbstractGeodataOrtCreateNoScopeView extends de.ivu.elect.presentation.AbstractElectCreateView<GeodataOrt> {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "geodataOrtCreateView";

  public static final String VIEW_ID = "/geodata/geodata-ort-create.xhtml";

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

  @Inject
  protected transient GeodataOrtBoundaryCreate create;

  protected GeodataOrt entity;

  @Inject
  protected transient GeodataOrtMeta entityMeta;

  
  @PostConstruct
  public void init() {
    setEntity(new  GeodataOrt());
  }

  

  public GeodataOrt getEntity() {
    return entity;
  }

  protected void setEntity(GeodataOrt entity) {
    this.entity = entity;
  }

  @Redirect
  @UseCaseSecure(UseCasePermission.GEODATA_ORT_CREATE)
  public String save() {
    callSave(getEntity());

    addMessageSaveSuccess(getEntity());

    resetStateAfterAction();

    return goBack(getParentViewId());
  }

  protected void callSave(GeodataOrt entity) {
    create.save(entity);
  }

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

  public boolean isBtnSaveRendered() {
    return hasPermission(UseCasePermission.GEODATA_ORT_CREATE);
  }

  @Redirect
  public String reset() {
    resetStateAfterAction();

    init();

    return getViewId();
  }

  public boolean isBtnResetRendered() {
    return true;
  }

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

    return goBack(getParentViewId());
  }

  public boolean isBtnCancelRendered() {
    return true;
  }

  protected String getViewId() {
    return VIEW_ID;
  }

  protected String getParentViewId() {
    return PARENT_VIEW_ID;
  }

  protected void resetStateAfterAction() {
    // Do nothing.
  }
}
