
package de.ivu.elect.presentation.benutzerverwaltung;

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

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

import de.ivu.elect.business.benutzerverwaltung.boundary.RolleConversationStore;
import de.ivu.elect.business.benutzerverwaltung.entity.Rolle;
import de.ivu.elect.business.benutzerverwaltung.entity.RolleProperties;
import de.ivu.elect.business.security.UseCasePermission;
import de.ivu.elect.jee.presentation.dynamicform.FormBuilder;
import de.ivu.elect.jee.presentation.redirect.Redirect;
import de.ivu.elect.presentation.hilfe.OnlineHilfeId;

@OnlineHilfeId("484")
@Named(RolleCreateView.COMPONENT_NAME)
public class RolleCreateView extends AbstractRolleCreateView {

  private static final long serialVersionUID = 1L;

  @Inject
  private transient RolleUniqueUKFacesValidator rolleUniqueUKFacesValidator;

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

  public RolleCreateView() {
    super();
  }

  @Override
  protected void setEntity(Rolle entity) {
    entity.setMandant(getMandant());
    super.setEntity(entity);
  }

  @Override
  public void addProperties(FormBuilder formBuilder, Rolle rolle) {
    RolleProperties rolleProperties = rolle.getProperties();

    formBuilder.addProperty(rolleProperties.getNameProperty()).withValidatorAndPk(rolleUniqueUKFacesValidator, rolle.getPk());
    formBuilder.addProperty(rolleProperties.getBeschreibungProperty());
  }

  @Override
  @Redirect
  public String save() {
    Rolle rolle = getEntity();
    String result = super.save();

    // Rechte zuweisen
    if (hasPermission(UseCasePermission.ROLLE_EDIT) && rolle.getPk() != null) {
      conversationStore.setEntityPK(rolle.getPk());
      return RolleEditView.VIEW_ID;
    }
    return result;
  }


}
