/**
   * Form to select the competencies of the actor.
   *
   * @param id the actor id
   */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result editCompetencies(Long id) {

    // get the actor
    Actor actor = ActorDao.getActorById(id);

    // construct the form
    Form<ActorCompetenciesFormData> competenciesForm =
        competenciesFormTemplate.fill(new ActorCompetenciesFormData(actor));

    return ok(
        views.html.core.actor.competencies_edit.render(
            actor, ActorDao.getCompetencyActiveAsVH(), competenciesForm));
  }
  /** Process the form to select the competencies of the actor. */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result processEditCompetencies() {

    // bind the form
    Form<ActorCompetenciesFormData> boundForm = competenciesFormTemplate.bindFromRequest();

    // get the actor
    Long id = Long.valueOf(boundForm.data().get("id"));
    Actor actor = ActorDao.getActorById(id);

    if (boundForm.hasErrors()) {
      return ok(
          views.html.core.actor.competencies_edit.render(
              actor, ActorDao.getCompetencyActiveAsVH(), boundForm));
    }

    ActorCompetenciesFormData competenciesFormData = boundForm.get();

    competenciesFormData.fill(actor);
    // actor.saveManyToManyAssociations("competencies");

    if (actor.competencies.size() == 0) {
      actor.defaultCompetency = null;
      actor.save();
    } else if (actor.competencies.size() == 1) {
      actor.defaultCompetency = actor.competencies.get(0);
      actor.save();
    } else {
      Form<ActorDefaultCompetencyFormData> defaultCompetencyForm =
          defaultCompetencyFormTemplate.fill(new ActorDefaultCompetencyFormData(actor));
      // force the default competency to an existing one
      actor.defaultCompetency = actor.competencies.get(0);
      actor.save();
      return ok(
          views.html.core.actor.default_competency_edit.render(
              actor,
              new DefaultSelectableValueHolderCollection<Long>(actor.competencies),
              defaultCompetencyForm));
    }

    Utilities.sendSuccessFlashMessage(Msg.get("core.actor.competencies.edit.successful"));

    return redirect(controllers.core.routes.ActorController.view(actor.id));
  }