I have created CSV service (I call it ExcelService) to do that and I pass to that service an OGNL expression to get the necessary resultset and list of OGNL expressions to get necessary attributes:
Than call to the service looks like this:
<a href="" jwcid="@CSVDownloadLink" csvSource="brokers" attributes="name,idNumber">CSV</a>


CSVDownloadLink.jwc
<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE component-specification PUBLIC
 "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
 "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd";>

<component-specification class="xxxx.CSVDownloadLink"
 allow-body="yes"
 allow-informal-parameters="yes">

<parameter name="pageName" required="no" type="java.lang.String" direction="in" />
<parameter name="csvSource" required="yes" type="java.lang.String" direction="auto" />
<parameter name="attributes" required="yes" type="java.lang.String" direction="auto" />


</component-specification>

public abstract class CSVDownloadLink extends AbstractComponent{

 public abstract String getPageName();
 public abstract String getCsvSource();
 public abstract String getAttributes();

protected void renderComponent(IMarkupWriter w, IRequestCycle cycle) {
if( cycle.isRewinding() ) return;
IEngineService srv = cycle.getEngine().getService( ExcelService.SERVICE_NAME );
String pageName = ( getPageName() == null)? cycle.getPage().getPageName(): getPageName();
ILink lnk = srv.getLink( cycle, this, new Object[]{ pageName, getCsvSource(),getAttributes()});
w.begin("a");
w.attribute("href", lnk.getURL() );
renderInformalParameters( w, cycle);
renderBody( w, cycle );
//w.closeTag();
w.end();


 }
}

and service itself:

import org.apache.tapestry.engine.AbstractService;
import org.apache.tapestry.engine.IEngineServiceView;
import org.apache.tapestry.engine.ILink;
import org.apache.tapestry.IRequestCycle;
import org.apache.tapestry.IComponent;
import org.apache.tapestry.request.ResponseOutputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Iterator;

import ognl.Ognl;
import ognl.OgnlException;
import com.Ostermiller.util.ExcelCSVPrinter;
import com.teamics.common.util.DataHelper;

/**
* //TODO: Describe purpose of the class
*/
public class ExcelService extends AbstractService{
 public static final String SERVICE_NAME = "ExcelService";

 public String getName(){
   return SERVICE_NAME;
 }

/**
* parameters[page,ognlExpressionToGetSource]
* @param iEngineServiceView
* @param cycle
* @param responseOutputStream
* @throws ServletException
* @throws IOException
*/
public void service( IEngineServiceView iEngineServiceView, IRequestCycle cycle, ResponseOutputStream responseOutputStream ) throws ServletException, IOException{
Object[] params = getParameters( cycle ) ;
String pageName = (String) params[0];
String srcExpression = (String) params[1];
String[] attributes = ((String) params[2]).split(",");
responseOutputStream.setContentType( "text/csv" );
HttpServletResponse response = cycle.getRequestContext().getResponse();
response.setHeader("Expires", "0");
response.setHeader("Cache-Control", "must-revalidate, post-check=0, pre-check=0");
response.setHeader("Pragma", "public");
response.setHeader("Content-Disposition", "attachment; filename=\"" + pageName +".csv\"");


try {
List rows = (List) Ognl.getValue( Ognl.parseExpression( srcExpression), cycle.getPage( pageName ) );
if( rows!= null ){
ExcelCSVPrinter csvp = new ExcelCSVPrinter( responseOutputStream);
csvp.setAlwaysQuote( true );
for (Iterator j = rows.iterator(); j.hasNext();) {
Object r = (Object) j.next();
csvp.println( (String[]) getObjectAttributes(r, attributes ));
}
csvp.flush();
}


   } catch (OgnlException e) {
     throw new ServletException( e );
   }

 }

private String[] getObjectAttributes(Object r, String[] attributes) throws OgnlException {
String[] res = new String[ attributes.length ];
for( int i = 0; i < attributes.length; i++){
res[i] = DataHelper.convert(Ognl.getValue( Ognl.parseExpression(attributes[i]), r ));
}
return res;
}


public ILink getLink( IRequestCycle iRequestCycle, IComponent iComponent, Object[] params ){
return this.constructLink( iRequestCycle,getName(),null, params,false );
}


 public static void main(String[] args) {
   try {

     ArrayList arrayList = new ArrayList();
     arrayList.add( "");
     arrayList.add( "");
     arrayList.add( "");
     Object o = Ognl.getValue( Ognl.parseExpression("size"), arrayList );
     System.out.println("o = " + o);
   } catch (OgnlException e) {
     e.printStackTrace();
   }
 }
}

effrey Sze wrote:

Hi,

I already have a page that display the result of a search.  How can I
export the same result in CSV format?

Since there are many search criteria, passing them all to a service
through a ServiceLink is not possible.  Is there a easier way to pass
an object to a service?

Thanks.

Jeffrey

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]






--
Thanks,

Konstantin Ignatyev

http://www.kgionline.com





PS: If this is a typical day on planet earth, humans will add fifteen million 
tons of carbon to the atmosphere, destroy 115 square miles of tropical 
rainforest, create seventy-two miles of desert, eliminate between forty to one 
hundred species, erode seventy-one million tons of topsoil, add 2.700 tons of 
CFCs to the stratosphere, and increase their population by 263.000

Bowers, C.A. The Culture of Denial: Why the Environmental Movement Needs a Strategy for Reforming Universities and Public Schools. New York: State University of New York Press, 1997: (4) (5) (p.206)


--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to