Auto Vaadin Theme Deployment

Introduction

So in two previous blogs I introduced Auto Vaadin Add On Deployment and Vaadin 7 Theming.  At the end of the Vaadin 7 Theming blog I said that there was a similar mechanism for handling the deployment of your themes using the Vaadin 7 Control Panel, the mechanism introduced in the Auto Vaadin Add On Deployment blog.

So this blog is going to cover that mechanism by showing how to handle Auto Vaadin Theme Deployment.

Preparation

Follow the instructions from the Auto Vaadin Add On Deployment blog through the addition of the required-deployment-contexts entry in the liferay-plugin-package.properties file, then come back here...

Implementation

We also need to add a startup action class that extends one of the classes from the Vaadin 7 Control Panel.  The class is below:

package com.dnebinger.liferay.vaadin.sample; 
import com.dnebinger.liferay.vaadin.startup.AbstractVaadinThemeDeployStartupAction; import org.apache.commons.lang.StringUtils; 
import java.util.ArrayList; import java.util.List; 
/**  * SampleThemeDeployStartupAction: A sample class to demonstrate how to do automatic theme deployment.  * @author dnebinger  */ public class SampleThemeDeployStartupAction extends AbstractVaadinThemeDeployStartupAction {  private static final String SAMPLE_THEME = "sample.zip";   /**  * getThemeNames: Returns the names of the theme zip files from the WEB-INF/vaadin/themes directory that should be deployed.  * @return List The list of theme names.  */  @Override  protected List<String> getThemeNames() {  List<String> names = new ArrayList<String>(1); 
 names.add(SAMPLE_THEME); 
 return names;  } 
 /**  * getThemeVersion: Returns the version number to associate with the theme.  * @param themeName Name of the theme file to get the version number for.  * @return int The version number for the theme file.  */  @Override  protected int getThemeVersion(String themeName) {  if (StringUtils.isBlank(themeName))  return 0; 
 if (SAMPLE_THEME.equals(themeName)) {  return 100;  } 
 return 0;  } } 

This class does two things:

  1. It returns the list of themes to be deployed.
  2. It provides the version number to use for the theme.

To get the theme zip file, create and test your theme using the Vaadin 7 Control Panel.  When you are satisfied with the theme, use the Export button to export the theme as a zip file on your desktop.

The zip file must be added to your project in the src/main/webapp/WEB-INF/vaadin/themes folder.

The theme version number is used to determine if the theme has already been deployed.  If you are shipping an updated version of the theme, be sure to increment the version number.

We also need to add the src/webapp/WEB-INF/liferay-hook.xml file to add the hook to the portlet:

<?xml version="1.0"?>
<!DOCTYPE hook PUBLIC "-//Liferay//DTD Hook 6.2.0//EN" "http://www.liferay.com/dtd/liferay-hook_6_2_0.dtd"> 
<hook>  <portal-properties>vaadin-sample-portlet-hook.properties</portal-properties> </hook>

We also need to create the src/resources/vaadin-sample-portlet-hook.properties file:

# # vaadin-sample-portlet-hook.properties - Properties for the hook to add the app startup action. # 
application.startup.events=com.dnebinger.liferay.vaadin.sample.SampleThemeDeployStartupAction

That's it!

Conclusion

With these changes in place, when you build and deploy the portlet, when it is started in the application container the startup action will process the sample.zip theme file.

If it has either not been deployed before or if the version is greater than the currently installed version (considered a theme upgrade), the theme will be copied to the portal's html/VAADIN/themes directory and compiled to be ready for immediate use.

If it has already been deployed or the version is less than or equal to the deployed version, nothing happens (since it's already included).

So this eliminates the prework involved with deploying and compiling the themes, so you're back to just deploying your snazzy new Vaadin 7 portlet.

Just as with the automatic Add On deployment, you can include your theme with your project instead of treating them as separate deployments.