You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tapestry.apache.org by Jeffrey Sze <te...@gmail.com> on 2005/04/11 08:17:30 UTC

Non-HTML output

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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Non-HTML output

Posted by Konstantin Iignatyev <kg...@yahoo.com>.
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: tapestry-user-unsubscribe@jakarta.apache.org
>For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
>
>
>  
>


-- 
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: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Non-HTML output

Posted by Javier Sanchez <ja...@gmail.com>.
To export data to CVS and Excel, we use JasperReports.

If you already have your data on a contrib:table, you may use it as a
datasource for JasperReports (no so much code is needed) and then,
export your report to CVS.

JAVIER SANCHEZ.

On Apr 11, 2005 1:17 AM, Jeffrey Sze <te...@gmail.com> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Non-HTML output

Posted by Jeffrey Sze <te...@gmail.com>.
Thanks.  I followed the method in FAQ 2.7 and get my CSV file. 
Although the file's content is correct, the server keep throwing
exception saying "getWriter method has already been called for this
response".  (I changed the example to call getWriter on the response
so it is easier for me to output String).

Any idea?

I am using JBoss 3.2.3 

Thanks.





On Apr 11, 2005 3:05 AM, Ron Piterman <mp...@vollbio.de> wrote:
> This is illustrated in FAQ 2.7:
> 
> http://jakarta.apache.org/tapestry/faq.html
> 
> ציטוט Jeffrey Sze:
> > 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: tapestry-user-unsubscribe@jakarta.apache.org
> > For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> >
> >
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
>

---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org


Re: Non-HTML output

Posted by Ron Piterman <mp...@vollbio.de>.
This is illustrated in FAQ 2.7:

http://jakarta.apache.org/tapestry/faq.html


ציטוט Jeffrey Sze:
> 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: tapestry-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: tapestry-user-help@jakarta.apache.org
> 
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: tapestry-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: tapestry-user-help@jakarta.apache.org