Skip to content

Commit

Permalink
Student Scheduling Dashboards, Solver Reports: XLS Export
Browse files Browse the repository at this point in the history
- added XLS export
  • Loading branch information
tomas-muller committed Jul 18, 2024
1 parent 3ccfa43 commit 26d458f
Show file tree
Hide file tree
Showing 8 changed files with 186 additions and 13 deletions.
38 changes: 35 additions & 3 deletions JavaSource/org/unitime/timetable/export/XLSPrinter.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@
import org.unitime.timetable.export.Exporter.Printer;
import org.unitime.timetable.export.PDFPrinter.A;
import org.unitime.timetable.export.PDFPrinter.F;
import org.unitime.timetable.util.Formats;
import org.unitime.timetable.util.Formats.Format;

/**
* @author Tomas Muller
*/
public class XLSPrinter implements Printer {
private static Pattern sNumber = Pattern.compile("[+-]?[0-9]*\\.?[0-9]*[a-z]?");
private static Pattern sNumber = Pattern.compile("[+-]?[0-9\\,]*\\.?[0-9]*[a-z\\%]?|N/A|NaN|NaN%|\u221e");
private static Format<Number> sDFP = Formats.getNumberFormat("#,###,##0.###%");
private static Format<Number> sDF = Formats.getNumberFormat("#,###,##0.###");
private OutputStream iOutput;
private Workbook iWorkbook;
private Object[] iLastLine = null;
Expand Down Expand Up @@ -109,6 +113,13 @@ public XLSPrinter(OutputStream output, boolean checkLast) {
style.setFont(getFont(false, false, false, Color.BLACK));
iStyles.put("number", style);

style = iWorkbook.createCellStyle();
style.setAlignment(HorizontalAlignment.RIGHT);
style.setVerticalAlignment(VerticalAlignment.TOP);
style.setFont(getFont(false, false, false, Color.BLACK));
style.setDataFormat(iWorkbook.createDataFormat().getFormat("#,###,##0.0##%"));
iStyles.put("percent", style);

newSheet();
}

Expand Down Expand Up @@ -190,9 +201,30 @@ public void printLine(String... fields) {
if (f == null || f.isEmpty()) {
} else if (number) {
try {
cell.setCellValue(Double.valueOf(f));
Double val = Double.parseDouble(f);
if (!val.isInfinite() && !val.isNaN())
cell.setCellValue(val);
} catch (NumberFormatException e) {
cell.setCellValue(f);
try {
if (f.endsWith("%")) {
Double val = sDFP.parse(f).doubleValue();
if (!val.isInfinite() && !val.isNaN()) {
cell.setCellValue(val);
cell.setCellStyle(iStyles.get("percent"));
} else {
cell.setCellValue(f);
}
} else {
Double val = sDF.parse(f).doubleValue();
if (!val.isInfinite() && !val.isNaN()) {
cell.setCellValue(val);
} else {
cell.setCellValue(f);
}
}
} catch (Exception p) {
cell.setCellValue(f);
}
}
} else {
nrLines = Math.max(nrLines, f.split("\n").length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,14 @@ public void export(ExportHelper helper) throws IOException {
}
}

protected void populateCourseTable(ExportHelper helper, List<EnrollmentInfo> enrollments) throws IOException {
protected Printer getPrinter(ExportHelper helper) throws IOException {
Printer out = new CSVPrinter(helper, false);
helper.setup(out.getContentType(), reference(), false);
return out;
}

protected void populateCourseTable(ExportHelper helper, List<EnrollmentInfo> enrollments) throws IOException {
Printer out = getPrinter(helper);

boolean hasSnapshot = false;
for (EnrollmentInfo e: enrollments) {
Expand Down Expand Up @@ -246,8 +251,7 @@ protected void populateCourseTable(ExportHelper helper, List<EnrollmentInfo> enr
}

protected void populateStudentTable(ExportHelper helper, boolean online, List<StudentInfo> students) throws IOException {
Printer out = new CSVPrinter(helper, false);
helper.setup(out.getContentType(), reference(), false);
Printer out = getPrinter(helper);

boolean hasExtId = false;
boolean hasEnrollment = false, hasWaitList = false, hasArea = false, hasMajor = false, hasGroup = false, hasAcmd = false, hasReservation = false,
Expand Down Expand Up @@ -572,8 +576,7 @@ protected void populateStudentTable(ExportHelper helper, boolean online, List<St
}

protected void populateChangeLogTable(ExportHelper helper, List<SectioningAction> changelog) throws IOException {
Printer out = new CSVPrinter(helper, false);
helper.setup(out.getContentType(), reference(), false);
Printer out = getPrinter(helper);

boolean hasExtId = false;
if (changelog != null)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* The Apereo Foundation licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.unitime.timetable.export.students;

import java.io.IOException;

import org.springframework.stereotype.Service;
import org.unitime.timetable.export.ExportHelper;
import org.unitime.timetable.export.XLSPrinter;

/**
* @author Tomas Muller
*/
@Service("org.unitime.timetable.export.Exporter:student-dashboard.xls")
public class StudentSchedulingDashboardExportXLS extends StudentSchedulingDashboardExportCSV {

@Override
public String reference() {
return "student-dashboard.xls";
}

protected Printer getPrinter(ExportHelper helper) throws IOException {
Printer out = new XLSPrinter(helper.getOutputStream(), false);
helper.setup(out.getContentType(), reference(), false);
return out;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,11 +245,38 @@ public void onSuccess(EncodeQueryRpcResponse result) {
});
}
});

iHeader.addButton("exportXls", MESSAGES.buttonExportXLS(), 85, new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
if (iReportSelector.getWidget().getSelectedIndex() <= 0) {
iHeader.setErrorMessage(MESSAGES.errorNoReportSelected());
return;
}
ReportTypeInterface type = getReportType(iReportSelector.getWidget().getValue(iReportSelector.getWidget().getSelectedIndex()));
String query = "output=sct-report.xls&name=" + type.getReference() + "&report=" + type.getImplementation() + "&online=" + (iOnline ? "true" : "false") + "&sort=" + iLastSort;
if (type.isFilter())
query += "&filter=" + URL.encodeQueryString(iFilter.getValue());
for (int i = 0; i + 1 < type.getParameters().length; i += 2)
query += "&" + type.getParameters()[i] + "=" + type.getParameters()[i + 1];

RPC.execute(EncodeQueryRpcRequest.encode(query), new AsyncCallback<EncodeQueryRpcResponse>() {
@Override
public void onFailure(Throwable caught) {
}
@Override
public void onSuccess(EncodeQueryRpcResponse result) {
ToolBox.open(GWT.getHostPageBaseURL() + "export?q=" + result.getQuery());
}
});
}
});

iForm.addHeaderRow(iHeader);
iHeader.setEnabled("execute", false);
iHeader.setEnabled("print", false);
iHeader.setEnabled("export", false);
iHeader.setEnabled("exportXls", false);

iForm.getColumnFormatter().setWidth(0, "120px");
iForm.getColumnFormatter().setWidth(1, "100%");
Expand Down Expand Up @@ -431,9 +458,11 @@ private void queryChanged() {
if (iReportSelector.getWidget().getSelectedIndex() <= 0) {
iHeader.setEnabled("execute", false);
iHeader.setEnabled("export", false);
iHeader.setEnabled("exportXls", false);
} else {
iHeader.setEnabled("execute", true);
iHeader.setEnabled("export", true);
iHeader.setEnabled("exportXls", true);
}
}

Expand Down Expand Up @@ -934,6 +963,7 @@ public void onValueChange(ValueChangeEvent<Boolean> event) {
iTable.setColumnVisible(0, !iHead.getCell(0).startsWith("__") || (iSectioningProperties != null && "__Student".equals(iHead.getCell(0)) && iSectioningProperties.isCanSelectStudent()));
iHeader.setEnabled("print", true);
iHeader.setEnabled("export", true);
iHeader.setEnabled("exportXls", true);
if (iData.size() <= 100 && iFirstLine == 0)
iTableHeader.setMessage(MESSAGES.infoShowingAllLines(iData.size()));
else
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,17 @@ public void execute() {
}
}

if (iExport.isVisible()) {
MenuItem item = new MenuItem(UniTimeHeaderPanel.stripAccessKey(GWT_MESSAGES.buttonExportXLS()), true, new Command() {
@Override
public void execute() {
popup.hide();
exportData("xls");
}
});
menu.addItem(item);
}

popup.add(menu);
popup.showRelativeTo((UIObject)event.getSource());
menu.focus();
Expand Down Expand Up @@ -527,7 +538,7 @@ public void onClick(ClickEvent event) {

iExport.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
exportData();
exportData("csv");
}
});

Expand Down Expand Up @@ -1032,12 +1043,12 @@ public void onSuccess(final List<SectioningAction> result) {
}
}

private void exportData() {
private void exportData(String format) {
int tab = iTabIndex;
if (tab < 0)
tab = SectioningStatusCookie.getInstance().getTab(iOnline);

String query = "output=student-dashboard.csv&online=" + (iOnline ? 1 : 0) + "&tab=" + tab + "&sort=" + SectioningStatusCookie.getInstance().getSortBy(iOnline, tab);
String query = "output=student-dashboard." + format + "&online=" + (iOnline ? 1 : 0) + "&tab=" + tab + "&sort=" + SectioningStatusCookie.getInstance().getSortBy(iOnline, tab);
if (tab == 0)
for (Long courseId: iSelectedCourseIds)
query += "&c=" + courseId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ public class SectioningReportsExporter implements Exporter {
public String reference() {
return "sct-report.csv";
}

protected Printer getPrinter(ExportHelper helper) throws IOException {
Printer out = new CSVPrinter(helper, false);
helper.setup(out.getContentType(), helper.getParameter("name").toLowerCase().replace('_', '-') + ".csv", false);
return out;
}

@Override
public void export(ExportHelper helper) throws IOException {
Expand Down Expand Up @@ -139,8 +145,7 @@ public void export(ExportHelper helper) throws IOException {
if (csv == null)
throw new GwtRpcException("No report was created.");

Printer out = new CSVPrinter(helper, false);
helper.setup(out.getContentType(), helper.getParameter("name").toLowerCase().replace('_', '-') + ".csv", false);
Printer out = getPrinter(helper);

if (config != null) {
String term = config.getProperty("Data.Term");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Licensed to The Apereo Foundation under one or more contributor license
* agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* The Apereo Foundation licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.unitime.timetable.server.sectioning;

import java.io.IOException;

import org.springframework.stereotype.Service;
import org.unitime.timetable.export.ExportHelper;
import org.unitime.timetable.export.XLSPrinter;

/**
* @author Tomas Muller
*/
@Service("org.unitime.timetable.export.Exporter:sct-report.xls")
public class SectioningReportsExporterXLS extends SectioningReportsExporter {

@Override
public String reference() {
return "sct-report.xls";
}

@Override
protected Printer getPrinter(ExportHelper helper) throws IOException {
Printer out = new XLSPrinter(helper.getOutputStream(), false);
helper.setup(out.getContentType(), helper.getParameter("name").toLowerCase().replace('_', '-') + ".xls", false);
return out;
}

}
2 changes: 2 additions & 0 deletions WebContent/help/Release-Notes.xml
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
<line>Added Conflicting Course Requests report: variant of Not-Assigned Course Requests report, but only showing course requests with time conflict.</line>
<line>Added Not-Used Curriculum Reservations report.</line>
<line>Added Reservations report: showing all reservations and their current enrollments / limits.</line>
<line>Added XLS export.</line>
</description>
</item>
<item>
Expand All @@ -86,6 +87,7 @@
<line>Show flags for curriculum override reservations.</line>
<line>Individual, Curriculum: Limit the number of restrictions to 5 lines.</line>
</line>
<line>Added XLS export.</line>
</description>
</item>
</category>
Expand Down

0 comments on commit 26d458f

Please sign in to comment.