/**
  * Returns all the existing tasks for that are owned by the projectIdOfLists and are linked to the
  * projectIdToBookmark
  *
  * @param db - the database connection
  * @param projectIdOfLists - this is the main project where the lists are stored against
  * @param projectIdToBookmark - the project that has been linked (bookmarked) to the above project
  *     by a list
  * @return taskList - the list of existing tasks matching the parameters
  * @throws SQLException - generated trying to retrieve data
  */
 private TaskList findExistingTasksForProjects(
     Connection db, int projectIdOfLists, int projectIdToBookmark) throws SQLException {
   TaskList taskList = new TaskList();
   taskList.setLinkModuleId(Constants.TASK_CATEGORY_PROJECTS);
   taskList.setLinkItemId(projectIdToBookmark);
   taskList.setProjectId(projectIdOfLists);
   taskList.buildList(db);
   return taskList;
 }
 /**
  * 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
    }
  }
  @Override
  public void processAction(ActionRequest request, ActionResponse response)
      throws PortletException, IOException {
    String ctx = request.getContextPath();
    boolean isClose = "true".equals(request.getParameter("close"));

    if (isClose) {
      // Clean up the session
      LOG.debug("Closing the form");
      request.getPortletSession().removeAttribute(VIEW_TYPE);
      response.sendRedirect(ctx + CLOSE_PAGE);
      return;
    }

    int projectIdToBookmark = Integer.valueOf(request.getParameter(PROJECT_ID_TO_BOOKMARK));
    int projectIdOfLists = Integer.valueOf(request.getParameter(PROJECT_ID_OF_LISTS));
    String newListName = request.getParameter(NEW_LIST_NAME);

    // If the user selected an item in the drop-down, then that means they are
    // changing lists
    int pidToCompare = Integer.valueOf(request.getParameter("pidToCompare"));
    boolean isChangePidOfLists = pidToCompare != projectIdOfLists;
    if (isChangePidOfLists) {
      LOG.debug("A new project has been selected: " + projectIdOfLists);
      response.setRenderParameter(PROJECT_ID_OF_LISTS, String.valueOf(projectIdOfLists));
      return;
    }

    try {
      LOG.debug("Saving the form...");

      boolean isSuccess;

      User user = PortalUtils.getUser(request);
      int userId = user.getId();
      Connection db = PortalUtils.useConnection(request);
      Project projectOfLists = new Project(db, projectIdOfLists);
      Project projectToBookmark = new Project(db, projectIdToBookmark);
      Collection<Integer> listIds = getListIds(request.getParameterValues(LIST));
      // verify user can modify lists for project
      boolean isAddNewList = false;
      if (ProjectUtils.hasAccess(projectOfLists.getId(), user, "project-lists-modify")) {

        if (!StringUtils.hasText(newListName) && (listIds.size() == 0)) {
          System.out.println("Error need to show From");
          request.getPortletSession().setAttribute(ACTION_ERROR, "Choose a list or create one");
          request.getPortletSession().setAttribute(VIEW_TYPE, VIEW_FORM_PAGE);
          return;
        }

        if (StringUtils.hasText(newListName)) {
          if (!ProjectUtils.hasAccess(projectOfLists.getId(), user, "project-lists-add")) {
            request
                .getPortletSession()
                .setAttribute(ACTION_ERROR, "Not authorized to create new list");
            request.getPortletSession().setAttribute(VIEW_TYPE, VIEW_FORM_PAGE);
            return;
          }
          int newListId = saveNewList(db, projectIdOfLists, newListName);
          if (newListId == -1) {
            request.getPortletSession().setAttribute(ACTION_ERROR, "Unable to create new list.");
            request.getPortletSession().setAttribute(VIEW_TYPE, SAVE_FAILURE);
            return;
          } else {
            listIds.add(newListId);
            isAddNewList = true;
          }
        }
        TaskList existingTasks =
            findExistingTasksForProjects(db, projectIdOfLists, projectIdToBookmark);
        // check to see if the user is deleting tasks (listItems)
        if ((isAddNewList && existingTasks.size() > listIds.size() - 1)
            || !isAddNewList && existingTasks.size() > listIds.size()) {
          if (!ProjectUtils.hasAccess(projectOfLists.getId(), user, "project-lists-delete")) {
            request
                .getPortletSession()
                .setAttribute(ACTION_ERROR, "Not authorized to delete items");
            request.getPortletSession().setAttribute(VIEW_TYPE, VIEW_FORM_PAGE);
            return;
          } else {
            deleteFromLists(db, existingTasks, listIds);
          }
        }
        isSuccess =
            saveToLists(
                db,
                existingTasks,
                listIds,
                userId,
                projectIdToBookmark,
                projectToBookmark.getTitle(),
                projectIdOfLists,
                request);
      } else {
        isSuccess = false;
        request.getPortletSession().setAttribute(ACTION_ERROR, "Not authorized to bookmark");
      }

      if (isSuccess) {
        // Close the panel, everything went well
        response.sendRedirect(ctx + "/close_panel_refresh.jsp");
      } else {
        request.getPortletSession().setAttribute(VIEW_TYPE, SAVE_FAILURE);
      }
    } catch (SQLException e) {
      e.printStackTrace();
      throw new RuntimeException(e);
    }
  }