/**
   * Update the grading of the assessment.
   *
   * @param ag the assessment grading.
   * @param g the Gradebook Service
   * @throws java.lang.Exception
   */
  public void updateExternalAssessmentScore(AssessmentGradingIfc ag, GradebookService g)
      throws Exception {
    boolean testErrorHandling = false;
    // log.info("GradebookService instance=" + g);
    PublishedAssessmentService pubService = new PublishedAssessmentService();
    GradingService gradingService = new GradingService();
    PublishedAssessmentIfc pub =
        (PublishedAssessmentIfc)
            gradingService.getPublishedAssessmentByAssessmentGradingId(
                ag.getAssessmentGradingId().toString());

    String gradebookUId = pubService.getPublishedAssessmentOwner(pub.getPublishedAssessmentId());
    if (gradebookUId == null) {
      return;
    }

    // SAM-1562 We need to round the float score and covert to a double -DH
    float fScore = MathUtils.round(ag.getFinalScore(), 2);
    Double score = Float.valueOf(fScore).doubleValue();
    log.info("rounded:  " + ag.getFinalScore() + " to: " + score.toString());
    g.updateExternalAssessmentScore(
        gradebookUId, ag.getPublishedAssessmentId().toString(), ag.getAgentId(), score);
    if (testErrorHandling) {
      throw new Exception("Encountered an error in update ExternalAssessmentScore.");
    }
  }
  /**
   * Update a gradebook.
   *
   * @param publishedAssessment the published assessment
   * @param g the Gradebook Service
   * @return false: cannot update the gradebook
   * @throws java.lang.Exception
   */
  public boolean updateGradebook(PublishedAssessmentIfc publishedAssessment, GradebookService g)
      throws Exception {
    log.debug("updateGradebook start");
    String gradebookUId = GradebookFacade.getGradebookUId();
    if (gradebookUId == null) {
      return false;
    }

    log.debug("before g.isAssignmentDefined()");
    g.updateExternalAssessment(
        gradebookUId,
        publishedAssessment.getPublishedAssessmentId().toString(),
        null,
        publishedAssessment.getTitle(),
        publishedAssessment.getTotalScore().doubleValue(),
        publishedAssessment.getAssessmentAccessControl().getDueDate());
    return true;
  }
  private void populateSections(
      PublishedAssessmentIfc publishedAssessment,
      QuestionScoresBean bean,
      TotalScoresBean totalBean,
      ArrayList scores,
      PublishedAssessmentService pubService) {
    ArrayList sections = new ArrayList();
    log.debug(
        "questionScores(): populate sctions publishedAssessment.getSectionArraySorted size = "
            + publishedAssessment.getSectionArraySorted().size());
    Iterator iter = publishedAssessment.getSectionArraySorted().iterator();
    int i = 1;
    while (iter.hasNext()) {
      SectionDataIfc section = (SectionDataIfc) iter.next();
      ArrayList items = new ArrayList();
      PartData part = new PartData();
      boolean isRandomDrawPart =
          pubService.isRandomDrawPart(
              publishedAssessment.getPublishedAssessmentId(), section.getSectionId());
      part.setIsRandomDrawPart(isRandomDrawPart);
      part.setPartNumber("" + i);
      part.setId(section.getSectionId().toString());

      if (isRandomDrawPart) {
        if (section.getSectionMetaDataByLabel(SectionDataIfc.NUM_QUESTIONS_DRAWN) != null) {
          int numberToBeDrawn =
              Integer.parseInt(
                  section.getSectionMetaDataByLabel(SectionDataIfc.NUM_QUESTIONS_DRAWN));
          part.setNumberQuestionsDraw(numberToBeDrawn);
        }
        PublishedAssessmentService publishedAssessmentService = new PublishedAssessmentService();
        HashSet itemSet =
            publishedAssessmentService.getPublishedItemSet(
                publishedAssessment.getPublishedAssessmentId(), section.getSectionId());
        section.setItemSet(itemSet);
      } else {
        GradingService gradingService = new GradingService();
        HashSet itemSet =
            gradingService.getItemSet(
                publishedAssessment.getPublishedAssessmentId(), section.getSectionId());
        section.setItemSet(itemSet);
      }
      Iterator iter2 = section.getItemArraySortedForGrading().iterator();
      int j = 1;
      while (iter2.hasNext()) {
        ItemDataIfc item = (ItemDataIfc) iter2.next();
        PartData partitem = new PartData();

        partitem.setPartNumber("" + j);
        partitem.setId(item.getItemId().toString());
        log.debug("*   item.getId = " + item.getItemId());
        partitem.setLinked(true);

        // Iterator iter3 = scores.iterator();
        items.add(partitem);
        j++;
      }
      log.debug("questionScores(): items size = " + items.size());
      part.setQuestionNumberList(items);
      sections.add(part);
      i++;
    }
    log.debug("questionScores(): sections size = " + sections.size());
    bean.setSections(sections);
  }