Skip to content

Commit

Permalink
SAK-31923 Exporting to csv always use comma as separator (sakaiprojec…
Browse files Browse the repository at this point in the history
…t#3825)

* SAK-31923 Exporting to csv always use comma as separator

* Update AssignmentService.java
  • Loading branch information
annuelo authored and ern committed Feb 15, 2017
1 parent dc52496 commit 8c0b4ae
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -269,8 +269,6 @@ public interface AssignmentService extends EntityProducer
public boolean allowAddSubmission(String context);

/**
* SAK-21525
*
* @param context
* @param assignment - An Assignment object. Needed for the groups to be checked.
* @return
Expand Down Expand Up @@ -914,8 +912,6 @@ public void removeAssignmentContent(AssignmentContentEdit content) throws Assign
/*
* If the assignment uses anonymous grading returns true, else false
*
* SAK-27824
*
* Params: AssignmentSubmission s
*/
public boolean assignmentUsesAnonymousGrading(Assignment a);
Expand All @@ -929,8 +925,6 @@ public void removeAssignmentContent(AssignmentContentEdit content) throws Assign
/*
* This method allows you to know if there are submissions submitted
*
* SAK-30062
*
* Params: AssignmentSubmission s
*/
public boolean hasBeenSubmitted(AssignmentSubmission s);
Expand Down Expand Up @@ -958,4 +952,11 @@ public String getDeepLinkWithPermissions(String context, String assignmentId, bo
* @return The url as a String
*/
public String getDeepLink(String context, String assignmentId) throws Exception;

/**
* get csv separator for exporting to CSV. It can be a comma or point configured through
* csv.separator sakai property
* @return
*/
public String getCsvSeparator();
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;

import org.sakaiproject.site.api.Group;
import org.sakaiproject.assignment.api.Assignment;
import org.sakaiproject.assignment.api.AssignmentSubmission;
Expand Down Expand Up @@ -884,4 +885,12 @@ public static boolean hasBeenSubmitted(

return service.hasBeenSubmitted(param0);
}

public static String getCsvSeparator() {
org.sakaiproject.assignment.api.AssignmentService service = getInstance();
if (service == null)
return null;

return service.getCsvSeparator();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5120,7 +5120,7 @@ protected void zipGroupSubmissions(String assignmentReference, String assignment
String root = escapeInvalidCharsEntry(Validator.escapeZipEntry(assignmentTitle)) + Entity.SEPARATOR;

SpreadsheetExporter.Type type = SpreadsheetExporter.Type.valueOf(gradeFileFormat.toUpperCase());
SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle, gradeTypeString);
SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle, gradeTypeString, getCsvSeparator());

String submittedText = "";
if (!submissions.hasNext())
Expand Down Expand Up @@ -5351,7 +5351,7 @@ protected void zipSubmissions(String assignmentReference, String assignmentTitle
String root = escapeInvalidCharsEntry(Validator.escapeZipEntry(assignmentTitle)) + Entity.SEPARATOR;

SpreadsheetExporter.Type type = SpreadsheetExporter.Type.valueOf(gradeFileFormat.toUpperCase());
SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle, gradeTypeString);
SpreadsheetExporter sheet = SpreadsheetExporter.getInstance(type, assignmentTitle, gradeTypeString, getCsvSeparator());

String submittedText = "";
if (!submissions.hasNext())
Expand Down Expand Up @@ -14125,5 +14125,16 @@ public String getDeepLinkWithPermissions(String context, String assignmentId, bo
}
return "";
}

public String getCsvSeparator() {
String defaultSeparator = ",";
//If the decimal separator is a comma
if (",".equals(FormattedText.getDecimalSeparator())) {
defaultSeparator = ";";
}

return m_serverConfigurationService.getString("csv.separator", defaultSeparator);
}

} // BaseAssignmentService

Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sakaiproject.assignment.impl;

import au.com.bytecode.opencsv.CSVWriter;

import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.IndexedColors;
Expand All @@ -25,9 +26,9 @@ enum Type {CSV, EXCEL};
* @param gradeType The grade type being exported.
* @return A new SpreadsheetExporter that can be used.
*/
public static SpreadsheetExporter getInstance(Type type, String title, String gradeType) {
public static SpreadsheetExporter getInstance(Type type, String title, String gradeType, String csvSep) {
if (Type.CSV.equals(type)) {
return new CsvExporter(title, gradeType);
return new CsvExporter(title, gradeType, csvSep);
} else if (Type.EXCEL.equals(type)) {
return new ExcelExporter(title, gradeType);
} else {
Expand Down Expand Up @@ -69,9 +70,9 @@ class CsvExporter extends SpreadsheetExporter {
private final ByteArrayOutputStream gradesBAOS;
private final CSVWriter gradesBuffer;

CsvExporter(String title, String gradeType) {
CsvExporter(String title, String gradeType, String csvSep) {
gradesBAOS = new ByteArrayOutputStream();
gradesBuffer = new CSVWriter(new OutputStreamWriter(gradesBAOS));
gradesBuffer = new CSVWriter(new OutputStreamWriter(gradesBAOS), csvSep.charAt(0));
addRow(title, gradeType);
addRow("");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class SpreadsheetExporterTest {

@Test
public void testSimpleCSV() throws IOException {
SpreadsheetExporter exporter = SpreadsheetExporter.getInstance(SpreadsheetExporter.Type.CSV, "title", "type");
SpreadsheetExporter exporter = SpreadsheetExporter.getInstance(SpreadsheetExporter.Type.CSV, "title", "type", ",");
exporter.addHeader("header");
exporter.addRow("row1", "row1");
exporter.addRow("row2", "row2");
Expand All @@ -29,7 +29,7 @@ public void testSimpleCSV() throws IOException {

@Test
public void testSimpleExcel() throws IOException {
SpreadsheetExporter exporter = SpreadsheetExporter.getInstance(SpreadsheetExporter.Type.EXCEL, "title", "type");
SpreadsheetExporter exporter = SpreadsheetExporter.getInstance(SpreadsheetExporter.Type.EXCEL, "title", "type", ",");
exporter.addHeader("header");
exporter.addRow("row1", "row1");
exporter.addRow("row2", "row2");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16041,7 +16041,9 @@ private HashMap uploadAll_parseZipFile(SessionState state,
zipHasGradeFile = true;

// read grades.cvs from zip
CSVReader reader = new CSVReader(new InputStreamReader(zipFile.getInputStream(entry)));

String csvSep = assignmentService.getCsvSeparator();
CSVReader reader = new CSVReader(new InputStreamReader(zipFile.getInputStream(entry)), csvSep.charAt(0));

List <String[]> lines = reader.readAll();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,11 @@
# Default: true
# contentreview.option.exclude_self_plag.default=true

# SAK-31923
# Sets the separation char on exporting to CSV
# Default: ,
# csv.separator=;

## ANNOUNCEMENTS
# Allow reordering of Announcements using Fluid drag-and-drop reordering component
# DEFAULT: true
Expand Down

0 comments on commit 8c0b4ae

Please sign in to comment.