/** Process the form to create/edit an allocation with an activity. */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result processManageAllocatedActivity() {

    // bind the form
    Form<TimesheetActivityAllocatedActorFormData> boundForm =
        allocatedActivityFormTemplate.bindFromRequest();

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

    if (boundForm.hasErrors()
        || this.getCustomAttributeManagerService()
            .validateValues(boundForm, TimesheetActivityAllocatedActor.class)) {
      return ok(views.html.core.actor.allocated_activity_manage.render(actor, boundForm));
    }

    TimesheetActivityAllocatedActorFormData allocatedActivityFormData = boundForm.get();

    TimesheetActivityAllocatedActor allocatedActivity = null;

    if (allocatedActivityFormData.allocatedActivityId == null) { // create
      // case

      allocatedActivity = new TimesheetActivityAllocatedActor();
      allocatedActivityFormData.fill(allocatedActivity);
      allocatedActivity.save();

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

    } else { // edit case

      allocatedActivity =
          TimesheetDao.getTimesheetActivityAllocatedActorById(
              allocatedActivityFormData.allocatedActivityId);

      // security: the actor must be related to the object
      if (!allocatedActivity.actor.id.equals(id)) {
        return forbidden(views.html.error.access_forbidden.render(""));
      }

      allocatedActivityFormData.fill(allocatedActivity);
      allocatedActivity.update();

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

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

    return redirect(controllers.core.routes.ActorController.allocationDetails(id, 0, 0, false));
  }
  /** Get the activities of a type. */
  @SubjectPresent
  public Result getActivities() {

    JsonNode json = request().body().asJson();
    Long activityTypeId = json.findPath("activityTypeId").asLong();

    List<TimesheetActivity> activities =
        TimesheetDao.getTimesheetActivityAsListByActivityType(activityTypeId);
    List<OptionData> options = new ArrayList<OptionData>();

    for (TimesheetActivity activity : activities) {
      options.add(new OptionData(activity.id, activity.getName()));
    }

    ObjectMapper mapper = new ObjectMapper();

    return ok((JsonNode) mapper.valueToTree(options));
  }
  /**
   * Delete an allocated activity.
   *
   * @param id the actor id
   * @param allocatedActivityId the allocated activity id
   */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result deleteAllocatedActivity(Long id, Long allocatedActivityId) {

    // get the allocated activity
    TimesheetActivityAllocatedActor allocatedActivity =
        TimesheetDao.getTimesheetActivityAllocatedActorById(allocatedActivityId);

    // security: the actor must be related to the object
    if (!allocatedActivity.actor.id.equals(id)) {
      return forbidden(views.html.error.access_forbidden.render(""));
    }

    // set the delete flag to true
    allocatedActivity.doDelete();

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

    return redirect(controllers.core.routes.ActorController.allocationDetails(id, 0, 0, false));
  }
  /**
   * Form to create/edit an allocation with an activity.
   *
   * @param id the actor id
   * @param allocatedActivityId the allocated activity id (0 for create)
   */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_EDIT_DYNAMIC_PERMISSION)
  public Result manageAllocatedActivity(Long id, Long allocatedActivityId) {

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

    // initiate the form with the template
    Form<TimesheetActivityAllocatedActorFormData> allocatedActivityForm =
        allocatedActivityFormTemplate;

    // edit case: inject values
    if (!allocatedActivityId.equals(Long.valueOf(0))) {

      TimesheetActivityAllocatedActor allocatedActivity =
          TimesheetDao.getTimesheetActivityAllocatedActorById(allocatedActivityId);

      // security: the actor must be related to the object
      if (!allocatedActivity.actor.id.equals(id)) {
        return forbidden(views.html.error.access_forbidden.render(""));
      }

      allocatedActivityForm =
          allocatedActivityFormTemplate.fill(
              new TimesheetActivityAllocatedActorFormData(id, allocatedActivity));

      // add the custom attributes values
      this.getCustomAttributeManagerService()
          .fillWithValues(
              allocatedActivityForm, TimesheetActivityAllocatedActor.class, allocatedActivityId);
    } else {
      // add the custom attributes default values
      this.getCustomAttributeManagerService()
          .fillWithValues(allocatedActivityForm, TimesheetActivityAllocatedActor.class, null);
    }

    return ok(views.html.core.actor.allocated_activity_manage.render(actor, allocatedActivityForm));
  }
  /**
   * Display the allocations (portfolio entry and activity) of the actor.
   *
   * @param id the actor id
   * @param pagePortfolioEntry the current page for the portfolio entry allocations
   * @param pageActivity the current page for the activity allocations
   * @param viewAllActivities set to true if all activities (including the past ones) must be
   *     displayed
   */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_VIEW_DYNAMIC_PERMISSION)
  public Result allocationDetails(
      Long id, Integer pagePortfolioEntry, Integer pageActivity, Boolean viewAllActivities) {

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

    // construct the "portfolio entry" allocations tables

    Pagination<PortfolioEntryResourcePlanAllocatedActor> portfolioEntryPagination =
        PortfolioEntryResourcePlanDAO.getPEPlanAllocatedActorAsPaginationByActorAndActive(id, true);
    portfolioEntryPagination.setCurrentPage(pagePortfolioEntry);
    portfolioEntryPagination.setPageQueryName("pagePortfolioEntry");

    List<PortfolioEntryResourcePlanAllocatedActorListView> allocationListView =
        new ArrayList<PortfolioEntryResourcePlanAllocatedActorListView>();
    for (PortfolioEntryResourcePlanAllocatedActor allocatedActor :
        portfolioEntryPagination.getListOfObjects()) {
      allocationListView.add(new PortfolioEntryResourcePlanAllocatedActorListView(allocatedActor));
    }

    Set<String> columnsToHide = new HashSet<String>();
    columnsToHide.add("editActionLink");
    columnsToHide.add("removeActionLink");
    columnsToHide.add("followPackageDates");
    columnsToHide.add("actor");
    if (!getBudgetTrackingService().isActive()) {
      columnsToHide.add("currency");
      columnsToHide.add("dailyRate");
      columnsToHide.add("forecastDays");
      columnsToHide.add("forecastDailyRate");
    }

    Table<PortfolioEntryResourcePlanAllocatedActorListView> portfolioEntryTable =
        this.getTableProvider()
            .get()
            .portfolioEntryResourcePlanAllocatedActor
            .templateTable
            .fill(allocationListView, columnsToHide);

    portfolioEntryTable.setLineAction(
        new IColumnFormatter<PortfolioEntryResourcePlanAllocatedActorListView>() {
          @Override
          public String apply(
              PortfolioEntryResourcePlanAllocatedActorListView
                  portfolioEntryResourcePlanAllocatedActorListView,
              Object value) {
            return controllers.core.routes.PortfolioEntryPlanningController.resources(
                    portfolioEntryResourcePlanAllocatedActorListView.portfolioEntryId)
                .url();
          }
        });

    // construct the "activity" allocations tables

    Pagination<TimesheetActivityAllocatedActor> activityPagination =
        TimesheetDao.getTimesheetActivityAllocatedActorAsPaginationByActor(id, !viewAllActivities);
    activityPagination.setCurrentPage(pageActivity);
    activityPagination.setPageQueryName("pageActivity");

    List<TimesheetActivityAllocatedActorListView> activityAllocationListView =
        new ArrayList<TimesheetActivityAllocatedActorListView>();
    for (TimesheetActivityAllocatedActor allocatedActor : activityPagination.getListOfObjects()) {
      activityAllocationListView.add(new TimesheetActivityAllocatedActorListView(allocatedActor));
    }

    Set<String> columnsToHideForActivity = new HashSet<String>();
    columnsToHideForActivity.add("actor");

    Table<TimesheetActivityAllocatedActorListView> activityTable =
        this.getTableProvider()
            .get()
            .timesheetActivityAllocatedActor
            .templateTable
            .fill(activityAllocationListView, columnsToHideForActivity);

    return ok(
        views.html.core.actor.actor_allocation_details.render(
            actor,
            portfolioEntryTable,
            portfolioEntryPagination,
            activityTable,
            activityPagination,
            viewAllActivities));
  }
  /**
   * Display the gantt of allocations of the actor.
   *
   * @param id the actor id
   */
  @With(CheckActorExists.class)
  @Dynamic(IMafConstants.ACTOR_VIEW_DYNAMIC_PERMISSION)
  public Result allocation(Long id) {

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

    // prepare the data (to order them)
    SortableCollection<DateSortableObject> sortableCollection = new SortableCollection<>();
    for (PortfolioEntryResourcePlanAllocatedActor allocatedActor :
        PortfolioEntryResourcePlanDAO.getPEPlanAllocatedActorAsListByActorAndActive(id, true)) {
      if (allocatedActor.endDate != null) {
        sortableCollection.addObject(
            new DateSortableObject(allocatedActor.endDate, allocatedActor));
      }
    }
    for (TimesheetActivityAllocatedActor allocatedActivity :
        TimesheetDao.getTimesheetActivityAllocatedActorAsListByActor(id, true)) {
      if (allocatedActivity.endDate != null) {
        sortableCollection.addObject(
            new DateSortableObject(allocatedActivity.endDate, allocatedActivity));
      }
    }

    // construct the gantt

    List<SourceItem> items = new ArrayList<SourceItem>();

    for (DateSortableObject dateSortableObject : sortableCollection.getSorted()) {

      if (dateSortableObject.getObject() instanceof PortfolioEntryResourcePlanAllocatedActor) {

        PortfolioEntryResourcePlanAllocatedActor allocatedActor =
            (PortfolioEntryResourcePlanAllocatedActor) dateSortableObject.getObject();

        // get the from date
        Date from = allocatedActor.startDate;

        // get the to date
        Date to = allocatedActor.endDate;

        // get the portfolio entry
        Long portfolioEntryId =
            allocatedActor.portfolioEntryResourcePlan.lifeCycleInstancePlannings.get(0)
                .lifeCycleInstance
                .portfolioEntry
                .id;
        PortfolioEntry portfolioEntry = PortfolioEntryDao.getPEById(portfolioEntryId);

        String packageName =
            allocatedActor.portfolioEntryPlanningPackage != null
                ? allocatedActor.portfolioEntryPlanningPackage.getName()
                : "";

        SourceItem item = new SourceItem(portfolioEntry.getName(), packageName);

        String cssClass = null;

        if (from != null) {

          to = JqueryGantt.cleanToDate(from, to);
          cssClass = "";

        } else {

          from = to;
          cssClass = "diamond diamond-";
        }

        if (allocatedActor.isConfirmed) {
          cssClass += "success";
        } else {
          cssClass += "warning";
        }

        SourceDataValue dataValue =
            new SourceDataValue(
                controllers.core.routes.PortfolioEntryPlanningController.resources(
                        portfolioEntry.id)
                    .url(),
                null,
                null,
                null,
                null);

        item.values.add(
            new SourceValue(
                from,
                to,
                "",
                views
                    .html
                    .framework_views
                    .parts
                    .formats
                    .display_number
                    .render(allocatedActor.days, null, false)
                    .body(),
                cssClass,
                dataValue));

        items.add(item);
      }

      if (dateSortableObject.getObject() instanceof TimesheetActivityAllocatedActor) {

        TimesheetActivityAllocatedActor allocatedActivity =
            (TimesheetActivityAllocatedActor) dateSortableObject.getObject();

        // get the from date
        Date from = allocatedActivity.startDate;

        // get the to date
        Date to = allocatedActivity.endDate;

        SourceItem item = new SourceItem(allocatedActivity.timesheetActivity.getName(), "");

        String cssClass = null;

        if (from != null) {

          to = JqueryGantt.cleanToDate(from, to);
          cssClass = "";

        } else {

          from = to;
          cssClass = "diamond diamond-";
        }

        cssClass += "info";

        SourceDataValue dataValue =
            new SourceDataValue(
                controllers.core.routes.ActorController.allocationDetails(actor.id, 0, 0, false)
                    .url(),
                null,
                null,
                null,
                null);

        item.values.add(
            new SourceValue(
                from,
                to,
                "",
                views
                    .html
                    .framework_views
                    .parts
                    .formats
                    .display_number
                    .render(allocatedActivity.days, null, false)
                    .body(),
                cssClass,
                dataValue));

        items.add(item);
      }
    }

    String ganttSource = "";
    try {
      ObjectWriter ow = new ObjectMapper().writer().withDefaultPrettyPrinter();
      ganttSource = ow.writeValueAsString(items);
    } catch (JsonProcessingException e) {
      Logger.error(e.getMessage());
    }

    return ok(views.html.core.actor.actor_allocation.render(actor, ganttSource));
  }