Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Generate report and open it with a Program in RAP& RCP(Generate report and open it with a Program in RAP& RCP)
Generate report and open it with a Program in RAP& RCP [message #720277] Tue, 30 August 2011 09:47 Go to next message
Angelo ZERR is currently offline Angelo ZERRFriend
Messages: 122
Registered: July 2009
Senior Member
Hi RAP Team,

We are developing an application called Dynaresume with RAP/RCP to manage resume (search resume, generate resume, etc). Generate resume is based on XDocReport to generate from docx or odt a resume by using data from DB.
So we have a resume form and an action in the toolbar "Generate". This button must generate a report and open MS Word or LibreOffice, etc

My question is how to implement that for RCP and RAP? My first idea is :

* for RCP : generate the report on the temp file and open it with MS Word, etc with org.eclipse.swt.program.Program
* for RAP : develop a servlet which generates the report and returns the content in the HTTP response OutputStream.

Before starting that, I would like know if my idea is correct?

Thank a lot for your help.

Regards Angelo
Re: Generate report and open it with a Program in RAP& RCP [message #721220 is a reply to message #720277] Thu, 01 September 2011 08:07 Go to previous messageGo to next message
Angelo ZERR is currently offline Angelo ZERRFriend
Messages: 122
Registered: July 2009
Senior Member
Hi,

I suppose I have not explained my need because I have no answer. So I have started to implement with a simple RCP/RAP Application which have a toolbar which have a an Action GenerateReportAction which generate a file text "Hello world!". When you click on the GenerateReportAction it generate the file text and open it with notepad. I explain how I have done that and I would like know if it's OK and I have some questions.

To manage single sourcing, the GenerateReportAction call an Eclipse command. I have a fragment for RCP and RAP which defines this Eclipse command mapped with the handler GenerateReportHandler.

1) For RCP, here my GenerateReportHandler :

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.swt.program.Program;

public class GenerateReportHandler extends AbstractHandler {

	@Override
	protected Object execute(ExecutionEvent event) throws ExecutionException {
		System.err.println("Report with RCP");
		String filename = "D:/tmp.txt";
		File f = new File(filename);
		FileWriter writer;
		try {
			writer = new FileWriter(f);
			writer.write("Hello world!");
			writer.flush();
			writer.close();
						
			Program.launch(filename);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

}


2) For RAP, I have followed teh same idea than RAP/Birt Integration PDF reports

The idea is that I register a GenerateReportServiceHandler which generate the file text :

IServiceManager manager = RWT.getServiceManager();
IServiceHandler handler = new GenerateReportServiceHandler();
manager.registerServiceHandler("downloadServiceHandler", handler);


And after you can call the URL ...?custom_service_handler=downloadServiceHandler which returns the output stream of the text file.

Here my GenerateReportHandler :

package fr.opensagres.xdocreport.eclipse.ui.handlers;

import java.net.URL;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.rwt.RWT;
import org.eclipse.rwt.service.IServiceHandler;
import org.eclipse.rwt.service.IServiceManager;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.browser.IWebBrowser;
import org.eclipse.ui.browser.IWorkbenchBrowserSupport;

public class GenerateReportHandler extends AbstractHandler {

	@Override
	protected Object execute(ExecutionEvent event) throws ExecutionException {
		System.err.println("Report with RAP");

		IServiceManager manager = RWT.getServiceManager();
		IServiceHandler handler = new GenerateReportServiceHandler();
		manager.registerServiceHandler("downloadServiceHandler", handler);

		IWorkbenchBrowserSupport browserSupport = PlatformUI.getWorkbench()
				.getBrowserSupport();
		try {
			IWebBrowser browser = browserSupport.createBrowser(
					IWorkbenchBrowserSupport.AS_EDITOR, "My", "Name",
					"My tooltip");

			browser.openURL(new URL("http://127.0.0.1:8081"
					+ createDownloadUrl()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	private String createDownloadUrl() {
		StringBuilder url = new StringBuilder();
		url.append(RWT.getRequest().getContextPath());
		url.append(RWT.getRequest().getServletPath());
		url.append("?");
		url.append(IServiceHandler.REQUEST_PARAM);
		url.append("=downloadServiceHandler");
		String encodedURL = RWT.getResponse().encodeURL(url.toString());
		return encodedURL;
	}

}


Here GenerateReportServiceHandler :

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletResponse;

import org.eclipse.rwt.RWT;
import org.eclipse.rwt.service.IServiceHandler;

public class GenerateReportServiceHandler implements IServiceHandler {

	public void service() throws IOException, ServletException {
		HttpServletResponse response = RWT.getResponse();
		response.setContentType("application/text");
		// response.setContentLength( download.length );
		String contentDisposition = "attachment; filename=\"XXX.txt\"";
		response.setHeader("Content-Disposition", contentDisposition);
		try {
			response.getWriter().write("Yes");
		} catch (IOException ioe) {
			throw new RuntimeException(ioe);
		}

	}
}


At first, as you can notice, I don't generate a file in the temporary folder. It seems than in the sample of RAP/Birt Integration PDF reports
it generated a file in a temporary folder and the browser open this file. Why do that? Why don't returns directly the ouput stream of the report to avoid generating a temporary file?

You can notice too, I had to create the URL by adding "http://127.0.0.1:8081". Do you know if it exists a RAP helper which generate that, or do I code that?

At end I create a GenerateReportServiceHandler each time I click on my GenerateReportAction, but is it a well mean to do that? I'm afraid to have some problems when several users click on my GenerateReportAction on the same time?

Hope I have more explained my need.

Thank a lot for your answer and your help.

Regards Angelo
Re: Generate report and open it with a Program in RAP& RCP [message #721337 is a reply to message #721220] Thu, 01 September 2011 13:58 Go to previous messageGo to next message
Cole Markham is currently offline Cole MarkhamFriend
Messages: 150
Registered: July 2009
Location: College Station, TX
Senior Member

Angelo,

Looks like you are on the right track.

Quote:
At first, as you can notice, I don't generate a file in the temporary folder. It seems than in the sample of RAP/Birt Integration PDF reports
it generated a file in a temporary folder and the browser open this file. Why do that? Why don't returns directly the ouput stream of the report to avoid generating a temporary file?

There are a couple of reasons they may have done this. First it may be a limitation of BIRT, I'm not sure whether is supports generating a PDF to an output stream. Second, if you wanted to allow the report to be downloaded more than once without regeneration. This may be to support restarting a failed download by the same user or having multiple users download the same file. Either way, your application would need to manage the temporary files.

Quote:
You can notice too, I had to create the URL by adding "http://127.0.0.1:8081". Do you know if it exists a RAP helper which generate that, or do I code that?

You could do it either way. If you choose to set it manually, I would recommend having the application read it from a config file. You can also build it programmatically using information from the http request. This is done in org.eclipse.rwt.internal.util.URLHelper, but that is an internal class that you shouldn't use (although we often use it in our code). If you want to construct it yourself, you can get the request via org.eclipse.rwt.RWT.getRequest();

Quote:
At end I create a GenerateReportServiceHandler each time I click on my GenerateReportAction, but is it a well mean to do that? I'm afraid to have some problems when several users click on my GenerateReportAction on the same time?

This is up to your application requirements and probably depends on how expensive the report is to generate vs how often the same report would be fetched. Keep in mind that each user will have a different instance of GenerateReportAction. The action shouldn't store state however, that would best be done via some other service. You might investigate org.eclipse.rwt.SessionSingletonBase if you want to create a singleton per user session.

Hope that helps,

Cole
Re: Generate report and open it with a Program in RAP& RCP [message #721587 is a reply to message #721337] Fri, 02 September 2011 08:14 Go to previous message
Angelo ZERR is currently offline Angelo ZERRFriend
Messages: 122
Registered: July 2009
Senior Member
Hi Cole,

Thank a lot for your answer!

Quote:
There are a couple of reasons they may have done this. First it may be a limitation of BIRT, I'm not sure whether is supports generating a PDF to an output stream. Second, if you wanted to allow the report to be downloaded more than once without regeneration. This may be to support restarting a failed download by the same user or having multiple users download the same file. Either way, your application would need to manage the temporary files.


Ok I understand more this choice. In my case I want regenerate my report every time.
Quote:

You could do it either way. If you choose to set it manually, I would recommend having the application read it from a config file. You can also build it programmatically using information from the http request. This is done in org.eclipse.rwt.internal.util.URLHelper, but that is an internal class that you shouldn't use (although we often use it in our code). If you want to construct it yourself, you can get the request via org.eclipse.rwt.RWT.getRequest();


Thank a lot for this information. In my case I have copy/paset URLHelper in my project to use it (IMHO, I think this class is very helpfull and should be perhaps in a none internal package). After I compute my url like this :

private String createDownloadUrl() {
		StringBuilder url = new StringBuilder();
		url.append(URLHelper.getContextURLString());
		url.append(RWT.getRequest().getServletPath());
		url.append("?");
		url.append(IServiceHandler.REQUEST_PARAM);
		url.append("=");
		url.append(REPORT_SERVICE_HANDLER);
		url.append("&filename=");
		url.append(System.currentTimeMillis());
		String encodedURL = RWT.getResponse().encodeURL(url.toString());
		return encodedURL;
	}


You can notice that I have removed

url.append(RWT.getRequest().getContextPath());


because
url.append(URLHelper.getContextURLString());


add already the context path.

And I have added a filename parameter with a value which change every time to have an unique URL, otherwise (I don't know why?), the browser returns me the first generated report.

Quote:
This is up to your application requirements and probably depends on how expensive the report is to generate vs how often the same report would be fetched. Keep in mind that each user will have a different instance of GenerateReportAction. The action shouldn't store state however, that would best be done via some other service. You might investigate org.eclipse.rwt.SessionSingletonBase if you want to create a singleton per user session.

In my case I want regerate every time my (resume) report, so I think my design is OK.

No I will try to create an editor with Browser. I think it should be the same thing that I have done.

Thank a lot again!

Regards Angelo
Previous Topic:How to checkout demo project?
Next Topic:Is it possible to get the request parameters in Post way
Goto Forum:
  


Current Time: Fri Apr 26 20:46:48 GMT 2024

Powered by FUDForum. Page generated in 0.04028 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top