Friday 7 December 2012

Vaadin portlet tips and tricks part 1

Hello,
    In this post I'll put usefull knowledge about Vaadin development.

   1) Debug window

         You analyze vaadin layouts with debug window, to see it simply put ?debug param to URL      (http://localhost:8080/web/guest/vaadin?debug)


   2) ThreadLocal pattern

        In the application you may need to have access to some common objects, for example user bean, locale, etc. Thread local pattern can help you to achieve that. Detailed info you can find here and here.
        Based on this pattern you can add in to your application some Controller class which will contain all necessary common accessed objects. It is usefull to put  there reference to you aplication, logged in user, current locale, messages notification methods.

public class Controller implements TransactionListener {
  
  ...

  public MyVaadinApplication getApplication() {
    return (MyVaadinApplication)application;
  }

  public static User getUser() {
    return INSTANCE.get().user;
  }

  public static void setUser(final User user) {
    INSTANCE.get().user = user;
  }

  public static void showInfo(final String string) {
    INSTANCE.get().application.getMainWindow().showNotification(
      string, Notification.TYPE_WARNING_MESSAGE);
  }
}
Controller.initialize(this);

Button btn = new Button("Notification");
btn.addListener(new ClickListener() {
 public void buttonClick(ClickEvent event) {
  Controller.showInfo("Here my notification from controller");
 }
});

  3) Spring integration

        You can vaadin with spring in few ways:
            3.1) By using ThreadLocal pattern
public class SpringHandler implements TransactionListener {
 ...
 /**
    * Gets the spring bean by it id.
    * 
    * @param beanRef
    *            The bean's id
    * @return The resulting ViewItem object
    */
   public static Object getBean(final String beanRef) {
    if (beanRef == null) {
     throw new IllegalArgumentException("Bean id may not be null");
    }
     
    PortletContext portletContext = ((PortletApplicationContext2) INSTANCE.get().application.getContext()).getPortletSession().getPortletContext();
    ApplicationContext context = PortletApplicationContextUtils.getRequiredWebApplicationContext(portletContext);

    return context.getBean(beanRef);
   }

}
SpringHandler.initialize(this);
SpringServiceImpl service = (SpringServiceImpl) SpringHandler.getBean("springService");

Label label = new Label(service.getLabelInfo());

         
              3.2) By using annotaions and AspectJ. Here you can find detailed information.

  4) Vaadin and portlet request integration.

        You can do it with PortletListener.
private class MyPortletListener implements PortletListener {
 private static final long serialVersionUID = -6973370282667721257L;

 @Override
 public void handleRenderRequest(final RenderRequest request,
         final RenderResponse response,
         final Window window) {
   if (PortletMode.VIEW.equals(request.getPortletMode())) {
  try {
    User user = PortalUtil.getUser(request);
    if (user == null) {
   throw new Exception("Please log in");
    }
    Controller.setUser(user);
  } catch (Exception e) {
    LOG.error("Some error happend. " + e);
    return;
  }
   }
 }
 /** {@inheritDoc}  */
 @Override
 public void handleActionRequest(final ActionRequest request,
         final ActionResponse response,
         final Window window) {
 }

 /** {@inheritDoc}  */
 @Override
 public void handleEventRequest(final EventRequest request,
           final EventResponse response,
           final Window window) {
 }

 /** {@inheritDoc}  */
 @Override
 public void handleResourceRequest(final ResourceRequest request,
           final ResourceResponse response,
           final Window window) {

 }
}
         
         Into init method of VaadinApplication put next code:
public void init() {
     ...
        // set portlet listener, check if application is portlet and init user with listener
        if (getContext() instanceof PortletApplicationContext2) {
          PortletApplicationContext2 ctx = (PortletApplicationContext2) getContext();
          ctx.addPortletListener(this, new MyPortletListener());
        } else {
          getMainWindow().showNotification("Not inited via Portal!", Notification.TYPE_ERROR_MESSAGE);
        }

      }
           Method  handleRenderRequest will be invoked at render request and you will be able to gete necessary inforamtion from it.

Complete application with all integrations above you can find here.
 
BR, 
Paul Butenko

1 comment:

  1. Harrah's New Orleans Casino & Hotel - Mapyro
    Find Harrah's New Orleans Casino & Hotel, 동해 출장샵 New Orleans, LA, United States, ratings, photos, prices, 서귀포 출장안마 expert advice, 구미 출장마사지 traveler 김포 출장샵 reviews and tips, 대전광역 출장샵

    ReplyDelete