views:

53

answers:

1

I'm new to Flex and BlazeDS and I'm trying to implement a simple application which uses Flex on the front end and a Spring/Hibernate application on the back end, with communication between the two going over a BlazeDS channel.

I'm seeking direction as to the best and/or simplest way to approach this. I have the UI set up in such a way that the user is presented with a file chooser in which they pick the image file they want to upload. When this is chosen and submitted (as a form submission) then the server side should receive the image file data as well as some related metadata such as a description and date, then populate a Hibernate entity/POJO with the image file data and related metadata, and then persist the entity/POJO into the database.

I have found some examples of how you would do a file upload and download using servlets here and the FileReference class (here and here) but these don't appear to address the problem in a way which leverages BlazeDS and/or Spring/Hibernate. I want to put the image file data and related metadata (description, capture date, etc.) into a value object within the Flex application and then send this over BlazeDS to a service provided by my Spring/Hibernate application running on Tomcat. In this service I want to extract the image data (both the actual JPG/PNG/GIF data and the related metadata such as description, etc.) from the value object sent from the Flex app into an entity/POJO which is then persisted via Hibernate in my database.

Can this be done, and if so what's the best way to go about it? Am I mistaken in assuming that if I use BlazeDS then I am somehow bypassing the need to provide HTTP-based services such as servlets on the server side and instead I can use my Java services as "RemoteObjects"? Is there necessarily a one-to-one mapping between Java POJO/entity class and the Flex value object class when making this sort of transfer? If so is there a tool which creates corresponding Flex value objects from Java POJOs or vice versa.

Thanks in advance for your help, comments, suggestions, etc.

--James

Update: Some code to make this more clear:

I have this as my value object in Flex:

package valueobjects
{
    import flash.utils.ByteArray;

    [Bindable]
    [RemoteClass(alias="com.abc.example.persistence.entity.Image")]

    public class Image
    {
        public var id:Number;
        public var captureDate:Date;
        public var description:String;
        public var imageData:ByteArray;

        public function Image() {}        
    }

I am assuming that this can be used as a one-to-one mapping to the POJO class used by my service and DAO classes on the server-side, which looks like this:

package com.abc.example.persistence.entity;

import java.sql.Blob;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity(name = "IMAGE")
public class Image
    extends AbstractBaseEntity<Long>
{
    private String description;
    private Date captureDate;
    private Blob imageData;

    @Column(name = "CAPTURE_DATE", nullable = true)
    public Date getCaptureDate()
    {
        return captureDate;
    }

    @Column(name = "DESCRIPTION", nullable = true)
    public String getDescription()
    {
        return description;
    }

    @Column(name = "IMAGE_DATA", nullable = true)
    public Blob getImageData()
    {
        return imageData;
    }

    public void setCaptureDate(final Date captureDate)
    {
        this.captureDate = captureDate;
    }

    public void setDescription(final String description)
    {
        this.description = description;
    }

    public void setImageData(final Blob imageData)
    {
        this.imageData = imageData;
    }
}

In my Flex application I populate the fields of an Image object with a description string, date, and image file data (based on the user's file selection and text input for the description) and then call a method on the RemoteObject which is mapped to the service running on Tomcat. I make the RemoteObject service call within my Flex code using the Image value object as the argument, but the service method running on the servier side actually expects an argument of the POJO/entity type, and it's here that I am thinking that some sort of conversion/transformation between the Flex value object and the Java POJO will occur (by virtue of the RemoteClass alias setting on the value object's class declaration), but it doesn't seem to be happening that way because when I debug the application the Java service is only getting null values when the service call is made.

In my Flex application I have a FileReference and Image value object as public, bindable variables:

[Bindable]
public var imageToBeArchivedFileReference:FileReference = new FileReference();
[Bindable]
public var imageToBeArchivedValueObject:valueobjects.Image = new valueobjects.Image();

There is also an event handler to browse for a file when the user clicks on a file select button:

protected function imageFileSelectButton_clickHandler(event:MouseEvent):void
{
    var imageFileFilter:FileFilter = new FileFilter("Images (*.jpg, *.jpeg, *.gif, *.png)", "*.jpg;*.jpeg;*.gif;*.png");
    var fileTypes:Array = new Array();
    fileTypes.push(imageFileFilter);
    imageToBeArchivedFileReference.addEventListener(Event.SELECT, imageToBeArchived_fileSelectHandler);
    imageToBeArchivedFileReference.browse(fileTypes);
}

There is an event handler which builds the value object when the image file has been selected:

private function imageToBeArchived_fileSelectHandler(event:Event):void
{
    imageToBeArchivedFileReference.load();
    imageToBeArchivedValueObject = new valueobjects.Image()
    imageToBeArchivedValueObject.imageData = imageToBeArchivedFileReference.data;
    imageToBeArchivedValueObject.description = imageToBeArchivedDescription.text;
    imageToBeArchivedValueObject.captureDate = imageToBeArchivedFileReference.creationDate;
}

and there's an event handler which is invoked when the user clicks on the submit button to perform the image save/upload:

protected function archiveImageButton_clickHandler(event:MouseEvent):void
{
    imageArchivalService.archiveImage(imageToBeArchived);
}

On the server side my Java class is doing a simple save of the POJO:

public void archiveImage(final Image image)
{
    imageDao.saveOrUpdate(image);
}

When I set a breakpoint in the method above and look at the image variable it looks to be empty, so I'm assuming that the transformation from the Flex value object to the Java POJO did not go as expected and that there's more to it than just adding a RemoteClass alias in the Flex value object class.

+1  A: 

Check out this example, it is all there.

http://biemond.blogspot.com/2008/08/flex-upload-and-download-with-blazeds.html

Don't use the loader class, use the readBytes call.

Make sure you go to the comments, there are valuable info there.

Cheers

Ernani Joppert
Thanks for this reference, Ernani, and for pointing out that I need to use the readBytes() call.The issue I still seem to be facing is that when I make the RemoteObject service call with a Flex value object as the parameter I don't seem to get an equivalent representation on the server side when the service call receives the message. When I look at the received service call in the debugger (on Tomcat) I see that all of the fields in the POJO parameter value (which I am expecting to correspond to the value object parameter in the original service call on the Flex side) are null.
James Adams
I suspect that I'm not correctly doing the RemoteClass aliasing in the Flex value object class and hence I'm not getting the mapping of the Flex value object to the Java POJO that I'm going for. I'll post another question on this specific topic if I can't find the clues I need via Google, and I'll update here with my findings in case it can help someone else in the future.
James Adams
This reference has helped me figure this out, more or less, especially by pointing out how to correctly map datatypes between Flex value object classes and Java POJO classes:http://livedocs.adobe.com/blazeds/1/blazeds_devguide/help.html?content=serialize_data_3.html
James Adams
Hey, welcome man! I am glad it assisted you in any way, not sure why I didn't got any e-mail notification about these replies. Hopefully you found everything, if not let me know. Cheers, Ernani
Ernani Joppert