Alejandro Tapia Lazcano wrote:
Hello!
Hello,
I have a grid made with tapestry5 and I need to pass this data to a
CSV (comma separated values).
After searching I found this component but for T4.
http://www.tapestrycomponents.org/Tassel/app?service=direct/1/Search/viewComponent&sp=SmschnyderCSV+Export
It is possible to migrate this T4 to T5?
No.
How??
I need the guide "Migrating components T4 to T5 for dummies" jajajaja
There is no easy migration path, see
http://tapestry.apache.org/tapestry5/ , Backwards Compatibility.
So, you have a list of elements (say, a list of map) and you want to
export there values to CSV ?
Just create a simple T5 page that return "onActivate()" a StreamResponse :
(note after writting the code : the code is not very clean (awful
cpoy/paste), I wrote it straight in the mail, so it should contains some
mistakes and mispellings and that sort of things :).
But it's just an example, and event if I haven't tested it, I really
think it should work. The main ideas are here, I think
Obviously, it would be better to make a component that takes as
parameter a list of beans, a bean model, etc. I let
it to you as an exercise ;P ).
8<--------------------
public class CsvExport {
public static final String CSV_CONTENT_TYPE = "text/plain";
public static final String DEFAULT_ATTRIBUTE_SEPARATOR = ",";
private static final String linebreak =
System.getProperty("line.separator");
//the list of element to export to CSV
private List<Map<String, String>> elements;
//the list of value names to export : ex: name,age
private List<String> colomns;
//the main response, that will export to csv your values
public StreamResponse onActivate() {
if(null == values || null == colomns || colomns.size() < 1) {
return new TextStreamResponse(CSV_CONTENT_TYPE, "");
}
csvLines = new StringBuilder();
//print the first line of the CSV
for(String header : colomns) {
csvLines.append(header).append(DEFAULT_ATTRIBUTE_SEPARATOR);
}
csvLines.deleteCharAt(csvLines.length()-1);
csvLines.append(linebreak);
//loop over lines and print values
for(Map<String,String> values : elements) {
for(String header : colomns) {
csvLines.append(values.get(header)).append(DEFAULT_ATTRIBUTE_SEPARATOR);
}
csvLines.deleteCharAt(csvLines.length()-1);
csvLines.append(linebreak);
}
//ok, so now we have the file to return, so we return it.
return new StreamResponse() {
public String getContentType() {
return CSV_CONTENT_TYPE;
}
public InputStream getStream() throws IOException {
return new
ByteArrayInputStream(csvLines.toString().getBytes());
}
public void prepareResponse(Response response) {
response.setContentLength(csvLines.length());
response.setHeader("Content-disposition", "attachment;
filename=myCSVexortedFile.csv");
response.setHeader("Pragma","no-cache");
response.setHeader("Cache-Control","must-revalidate,
post-check=0, pre-check=0, public");
response.setIntHeader("Expires", 0);
}
};
}
}
8<---------------------
Hope it will help you.
--
Francois Armand
Etudes & Développements J2EE
Groupe Linagora - http://www.linagora.com
Tél.: +33 (0)1 58 18 68 28
-----------
InterLDAP - http://interldap.org
FederID - http://www.federid.org/
Open Source identities management and federation
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]