details: /erp/devel/pi/rev/4ae0d87df0ce
changeset: 9284:4ae0d87df0ce
user: Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date: Tue Dec 21 18:17:17 2010 +0100
summary: [OBSERDS] Added ablity to export to csv using exportToFile
requestProperty.
details: /erp/devel/pi/rev/18eb4167ed6a
changeset: 9285:18eb4167ed6a
user: Gorka Ion Damián <gorkaion.damian <at> openbravo.com>
date: Tue Dec 21 18:18:40 2010 +0100
summary: [OBCQL] Export grid to csv.
diffstat:
modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java
| 22 ++-
modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListUtils.java
| 13 +-
modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js
| 31 ++--
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
| 65 +++++++++-
4 files changed, 102 insertions(+), 29 deletions(-)
diffs (276 lines):
diff -r cc25fcd18717 -r 18eb4167ed6a
modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java
---
a/modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java
Tue Dec 21 16:08:28 2010 +0100
+++
b/modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListDataSource.java
Tue Dec 21 18:18:40 2010 +0100
@@ -29,7 +29,6 @@
import org.openbravo.client.application.ParameterValue;
import org.openbravo.client.myob.WidgetClass;
import org.openbravo.client.myob.WidgetInstance;
-import org.openbravo.client.querylist.QueryListUtils.IncludeIn;
import org.openbravo.dal.core.OBContext;
import org.openbravo.dal.service.OBDal;
import org.openbravo.service.datasource.ReadOnlyDataSourceService;
@@ -64,19 +63,18 @@
try {
WidgetInstance widgetInstance =
OBDal.getInstance().get(WidgetInstance.class,
parameters.get("widgetInstanceId"));
+ boolean isExport = "true".equals(parameters.get("exportToFile"));
String viewMode = parameters.get("viewMode");
WidgetClass widgetClass = widgetInstance.getWidgetClass();
- // WidgetInstance widgetInstance =
widgetClass.getOBKMOWidgetInstanceList().get(0);
- IncludeIn includeIn = QueryListUtils.IncludeIn.WidgetView;
- List<OBCQL_QueryColumn> columns = QueryListUtils.getColumns(widgetClass
- .getOBCQLWidgetQueryList().get(0), includeIn);
+
Query widgetQuery = OBDal.getInstance().getSession().createQuery(
widgetClass.getOBCQLWidgetQueryList().get(0).getHQL());
String[] queryAliases = widgetQuery.getReturnAliases();
- if ("widget".equals(viewMode)) {
+
+ if (!isExport && "widget".equals(viewMode)) {
int rowsNumber = Integer.valueOf(parameters.get("rowsNumber"));
widgetQuery.setMaxResults(rowsNumber);
- } else {
+ } else if (!isExport) {
if (startRow > 0) {
widgetQuery.setFirstResult(startRow);
}
@@ -84,6 +82,7 @@
widgetQuery.setMaxResults(endRow - startRow + 1);
}
}
+
String[] params = widgetQuery.getNamedParameters();
if (params.length > 0) {
HashMap<String, Object> parameterValues =
getParameterValues(parameters, widgetInstance);
@@ -98,10 +97,12 @@
if (!isParamSet) {
// TODO: throw an exception
}
-
}
}
+ List<OBCQL_QueryColumn> columns = QueryListUtils.getColumns(widgetClass
+ .getOBCQLWidgetQueryList().get(0));
+
final List<Map<String, Object>> result = new ArrayList<Map<String,
Object>>();
for (Object objResult : widgetQuery.list()) {
final Map<String, Object> data = new HashMap<String, Object>();
@@ -115,7 +116,10 @@
for (OBCQL_QueryColumn column : columns) {
// TODO: throw an exception if the display expression doesn't match
any returned alias.
for (int i = 0; i < queryAliases.length; i++) {
- data.put(queryAliases[i], resultList[i]);
+ if (queryAliases[i].equals(column.getDisplayExpression())
+ || (!isExport &&
queryAliases[i].equals(column.getLinkExpression()))) {
+ data.put(queryAliases[i], resultList[i]);
+ }
}
}
result.add(data);
diff -r cc25fcd18717 -r 18eb4167ed6a
modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListUtils.java
---
a/modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListUtils.java
Tue Dec 21 16:08:28 2010 +0100
+++
b/modules/org.openbravo.client.querylist/src/org/openbravo/client/querylist/QueryListUtils.java
Tue Dec 21 18:18:40 2010 +0100
@@ -50,7 +50,7 @@
final List<JSONObject> jsonFields = new ArrayList<JSONObject>();
if (!widgetClass.getOBCQLWidgetQueryList().isEmpty()) {
for (OBCQL_QueryColumn column : QueryListUtils.getColumns(widgetClass
- .getOBCQLWidgetQueryList().get(0), includeIn)) {
+ .getOBCQLWidgetQueryList().get(0))) {
final JSONObject field = new JSONObject();
final Reference reference;
if (column.getReferenceSearchKey() != null) {
@@ -116,6 +116,13 @@
field.put("summaryFunction", column.getSummarizeType());
}
+ field.put("canExport", true);
+ if ("E".equals(column.getIncludeIn())
+ || ("M".equals(column.getIncludeIn()) &&
includeIn.equals(IncludeIn.WidgetView))) {
+ field.put("hidden", true);
+ field.put("showIf", "false");
+ }
+
try {
final String fieldProperties =
uiDefinition.getFieldProperties(null);
if (fieldProperties != null && fieldProperties.trim().length() >
0) {
@@ -142,12 +149,10 @@
}
- public static List<OBCQL_QueryColumn> getColumns(OBCQL_WidgetQuery query,
IncludeIn includeIn) {
+ public static List<OBCQL_QueryColumn> getColumns(OBCQL_WidgetQuery query) {
OBCriteria<OBCQL_QueryColumn> obcColumns =
OBDal.getInstance().createCriteria(
OBCQL_QueryColumn.class);
obcColumns.add(Expression.eq(OBCQL_QueryColumn.PROPERTY_WIDGETQUERY,
query));
- obcColumns.add(Expression.in(OBCQL_QueryColumn.PROPERTY_INCLUDEIN,
includeIn
- .getIncludedValues()));
obcColumns.addOrderBy(OBCQL_QueryColumn.PROPERTY_SEQUENCENUMBER, true);
return obcColumns.list();
}
diff -r cc25fcd18717 -r 18eb4167ed6a
modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js
---
a/modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js
Tue Dec 21 16:08:28 2010 +0100
+++
b/modules/org.openbravo.client.querylist/web/org.openbravo.client.querylist/js/ob-querylist-widget.js
Tue Dec 21 18:18:40 2010 +0100
@@ -33,12 +33,12 @@
initWidget: function(){
this.Super('initWidget', arguments);
- // Calculate heigth
+ // Calculate height
var currentHeight = this.getHeight(),
//currentBodyHeight = this.body.getHeight(),
headerHeight = this.headerDefaults.height,
newGridHeight = this.grid.headerHeight
- + (this.grid.cellHeight * this.rowsNumber)
+ + (this.grid.cellHeight * (this.rowsNumber ?
this.rowsNumber : 10))
+ this.grid.summaryRowHeight
+ 2;
@@ -80,25 +80,21 @@
reloadGrid: function(rpcResponse, data, rpcRequest) {
- this.fields = data.fields;
- this.grid.setDataSource(this.grid.dataSource);
this.grid.invalidateCache();
this.grid.filterData();
-// this.grid.destroy();
-// this.grid = isc.OBQueryListGrid.create(isc.addProperties({
-// widget: this,
-// fields: this.fields
-// }, this.gridProperties));
- this.markForRedraw();
},
exportGrid: function() {
var grid = this.widget.grid;
- grid.exportData({
- exportAs: 'csv',
- // exportFilename: 'Query/List_widget.csv',
- exportDisplay: 'download'
- });
+ var requestProperties = {
+ exportAs: 'csv',
+ //exportFilename: 'Query/List_widget.csv',
+ exportDisplay: 'download',
+ params: {
+ exportToFile: true
+ }
+ }
+ grid.exportData(requestProperties);
}
});
@@ -161,6 +157,11 @@
requestProperties = {};
}
requestProperties.showPrompt = false;
+ if (requestProperties.params.exportToFile) {
+ this.dataSource.dataFormat = requestProperties.exportAs;
+ } else {
+ this.dataSource.dataFormat = 'json';
+ }
criteria.widgetInstanceId = this.widget.dbInstanceId;
criteria.rowsNumber = this.widget.rowsNumber;
criteria.viewMode = this.widget.viewMode;
diff -r cc25fcd18717 -r 18eb4167ed6a
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
---
a/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
Tue Dec 21 16:08:28 2010 +0100
+++
b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
Tue Dec 21 18:18:40 2010 +0100
@@ -20,10 +20,13 @@
import java.io.BufferedReader;
import java.io.IOException;
+import java.io.PrintWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.HashMap;
+import java.util.Iterator;
import java.util.Map;
+import java.util.Vector;
import javax.inject.Inject;
import javax.servlet.ServletConfig;
@@ -31,7 +34,9 @@
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
+import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
+import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.openbravo.base.exception.OBException;
@@ -161,7 +166,19 @@
// now do the action
String result = getDataSource(request).fetch(parameters);
- writeResult(response, result);
+
+ boolean isExport = "true".equals(parameters.get("exportToFile"));
+ if (isExport) {
+ String exportFormat = parameters.get("exportFormat");
+ if (StringUtils.isEmpty(exportFormat)) {
+ exportFormat = "csv";
+ }
+ if ("csv".equals(exportFormat)) {
+ writeResultCSV(response, result);
+ }
+ } else {
+ writeResult(response, result);
+ }
} catch (Exception e) {
handleException(e, response);
}
@@ -402,6 +419,52 @@
w.close();
}
+ private void writeResultCSV(HttpServletResponse response, String result)
throws IOException,
+ JSONException {
+ response.setContentType("text/csv; charset=UTF-8");
+ response.setHeader("Content-Disposition", "attachment;
filename=Results.csv");
+
+ JSONObject jsonResult = new JSONObject(result);
+ JSONArray data = jsonResult.getJSONObject("response").getJSONArray("data");
+
+ StringBuffer csv = new StringBuffer();
+ if (data.length() > 0) {
+ final JSONObject headerRow = data.getJSONObject(0);
+
+ final Iterator<?> itKeys = headerRow.keys();
+ Vector<String> keys = new Vector<String>();
+ boolean isFirst = true;
+ while (itKeys.hasNext()) {
+ if (isFirst) {
+ isFirst = false;
+ } else {
+ csv.append(",");
+ }
+ String key = (String) itKeys.next();
+ keys.add(key);
+ csv.append("'").append(key).append("'");
+ }
+
+ for (int i = 0; i < data.length(); i++) {
+ JSONObject row = data.getJSONObject(i);
+ csv.append("\n");
+ isFirst = true;
+ for (String key : keys) {
+ if (isFirst) {
+ isFirst = false;
+ } else {
+ csv.append(",");
+ }
+ csv.append("'").append(row.get(key)).append("'");
+ }
+ }
+ }
+
+ final PrintWriter pw = response.getWriter();
+ pw.write(csv.toString());
+ pw.close();
+ }
+
private String getRequestContent(HttpServletRequest request) throws
IOException {
final BufferedReader reader = request.getReader();
if (reader == null) {
------------------------------------------------------------------------------
Forrester recently released a report on the Return on Investment (ROI) of
Google Apps. They found a 300% ROI, 38%-56% cost savings, and break-even
within 7 months. Over 3 million businesses have gone Google with Google Apps:
an online email calendar, and document program that's accessible from your
browser. Read the Forrester report: http://p.sf.net/sfu/googleapps-sfnew
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits