/**
   * Delete an actor.
   *
   * @param id the actor id
   */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_DELETE_DYNAMIC_PERMISSION)
  public Result delete(Long id) {

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

    // delete the actor
    actor.doDelete();

    // success message
    Utilities.sendSuccessFlashMessage(Msg.get("core.actor.delete.successful"));

    return redirect(controllers.core.routes.SearchController.index());
  }
  /** Process the form to edit the default competency of an actor. */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result processEditDefaultCompetency() {

    // bind the form
    Form<ActorDefaultCompetencyFormData> boundForm =
        defaultCompetencyFormTemplate.bindFromRequest();

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

    ActorDefaultCompetencyFormData defaultCompetencyFormData = boundForm.get();

    defaultCompetencyFormData.fill(actor);
    actor.save();

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

    return redirect(controllers.core.routes.ActorController.view(actor.id));
  }
  /** 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));
  }
  /** Process the save of an actor (create and edit cases). */
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result save() {

    // bind the form
    Form<ActorFormData> boundForm = formTemplate.bindFromRequest();

    if (boundForm.hasErrors()
        || this.getCustomAttributeManagerService().validateValues(boundForm, Actor.class)) {

      if (boundForm.data().get("id") != null) { // edit case
        // get the actor
        Long id = Long.valueOf(boundForm.data().get("id"));
        Actor actor = ActorDao.getActorById(id);
        return ok(
            views.html.core.actor.actor_edit.render(
                actor, boundForm, ActorDao.getActorTypeActiveAsVH()));
      } else { // new case
        return ok(
            views.html.core.actor.actor_new.render(boundForm, ActorDao.getActorTypeActiveAsVH()));
      }
    }

    ActorFormData actorFormData = boundForm.get();

    // check the uid is not already used by another actor
    if (actorFormData.uid != null && !actorFormData.uid.equals("")) {
      Actor testActor = ActorDao.getActorByUid(actorFormData.uid);
      if (testActor != null) { // edit case

        if (actorFormData.id != null) {
          if (!testActor.id.equals(actorFormData.id)) {
            boundForm.reject("uid", Msg.get("object.actor.uid.invalid"));
            Actor actor = ActorDao.getActorById(actorFormData.id);
            return ok(
                views.html.core.actor.actor_edit.render(
                    actor, boundForm, ActorDao.getActorTypeActiveAsVH()));
          }
        } else { // new case
          boundForm.reject("uid", Msg.get("object.actor.uid.invalid"));
          return ok(
              views.html.core.actor.actor_new.render(boundForm, ActorDao.getActorTypeActiveAsVH()));
        }
      }
    }

    Actor actor = null;

    if (actorFormData.id != null) { // edit case

      actor = ActorDao.getActorById(actorFormData.id);
      actorFormData.fill(actor);
      actor.update();

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

    } else { // new case

      actor = new Actor();
      actorFormData.fill(actor);
      actor.save();

      Utilities.sendSuccessFlashMessage(Msg.get("core.actor.new.successful"));
    }

    // save the custom attributes
    this.getCustomAttributeManagerService().validateAndSaveValues(boundForm, Actor.class, actor.id);

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