Use the below sample code to register a Java Snippet that could be used to generate a CSV file for user download.

Step 1: Define a Java Snippet and give it a name and write similar code as displayed below. Note: make sure to change the parameter name, Datasource name etc. based on your needs.



Step 2: Define a button or link and add the below ActionLink with Link Tlken as "/hs/js/<jsName>?paramName={DatasourceName.attributeName}". With this, when the user click on the button, a csv file will be downloaded to the user's desktop.



import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.rapapp.platform.client.mdata.RPCModelData;
import org.apache.commons.io.FileUtils;
import java.io.*;
import java.util.*;
import java.text.SimpleDateFormat;

String encodeString(String val) {
if(val != null && val.trim().length() > 0) {
if(val.indexOf("\"") > -1) {
val = val.replaceAll("\"","/\"");
}
}
val = "\""+val+"\"";
return val;
}

String fileName = "/tmp/_hs"+ db.getUserId().longValue()+".csv";
FileWriter writer = new FileWriter(fileName);

String paramId = request.getParameter("paramId");
static final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
try{
Map<String,Object> map = new HashMap<String,Object>();
map.put("paramId",Double.parseDouble(paramId));

List<RAObjectAttribute> objectAttributes = db.getRAObject("DatasourceName").getAttrList();
int size = objectAttributes.size();
List<RPCModelData> rows = db.getObjectsByNameValues("DatasourceName",map,0,5000);
Iterator<RPCModelData> ite = rows.listIterator();

for(int counter = 0; counter < size; counter++) {
String key = objectAttributes.get(counter).getAttributeName();
if(counter != 0) {
writer.append(",");
}
writer.append(key);
}
writer.append("\n");
String value = null, key;
Object _value = null;
while(ite.hasNext()) {
RPCModelData row = ite.next();

for(int counter = 0; counter < size; counter++) {
key = objectAttributes.get(counter).getAttributeCode();
_value = row.get(key);

if(counter != 0) {
writer.append(",");
}

if(_value instanceof String) {
value = encodeString((String)_value);
writer.append(value);
} else if(_value instanceof Date) {
writer.append(sdf.format(_value));
} else if(_value instanceof Double) {
value = _value.toString();
if (value.endsWith(".0")) {
value = value.substring(0, value.length() - 2);
}
writer.append(value);
} else if(_value != null){
writer.append(_value.toString());
}else{
// null so ignore
// writer.append("");
}
}
writer.append("\n");
}

writer.flush(); writer.close();
File f = new File(fileName);
String contentDisposition = "attachment; filename=FileName.csv";
response.setHeader("Content-Disposition", contentDisposition);
response.setContentType("text/csv");
response.getOutputStream().write(FileUtils.readFileToByteArray(f));
f.delete();
}catch(Exception exp) {
response.getWriter().write("Error generating csv file: "+exp.getMessage());
}

Location

101 California Street, Suite 2710
San Francisco, CA 94111


440 N. Wolfe Rd.
Sunnyvale, CA 94085


Office 11, 5th Floor, Building 9,
Mindspace IT Park
Hyderabad 500081

p: +1 (844) AT CloudIO (844-282-5683)
f: +1 (650) 300-5247 | e: sales@cloudio.io
© 2009, 2017 CloudIO Inc. | Terms of Use | Privacy Policy