Portlet 2.0 new methods

Hi

I’m testing Vaddin 6.2.0 and Portlets 2.0. I created a new portlet 2.0 based on PortletDemo. I had to add a reference to PortletApplicationContext2 in this way


...
		if (getContext() instanceof PortletApplicationContext) {
			PortletApplicationContext ctx = (PortletApplicationContext) getContext();
			ctx.addPortletListener(this, new DemoPortletListener());
		}else if(getContext() instanceof PortletApplicationContext2 ){
			PortletApplicationContext2 ctx2 = (PortletApplicationContext2) getContext();
			ctx2.addPortletListener(this,   new DemoPortletListener());
		} else {
			getMainWindow().showNotification("Not inited via Portal! "+getContext().getClass().toString(),Notification.TYPE_ERROR_MESSAGE);      //I added the class.toString() to know which was the class of getContext()
		}
}

private class DemoPortletListener implements PortletListener, com.vaadin.terminal.gwt.server.PortletApplicationContext2.PortletListener {
....
	public void handleEventRequest(EventRequest request,
				EventResponse response) {
			// TODO Auto-generated method stub
			
		}

		public void handleResourceRequest(ResourceRequest request,
				ResourceResponse response) {
			// TODO Auto-generated method stub
			
		}
  1. Test if getContext() is instance of class PortletApplicationContext2
  2. Make DemoPortletListener implement …PortletApplicationContext2.PortletListener
  3. Add two new methods to implement that interface

The problem is whenever there is an action I see this error in the Tomcat log
16:45:50,430 ERROR [PortletServlet:104]
javax.portlet.PortletException: doEdit method not implemented
javax.portlet.PortletException: doEdit method not implemented
at javax.portlet.GenericPortlet.doEdit(GenericPortlet.java:388)
at javax.portlet.GenericPortlet.doDispatch(GenericPortlet.java:330)
at javax.portlet.GenericPortlet.render(GenericPortlet.java:233)
at com.sun.portal.portletcontainer.appengine.filter.FilterChainImpl.doFilter(FilterChainImpl.java:126)
at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:69)

Is required any code in those methods or any other processing to support Portlet 2.0?

Thanks

Typically a portlet is written either for Portlet 2.0 (JSR-286) or for Portlet 1.0 (JSR-168). Therefore, the complex if/else structure and implementing two listeners should not be necessary in most cases. Some of the bundled demos follow the JSR-286 and others JSR-168.

Also, the “not inited by portlet” case should only be a concern when running a Portlet 1.0 portlet and directly accessing the corresponding servlet bypassing the portlet before using it as a portlet. With Portlet 2.0, there is no servlet behind the portlet - even having a web.xml is unnecessary except for testing the portlet as a servlet outside a portal (assuming the application does not use any portlet specific features).

In Vaadin 6.2.1 there will be some fixes and improvements to make it easier to use portlet modes other than the basic view mode. Until its release, you can test with a 6.2 nightly build.

Using the edit (or help) mode will be as simple as declaring it as an allowed mode for the portlet in portlet.xml, and registering a window with the name “edit” in the application.

Application:


public void init() {
    // first create and register main window
    ...
    // add edit mode window
    Window editWindow = new Window("Edit mode");
    // window name is used to map between portlet modes and windows
    editWindow.setName(PortletMode.EDIT.toString());
    editWindow.addComponent(new Label("Edit functionality (preferences) goes here"));
    addWindow(editWindow);
}

portlet.xml:


    ...
    <supports>
        <mime-type>text/html</mime-type>
        <portlet-mode>view</portlet-mode>
        <portlet-mode>edit</portlet-mode>
    </supports>
    ...

Using custom modes requires implementing your own portlet class and some more work, and might or might not work properly depending on the portal and its version - e.g. some versions of Liferay have some problems with custom portlet modes.

You can find examples on the use of standard and custom portlet modes in the sample portlet files
PortletModeExample.java
and
PortletModePortlet.java
.

Hi Heri

It’s required to undeploy the 6.2.0 portlet (delete portlet folder fom webapps -Tomcat-) and then deploy the new one. Otherwise both vaadin jars are kept in WEB-INF/lib folder and the mode changing doesn’t work.

Regards
Aniceto

Henri

If I change the main window, it’s not redrawn until a full page redraw. One way is to switch between modes. How to get a Vaadin portlet fully redrawn?

Thanks
Aniceto

I found it by myself. A way is using a custom mode.

Add a custom mode to portlet.xml

<portlet-mode>zas</portlet-mode>

Create windows for view and zas modes whit buttons to change between them:


		mainWindow.addComponent(new Button("Zas", new Button.ClickListener() {

			public void buttonClick(ClickEvent event) {
				PortletApplicationContext2 ctx = (PortletApplicationContext2) getContext();
				try {
					ctx.setPortletMode(mainWindow, new PortletMode("zas"));
				} catch (IllegalStateException e) {
			 		e.printStackTrace();
				} catch (PortletModeException e) {
			 		e.printStackTrace();
				}
				setMainWindow(zasWindow);
			}
		}));
		setMainWindow(mainWindow);

		// zas
		zasWindow = new Window("Zas");
		zasWindow.setName("Zas");
		zasWindow.addComponent(new Label("Zas processing"));
		zasWindow.addComponent(new Button("Main", new Button.ClickListener() {

			public void buttonClick(ClickEvent event) {
				PortletApplicationContext2 ctx = (PortletApplicationContext2) getContext();
				try {
					ctx.setPortletMode(zasWindow, PortletMode.VIEW);
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (PortletModeException e) {
					e.printStackTrace();
				}	
				setMainWindow(mainWindow);
			}
		}));
		addWindow(zasWindow);

and that’s all.

Take into account the window argument in the setPortletMode is the current window.

Aniceto

Hi Henri,

There are some changes in Vaadin 6.3 and this is not working any more, what should be the right way to support different portlet modes with the new version?

Thanks

Unfortunately, some of the portlet mode by window related changes had to be rolled back, as they caused problems with multi-window support (multiple browser windows to the same portlet with separate state).

There is some material about handling different portlet modes in the latest version of the
Book of Vaadin
.

Briefly, you usually need a PortletListener that changes the main layout of the window. Note that now the methods of the PortletListener have an additional Window parameter, which should make changing the views in the correct window a little easier in practice.