Created
October 23, 2009 00:33
-
-
Save mraible/216496 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.appfuse.gwt.service.client.service; | |
import org.appfuse.gwt.service.client.event.CollectionLoadedEvent; | |
import org.appfuse.gwt.service.client.event.ResourceDeletedEvent; | |
import org.appfuse.gwt.service.client.event.ResourceLoadedEvent; | |
import org.appfuse.gwt.service.client.event.ResourceSavedEvent; | |
import org.appfuse.gwt.service.client.rest.Callback; | |
import org.appfuse.gwt.service.client.rest.Deferred; | |
import org.appfuse.gwt.service.client.rest.Representation; | |
import org.appfuse.gwt.service.client.rest.RestRequest; | |
import org.appfuse.gwt.service.client.rest.jso.AbstractJSOModel; | |
import org.appfuse.gwt.service.client.rest.jso.JSOModel; | |
import com.google.gwt.core.client.JsArray; | |
import com.google.gwt.http.client.URL; | |
import net.customware.gwt.presenter.client.EventBus; | |
import java.io.Serializable; | |
import java.util.ArrayList; | |
import java.util.List; | |
/** | |
* This class serves as the Base class for GWT Services - namely to hold | |
* common CRUD methods that they might all use. You should only need to extend | |
* this class when your require custom CRUD logic. | |
* | |
* @author mraible | |
* @param <M> a Model that extends AbstractJSOModel | |
* @param <PK> the primary key for that type | |
*/ | |
public abstract class GenericServiceImpl<M extends AbstractJSOModel, PK extends Serializable> | |
implements GenericService<M, PK> { | |
/** | |
* EventBus instance, set by constructor of this class | |
*/ | |
protected EventBus eventBus; | |
/** | |
* URL of server-side resource location | |
*/ | |
protected String location; | |
/** | |
* Public constructor for creating a new GenericServiceImpl. | |
* | |
* @param eventBus the EventBus to use for firing Events | |
* @param location the URL of the service | |
*/ | |
public GenericServiceImpl(final EventBus eventBus, String location) { | |
this.eventBus = eventBus; | |
this.location = location; | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void getAll() { | |
Deferred<Representation> d = RestRequest.get(location).build(); | |
d.addCallback(new Callback<Representation>() { | |
public void onSuccess(Representation result) { | |
JsArray<JSOModel> array = JSOModel.arrayFromJson(result.getData()); | |
List<M> models = new ArrayList<M>(); | |
for (int i = 0; i < array.length(); i++) { | |
models.add(convertResultToModel(array.get(i))); | |
} | |
eventBus.fireEvent(new CollectionLoadedEvent<M>(models)); | |
} | |
}); | |
d.run(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void get(PK id) { | |
Deferred<Representation> d = | |
RestRequest.get(location + "/" + URL.encode(id.toString())).build(); | |
d.addCallback(new Callback<Representation>() { | |
public void onSuccess(Representation result) { | |
M model = convertResultToModel(result); | |
eventBus.fireEvent(new ResourceLoadedEvent<M>(model)); | |
} | |
}); | |
d.run(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void save(M model) { | |
Deferred<Representation> d = RestRequest.post(location) | |
.setRequestData(model.toJson()).build(); | |
d.addCallback(new Callback<Representation>() { | |
public void onSuccess(Representation result) { | |
M model = convertResultToModel(result); | |
eventBus.fireEvent(new ResourceSavedEvent<M>(model)); | |
} | |
}); | |
d.run(); | |
} | |
/** | |
* {@inheritDoc} | |
*/ | |
public void delete(PK id) { | |
Deferred<Representation> d = | |
RestRequest.post(location + "/" + URL.encode(id.toString())).build(); | |
d.addCallback(new Callback<Representation>() { | |
public void onSuccess(Representation result) { | |
eventBus.fireEvent(new ResourceDeletedEvent()); | |
} | |
}); | |
d.run(); | |
} | |
/** | |
* Method to convert Representation to a model object. | |
* | |
* @param result the result of a resource request. | |
* @return the populated object. | |
*/ | |
@SuppressWarnings("unchecked") | |
protected M convertResultToModel(Representation result) { | |
return convertResultToModel(JSOModel.fromJson(result.getData())); | |
} | |
/** | |
* Method to convert a JSOModel to an AbstractJSOModel | |
* | |
* @param data a JSOModel object | |
* @return the populated model object. | |
*/ | |
@SuppressWarnings("unchecked") | |
protected abstract M convertResultToModel(JSOModel data); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment