/**
  * Retrieves all task categories that contain this projectToBookmark as a task and are owned by
  * the projectOfLists
  *
  * @param db - database connection
  * @param projectIdOfLists - the id of the project that owns the lists (taskCategories)
  * @param projectIdToBookmark - the id of the project that is linked by bookmark
  * @return Map<Integer, TaskCategory> all taskCategories found mapped by their id
  * @throws SQLException - generated trying to retrieve data
  */
 private Map<Integer, TaskCategory> findExistingTaskCategorysForProjects(
     Connection db, int projectIdOfLists, int projectIdToBookmark) throws SQLException {
   Map<Integer, TaskCategory> map = new HashMap<Integer, TaskCategory>();
   TaskCategoryList categoryList = new TaskCategoryList();
   categoryList.setTaskLinkModuleId(Constants.TASK_CATEGORY_PROJECTS);
   categoryList.setTaskLinkItemId(projectIdToBookmark);
   categoryList.setProjectId(projectIdOfLists);
   categoryList.buildList(db);
   for (TaskCategory tc : categoryList) {
     map.put(tc.getId(), tc);
   }
   return map;
 }
 /**
  * Retrieves all the lists available to the project
  *
  * <p>If no lists are available then the empty list is returned
  *
  * @param db - database connection
  * @param projectId - id of project to find it's available lists
  * @return TaskCategoryList - the lists available to the user
  * @throws SQLException - generated trying to retrieve data
  */
 private TaskCategoryList getAvailableLists(Connection db, int projectId) throws SQLException {
   TaskCategoryList availableLists = new TaskCategoryList();
   availableLists.setProjectId(projectId);
   availableLists.buildList(db);
   return availableLists;
 }
  /**
   * 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
    }
  }