HomeAbout Mark van HedelContact Mark van Hedel



Blaze DS and Spring :: Part 2

Ok let's continue, now let's see what we want to send over the line, for our application, first when a user fills in it's username we'll send a string to the server containing this username. Since we don't want to use with strings, the next thing we are going to do is find a user object on the server and send this back to the client to tell the client that this user is connected. Last thing in the demo we want to send over the line now is a task object as soon as that is created so it can be shown at the task list of the user.

So let's look at the Java side of things, first let's create a user object that we can use to save to a database and pass on to FLEX.

Now there is one thing we had to change in our object to prevent Blaze DS errors, that is the Lazy initialization of hibernate, since this user object has a list of tasks assigned to him. Now we only want the user object for now and don't need everything, but Blaze needs the entire object and dependencies in order to send it to FLEX, so we need to remove the tasks here or set the fetchtype to eager to retrieve the entire object. I know setting this to eager is a bad way to work, but in this case we did it for demo purposes. So please don't shoot me for it.

package org.flexpair.flex.presentation.task.manager.core.domain;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.hibernate.annotations.LazyCollection;

/**
* The Interface User.
*/
@Entity
@Table(name = "USER")
public class User {

/** The id. */
@Id
@GeneratedValue
@Column(name = "ID")
private Long id;

/** The user name. */
@Column(name = "USER_NAME")
private String userName;

/** The online. */
@Column(name = "ONLINE")
private boolean online = true;

/** The tasks. */
@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
@JoinColumn(name = "USER_ID")
private List<Task> tasks = new ArrayList<Task>();

/**
* Instantiates a new user.
*
* @param userName the user name
*/
public User(String userName) {
this.userName = userName;
}

/**
* Instantiates a new user.
*/
public User() {
// default constructor...
}

/**
* Gets the tasks.
*
* @return the tasks
*/
public List<Task> getTasks() {
return tasks;
}

/**
* Sets the tasks.
*
* @param tasks
* the new tasks
*/
public void setTasks(List<Task> tasks) {
this.tasks = tasks;
}

/**
* Gets the id.
*
* @return the id
*/
public Long getId() {
return id;
}

/**
* Sets the id.
*
* @param id
* the new id
*/
public void setId(Long id) {
this.id = id;
}

/**
* Gets the user name.
*
* @return the user name
*/
public String getUserName() {
return userName;
}

/**
* Sets the user name.
*
* @param userName
* the new user name
*/
public void setUserName(String userName) {
this.userName = userName;
}

/**
* Checks if is online.
*
* @return true, if is online
*/
public boolean isOnline() {
return online;
}

/**
* Sets the online.
*
* @param online
* the new online
*/
public void setOnline(boolean online) {
this.online = online;
}
}

I will not bore you all with the entire application, just with the parts that are needed in order to get the entire application to work, and at least give you all an example of how it's done. So let's continue, what we wanted to do next was receive a string in Java and check that with the user table and return a user instance to FLEX, not as a return to the call with the user string but as a separate call since we want this user object in different applications and not just in the application where the user entered the username.

So let's create a Spring service that we can use to do our work and that can be contacted by Flex later. Now let's just look at our Java method we want to use and forget the rest of this class for a second because we are not going to use it for now.

/**
    * {@inheritDoc}
    */
   public void loginUser(String userName) {
      User user = userDao.getUser(userName);
      if (user == null) {
         userDao.createUser(userName);
         user = userDao.getUser(userName);
      }
      MessageBroker msgBroker = FlexContext.getMessageBroker();
      AsyncMessage msg = new AsyncMessage();
      Map<String,Object> body = new HashMap<String,Object>();
      user.setOnline(true);
      body.put("newUser", user);
      msg.setDestination("taskusers");
      msg.setBody(body);
      msg.setHeader("username", user.getUserName());
      msg.setClientId(UUIDUtils.createUUID());
      msg.setMessageId(UUIDUtils.createUUID());
      msg.setTimestamp(System.currentTimeMillis());
      msgBroker.routeMessageToService(msg, null);
    LOG.info("User: " + userName + " logged in...");
   }

This method receives a string and checks if there is a userobject in the database with this username, if there is it retrieves our user object, in case there isn't one we create a user with this name. Now what we do next is ask Blaze DS for the messagebroker it uses to route messages to all clients. Now that that is done we can create a message of type AsyncMessage. This is the same message class the Flex frontend uses to send messages to the messaging server. This message object that we create can contain key/value pairs in the header property with all simple values like strings.

These can be used to filter on messages you want to receive, so you may enter that you only want to receive messages that are only directed to you. In the body property of this object you can add a lot of different types of objects, in this case we create a Map and add the user object there to send it to all connected clients. Now there is one more thing we need to add to the client and that is a destination. This destination is the destination we are going to define in Blaze dataservices and going to subscribe to later in FLEX.

Last action we do here is route this message to the messageservice of Blaze and let Blaze do it's magic. I'll continue this in my next post, but I hope this part will already help you in creating the server part of your application.

Related Blog Entries

Comments
BlogCFC was created by Raymond Camden. This blog is running version 5.9.002. Contact Blog Owner