Skip to content

Commit

Permalink
Detect when Gradebook Item already exists or was previously entered d…
Browse files Browse the repository at this point in the history
…uring import process (sakaiproject#3298)

* Remove unused code and cleanup other compiler warnings

* #3152 Detect when gradebook item already exists or was previously entered during import process
  • Loading branch information
steveswinsburg authored and payten committed Sep 5, 2016
1 parent 51b6ea3 commit d2c898d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package org.sakaiproject.gradebookng.tool.panels.importExport;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.wicket.Component;
import org.apache.wicket.markup.html.basic.Label;
Expand All @@ -9,6 +11,8 @@
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;
import org.apache.wicket.model.StringResourceModel;
import org.apache.wicket.spring.injection.annot.SpringBean;
import org.sakaiproject.gradebookng.business.GradebookNgBusinessService;
import org.sakaiproject.gradebookng.business.model.ProcessedGradeItem;
import org.sakaiproject.gradebookng.tool.model.ImportWizardModel;
import org.sakaiproject.gradebookng.tool.pages.ImportExportPage;
Expand All @@ -25,6 +29,9 @@ public class CreateGradeItemStep extends Panel {

private static final long serialVersionUID = 1L;

@SpringBean(name = "org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
private GradebookNgBusinessService businessService;

private final String panelId;
private final IModel<ImportWizardModel> model;

Expand Down Expand Up @@ -55,42 +62,53 @@ public void onInitialize() {

final Model<Assignment> assignmentModel = new Model<>(assignment);

@SuppressWarnings("unchecked")
final
Form<Assignment> form = new Form("form", assignmentModel) {
final Form<Assignment> form = new Form<Assignment>("form", assignmentModel) {
private static final long serialVersionUID = 1L;

@Override
protected void onSubmit() {

final Assignment a = (Assignment)getDefaultModel().getObject();
final Assignment newAssignment = (Assignment)getDefaultModel().getObject();

//add to model
importWizardModel.getAssignmentsToCreate().add(a);
log.debug("Assignment: " + newAssignment);

log.debug("Assignment: " + assignment);
boolean validated = true;

// sync up the assignment data so we can present it for confirmation
processedGradeItem.setAssignmentTitle(a.getName());
processedGradeItem.setAssignmentPoints(a.getPoints());
// validate name is unique
final List<Assignment> existingAssignments = CreateGradeItemStep.this.businessService.getGradebookAssignments();
existingAssignments.addAll(importWizardModel.getAssignmentsToCreate());

//Figure out if there are more steps
//If so, go to the next step (ie do it all over again)
Component newPanel = null;
if (step < importWizardModel.getTotalSteps()) {
importWizardModel.setStep(step+1);
newPanel = new CreateGradeItemStep(CreateGradeItemStep.this.panelId, Model.of(importWizardModel));
} else {
//If not, continue on in the wizard
newPanel = new GradeImportConfirmationStep(CreateGradeItemStep.this.panelId, Model.of(importWizardModel));
if(!assignmentNameIsUnique(existingAssignments, newAssignment.getName())) {
validated = false;
error(getString("error.addgradeitem.title"));
}

// clear any previous errors
final ImportExportPage page = (ImportExportPage) getPage();
page.clearFeedback();

newPanel.setOutputMarkupId(true);
CreateGradeItemStep.this.replaceWith(newPanel);
if (validated) {
//add to model
importWizardModel.getAssignmentsToCreate().add(newAssignment);

// sync up the assignment data so we can present it for confirmation
processedGradeItem.setAssignmentTitle(newAssignment.getName());
processedGradeItem.setAssignmentPoints(newAssignment.getPoints());

//Figure out if there are more steps
//If so, go to the next step (ie do it all over again)
Component newPanel = null;
if (step < importWizardModel.getTotalSteps()) {
importWizardModel.setStep(step+1);
newPanel = new CreateGradeItemStep(CreateGradeItemStep.this.panelId, Model.of(importWizardModel));
} else {
//If not, continue on in the wizard
newPanel = new GradeImportConfirmationStep(CreateGradeItemStep.this.panelId, Model.of(importWizardModel));
}

// clear any previous errors
final ImportExportPage page = (ImportExportPage) getPage();
page.clearFeedback();

newPanel.setOutputMarkupId(true);
CreateGradeItemStep.this.replaceWith(newPanel);
}

}
};
Expand Down Expand Up @@ -142,4 +160,18 @@ public void onSubmit() {
form.add(new AddOrEditGradeItemPanelContent("subComponents", assignmentModel));

}

/**
* Checks if an assignment is unique given a list of existing assignments
* @param assignments
* @param name
* @return
*/
private boolean assignmentNameIsUnique(final List<Assignment> assignments, final String name) {
return !(assignments
.stream()
.filter(a -> StringUtils.equals(a.getName(), name))
.findFirst()
.isPresent());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class ExportPanel extends Panel {
private static final long serialVersionUID = 1L;

@SpringBean(name = "org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
protected GradebookNgBusinessService businessService;
private GradebookNgBusinessService businessService;

private static final String CUSTOM_EXPORT_COLUMN_PREFIX = "# ";
private static final String COLUMN_COLUMN_PREFIX = "* ";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class GradeImportConfirmationStep extends Panel {
private static final long serialVersionUID = 1L;

@SpringBean(name = "org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
protected GradebookNgBusinessService businessService;
private GradebookNgBusinessService businessService;

private final String panelId;
private final IModel<ImportWizardModel> model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public class GradeImportUploadStep extends Panel {
private final String panelId;

@SpringBean(name = "org.sakaiproject.gradebookng.business.GradebookNgBusinessService")
protected GradebookNgBusinessService businessService;
private GradebookNgBusinessService businessService;

public GradeImportUploadStep(final String id) {
super(id);
Expand Down

0 comments on commit d2c898d

Please sign in to comment.