ref 12119:AccountingDashboard - Create a new Accounting Dashboard

https://support.d4science.org/issues/12119

Updated Export file support. Added CSV file export.

git-svn-id: https://svn.d4science.research-infrastructures.eu/gcube/trunk/portlets/user/accounting-dashboard@169855 82a268e6-3cf1-43bd-a215-b396298e98cf
This commit is contained in:
Giancarlo Panichi 2018-07-19 15:59:37 +00:00
parent 713dc78c52
commit 842d36cc93
7 changed files with 205 additions and 80 deletions

View File

@ -28,6 +28,7 @@ public class MainAreaPresenter
extends Presenter<MainAreaPresenter.MainAreaView, MainAreaPresenter.MainAreaPresenterProxy>
implements MainAreaUiHandlers {
@SuppressWarnings("unused")
private static Logger logger = Logger.getLogger("");
interface MainAreaView extends View, HasUiHandlers<MainAreaPresenter> {
@ -43,7 +44,9 @@ public class MainAreaPresenter
public static final SingleSlot<FilterAreaPresenter> SLOT_FILTER = new SingleSlot<>();
public static final SingleSlot<ReportAreaPresenter> SLOT_REPORT = new SingleSlot<>();
@SuppressWarnings("unused")
private EventBus eventBus;
@SuppressWarnings("unused")
private Controller controller;
private FilterAreaPresenter filterAreaPresenter;
private ReportAreaPresenter reportAreaPresenter;

View File

@ -19,6 +19,7 @@ import com.gwtplatform.mvp.client.ViewWithUiHandlers;
*/
public class MainAreaView extends ViewWithUiHandlers<MainAreaPresenter> implements MainAreaPresenter.MainAreaView {
@SuppressWarnings("unused")
private static Logger logger = java.util.logging.Logger.getLogger("");
interface Binder extends UiBinder<Widget, MainAreaView> {
@ -31,6 +32,7 @@ public class MainAreaView extends ViewWithUiHandlers<MainAreaPresenter> implemen
HTMLPanel reportPanel;
@SuppressWarnings("unused")
private AppResources resources;
@Inject

View File

@ -9,6 +9,7 @@ import org.gcube.portlets.user.accountingdashboard.client.application.event.Scop
import org.gcube.portlets.user.accountingdashboard.shared.data.RequestReportData;
import org.gcube.portlets.user.accountingdashboard.shared.data.ScopeData;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.inject.Inject;
import com.google.web.bindery.event.shared.EventBus;
import com.gwtplatform.mvp.client.HasUiHandlers;
@ -31,6 +32,7 @@ public class FilterAreaPresenter extends PresenterWidget<FilterAreaPresenter.Fil
}
@SuppressWarnings("unused")
private EventBus eventBus;
private Controller controller;
@ -77,8 +79,32 @@ public class FilterAreaPresenter extends PresenterWidget<FilterAreaPresenter.Fil
}
private boolean checkDate(Date dateStart, Date dateEnd) {
if (dateStart.compareTo(dateEnd) > 0) {
private boolean checkDate(String dateStart, String dateEnd) {
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-MM-dd");
Date dateStartD = null;
try {
logger.fine("DateTemp1: " + dateStart);
dateStartD = dateTimeFormat.parse(dateStart);
logger.fine("DateStart: " + dateStartD);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error in start date: " + e.getLocalizedMessage(), e);
return false;
}
Date dateEndD = null;
try {
logger.fine("DateTemp2: " + dateEnd);
dateEndD = dateTimeFormat.parse(dateEnd);
logger.fine("DateEnd: " + dateEndD);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error in end date: " + e.getLocalizedMessage(), e);
return false;
}
if (dateStartD.compareTo(dateEndD) > 0) {
controller.errorShow("Attention the start date must be less than the end date!");
return false;
} else {

View File

@ -28,9 +28,7 @@ import com.google.gwt.user.cellview.client.CellTree;
import com.google.gwt.user.cellview.client.TreeNode;
import com.google.gwt.user.client.ui.HTMLPanel;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.user.datepicker.client.CalendarUtil;
import com.google.gwt.view.client.ListDataProvider;
import com.gwtplatform.mvp.client.ViewWithUiHandlers;
/**
@ -97,9 +95,6 @@ public class FilterAreaView extends ViewWithUiHandlers<FilterAreaPresenter>
for (int i = 2015; i <= year; i++) {
yearStart.addItem(String.valueOf(i));
yearEnd.addItem(String.valueOf(i));
if (i == year) {
}
}
yearStart.setSelectedValue(String.valueOf(year));
yearEnd.setSelectedValue(String.valueOf(year));
@ -130,7 +125,7 @@ public class FilterAreaView extends ViewWithUiHandlers<FilterAreaPresenter>
monthEnd = new ListBox();
monthEnd.setMultipleSelect(false);
for (int i = 0; i < months.length; i++) {
monthStart.addItem(months[i]);
monthEnd.addItem(months[i]);
@ -192,37 +187,44 @@ public class FilterAreaView extends ViewWithUiHandlers<FilterAreaPresenter>
}
private void requestReport() {
DateTimeFormat dateTimeFormat = DateTimeFormat.getFormat("yyyy-M-dd");
String[] months = LocaleInfo.getCurrentLocale().getDateTimeFormatInfo().monthsFull();
logger.log(Level.FINE, "Months: " + months.length);
String yearS = yearStart.getValue();
String monthS = monthStart.getValue();
Date dateStart = null;
try {
String dStart = yearS + "-" + monthS + "-01";
logger.fine("DateTemp1: " + dStart);
dateStart = dateTimeFormat.parse(dStart);
logger.fine("DateStart: " + dStart);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error in start date: " + e.getLocalizedMessage(), e);
String monthSN=null;
for(int i=0; i<months.length; i++){
if(months[i].compareTo(monthS)==0){
int v=i+1;
if(v>9){
monthSN=""+v;
} else {
monthSN="0"+v;
}
}
}
String dateStart = yearS + "-" + monthSN + "-01";
logger.fine("DateStart: " + dateStart);
String yearE = yearEnd.getValue();
String monthE = monthEnd.getValue();
Date dateEnd = null;
try {
String dEnd = yearE + "-" + monthE + "-01";
logger.fine("DateTemp2: " + dEnd);
dateEnd = dateTimeFormat.parse(dEnd);
CalendarUtil.addMonthsToDate(dateEnd, 1);
CalendarUtil.addDaysToDate(dateEnd, -1);
logger.fine("DateEnd: " + dateEnd);
} catch (Exception e) {
logger.log(Level.SEVERE, "Error in end date: " + e.getLocalizedMessage(), e);
String monthEN=null;
for(int i=0; i<months.length; i++){
if(months[i].compareTo(monthE)==0){
int v=i+1;
if(v>9){
monthEN=""+v;
} else {
monthEN="0"+v;
}
}
}
String dateEnd = yearE + "-" + monthEN + "-01";
logger.fine("DateEnd: " + dateEnd);
RequestReportData requestReportData = new RequestReportData(scopeData, dateStart, dateEnd);
getUiHandlers().getReport(requestReportData);

View File

@ -21,11 +21,13 @@ public class Chart extends HTMLPanel {
+ "type='button' style='float:right;' data-toggle='dropdown'><span class='"
+ resources.uiDataCss().uiDataIconSettings() + "'></span>" + "<span class='caret'></span>" + "</button>"
+ "<ul class='" + resources.uiDataCss().uiDataChartMenuPosition() + " dropdown-menu'>" + "<li><a id='"
+ name + "_ExportPNG' href='#' download='" + reportElementData.getLabel() + ".png'>Export PNG</a>"
+ "</li>" + "<li><a id='" + name + "_ExportJPEG' href='#' download='" + reportElementData.getLabel()
+ ".jpeg'>Export JPEG</a>" + "</li>" + "<li><a id='" + name + "_ExportPDF' href='#' download='"
+ reportElementData.getLabel() + ".pdf'>Export PDF</a>" + "</li>" + "</ul>" + "</div>" + "<canvas id="
+ name + " class='" + resources.uiDataCss().uiDataChartCanvas() + "'></canvas>");
+ name + "_ExportJPEG' href='#' download='" + reportElementData.getLabel() + ".jpeg'>Export JPEG</a>"
+ "</li>" + "<li><a id='" + name + "_ExportPNG' href='#' download='" + reportElementData.getLabel()
+ ".png'>Export PNG</a>" + "</li>" + "<li><a id='" + name + "_ExportPDF' href='#' download='"
+ reportElementData.getLabel() + ".pdf'>Export PDF</a>" + "</li>" + "<li><a id='" + name
+ "_ExportCSV' href='#' download='" + reportElementData.getLabel() + ".csv'>Export CSV</a>" + "</li>"
+ "</ul>" + "</div>" + "<canvas id=" + name + " class='" + resources.uiDataCss().uiDataChartCanvas()
+ "'></canvas>");
this.name = name;
this.reportElementData = reportElementData;
this.addStyleName(resources.uiDataCss().uiDataChartWrapper());
@ -70,13 +72,6 @@ public class Chart extends HTMLPanel {
console.log('This: ' + this);
var name = this.@org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::name;
//Export PNG
var elementNamePNG = name + '_ExportPNG';
console.log('ExportPNG search: ' + elementNamePNG);
var exportPNGElement = $doc.getElementById(elementNamePNG);
console.log('ExportPNGElement: ' + exportPNGElement);
exportPNGElement.onclick = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::saveImagePNG(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;)(this);
//Export JPEG
var elementNameJPEG = name + '_ExportJPEG';
console.log('ExportJPEG search: ' + elementNameJPEG);
@ -84,6 +79,13 @@ public class Chart extends HTMLPanel {
console.log('ExportJPEGElement: ' + exportJPEGElement);
exportJPEGElement.onclick = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::saveImageJPEG(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;)(this);
//Export PNG
var elementNamePNG = name + '_ExportPNG';
console.log('ExportPNG search: ' + elementNamePNG);
var exportPNGElement = $doc.getElementById(elementNamePNG);
console.log('ExportPNGElement: ' + exportPNGElement);
exportPNGElement.onclick = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::saveImagePNG(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;)(this);
//Export PDF
var elementNamePDF = name + '_ExportPDF';
console.log('ExportPDF search: ' + elementNamePDF);
@ -91,6 +93,38 @@ public class Chart extends HTMLPanel {
console.log('ExportPDFElement: ' + exportPDFElement);
exportPDFElement.onclick = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::saveFilePDF(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;Ljava/lang/String;)(this,exportPDFElement.download);
//Export CSV
var elementNameCSV = name + '_ExportCSV';
console.log('ExportCSV search: ' + elementNameCSV);
var exportCSVElement = $doc.getElementById(elementNameCSV);
console.log('ExportCSVElement: ' + exportCSVElement);
exportCSVElement.onclick = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::saveFileCSV(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;)(this);
}-*/;
private static native void saveImageJPEG(Chart chart)/*-{
console.log('saveImageJPEG()');
var name = chart.@org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::name;
console.log('Element name: ' + name);
var canvas = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::getCanvas(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;)(chart);
return function() {
console.log('Save file jpeg');
//Set Background White
var w = canvas.width;
var h = canvas.height;
var ctx = canvas.getContext('2d');
var data = ctx.getImageData(0, 0, w, h);
var compositeOperation = ctx.globalCompositeOperation;
ctx.globalCompositeOperation = "destination-over";
ctx.fillStyle = '#ffffff';
ctx.fillRect(0, 0, w, h);
var image = canvas.toDataURL("image/jpeg").replace("image/jpeg",
"image/octet-stream");
ctx.clearRect(0, 0, w, h);
ctx.putImageData(data, 0, 0);
ctx.globalCompositeOperation = compositeOperation;
this.href = image;
};
}-*/;
private static native void saveImagePNG(Chart chart)/*-{
@ -102,26 +136,11 @@ public class Chart extends HTMLPanel {
console.log('Save file png');
var image = canvas.toDataURL("image/png").replace("image/png",
"image/octet-stream");
console.log('Image url: ' + image);
this.href = image;
//window.open(image, '_blank');
};
}-*/;
private static native void saveImageJPEG(Chart chart)/*-{
console.log('saveImageJPEG()');
var name = chart.@org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::name;
console.log('Element name: ' + name);
var canvas = @org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::getCanvas(Lorg/gcube/portlets/user/accountingdashboard/client/application/mainarea/report/chartjs/Chart;)(chart);
return function() {
console.log('Save file jpeg');
var image = canvas.toDataURL("image/jpeg").replace("image/jpeg",
"image/octet-stream");
console.log('Image url: ' + image);
this.href = image;
};
}-*/;
private static native void saveFilePDF(Chart chart, String filename)/*-{
console.log('saveFilePDF()');
var name = chart.@org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::name;
@ -136,12 +155,54 @@ public class Chart extends HTMLPanel {
doc.addImage(image, 'JPEG', 10, 10, 280, 150);
doc.save(filename);
return false;
//console.log('Image url: ' + image);
//this.href = image;
};
}-*/;
private static native void saveFileCSV(Chart chart)/*-{
console.log('saveFileCSV()');
var reportElementData = chart.@org.gcube.portlets.user.accountingdashboard.client.application.mainarea.report.chartjs.Chart::reportElementData;
console.log('ReportElementData: ' + reportElementData);
return function() {
console.log('Save file csv');
var serieses = reportElementData.getSerieses();
console.log('Serieses: ' + serieses);
var seriesesLen = serieses.length;
console.log('Serieses lenght: ' + seriesesLen);
var csvContent = "data:text/csv;charset=utf-8,";
for (var i = 0; i < seriesesLen; i++) {
var seriesData = serieses[i];
var dataRow = seriesData.getDataRow();
var dataRowLen = dataRow.length;
var dataArray = [];
if (i == 0) {
var heading = "Date";
var datasetRow = seriesData.getLabel();
for (var j = 0; j < dataRowLen; j++) {
var recordData = dataRow[j];
heading += "," + recordData.getX();
datasetRow += "," + recordData.getY();
}
csvContent += heading + "\r\n";
csvContent += datasetRow + "\r\n";
} else {
var datasetRow = seriesData.getLabel();
for (var j = 0; j < dataRowLen; j++) {
var recordData = dataRow[j];
datasetRow += "," + recordData.getY();
}
csvContent += datasetRow + "\r\n";
}
}
this.href = csvContent;
};
}-*/;
private native void redrawChart() /*-{
console.log('RedrawChart()');
@ -190,7 +251,6 @@ public class Chart extends HTMLPanel {
console.log('ReportElementData: ' + reportElementData);
var serieses = reportElementData.getSerieses();
console.log('Serieses: ' + serieses);
var seriesesLen = serieses.length;
console.log('Serieses lenght: ' + seriesesLen);
@ -242,8 +302,6 @@ public class Chart extends HTMLPanel {
var yAxisLabel = reportElementData.getyAxis();
var ctx = canvas.getContext('2d');
ctx.fillStyle = 'white';
ctx.fillRect(0, 0, canvas.width, canvas.height);
console.log('Create chart');
var chart = new Chart(ctx, {

View File

@ -1,7 +1,7 @@
package org.gcube.portlets.user.accountingdashboard.server.accounting;
import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import javax.servlet.http.HttpServletRequest;
@ -20,6 +20,8 @@ import org.gcube.portlets.user.accountingdashboard.shared.exception.ServiceExcep
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author Giancarlo Panichi
@ -116,12 +118,44 @@ public class AccountingService {
ScopeDescriptor scopeDescriptor = searchScopeDescriptor(httpServletRequest,
requestReportData.getScopeData());
Date dateFrom = requestReportData.getDateFrom();
Date dateTo = requestReportData.getDateTo();
String dateStart = requestReportData.getDateFrom();
String dateEnd = requestReportData.getDateTo();
logger.debug("getReportByScope(): [ScopeDescriptor=" + scopeDescriptor + ", dateFrom=" + dateFrom
+ ", dateTo=" + dateTo + ", measureResolution=" + MeasureResolution.MONTHLY + "]");
Report report = dao.getReportByScope(scopeDescriptor, dateFrom, dateTo, MeasureResolution.MONTHLY);
if (dateStart == null || dateStart.isEmpty() || dateEnd == null || dateEnd.isEmpty()) {
logger.error("Invalid date: [dateStart=" + dateStart + ", dateEnd=" + dateEnd + "]");
throw new ServiceException(
"Invalid format: [dateStart=" + dateStart + ", dateEnd=" + dateEnd + "]");
}
GregorianCalendar dateFrom;
GregorianCalendar dateTo;
try {
int yearS = Integer.parseInt(dateStart.substring(0, 4));
logger.debug("yearS: " + yearS);
int monthS = Integer.parseInt(dateStart.substring(5, 7)) - 1;
logger.debug("monthS: " + monthS);
int dayS = Integer.parseInt(dateStart.substring(8, 10));
logger.debug("dayS: " + dayS);
dateFrom = new GregorianCalendar(yearS, monthS, dayS);
int yearE = Integer.parseInt(dateEnd.substring(0, 4));
int monthE = Integer.parseInt(dateEnd.substring(5, 7)) - 1;
int dayE = Integer.parseInt(dateEnd.substring(8, 10));
dateTo = new GregorianCalendar(yearE, monthE, dayE);
} catch (Throwable e) {
logger.error("Invalid date format: [dateStart=" + dateStart + ", dateEnd=" + dateEnd + "]");
throw new ServiceException(
"Invalid date format: [dateStart=" + dateStart + ", dateEnd=" + dateEnd + "]");
}
logger.debug("getReportByScope(): [ScopeDescriptor=" + scopeDescriptor + ", dateFrom="
+ dateFrom.getTime() + ", dateTo=" + dateTo.getTime() + ", measureResolution="
+ MeasureResolution.MONTHLY + "]");
Report report = dao.getReportByScope(scopeDescriptor, dateFrom.getTime(), dateTo.getTime(),
MeasureResolution.MONTHLY);
logger.debug("Report: " + report);
ReportDataBuilder reportDataBuilder = new ReportDataBuilder(report);
@ -180,7 +214,8 @@ public class AccountingService {
int i) throws ServiceException {
try {
for (ScopeDescriptor child : scopeDescriptor.getChildren()) {
if (child.getName()!=null&&!child.getName().isEmpty()&&child.getName().compareTo(searchPath[i]) == 0) {
if (child.getName() != null && !child.getName().isEmpty()
&& child.getName().compareTo(searchPath[i]) == 0) {
i++;
if (i < searchPath.length) {
return searchInChild(child, scopeToSearch, searchPath, i);

View File

@ -1,7 +1,6 @@
package org.gcube.portlets.user.accountingdashboard.shared.data;
import java.io.Serializable;
import java.util.Date;
/**
*
@ -12,14 +11,14 @@ public class RequestReportData implements Serializable {
private static final long serialVersionUID = -7428707426843173730L;
private ScopeData scopeData;
private Date dateFrom;
private Date dateTo;
private String dateFrom;
private String dateTo;
public RequestReportData() {
super();
}
public RequestReportData(ScopeData scopeData, Date dateFrom, Date dateTo) {
public RequestReportData(ScopeData scopeData, String dateFrom, String dateTo) {
super();
this.scopeData = scopeData;
this.dateFrom = dateFrom;
@ -34,19 +33,19 @@ public class RequestReportData implements Serializable {
this.scopeData = scopeData;
}
public Date getDateFrom() {
public String getDateFrom() {
return dateFrom;
}
public void setDateFrom(Date dateFrom) {
public void setDateFrom(String dateFrom) {
this.dateFrom = dateFrom;
}
public Date getDateTo() {
public String getDateTo() {
return dateTo;
}
public void setDateTo(Date dateTo) {
public void setDateTo(String dateTo) {
this.dateTo = dateTo;
}