Monday, October 3, 2011

Overlay Types combined with POJO. Found a solution (ugly?!). But is there a better one?

Hi!

I'm building a web app with JSON and PHP (+MySQL). Right now I'm
coding an image gallery. In the database I've got the following
structure:
TABLE: gallery {
int galleryId
String description
String primaryImageUrl (<- this should be imageId, but then it
gets even more complicated with JSON)
}

TABLE: image {
int imageId
int galleryId (<- just for the query, doesn't get fetched)
String url
}

It's clear that each gallery contains a list of images. In Java this
would mean that I've got a HashMap<GalleryId, Gallery> and each
Gallery has a HashMap<ImageId, Image>. Now I'm using the easiest way
of retrieving this data: JSON. And for best user experience I only
fetch the data that is actually required. This means: When starting
the gallery I fetch ONLY the gallerys, for sure as JS Overlay Types.
When now a gallery is clicked I open the gallery and fetch all images
belonging to it. Pretty simple. But now comes the complicated part:
All those Images are belonging to that single gallery, though I think
it would be best to have them INSIDE the Gallery Object (remember it's
an overlay Type). For performance reasons I want to store those images
in a HashMap. That's the simple side. Just take your JsArray<ImageJso>
and put every element to the HashMap.

Now I've done the following (maybe stupid thing) to put it all
together:


public class GalleryJso extends JavaScriptObject implements Gallery {
protected GalleryJso() {}

private static HashMap<Gallery, HashMap<Integer, Image>> imagesMap =
new HashMap<Gallery, HashMap<Integer, Image>>();

/* JSNI getters */

@Override
public final HashMap<Integer, Image> getImages() {
if(!imagesMap.containsKey(this)) {
imagesMap.put(this, new HashMap<Integer, Image>());
}
return imagesMap.get(this);
}
}

It's allowed to have static fields in a JSO (but no instance fields).
Now I'd like to hear from you if there's a better way to get things
done the way I want (or maybe change the way to make it even easier or
faster). I'm not really familiar with Overlay Types, but my
programming experience tells me that the way I've done it is quite
ugly.

And there's another problem: I now store TWO HashMaps of the same
type. In my GalleryPresenter there's another one: HashMap<Integer,
Gallery> (so i can get fast a gallery for a specific galleryId). Do I
really have two? Or are both using the same data? And it's just a
pointer and not a complete copy of the data?

Maybe someone could help me improving my application (it's not that
big, but it may grow and i don't want to have possible bottlenecks
again and again).


Bye and thanks :)

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment