示例#1
0
 @SuppressWarnings("unchecked")
 public void sortDown() {
   Long assignmentId = getAssignmentIdFromParam();
   if (logger.isDebugEnabled()) {
     logger.debug("Sort: sorting down: " + assignmentId);
   }
   List<Assignment> assignments = getGradebookManager().getAssignments(getGradebookId());
   if (assignments.size() > 1) {
     ensureAssignmentsSorted(assignments, GradableObject.sortingComparator, true);
     // now adjust the numbering
     for (int i = 0; i < assignments.size(); i++) {
       Assignment a1 = assignments.get(i);
       if (a1.getId().equals(assignmentId)) {
         if (i < (assignments.size() - 1)) {
           Assignment a2 = assignments.get(i + 1);
           // only swap items which are in the same category
           if ((a1.getCategory() == null && a2.getCategory() == null)
               || (a1.getCategory().equals(a2.getCategory()))) {
             // swap the ordering of this item and the item below it
             Integer holder = a1.getSortOrder();
             a1.setSortOrder(a2.getSortOrder());
             a2.setSortOrder(holder);
             logger.info(
                 "Sort: DOWN swapping: "
                     + a1.getId()
                     + " (to "
                     + a1.getSortOrder()
                     + " from "
                     + holder
                     + ") with "
                     + a2.getId());
             // save the new orders
             getGradebookManager().updateAssignment(a1);
             getGradebookManager().updateAssignment(a2);
           } else {
             logger.info(
                 "Sort: DOWN: unable to swap items ("
                     + a1.getId()
                     + ","
                     + a2.getId()
                     + ") in different categories");
           }
         }
         break;
       }
     }
   }
 }
示例#2
0
  @SuppressWarnings("unchecked")
  private void ensureAssignmentsSorted(List assignments, Comparator comparator, boolean save) {
    if (logger.isDebugEnabled()) {
      logger.debug(
          "ensureAssignmentsSorted: comparator="
              + comparator
              + ", save="
              + save
              + ", assignments= "
              + Arrays.toString(assignments.toArray()));
    }
    // remove any non-assignments first
    List gradeables = new ArrayList();
    for (Iterator iterator = assignments.iterator(); iterator.hasNext(); ) {
      GradableObject go = (GradableObject) iterator.next();
      if (!(go instanceof Assignment)) { // ! go.isAssignment()) {
        gradeables.add(go);
        iterator.remove();
      }
    }
    Collections.sort(gradeables, GradableObject.nameComparator);

    // put everything in the established sort order first (if needed)
    if (comparator == null) {
      comparator = GradableObject.dateComparator;
      if (logger.isDebugEnabled()) {
        logger.debug("ensureAssignmentsSorted: setting default comparator=" + comparator);
      }
    }
    if (!NoChangeMarkerComparator.class.isAssignableFrom(comparator.getClass())) {
      // only sort if this is not the no-sort marker
      if (logger.isDebugEnabled()) {
        logger.debug("ensureAssignmentsSorted: sorting with comparator=" + comparator);
      }
      Collections.sort(assignments, comparator);
    } else {
      if (logger.isDebugEnabled()) {
        logger.debug(
            "ensureAssignmentsSorted: no sort, using NoChangeMarkerComparator=" + comparator);
      }
    }
    // always need to sort by category
    Collections.sort(assignments, GradableObject.categoryComparator);
    // now ensure the numbering is set and correct
    int saveCount = 0;
    int updateCount = 0;
    for (int i = 0; i < assignments.size(); i++) {
      Assignment assignment = (Assignment) assignments.get(i);
      Integer curOrder = assignment.getSortOrder();
      if (logger.isDebugEnabled()) {
        logger.debug(
            "ensureAssignmentsSorted: checking if current order ("
                + curOrder
                + ") matches correct order ("
                + i
                + ") for assignment: "
                + assignment);
      }
      if (curOrder == null || i != curOrder.intValue()) {
        // no match so we need to update it (else it is already set correctly, only save if needed)
        assignment.setSortOrder(i);
        updateCount++;
        if (logger.isDebugEnabled()) {
          logger.debug(
              "ensureAssignmentsSorted: setting sort order ("
                  + i
                  + ") for assignment: "
                  + assignment);
        }
        if (save) {
          getGradebookManager().updateAssignment(assignment);
          saveCount++;
          if (logger.isDebugEnabled()) {
            logger.debug("ensureAssignmentsSorted: saving assignment: " + assignment);
          }
        }
      }
    }

    // set the ordering up in the assignment with support for categories
    Map<String, List<Assignment>> categoryAssignments =
        new LinkedHashMap<String, List<Assignment>>();
    for (Assignment assignment : (List<Assignment>) assignments) {
      String category = "NULL";
      if (assignment.getCategory() != null) {
        category = assignment.getCategory().getName();
      }
      if (!categoryAssignments.containsKey(category)) {
        categoryAssignments.put(category, new ArrayList<Assignment>());
      }
      categoryAssignments.get(category).add(assignment);
      // assignment.assignSorting(assignments.size(), i);
    }
    for (Entry<String, List<Assignment>> entry : categoryAssignments.entrySet()) {
      List<Assignment> l = entry.getValue();
      for (int i = 0; i < l.size(); i++) {
        Assignment assignment = l.get(i);
        // assign the counter for ordering
        assignment.assignSorting(l.size(), i);
        if (logger.isDebugEnabled()) {
          logger.debug("ensureAssignmentsSorted: ordered: " + i + " : " + assignment);
        }
      }
    }
    // add back in the gradeables to the end
    for (Object gradeable : gradeables) {
      assignments.add(gradeable);
    }
    if (logger.isDebugEnabled()) {
      logger.debug(
          "ensureAssignmentsSorted: sorted assignments (updated="
              + updateCount
              + ", saved="
              + saveCount
              + "): "
              + Arrays.toString(assignments.toArray()));
    }
  }