Place Service

30 views
Skip to first unread message

plcoirier

unread,
Aug 10, 2009, 9:16:07 PM8/10/09
to Mvp4g
I'd like to add the place service described by Ray Ryan to the Mvp4g
framework to ease history management.

In GWT forum, Thomas Broyer gave a good description of this service:
"""The whole point of the "place" service is to decouple your "URL
patterns" from your components: the place service just:
- listens to history change events, "interprets" them and fire
appropriate events on the event bus ("please display the contact with
ID=12345", "please display the list of contacts")
- listens to events fired by other components and updates the URL
(History.newItem(newToken, false)); for instance, when you click on a
contact in the contact list, the list fires a "picked contact (the
one
with ID=12345)" event, some component listens to it and updates the
display to show the contact info, and the place service listens to
it,
maps it to a history token and updates the History. Or maybe the
component that displays the contact fires a "contact displayed (the
one with ID=12345)" event, and the place service listens to this one
instead. """

I think we could add a Place class that acts like a presenter but with
no view.

The main question I'm wondering, can the translation from history to
an event (and inversely) be automate?

I think that most of the time an event that trigger the Place module
to change the URL won't be the one sent when history is updated with
this url. Another way to say it is that:
translateURLtoEvent( translateEventtoURL ( event ) ) won't be equals
to event.

Indeed I don't think that a presenter will react the same way when he
is indirectly triggered by another presenter as when he is indirectly
triggered by History update. For example, for History update, it may
have to check that data are available and if it isn't the case,
retrieve it thanks to a RPC call. So that's why another event should
be sent in order the presenter to handle it differently.
Maybe we could say that when the place module receives an event with
type "xxx" to update the url, then when history is updated with this
url, the event "xxxHistory" should be send.

Another problem is how can the object sent with the event be
automaticaly translated to an URL and inversily how can the URL be
automaticaly translated to an object ? And who realise this
translation?
It can't be the presenter because you should be able to remove a
presenter as an handler without consequences on the rest of the
application and above all there are a lot of problems bc one-to-many
presenters can be associated to an event.
It could be the Place service. The developper will then need to extend
a place service class to write his translation.
It could be the model. We can image that bean that can be translated
implements an interface with two methods (fromUrl and toUrl).

I'd like your opinions and your feedbacks on history management before
starting this feature.

Thanks


plcoirier

unread,
Sep 17, 2009, 2:31:13 AM9/17/09
to Mvp4g
I added a new wiki page with UML diagrams to show how I imagine the
place service with Mvp4g: http://code.google.com/p/mvp4g/wiki/PlaceServiceInMvp4g

Thanks for any advices or opinions you can give me.

sasajovancic

unread,
Sep 21, 2009, 4:47:51 AM9/21/09
to Mvp4g
I think place service is good proposal.

I have one concern how some big event objects like Widgets are going
to be stored in string. Or they should be converted to db or some
cache file system.

What will happen if widget state is changed (state of widget, view
changed in meantime) before we go back?

plcoirier

unread,
Sep 22, 2009, 12:54:25 AM9/22/09
to Mvp4g


On Sep 21, 10:47 am, sasajovancic <sasajovan...@hotmail.com> wrote:
> I think place service is good proposal.
>
> I have one concern how some big event objects like Widgets are going
> to be stored in string. Or they should be converted to db or some
> cache file system.
I don't think you will store widgets but more beans that contain
information needed to retrieve the same state you were at.
You are free to store the bean the way you want.

For example with the following bean:

public class UserBean{
String lastName;
String firstName;
int id;

....

}

You could decide to store all the information in the URL:
myurl#myAction?lastName=...&firstName=...&id=...

so you could have the following History Adapter:

public MyHistoryAdapter(){

...

public String convertToToken(UserBean user){
return "lastName=" + user.getLastName() + "&firstName=" +
user.getFirstName() + "&id=" + user.getId();
}

public void convertFromToken(String event, String token, EventBus
eventBus){
String[] attributes = token.split('&'');
UserBean user =new UserBean();
user.setLastName(attributes[0].split('=')[1])
...

eventBus.dispatch(event, user);
}

}

You could also decide to store in the URL only a part of your bean
(for example the key to identify it) and retrieve the rest of the
information from the server:
myurl#myAction?id=...

You will have then the following History adapter:

public MyHistoryAdapter(){

...

public String convertToToken(UserBean user){
return "id=" + user.getId();
}

public void convertFromToken(String event, String token, EventBus
eventBus){
UserBean user =new UserBean();
user.setId(attributes[0].split('=')[1])

services.retrieveUserInformation(user, new AsyncCallback<UserBean>
() {
public void onFailure( Throwable caught ) {
//do sthg
}

public void onSuccess( UserBean user ) {
eventBus.dispatch(event, user);
}
}

}

I actually just noticed that History Adapter functions don't manage
asynchronious called. Maybe I will add the event bus as an attribute
of convertFromToken function(as I did above) but I will have to think
about it.


>
> What will happen if widget state is changed (state of widget, view
> changed in meantime) before we go back?

I think when you deal with history, there are at least 3 cases to
manage when history is fired:
-You can retrieve needed information for the action and the state of
your application (ie your widgets) allows you to execute it
=> in this case, you can just fire the event to the event bus.
-You can retrieve needed information for the action but the state of
your application don't allow you to execute your action (for example
extra information is needed because information that were present when
you stored your token is not present anymore)
=> in this case, you need to fire an event different from the one you
stored but with the same information to tell your application that you
try to execute the stored event but it needs to do something else
first).
-You can't retrieve information (for example session is expired)
=> in this case, you send an event with no information in order your
application to display an error message.

The framework won't be able to automaticaly manage these cases. It
will be the developer job to tell the framework what event to send by
coding the convertFromToken function.

For example:

public void convertFromToken(String myEvent, String token, EventBus
eventBus){
UserBean user =new UserBean();
user.setId(attributes[0].split('=')[1])

services.retrieveUserInformation(user, new AsyncCallback<UserBean>
() {
public void onFailure( Throwable caught ) {
// information can't be retrieve, send an error message
eventBus.dispatch(myEventError)
}

public void onSuccess( UserBean user ) {
if (stateApplicationOk){
//state of the application and information ok
eventBus.dispatch(event, user);
}
else{
//state of the application ko
eventBus.dispatch(otherEvent, user);
}
}
}

plcoirier

unread,
Sep 28, 2009, 12:56:10 PM9/28/09
to Mvp4g
I started an example to show what I'm thinking doing with the place
service.
You can find the project here:
http://code.google.com/p/mvp4g/source/browse/#svn/trunk/v1.0/examples/Mvp4gHistory
and test it here: http://mvp4ghistory.appspot.com

This example shows how to manage history:
-for an event where information are stored in database.
-for an event where user needs to be logged (a cookie keeps track of
user log).

For this example I didn't modify the Mvp4g library, I directly added
new classes to my example source folder (src/com/mvp4g/example/client/
lib).
In this folder you find 2 classes: HistoryConverter and PlaceService
that will later be included to the library.
You also find a Mvp4gStarter implementation. This class is normaly
generated thanks to the Mvp4g xml configuration file but as the place
service is not part of the library yet, I had to manualy create it. In
this class, I added a comment "//NEW" before each line that the
library will automaticaly add.

Here are the changes I made compared to the wiki page:
-The function convertToToken doesn't manage asynchronous calls. I
think that if any information needs to be stored in database, the
presenter should store it because This information is probably needed
for other purposes than just history support (I couldn't think of a
case where you need to store information in database just for
history). As a best practice, I think it's better history not to be
able communicate with services when adding a new token.

-The function convertFromToken does support asynchronous calls in
order to be able to retrieve information before sending an event. It
is also in charge of sending directly the event to the event bus in
order to easily manage asynchronous calls and to let the developer
decide what type of events should be sent (you may want to send
another event type in case of error while retrieving information).

-The place service should have a default event to send in case the
history token is empty. For example, this case happens when the user
clicks on the back arrow of the browser and go back to the first page
of the application.

What do you think?

Sasa Jovancic

unread,
Sep 29, 2009, 3:48:16 PM9/29/09
to Mvp4g
It looks great, nice and clean implementation and it works excellent.

When we can expect this Place Service in mvp framework?

--------------------------------------------------
From: "plcoirier" <plco...@gmail.com>
Sent: Monday, 28 September, 2009 18:56
To: "Mvp4g" <mv...@googlegroups.com>
Subject: Re: Place Service

plcoirier

unread,
Sep 30, 2009, 10:05:41 AM9/30/09
to Mvp4g
I will try to integrate it by the end of next week. If it's ready
sooner, I will let you know.

On Sep 29, 9:48 pm, "Sasa Jovancic" <sasajovan...@hotmail.com> wrote:
> It looks great, nice and clean implementation and it works excellent.
>
> When we can expect this Place Service in mvp framework?
>
> --------------------------------------------------
> From: "plcoirier" <plcoir...@gmail.com>
> Sent: Monday, 28 September, 2009 18:56
> To: "Mvp4g" <mv...@googlegroups.com>
> Subject: Re: Place Service
>
>
>
>
>
> > I started an example to show what I'm thinking doing with the place
> > service.
> > You can find the project here:
> >http://code.google.com/p/mvp4g/source/browse/#svn/trunk/v1.0/examples...
> >>   }- Hide quoted text -
>
> - Show quoted text -

plcoirier

unread,
Oct 2, 2009, 3:28:27 PM10/2/09
to Mvp4g
I'm done integrating the Place Service. I'm not going to have a
release right now bc I plan on having an official 1.0 release within 2
weeks. However if you want to use a version of Mvp4g with the Place
Service, you can find a jar ready to use here (the one used in
Mvp4gExample): http://mvp4g.googlecode.com/svn/trunk/v1.0/examples/Mvp4gHistory/lib/mvp4g-0.14-beta.jar
> > - Show quoted text -- Hide quoted text -

Sasa Jovancic

unread,
Oct 4, 2009, 2:33:02 PM10/4/09
to mv...@googlegroups.com
You are great.
Thanks.

PS: If you need some help in writing code or or in any other task, feel free
to ask. I would be more then glad to help on this project.
--------------------------------------------------
From: "plcoirier" <plco...@gmail.com>
Sent: Friday, 02 October, 2009 21:28

plcoirier

unread,
Oct 15, 2009, 12:26:08 PM10/15/09
to Mvp4g
> Thanks.
No problem, I'm glad this framework can be useful.

> PS: If you need some help in writing code or or in any other task, feel free
> to ask. I would be more then glad to help on this project.
Thanks for your offer, if I need your help, I will let you know. Also
if you come with any ideas to improve the framework, let me know.

By the way, I released the 1.0.0 version. You can now find the example
of the place service in the mvp4g-1.0.0-examples.zip file.

> --------------------------------------------------
> From: "plcoirier" <plcoir...@gmail.com>
> Sent: Friday, 02 October, 2009 21:28
> To: "Mvp4g" <mv...@googlegroups.com>
> Subject: Re: Place Service
>
>
>
>
>
> > I'm done integrating the Place Service. I'm not going to have a
> > release right now bc I plan on having an official 1.0 release within 2
> > weeks. However if you want to use a version of Mvp4g with the Place
> > Service, you can find a jar ready to use here (the one used in
> > Mvp4gExample):
> >http://mvp4g.googlecode.com/svn/trunk/v1.0/examples/Mvp4gHistory/lib/...
Reply all
Reply to author
Forward
0 new messages