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

2 comments

  1. Hey rioasmara,

    That was fast, thank you so much, it’s been of great help … keep on posting good stuff like that. I’ll test the set up tomorow and let you know how it goes for me !!

    Again thank a lot

    jamrok

  2. This is a great tutorial and thanks for the great code, only one question is it possible to enable aspectj support to the project and how? if you can please help I am busting my head to find this with no luck so far!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s