Report portlet

Một phần của tài liệu Phát triển portlet mới cho người dùng ( BÀI TẬP LỚN CÔNG NGHỆ WEB VÀ DỊCH VỤ TRỰC TRUYẾN ) (Trang 32 - 36)

Cuối cùng, Liferay portal còn hỗ trợ người dùng in các thông tin trên portal dưới dạng các bản in như pdf và excel. Ví dụ này sẽ trình bày về việc xuất các thông tin ra dưới dạng các file pdf và excel.

Thực chất của việc này là tạo ra một cuộc gọi ajax đến server, server sẽ xử lý yêu cầu qua mã java và và trả lại cho client dữ liệu dưới dạng muitimedia (pdf, excel)

Khi click vào button “PDF Report” hoặc “Excel Report” nó sẽ tạo một cuộc gọi ajax đến server. Tùy theo tham số truyền đến mà server sẽ xử lý. Đây là 2 đoạn code xử lý chính để xuất ra file pdf và excel:

PDF:

Document document = new Document(); ByteArrayOutputStream baos = new ByteArrayOutputStream();

PdfWriter.getInstance(document, baos); document.open();

document.add(new Paragraph(msg)); document.add(Chunk.NEWLINE);

document.add(new Paragraph("It is a sunny day today.") );

document.close();

res.setContentType("application/pdf"); res.addProperty(

HttpHeaders.CACHE_CONTROL, "max-age=3600, must- revalidate"); res.addProperty( HttpHeaders.CONTENT_DISPOSITION, "attachment;file name=pdfReport.pdf"); res.addProperty(HttpHeaders.CONTENT_ENCODING, "Binary" ); res.setContentLength(baos.size());

OutputStream out = res.getPortletOutputStream(); //FileOutputStream out = new

FileOutputStream("pdfReport.pdf"); baos.writeTo(out);

out.flush(); out.close();

Excel:

HSSFWorkbook wb = new HSSFWorkbook(); HSSFSheet s = wb.createSheet(); HSSFRow r = null; HSSFCell c = null; r = s.createRow(0); c = r.createCell(0);

c.setCellValue(new HSSFRichTextString("Cell_1")); c = r.createCell(1);

c.setCellValue(new HSSFRichTextString("Cell_2")); res.setContentType("application/vnd.ms-excel"); res.addProperty(

HttpHeaders.CACHE_CONTROL, "max-age=3600, must-revalidate");

res.addProperty(

HttpHeaders.CONTENT_DISPOSITION, "attachment;filename= excelReport.xls");

res.addProperty(HttpHeaders.CONTENT_ENCODING, "Bi nary"); OutputStream out = Res.getPortletOutputStream(); wb.write(out); out.flush(); out.close(); 3.2 Vaadin portlet 3.2.1 FirstVaadin portlet

Trước tiên, ta quan sát một phần code :

public void init() {

Window window = new Window("Vaadin Portlet Application");

setMainWindow(window);

window.addComponent(new Label("Hello Vaadin user!"));

}

Có thể thấy, các bước cơ bản của một Vaadin portlet như sau:

Đầu tiên là override hàm khởi tạo init(), khai báo một đối tượng lớp Window

Đối tượng mainWindow này sẽ như một thùng chứa, chứa các đối tượng khác. Những đối tượng này được coi như các component (thành phần con) sẽ được thêm vào thông qua phương thức (adsbygoogle = window.adsbygoogle || []).push({});

addComponent

Hiện tại, trong ví dụ này, chúng ta chưa cần xử lý bất cứ gì cho giao diện người dùng. Nó sẽ được Vaadin quản lý. Tất nhiên, Vaadin cũng hỗ trợ chúng ta có thể tùy chọn quản lý các giao diện người dùng. Ta có thể hình dung nó rất gần với việc khởi tạo một winform vốn đã quen thuộc với mọi người.

Kết quả hiển thị trên trình duyệt:

3.2.2 NotifyVaadin portlet

Chúng ta quan sát phần code khác và nhận xét: @Override

public void init() {

final Window mainWindow = new

Window("Vaadinproject2 Application");

Label label = new Label("Hello Vaadin user"); mainWindow.addComponent(label);

mainWindow.addComponent(

new Button("What is the time?", new Button.ClickListener() {

public void

buttonClick(ClickEvent event) {

mainWindow.showNotification( "The time is " + new

Date());

} }));

setMainWindow(mainWindow); }

Về cơ bản, cấu trúc các thành phần vẫn như ở ví dụ trên : khởi tạo mainWindow, add các component …

Chúng ta sẽ thêm một component là một Button và thêm vào button này một sự kiện ClickListener tức là button này sẽ ở trạng thái luôn luôn lắng nghe sự kiện « click » từ người dùng, bất cứ nào người dùng click vào nó, nó sẽ hiện lên một notify.

Kết quả :

Và kết quả khi bấm nút hiện ta notify thông báo giờ:

Một phần của tài liệu Phát triển portlet mới cho người dùng ( BÀI TẬP LỚN CÔNG NGHỆ WEB VÀ DỊCH VỤ TRỰC TRUYẾN ) (Trang 32 - 36)