Vaadin Chart with Server Push

Hi everybody, Today I’m going to show you a little tutorial regarding Vaadin server push and also Vaadin charting. I found this two feature are amazing. Server push is a new feature that Vaadin added to the line since version 7.00. Server push is a capability to push update in the internal server to the web browser by keeping the connection alive between server and web browser. There are few ways to enable this functionality in vaadin, one of the way is to add push annotation on your view (@Push).

The second great feature that is now available is Vaadin Chart. Vaadin has working to integrate this functionality in very beautiful. Without further a do, Lets code!!!!

1. Prepare your Vaadin maven project.

2. Add Vaadin chart library to your pom

<dependency>
 <groupId>com.vaadin.addon</groupId>
 <artifactId>vaadin-charts</artifactId>
 <version>1.1.4</version>
</dependency>

<repository>
 <id>vaadin-addons</id>
 <url>http://maven.vaadin.com/vaadin-addons</url>
</repository>

2. Now to enable the server push capability is to just add @Push on your class. Simple right …


@Theme("mytheme")
@SuppressWarnings("serial")
@Push
public class MyVaadinUI extends UI {

@Override
 protected void init(VaadinRequest request) {
 final VerticalLayout layout = new VerticalLayout();
 layout.setMargin(true);
 setContent(layout);

3. Now let do the charting. In this scenario, I will create a chart with predefined data (Random data) where later on after the page opened in the browser the chart data will be updated by a java timer (threads) to generate the random data. Every update on the data will be pushed to the client side.


@Theme("mytheme")
@SuppressWarnings("serial")
@Push
public class MyVaadinUI extends UI {

@Override
 protected void init(VaadinRequest request) {
 final VerticalLayout layout = new VerticalLayout();
 layout.setMargin(true);
 setContent(layout);

Chart mychart = new Chart();
 Configuration configuration = mychart.getConfiguration();
 configuration.setTitle("Simple Tutorial");

XAxis xAxis = configuration.getxAxis();
 xAxis.setType(AxisType.LINEAR);

final DataSeries listSeries1 = new DataSeries();
 listSeries1.setPlotOptions(new PlotOptionsArea());
 configuration.addSeries(listSeries1);
 layout.addComponent(mychart);
 int i=0;
 for(i=0; i < 100 ; i++){
 DataSeriesItem item = new DataSeriesItem(i, 1 + (Math.random()*100)*3);
 listSeries1.add(item);
 }

final int aa = i;
 new Timer().scheduleAtFixedRate(new TimerTask() {
 int ff = aa;
 public void run() {
 DataSeriesItem item1 = new DataSeriesItem(ff++, 1 + (Math.random()*100)*3);
 listSeries1.add(item1, true, true);
 getUI().push();
 }
 }, new Date(), 1000);
 }

@WebServlet(value = "/*", asyncSupported = true)
 @VaadinServletConfiguration(productionMode = false, ui = MyVaadinUI.class, widgetset = "Vaadin9.AppWidgetSet")
 public static class Servlet extends VaadinServlet {
 }

}

5. Yeah finally you can deploy the application to Jboss or Jetty that support push technology.

chart

Integration Vaadin and Activiti BPM by Spring framework

In some of my project in the past I remembered that in order to do a business flow we need to do it manually in the coding level so that it gives your project  risk when you are managing  big project since business process in the organization might change during your software development and for sure it is not good for your development.

Finally, I found a simple tools called JBoss BPM 3.0 on that time, This tools was so good and useful helping the software designer integrating the business process defined by the business with the application code. Playing with the BPM is so fun. Lately, I found an application called Activiti BPM. Put some hand on it for several days give me a lot of fun. Since then I use this BPM as my base engine to manage the process management. I think it is time to share some basic after 1 years worked on it.

I will explain the very basic configuration to integrate Vaadin and Activiti with spring framework.

1. Setup your eclipse and Jboss AS configuration

2. Setup a Vaadin project from Maven

3. Configure your web.xml as below to enable the spring bean injection

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>Vaadin Web Application</display-name>
 <context-param>
 <description>Vaadin production mode</description>
 <param-name>productionMode</param-name>
 <param-value>false</param-value>
 </context-param>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext-spring.xml</param-value>
</context-param>
<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

<servlet>
 <servlet-name>Vaadin Application Servlet</servlet-name>
 <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
 <init-param>
 <description>Vaadin application class to start</description>
 <param-name>application</param-name>
 <param-value>study.ActivitiExclusive.MyVaadinApplication</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>Vaadin Application Servlet</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>

2. Configure the applicationContext-spring.xml to manage the activiti bean creation and database connection

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <context:spring-configured />
 <context:annotation-config />

<context:component-scan base-package="study.ActivitiExclusive" />
 <bean id="dataSource"
 class="org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy">
 <property name="targetDataSource">
 <bean class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
 <property name="url" value="jdbc:mysql://localhost:3306/activiti" />
 <property name="driverClass" value="com.mysql.jdbc.Driver" />
 <property name="username" value="root" />
 <property name="password" value="*****" />
 </bean>
 </property>
 </bean>

<bean id="transactionManager"
 class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
 <property name="dataSource" ref="dataSource" />
 </bean>

<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
 <property name="databaseType" value="mysql" />
 <property name="dataSource" ref="dataSource" />
 <property name="transactionManager" ref="transactionManager" />
 <property name="databaseSchemaUpdate" value="true" />
 <property name="history" value="full" />
 <property name="jobExecutorActivate" value="false" />
 <property name="deploymentResources" value="/WEB-INF/MyProcess.bpmn" />

</bean>

<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
 <property name="processEngineConfiguration" ref="processEngineConfiguration" />
 </bean>

<bean id="repositoryService" factory-bean="processEngine"
 factory-method="getRepositoryService" />
 <bean id="runtimeService" factory-bean="processEngine"
 factory-method="getRuntimeService" />
 <bean id="taskService" factory-bean="processEngine"
 factory-method="getTaskService" />
 <bean id="historyService" factory-bean="processEngine"
 factory-method="getHistoryService" />
 <bean id="managementService" factory-bean="processEngine"
 factory-method="getManagementService" />
 <bean id="identityService" factory-bean="processEngine"
 factory-method="getIdentityService" />

<bean id="Printing" class="study.ActivitiExclusive.Printing" />

</beans>

 

3. Configure your maven POM to enable the all library completed

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
 <modelVersion>4.0.0</modelVersion>
 <groupId>study</groupId>
 <artifactId>ActivitiExclusive</artifactId>
 <packaging>war</packaging>
 <version>0.0.1-SNAPSHOT</version>
 <name>Vaadin Web Application</name>

<properties>
 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <vaadin.version>6.8.4</vaadin.version>
 <gwt.version>2.3.0</gwt.version>
 <gwt.plugin.version>2.2.0</gwt.plugin.version>
 </properties>

<build>
 <plugins>
 <plugin>
 <groupId>org.apache.maven.plugins</groupId>
 <artifactId>maven-compiler-plugin</artifactId>
 <configuration>
 <source>1.5</source>
 <target>1.5</target>
 </configuration>
 </plugin>
 </plugins>
 </build>

<repositories>
 <repository>
 <id>vaadin-snapshots</id>
 <url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
 <releases>
 <enabled>false</enabled>
 </releases>
 <snapshots>
 <enabled>true</enabled>
 </snapshots>
 </repository>
 <repository>
 <id>vaadin-addons</id>
 <url>http://maven.vaadin.com/vaadin-addons</url>
 </repository>
 <repository>
 <id>activiti</id>
 <name>Activiti Repository</name>
 <url>https://maven.alfresco.com/nexus/content/repositories/activiti/</url>
 </repository>
 </repositories>

<dependencies>
 <dependency>
 <groupId>com.vaadin</groupId>
 <artifactId>vaadin</artifactId>
 <version>${vaadin.version}</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjrt</artifactId>
 <version>1.7.0</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjweaver</artifactId>
 <version>1.7.0</version>
 </dependency>
 <dependency>
 <groupId>org.activiti</groupId>
 <artifactId>activiti-engine</artifactId>
 <version>5.10</version>
 </dependency>

<dependency>
 <groupId>org.activiti</groupId>
 <artifactId>activiti-spring</artifactId>
 <version>5.10</version>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.21</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-aspects</artifactId>
 <version>3.1.2.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>3.1.2.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-core</artifactId>
 <version>3.1.2.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-beans</artifactId>
 <version>3.1.2.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.1.2.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>log4j</groupId>
 <artifactId>log4j</artifactId>
 <version>1.2.17</version>
 </dependency>
 <dependency>
 <groupId>org.apache.tomcat</groupId>
 <artifactId>jsp-api</artifactId>
 <version>6.0.35</version>
 </dependency>
 </dependencies>

</project>

5. Create a simple activiti BPM as a sample “MyProcess.BPMN” in the WEB-INF directory

 

 

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
 <process id="process1" name="process1">
 <startEvent id="startevent1" name="Start"></startEvent>
 <userTask id="usertask1" name="Approve"></userTask>
 <sequenceFlow id="flow1" name="" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
 <endEvent id="endevent1" name="End"></endEvent>
 <serviceTask id="servicetask1" name="Service Task" activiti:expression="#{Printing.printMessage()}" ></serviceTask>
 <sequenceFlow id="flow2" name="" sourceRef="usertask1" targetRef="servicetask1"></sequenceFlow>
 <userTask id="usertask2" name="Makan2"></userTask>
 <sequenceFlow id="flow3" name="" sourceRef="servicetask1" targetRef="usertask2"></sequenceFlow>
 <endEvent id="endevent2" name="End"></endEvent>
 <sequenceFlow id="flow4" name="" sourceRef="usertask2" targetRef="endevent2"></sequenceFlow>
 </process>
 <bpmndi:BPMNDiagram id="BPMNDiagram_process1">
 <bpmndi:BPMNPlane bpmnElement="process1" id="BPMNPlane_process1">
 <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
 <omgdc:Bounds height="35" width="35" x="170" y="40"></omgdc:Bounds>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
 <omgdc:Bounds height="55" width="105" x="140" y="210"></omgdc:Bounds>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
 <omgdc:Bounds height="35" width="35" x="460" y="220"></omgdc:Bounds>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape bpmnElement="servicetask1" id="BPMNShape_servicetask1">
 <omgdc:Bounds height="55" width="105" x="300" y="210"></omgdc:Bounds>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
 <omgdc:Bounds height="55" width="105" x="460" y="210"></omgdc:Bounds>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2">
 <omgdc:Bounds height="35" width="35" x="620" y="220"></omgdc:Bounds>
 </bpmndi:BPMNShape>
 <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
 <omgdi:waypoint x="187" y="75"></omgdi:waypoint>
 <omgdi:waypoint x="192" y="210"></omgdi:waypoint>
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
 <omgdi:waypoint x="245" y="237"></omgdi:waypoint>
 <omgdi:waypoint x="300" y="237"></omgdi:waypoint>
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
 <omgdi:waypoint x="405" y="237"></omgdi:waypoint>
 <omgdi:waypoint x="460" y="237"></omgdi:waypoint>
 </bpmndi:BPMNEdge>
 <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
 <omgdi:waypoint x="565" y="237"></omgdi:waypoint>
 <omgdi:waypoint x="620" y="237"></omgdi:waypoint>
 </bpmndi:BPMNEdge>
 </bpmndi:BPMNPlane>
 </bpmndi:BPMNDiagram>
</definitions>

 

6. Finally in the application  I can autowire all the necessary activity bean from the spring context. (Please refer on how to create the injection for vaadin in my previous article)

package study.ActivitiExclusive;
import org.activiti.engine.IdentityService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.runtime.Execution;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;

import com.vaadin.ui.Window;

public class MyWindow extends Window {

@Autowired
 RepositoryService repositoryService;

@Autowired
 RuntimeService runtimeService;

@Autowired
 TaskService taskService;

@Autowired
 IdentityService identityService;
 public MyWindow() {

 Inject.Inject(this);
 }

}

 

7. Yeah just follow the configuration above then you can start playing Activiti BPM with Vaadin. In the above configuration don’t forget that you should have configure your Mysql database with the specified schema and DB.

 

Have a nice try all …

Vaadin and Spring simple autowire

I have been using vaadin and spring in some of my projects but I don’t any find a simple way to manage autowiring for the spring bean to vaadin. I tried the add-on from the vaadin to integrate spring and vaadin into one context but it fail and to many changes in the configuration and I have tried the autowiringbeanfactory.java to become the servlet of vaadin but it only serve autowiring for the main application. If you have so many autowired annotation spread in your bean then all the above solution will not work.

I found a simple way to manage the autowiring by adding a very minimum configuration changes and it becomes my favorite solution to my next project. Please check below the configuration

1. Setup the Vaadin maven project in eclipse + JbossAS installation

2. Configure the pom as below


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>study</groupId>
<artifactId>testIntegrate</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Vaadin Web Application</name>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vaadin.version>6.8.2</vaadin.version>
<gwt.version>2.3.0</gwt.version>
<gwt.plugin.version>2.2.0</gwt.plugin.version>
</properties>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>vaadin-snapshots</id>
<url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url>
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
</repositories>

<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.1.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.2.RELEASE</version>
<type>jar</type>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>6.0.35</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.7.0</version>
</dependency>
</dependencies>
</project>

3. Create the application-context.xml and put it into the WEB-INF with text below, please noted that to ensure the “annotation-config” is there because it will activate the autowiring via spring annotation such as @Autowire, @Component and etc


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:context="http://www.springframework.org/schema/context"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:neo4j="http://www.springframework.org/schema/data/neo4j"
 xmlns:tx="http://www.springframework.org/schema/tx"
 xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
 http://www.springframework.org/schema/data/neo4j http://www.springframework.org/schema/data/neo4j/spring-neo4j-2.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
 <context:spring-configured />
 <context:annotation-config />

<context:component-scan base-package="study.testIntegrate" />

</beans>

4. Edit your web.xml in order to spring aware of context creation from the beginning, please follow as below


<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 id="WebApp_ID" version="2.5">
 <display-name>Vaadin Web Application</display-name>
 <context-param>
 <description>Vaadin production mode</description>
 <param-name>productionMode</param-name>
 <param-value>false</param-value>
 </context-param>

<context-param>
 <param-name>contextConfigLocation</param-name>
 <param-value>/WEB-INF/applicationContext-spring.xml</param-value>
 </context-param>

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener>
 <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
 </listener>

<servlet>
 <servlet-name>Vaadin Application Servlet</servlet-name>
 <servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
 <init-param>
 <description>Vaadin application class to start</description>
 <param-name>application</param-name>
 <param-value>study.testIntegrate.MyVaadinApplication</param-value>
 </init-param>
 </servlet>
 <servlet-mapping>
 <servlet-name>Vaadin Application Servlet</servlet-name>
 <url-pattern>/*</url-pattern>
 </servlet-mapping>
</web-app>

5.  In order to make the injection to the autowiring to your bean working as expected then you should create a helper class to allow the spring knows your vaadin bean. You can include this class below into your source


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;

public class Inject {

static Application application;
 static ApplicationContext applicationContext;

public static void Inject(Object object) {
 if (applicationContext == null) {
 ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
 .currentRequestAttributes();
 HttpServletRequest request = requestAttributes.getRequest();
 HttpSession session = request.getSession(false);
 applicationContext = WebApplicationContextUtils
 .getRequiredWebApplicationContext(session
 .getServletContext());
 }

AutowireCapableBeanFactory beanFactory = applicationContext
 .getAutowireCapableBeanFactory();
 beanFactory.autowireBeanProperties(object,
 AutowireCapableBeanFactory.AUTOWIRE_AUTODETECT, false);

}

}

6. In order to use the helper class you my just call this static function “Inject” and pass the class you want to autowire, Please follow the sample class below


public class MyWindow extends Window {

@Autowired
 MyTest myTest;

Button button;

public MyWindow() {

 Inject.Inject(this);

 button = new Button();
 addComponent(button);
 button.addListener(Button.ClickEvent.class, this, "Check");
 }

public void Check(Button.ClickEvent event) {
 if (myTest != null) {
 showNotification("Ahhhaaa");
 }
 }
}

7. Then by calling the inject and pass the class itself then spring will autowire all the properties with the @autowire annotation for you. Here is the magic then. Very simple right to make the Vaadin aware to Spring autowiring with a very less configuration changes.

8. Here is my vaadin application init method


import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class MyVaadinApplication extends Application {
 private MyWindow window;

@Override
 public void init() {
 window = new MyWindow();
 setMainWindow(window);
 }

}

9. The last but not least is you dont forget to annotate your class with the @Component or @Service in order to let the spring bean scanner able to detect that it is a spring bean that need to be managed or autowired.

Hopefully my tutorial will make the integration of the vaadin and spring become easier so that the power of spring framework will available in combination with Vaadin superb technology.

Vaadin + Springdata + Neo4j Basic Conf with Spring Context

Hi all, Recently I checked out the Vaadin web and browsed the add-on directory. Wow it looks so fun to see this framework is getting bigger since alot of new add-on are coming in.

This time I am going to show the basic configuration for Vaadin + Springdata and Neo4j. I have post this kind of information in my previous post but in this tutorial is quite different since this basic configuration will use the spring contect rather than Vaadin’s

As usual my web application server is based on JBoss AS 7 so that I expected that you have had the basic knowledge of JBoss AS basic configuration

OK Lets get it start

1. Open your eclipse and start your JBoss AS

2. Create the vaadin project via Maven

3. Manage the POM.xml and setup as the entry below

<?xml version=”1.0″ encoding=”UTF-8″?>
<project xmlns=”http://maven.apache.org/POM/4.0.0&#8243; xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xsi:schemaLocation=”http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd”&gt;
<modelVersion>4.0.0</modelVersion>
<groupId>study</groupId>
<artifactId>springIntegration</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>Vaadin Web Application</name>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vaadin.version>6.8.2</vaadin.version>
<gwt.version>2.3.0</gwt.version>
<gwt.plugin.version>2.2.0</gwt.plugin.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>spring-snapshot</id>
<name>Spring Maven SNAPSHOT Repository</name>
<url>http://repo.springsource.org/libs-milestone</url&gt;
</repository>
<repository>
<id>vaadin-snapshots</id>
<url>http://oss.sonatype.org/content/repositories/vaadin-snapshots/</url&gt;
<releases>
<enabled>false</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>neo4j-release-repository</id>
<name>Neo4j Maven 2 release repository</name>
<url>http://m2.neo4j.org/releases</url&gt;
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
<repository>
<id>vaadin-addons</id>
<url>http://maven.vaadin.com/vaadin-addons</url&gt;
</repository>
<repository>
<id>springsource-repo</id>
<name>SpringSource Repository</name>
<url>http://repo.springsource.org/release</url&gt;
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin</artifactId>
<version>${vaadin.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j-rest</artifactId>
<version>2.1.0.RC3</version>
</dependency>
<dependency>
<groupId>org.neo4j</groupId>
<artifactId>neo4j-rest-graphdb</artifactId>
<version>1.7.2</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.2.RELEASE</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>6.0.35</version>
</dependency>
<dependency>
<groupId>org.dellroad</groupId>
<artifactId>dellroad-stuff</artifactId>
<version>1.0.507</version>
</dependency>
</dependencies>
</project>

4. Create file for the spring application-context.xml under the WEB-INF/classes

<?xml version=”1.0″ encoding=”UTF-8″?>
<web-app xmlns:xsi=”http://www.w3.org/2001/XMLSchema-instance&#8221;
xmlns=”http://java.sun.com/xml/ns/javaee&#8221; xmlns:web=”http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
xsi:schemaLocation=”http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd&#8221;
id=”WebApp_ID” version=”2.5″>
<display-name>Vaadin Web Application</display-name>
<context-param>
<description>Vaadin production mode</description>
<param-name>productionMode</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-spring.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
<servlet-name>Vaadin Application Servlet</servlet-name>
<servlet-class>com.vaadin.terminal.gwt.server.ApplicationServlet</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>study.springIntegration.HelloWorld</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application Servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

6. Yes all the configuration that you need it is now done and what you need is to create the hellowworld application

package study.springIntegration;

import org.dellroad.stuff.vaadin.SpringContextApplication;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.web.context.ConfigurableWebApplicationContext;

import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

public class HelloWorld extends SpringContextApplication implements
BeanFactoryAware, InitializingBean, DisposableBean {
public void destroy() throws Exception {
// TODO Auto-generated method stub

}

public void afterPropertiesSet() throws Exception {
// TODO Auto-generated method stub

}

public void setBeanFactory(BeanFactory arg0) throws BeansException {
// TODO Auto-generated method stub

}

@Override
protected void initSpringApplication(ConfigurableWebApplicationContext arg0) {
// TODO Auto-generated method stub
Window mainWindow = new Window(“Vaadin+Spring Demo #3”);
Label test = new Label();
test.setValue(“Hahahah”);
mainWindow.addComponent(test);
setMainWindow(mainWindow);
}

}

That is all the basic configuration for the Vaadin application that uses the spring context that is combined with the neo4j installation. Be careful that you should use at least the neo4j version 2.1 to be able to start the spring context unless you will face error in creation of bean REST in neo4j.

Vaadin + Springdata + MySQL + Hbncontainer

Hi Guys,

I’m recently a small project that required me to put my attention to the RDBMS after some long times ago develop application with Graph Database.

Playing again with it was like dejavu to me. OK, without any further a do so just get hand on to the all tools that is mentioned in the subject.

Today, I am going to put the step by step of using Vaadin with the combination of Springdata to access MySQL. Since I don’t like to get my hands dirty to create database table directly so that I use ORM as the bridge between my object and database. Here I use hibernate to manage the all query things and database table creation.

Let start the step below.

1. Setting up your environment (Jboss AS, Eclipse and Etc)

2. Create Vaadin project from maven

3. Follow the following dependencies to  POM.xml. Please ensure that you are using hibernate 4 and the latest sporingdata because the Hbncontainer is only suit with these requirement.

<properties></pre>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
 <vaadin.version>6.7.1</vaadin.version>
 <gwt.version>2.3.0</gwt.version>
 <gwt.plugin.version>2.2.0</gwt.plugin.version>
 </properties>

<dependencies>
 <dependency>
 <groupId>com.vaadin</groupId>
 <artifactId>vaadin</artifactId>
 <version>${vaadin.version}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-jpa</artifactId>
 <version>1.1.0.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>mysql</groupId>
 <artifactId>mysql-connector-java</artifactId>
 <version>5.1.20</version>
 </dependency>
 <dependency>
 <groupId>commons-dbcp</groupId>
 <artifactId>commons-dbcp</artifactId>
 <version>1.4</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-context</artifactId>
 <version>3.1.1.RELEASE</version>
 </dependency>

<dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-orm</artifactId>
 <version>3.1.1.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.hibernate</groupId>
 <artifactId>hibernate-entitymanager</artifactId>
 <version>4.1.1.Final</version>
 </dependency>
 <dependency>
 <groupId>cglib</groupId>
 <artifactId>cglib</artifactId>
 <version>2.2.2</version>
 <scope>runtime</scope>
 </dependency>

<dependency>
 <groupId>org.vaadin.addons</groupId>
 <artifactId>hbncontainer</artifactId>
 <version>1.1.0</version>
 </dependency>
 <dependency>
 <groupId>org.slf4j</groupId>
 <artifactId>slf4j-simple</artifactId>
 <version>1.6.6</version>
 </dependency>
</dependencies>
<pre>

4. Create the datasource-bean.xml in the application classpath as highlighted below


5. Here is the datasource-bean.xml looks like


<?xml version="1.0" encoding="UTF-8"?></pre>
<beans xmlns="http://www.springframework.org/schema/beans"
 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
 xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
 <bean id="sessionFactoryhibernate"
 class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
 <property name="dataSource" ref="mySqlDataSource" />

<property name="hibernateProperties">
 <props>
 <prop key="hibernate.show_sql">true</prop>
 <prop key="hibernate.current_session_context_class">thread</prop>
 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
 <prop key="hibernate.hbm2ddl.auto">update</prop>
 </props>
 </property>
 <property name="annotatedClasses">
 <list>
 <value>study.mehibernate.Biodata</value>
 </list>
 </property>
 </bean>
 <bean id="mySqlDataSource" class="org.apache.commons.dbcp.BasicDataSource">
 <property name="driverClassName" value="com.mysql.jdbc.Driver" />
 <property name="url" value="jdbc:mysql://localhost:3306/myappllication" />
 <property name="username" value="root" />
 <property name="password" value="***" />
 </bean>
 <bean id="tradeDAO" class="study.mehibernate.HibernateDAO">
 <property name="sessionFactory" ref="sessionFactoryhibernate" />
 </bean>

</beans>

6. Create the hibernate helper. This hibernate helper will manage the session management


package study.mehibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class HibernateDAO {
 SessionFactory sessionFactory = null;
 Session session = null;

 public SessionFactory getFactory() {
 return sessionFactory;
 }

public SessionFactory getSessionFactory() {
 return sessionFactory;
 }

public void setSessionFactory(SessionFactory sessionFactory) {
 this.sessionFactory = sessionFactory;
 }

public Session getSession() {
 return session;
 }

public void setSession(Session session) {
 this.session = session;
 }
}

7. Create the spring context helper


package study.mehibernate;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.orm.hibernate3.HibernateTransactionManager;

public class ContextHelper {

 public static ApplicationContext ctx = null;
 public static Session session = null;
 private static HibernateDAO dao = null;

public static ApplicationContext getCtx() {

 if(ctx == null){
 ctx = new ClassPathXmlApplicationContext("datasource-bean.xml");
 }
 return ctx;
 }

 public static Session getSession(){
 if(ctx == null){
 getCtx();
 }
 if(dao == null){
 dao = ctx.getBean("tradeDAO", HibernateDAO.class);
 }
 if(session == null){
 session = dao.getSessionFactory().openSession();
 }
 if(!session.getTransaction().isActive()){
 session.beginTransaction();
 }
 return session;
 }

 public static void InitHibernate(){
 if(ctx == null){
 getCtx();
 }
 if(dao == null){
 dao = ctx.getBean("tradeDAO", HibernateDAO.class);
 }
  }
}

8. Don’t forget to implement the vaadin hibernate util session manager

</pre>
package study.mehibernate;

import org.hibernate.Session;

import com.vaadin.data.hbnutil.HbnContainer.SessionManager;

public class DataSource implements SessionManager {

public Session getSession() {
 // TODO Auto-generated method stub
 return ContextHelper.getSession();
 }

}

9. Create the Hibernate entity, I use a very simple one to become example

</pre>
package study.mehibernate;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Biodata {

int id;
 String name;
 int age;

 @Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 @Column(name = "id", unique = true, nullable = false)
 public int getId() {
 return id;
 }

 public void setId(int id) {
 this.id = id;
 }

 @Column(name = "name", unique = false, nullable = false)
 public String getName() {
 return name;
 }
 public void setName(String name) {
 this.name = name;
 }

 @Column(name = "age", unique = false, nullable = false)
 public int getAge() {
 return age;
 }
 public void setAge(int age) {
 this.age = age;
 }
}

10. Finaly for the preparation above is all complete, so we can now create the application. Follow my example

</pre>
package study.mehibernate;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application
{
 private Window window;

@Override
 public void init()
 {
 ContextHelper.InitHibernate();
 window = new MyWindow();
 setMainWindow(window);

 }
}

  11. Create my own window


package study.mehibernate;

import java.util.Collection;
import java.util.List;

import org.hibernate.Session;

import com.vaadin.data.hbnutil.HbnContainer;
import com.vaadin.data.hbnutil.HbnContainer.SessionManager;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Table;
import com.vaadin.ui.Window;

public class MyWindow extends Window {

 Session session;
 Table table;
 HbnContainer<Biodata> container;
 List<Biodata> temp;
 ComboBox combo;

 public MyWindow() {
 // TODO Auto-generated constructor stub
 session = ContextHelper.getSession();

 table = new Table();
 container = new HbnContainer<Biodata>(Biodata.class, new DataSource());
 table.setContainerDataSource(container);
 table.setVisibleColumns(new String[] {"name", "age"});
 addComponent(table);
 combo = new ComboBox();

combo.setItemCaptionMode(ComboBox.ITEM_CAPTION_MODE_EXPLICIT);
 combo.setItemCaptionPropertyId("name");

 combo.setContainerDataSource(container);

 addComponent(combo);
 //initTable();
 }

 public void initTable(){
 session.beginTransaction();

 Biodata bio1 = new Biodata();
 bio1.setName("Rio");
 bio1.setAge(29);

 Biodata bio2 = new Biodata();
 bio2.setName("Annis");
 bio2.setAge(27);

 Biodata bio3 = new Biodata();
 bio3.setName("Daanish");
 bio3.setAge(2);

 session.save(bio1);
 session.save(bio2);
 session.save(bio3);

 session.getTransaction().commit();

 }

}

  Yes this is it all the code that you need .. and the result of the application is just like the picture below  

Vaadin + Spring Data + Neo4j (with REST)

Hi, I’m finally back to write a very simple tutorial for the one who love vaadin and neo4j. In this tutorial I changed a bit of the configuration of the database from the embedded to become REST thus give a better handling.

In this application my scenario is to create a very simple registration which involve of saving process and retrieving back the data.

OK, without any further intro here are the steps.

1. Start your eclipse.

2. Setup a Vaadin maven project (please check my previous tutorial on how to create the Vaadin maven project)

3. Edit your pom.xml and add this dependecies

<dependencies>
 <dependency>
 <groupId>com.vaadin</groupId>
 <artifactId>vaadin</artifactId>
 <version>6.7.8</version>
 </dependency>
 <dependency>
 <groupId>org.aspectj</groupId>
 <artifactId>aspectjtools</artifactId>
 <version>1.6.12</version>
 </dependency>
 <dependency>
 <groupId>org.apache.tomcat</groupId>
 <artifactId>jsp-api</artifactId>
 <version>6.0.35</version>
 </dependency>
 <dependency>
 <groupId>org.springframework</groupId>
 <artifactId>spring-webmvc</artifactId>
 <version>${spring.version}</version>
 </dependency>
 <dependency>
 <groupId>com.vaadin</groupId>
 <artifactId>vaadin</artifactId>
 <version>${vaadin.version}</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-neo4j</artifactId>
 <version>2.0.1.RELEASE</version>
 </dependency>
 <dependency>
 <groupId>org.springframework.data</groupId>
 <artifactId>spring-data-neo4j-rest</artifactId>
 <version>2.0.0.RELEASE</version>
 <exclusions>
 <exclusion>
 <artifactId>jersey-server</artifactId>
 <groupId>com.sun.jersey</groupId>
 </exclusion>
 </exclusions>
 </dependency>
 <dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-core-asl</artifactId>
 <version>1.9.4</version>
 </dependency>
 <dependency>
 <groupId>org.codehaus.jackson</groupId>
 <artifactId>jackson-mapper-asl</artifactId>
 <version>1.9.4</version>
 </dependency>
 </dependencies>

4. Edit the applicationContext-spring.xml and add the Neo4j url path and bean creation

<bean id="restGraphDatabase"
 class="org.springframework.data.neo4j.rest.SpringRestGraphDatabase">
 <constructor-arg value="http://localhost:7474/db/data/" />
 </bean>

 <neo4j:config graphDatabaseService="restGraphDatabase" />

5. Create SpringContextHelper Class as below codes, this class is needed in order to get the object that created by the spring context so that Vaadin may be able to use the object

package spring.neosample;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;

public class SpringContextHelper {

private ApplicationContext context;

public SpringContextHelper(Application application) {

ServletContext servletContext = ((WebApplicationContext) application
 .getContext()).getHttpSession().getServletContext();
 context = WebApplicationContextUtils
 .getRequiredWebApplicationContext(servletContext);
 }

public Object getBean(final String beanRef) {
 return context.getBean(beanRef);
 }

}

6. Create a simple vaadin window, this window is to simulate the registration form

package spring.neosample;

import com.vaadin.data.util.BeanItem;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Form;
import com.vaadin.ui.Window;

public class MyWindow extends Window {

 Form loginForm;
 BeanItem<loginObject> loginObjectDataBeanItem;
 loginObject myLoginEntity;
 Button createAccount;
 Button testAccount;
 LoginRepositoryImpl impl;

 public MyWindow(LoginRepositoryImpl impl) {
 this.impl = impl;
 loginForm = new Form();
 myLoginEntity = new loginObject();
 loginObjectDataBeanItem = new BeanItem<loginObject>(myLoginEntity);
 loginForm.setItemDataSource(loginObjectDataBeanItem);
 loginForm.setImmediate(true);

 //Manage the visible item in form
 ManageFormItem();

 addComponent(loginForm);
 //Create Button for action query and creation save to database
 CreateButton();
 }

 void ManageFormItem(){
 String titleTobeShown [] = new String[] {"userName"};
 loginForm.setVisibleItemProperties(titleTobeShown);
 }

 void CreateButton(){
 createAccount = new Button("Create Account");
 createAccount.addListener(ClickEvent.class, this, "CreateAccount");

 testAccount = new Button("Test Account");
 testAccount.addListener(ClickEvent.class, this, "TestAccount");

 addComponent(createAccount);
 addComponent(testAccount);
 }

 public void TestAccount(ClickEvent event){

 if(impl.checkAccount(myLoginEntity)){
 showNotification("Account is Found");
 }else {
 showNotification("Cannot Found Account");
 }
 }

 public void CreateAccount(ClickEvent event){

 impl.createAccount(myLoginEntity);
 showNotification("Account Created");
 }

}

7. Modify the vaadin application class to become look like below

</pre>
package spring.neosample;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

/**
 * The Application's "main" class
 */
@SuppressWarnings("serial")
public class MyVaadinApplication extends Application
{
 private Window window;

@Override
 public void init()
 {
 SpringContextHelper contextHelper = new SpringContextHelper(this);

 LoginRepositoryImpl impl = (LoginRepositoryImpl)contextHelper.getBean("LoginRepository");

 window = new MyWindow(impl);

 setMainWindow(window);
 }

}
<pre>

8. Create the Neo4j graph repository, this repository is needed to do the database transaction such as saving and query. Please create the class as below

package spring.neosample;</pre>
import org.springframework.data.neo4j.annotation.GraphId;
import org.springframework.data.neo4j.annotation.Indexed;
import org.springframework.data.neo4j.annotation.NodeEntity;

@NodeEntity
public class loginObject {

 @GraphId
 Long id;

 @Indexed
 String userName;

 public loginObject() {
 userName = new String();
 }

public Long getId() {
 return id;
 }

public void setId(Long id) {
 this.id = id;
 }

public String getUserName() {
 return userName;
 }

public void setUserName(String userName) {
 this.userName = userName;
 }
 }
<pre>

 

</pre>
package spring.neosample;

public interface LoginReporsitory {

}


&nbsp;

package spring.neosample;

import org.springframework.data.neo4j.repository.GraphRepository;
import org.springframework.data.neo4j.repository.NamedIndexRepository;

public interface LoginGraph extends LoginReporsitory, GraphRepository<loginObject>, NamedIndexRepository<loginObject> {

}


package spring.neosample;

import org.springframework.beans.factory.annotation.Autowired;

public class LoginRepositoryImpl implements LoginReporsitory {

 @Autowired
 LoginGraph neoGraph;

 public void createAccount(loginObject object){
 neoGraph.save(object);
 }

 public boolean checkAccount(loginObject object){
 loginObject result = neoGraph.findByPropertyValue("userName", object.getUserName());

if(result == null){
 return false;
 }
 return true;
 }

}

9. Modify your applicationContext-spring.xml and add the following code


<neo4j:repositories base-package="spring.neosample"></neo4j:repositories>

<bean id="LoginRepository" class="spring.neosample.LoginRepositoryImpl"> </bean>

<tx:annotation-driven mode="aspectj" />

10.  Try to deploy and run your application. BUT don’t forget to start your neo4j server before deploying your application since the application context will search the defined server during application start up or deployment.

Testing the application

1. Try to put any user name and click button “Test Account”, there should a notification that account not found

2. Try to put any user name e.g “Rio” then click on the button “Create Account”, there should notification says that the account is created

3. Try to put the user name that you’ve just created e.g “Rio” and click on the button “Test Account” and then now there should be a notification saying that “Account is found”

OK I think that is all. Just give me message any time there is something that doesn’t work in this tutorial. I’ll be happy to share my knowledge.

Please find the full code here.

http://goo.gl/i1BH2

Vaadin + Spring Data + Neo4j

Here is a little and simple basic configuration for those who wants to develop application based on Vaadin combined with Neo4j. First you shall download the tools.

1. Eclipse

2. JBoss AS 7.1

3. Install maven plugin for Eclipse

Firstly you need to setup  your eclipse to be able to run JBoss (download and install the Jboss Tools). After the IDE and the server is done then you can go further to setup your project configuration. Please kindly follow the steps below

1. Run Eclipse

2. Create maven project and select maven project and press Next

 

3. Just click next

4. On the filter you type Vaadin. And select for Vaadin-architype-client version 1.6.1. and Press Next

5. Give your project a name and finish

6. Yeah your vaadin maven project is now created

7. Start up the JBoss server from eclipse

8. Try to publish the application to the server. Don’t forget to include the gwt-user.jar

9. Yes it is now the Vaadin project is ready. Then the next is to setup the spring data framework to enable us to communicate with the application

10.  Edit your pom.xml and add the below dependencies

</pre>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<vaadin.version>6.7.6</vaadin.version>
<gwt.version>2.3.0</gwt.version>
<gwt.plugin.version>2.2.0</gwt.plugin.version>
<spring.version>3.1.0.RELEASE</spring.version>
 </properties>


<dependencies>

<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.6.12</version>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>jsp-api</artifactId>
<version>6.0.35</version>
</dependency>

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>

<dependency>
<groupId>com.vaadin</groupId>
<artifactId>vaadin</artifactId>
<version>${vaadin.version}</version>
</dependency>

<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-neo4j</artifactId>
<version>2.0.1.RELEASE</version>
</dependency>

</dependencies>

11. Edit  and add the following code to Web.xml

</pre>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext-spring.xml</param-value>
 </context-param>

<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
 </listener>
 <listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
 </listener>

12. Create application context file

13. Add this to the applicationContext-spring.xml

</pre>
<context:spring-configured />
 <context:annotation-config />
 <context:component-scan base-package="spring.TestMinimalist"></context:component-scan>
 <neo4j:config storeDirectory="C:/Users/Rio/Java/Neo4JDatabase2" />
 <neo4j:repositories base-package="spring.TestMinimalist"></neo4j:repositories>
 <bean id="myWindow" class="spring.TestMinimalist.MyWindow" ></bean>
 <tx:annotation-driven mode="aspectj" />

14. Create context helper class for spring.

</pre>
package spring.TestMinimalist;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import com.vaadin.Application;
import com.vaadin.terminal.gwt.server.WebApplicationContext;

public class SpringContextHelper {

private ApplicationContext context;

public SpringContextHelper(Application application) {
 ServletContext servletContext = ((WebApplicationContext) application
 .getContext()).getHttpSession().getServletContext();
 context = WebApplicationContextUtils
 .getRequiredWebApplicationContext(servletContext);
 }

public Object getBean(final String beanRef) {
 return context.getBean(beanRef);
 }

}

15. Now edit your vaadin file application file

</pre>
package spring.TestMinimalist;

import com.vaadin.Application;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.Window;

@SuppressWarnings("serial")
public class MyVaadinApplication extends Application
{
 private Window window;

@Override
 public void init()
 {
 SpringContextHelper contextHelper = new SpringContextHelper(this);
 MyWindow myWindow = (MyWindow)contextHelper.getBean("myWindow");
 setMainWindow(myWindow);

 }

}
<pre>

15. Here is the full directory list of my project

That is all the configuration needed, After all the setting required then you can publish to your web server to play on. To access the database i will explain it on the different post. you can find my full directory of my project on this link http://goo.gl/ntUen