/**
  * Deletes any existing task records that are not specified in the listIds. This method allows a
  * user to specify on the request which lists they want to bookmark too, if any existing links
  * were not specified they will be deleted.
  *
  * @param db - connection to database
  * @param existingTasks - list of tasks already persisted
  * @param listIds - list of tasks that were specified by user
  * @throws SQLException - generated trying to delete records
  */
 private void deleteFromLists(Connection db, TaskList existingTasks, Collection<Integer> listIds)
     throws SQLException {
   Set<Integer> deleteTaskIds = new HashSet<Integer>(existingTasks.size());
   for (Task task : existingTasks) {
     deleteTaskIds.add(task.getCategoryId());
   }
   // find all the task ids that were not requested (these will be deleted)
   deleteTaskIds.removeAll(listIds);
   if (deleteTaskIds.size() > 0) {
     for (Task task : existingTasks) {
       if (deleteTaskIds.contains(task.getCategoryId())) {
         task.delete(db);
       }
     }
   }
 }
  /**
   * Inserts the project to boomark in new listItem (task) records, a record is created for each of
   * the listIds (taskCategoryIds) specified.
   *
   * <p>Returns boolean on whether the operation was successful. Once one failure occurs the method
   * returns false without further processing.
   *
   * @param db - the database connection
   * @param existingTasks - the list of tasks already persisted
   * @param listIds - this list of listIds that will have the projectIdToBookmark saved
   * @param userId - the id of the user inserting the records
   * @param projectIdToBookmark - the id of the project that is being bookmarked
   * @param projectNameToBookmark - the name that will saved for the bookmark (task)
   * @param projectIdOfLists - the id of the project that owns the lists
   * @param request - the request
   * @return boolean on whether the operation was successful
   * @throws SQLException - generated trying to retrieve data
   */
  private boolean saveToLists(
      Connection db,
      TaskList existingTasks,
      Collection<Integer> listIds,
      int userId,
      int projectIdToBookmark,
      String projectNameToBookmark,
      int projectIdOfLists,
      ActionRequest request)
      throws SQLException {
    Set<Integer> existingIds = new HashSet<Integer>(existingTasks.size());
    Set<Integer> createForTaskCategoryIds = new HashSet<Integer>(listIds);
    for (Task task : existingTasks) {
      existingIds.add(task.getCategoryId());
    }
    // find all the task category ids that do not already have tasks (these will have tasks
    // inserted)
    createForTaskCategoryIds.removeAll(existingIds);
    if (!createForTaskCategoryIds.isEmpty()) {
      boolean recordInserted = false;
      LookupList priorityList = CacheUtils.getLookupList("lookup_task_priority");
      if (priorityList.isEmpty()) throw new RuntimeException("Could not load task priorities");
      // just default to the top priority
      int priorityId = priorityList.get(0).getId();
      for (LookupElement priority : priorityList) {
        if (priority.getDefaultItem()) {
          priorityList.get(0).getId();
          break;
        }
      }

      // Parameters
      TaskList taskList = new TaskList();
      for (Integer taskCategoryId : createForTaskCategoryIds) {

        Task task = new Task();
        task.setEnteredBy(userId);
        task.setOwner(userId);
        task.setDescription(projectNameToBookmark);
        task.setModifiedBy(userId);
        task.setProjectId(projectIdOfLists);
        task.setLinkModuleId(Constants.TASK_CATEGORY_PROJECTS);
        task.setLinkItemId(projectIdToBookmark);
        task.setCategoryId(taskCategoryId);
        task.setPriority(priorityId);
        // Verify the specified category is in the same project
        TaskCategoryList list = new TaskCategoryList();
        list.setProjectId(projectIdOfLists);
        list.setCategoryId(taskCategoryId);
        list.buildList(db);
        if (list.size() == 0) {
          return false;
        }
        recordInserted = task.insert(db);
        if (!recordInserted) {
          request.getPortletSession().setAttribute("task", task);
          return false;
        }
        taskList.add(task);
      }

      // Trigger the workflow
      PortalUtils.processInsertHook(request, taskList);

      return recordInserted;
    } else {
      return true; // no inserts needed
    }
  }