details:   https://code.openbravo.com/erp/devel/pi/rev/fb05bf3da92e
changeset: 24074:fb05bf3da92e
user:      Víctor Martínez Romanos <victor.martinez <at> openbravo.com>
date:      Thu Jul 17 18:30:20 2014 +0200
summary:   Fixed bug 27116: Added header and footer to CSV

diffstat:

 
modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml
                  |  24 +++
 
modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
 |  72 +++++++++-
 2 files changed, 91 insertions(+), 5 deletions(-)

diffs (156 lines):

diff -r a87a28d7b2ca -r fb05bf3da92e 
modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml
--- 
a/modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml
       Fri Jul 18 11:56:39 2014 +0200
+++ 
b/modules/org.openbravo.service.datasource/src-db/database/sourcedata/AD_REF_LIST.xml
       Thu Jul 17 18:30:20 2014 +0200
@@ -12,6 +12,30 @@
 <!--62980B3E5545466D91CA8B2AC352AF72-->  
<AD_MODULE_ID><![CDATA[A44B9BA75C354D8FB2E3F7D6EB6BFDC4]]></AD_MODULE_ID>
 <!--62980B3E5545466D91CA8B2AC352AF72--></AD_REF_LIST>
 
+<!--F5786C56B31E46868866D16FB46EC3F9--><AD_REF_LIST>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  
<AD_REF_LIST_ID><![CDATA[F5786C56B31E46868866D16FB46EC3F9]]></AD_REF_LIST_ID>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  
<VALUE><![CDATA[OBSERDS_CSVFooterMessage]]></VALUE>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  <NAME><![CDATA[CSV Footer 
Message]]></NAME>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  <DESCRIPTION><![CDATA[This property 
allows the user to set a message's search key or a plain text. Either the 
translated message or the plain text (if not message is found) will be included 
as the CSV footer.]]></DESCRIPTION>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  
<AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
+<!--F5786C56B31E46868866D16FB46EC3F9-->  
<AD_MODULE_ID><![CDATA[A44B9BA75C354D8FB2E3F7D6EB6BFDC4]]></AD_MODULE_ID>
+<!--F5786C56B31E46868866D16FB46EC3F9--></AD_REF_LIST>
+
+<!--F6A42E6508D0481C8FB1EE5946D05106--><AD_REF_LIST>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  
<AD_REF_LIST_ID><![CDATA[F6A42E6508D0481C8FB1EE5946D05106]]></AD_REF_LIST_ID>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  <AD_ORG_ID><![CDATA[0]]></AD_ORG_ID>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  <ISACTIVE><![CDATA[Y]]></ISACTIVE>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  
<VALUE><![CDATA[OBSERDS_CSVHeaderMessage]]></VALUE>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  <NAME><![CDATA[CSV Header 
Message]]></NAME>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  <DESCRIPTION><![CDATA[This property 
allows the user to set a message's search key or a plain text. Either the 
translated message or the plain text (if not message is found) will be included 
as the CSV header.]]></DESCRIPTION>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  
<AD_REFERENCE_ID><![CDATA[A26BA480E2014707B47257024C3CBFF7]]></AD_REFERENCE_ID>
+<!--F6A42E6508D0481C8FB1EE5946D05106-->  
<AD_MODULE_ID><![CDATA[A44B9BA75C354D8FB2E3F7D6EB6BFDC4]]></AD_MODULE_ID>
+<!--F6A42E6508D0481C8FB1EE5946D05106--></AD_REF_LIST>
+
 <!--FF80808130DA75580130DAA2E8C5001B--><AD_REF_LIST>
 <!--FF80808130DA75580130DAA2E8C5001B-->  
<AD_REF_LIST_ID><![CDATA[FF80808130DA75580130DAA2E8C5001B]]></AD_REF_LIST_ID>
 <!--FF80808130DA75580130DAA2E8C5001B-->  
<AD_CLIENT_ID><![CDATA[0]]></AD_CLIENT_ID>
diff -r a87a28d7b2ca -r fb05bf3da92e 
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
      Fri Jul 18 11:56:39 2014 +0200
+++ 
b/modules/org.openbravo.service.datasource/src/org/openbravo/service/datasource/DataSourceServlet.java
      Thu Jul 17 18:30:20 2014 +0200
@@ -11,7 +11,7 @@
  * under the License. 
  * The Original Code is Openbravo ERP. 
  * The Initial Developer of the Original Code is Openbravo SLU 
- * All portions are Copyright (C) 2009-2013 Openbravo SLU 
+ * All portions are Copyright (C) 2009-2014 Openbravo SLU 
  * All Rights Reserved. 
  * Contributor(s):  ______________________________________.
  ************************************************************************
@@ -69,6 +69,7 @@
 import org.openbravo.dal.service.OBDal;
 import org.openbravo.database.SessionInfo;
 import org.openbravo.erpCommon.businessUtility.Preferences;
+import org.openbravo.erpCommon.utility.OBMessageUtils;
 import org.openbravo.erpCommon.utility.PropertyException;
 import org.openbravo.erpCommon.utility.PropertyNotFoundException;
 import org.openbravo.erpCommon.utility.Utility;
@@ -233,9 +234,10 @@
             OBContext.restorePreviousMode();
           }
           response.setHeader("Content-Disposition", "attachment; 
filename=ExportedData.csv");
+          QueryJSONWriterToCSV writer;
           if (getDataSource(request) instanceof DefaultDataSourceService) {
-            QueryJSONWriterToCSV writer = new QueryJSONWriterToCSV(request, 
response, parameters,
-                getDataSource(request).getEntity());
+            writer = new QueryJSONWriterToCSV(request, response, parameters, 
getDataSource(request)
+                .getEntity());
             // when exporting a OB grid, the isActive filter should not be set
             parameters.put(JsonConstants.NO_ACTIVE_FILTER, "true");
             ((DefaultDataSourceService) 
getDataSource(request)).fetch(parameters, writer);
@@ -243,12 +245,14 @@
             String result = getDataSource(request).fetch(parameters);
             JSONObject jsonResult = new JSONObject(result);
             JSONArray data = 
jsonResult.getJSONObject("response").getJSONArray("data");
-            QueryJSONWriterToCSV writer = new QueryJSONWriterToCSV(request, 
response, parameters,
-                getDataSource(request).getEntity());
+            writer = new QueryJSONWriterToCSV(request, response, parameters, 
getDataSource(request)
+                .getEntity());
             for (int i = 0; i < data.length(); i++) {
               writer.write(data.getJSONObject(i));
             }
           }
+
+          writer.writeCSVFooterNote(parameters);
         } else {
           log.error("Unsupported export format: " + exportAs);
         }
@@ -462,6 +466,8 @@
             refLists.put(propKey, reflists);
           }
         }
+
+        writeCSVHeaderNote(parameters);
         if (fieldProperties.size() > 0) {
           // If the request came with the view state information, we get the 
properties from there
           for (int i = 0; i < fieldProperties.size(); i++) {
@@ -640,6 +646,62 @@
 
       return calendar.getTime();
     }
+
+    private void writeCSVHeaderNote(Map<String, String> parameters) throws 
IOException,
+        PropertyException {
+      final String csvHeaderMsg = getMessage(parameters, 
"OBSERDS_CSVHeaderMessage");
+
+      if (StringUtils.isNotBlank(csvHeaderMsg)) {
+        writer.append("\"").append(csvHeaderMsg).append("\"");
+        fillEmptyColumns();
+        writer.append("\n");
+      }
+    }
+
+    private void writeCSVFooterNote(Map<String, String> parameters) throws 
IOException,
+        PropertyException {
+      final String csvFooterMsg = getMessage(parameters, 
"OBSERDS_CSVFooterMessage");
+
+      if (StringUtils.isNotBlank(csvFooterMsg)) {
+        writer.append("\n").append("\"").append(csvFooterMsg).append("\"");
+        fillEmptyColumns();
+      }
+    }
+
+    private String getMessage(final Map<String, String> parameters, final 
String property)
+        throws PropertyException {
+      OBContext.setAdminMode(true);
+      try {
+        String csvMessage = null;
+        try {
+          Window window = parameters.get("tab") == null
+              || parameters.get("tab").equals("undefined") ? null : 
OBDal.getInstance()
+              .get(Tab.class, parameters.get("tab")).getWindow();
+          csvMessage = Preferences.getPreferenceValue(property, true, 
OBContext.getOBContext()
+              .getCurrentClient(), 
OBContext.getOBContext().getCurrentOrganization(), OBContext
+              .getOBContext().getUser(), OBContext.getOBContext().getRole(), 
window);
+        } catch (PropertyNotFoundException e) {
+          // There is no preference defined
+          csvMessage = null;
+        }
+
+        if (StringUtils.isNotBlank(csvMessage)) {
+          csvMessage = Replace.replace(Replace.replace(
+              Replace.replace(OBMessageUtils.messageBD(csvMessage), "\\n", 
"\n"), "&quot;", "\""),
+              "\"", "\"\"");
+        }
+
+        return csvMessage;
+      } finally {
+        OBContext.restorePreviousMode();
+      }
+    }
+
+    private void fillEmptyColumns() throws IOException {
+      for (int i = 1; i < fieldProperties.size(); i++) {
+        writer.append(fieldSeparator);
+      }
+    }
   }
 
   private void handleException(Exception e, HttpServletResponse response) 
throws IOException {

------------------------------------------------------------------------------
Want fast and easy access to all the code in your enterprise? Index and
search up to 200,000 lines of code with a free copy of Black Duck
Code Sight - the same software that powers the world's largest code
search on Ohloh, the Black Duck Open Hub! Try it now.
http://p.sf.net/sfu/bds
_______________________________________________
Openbravo-commits mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/openbravo-commits

Reply via email to