Skip to content

Commit

Permalink
#119 Used PUT method for new entities saving process instead of POST.
Browse files Browse the repository at this point in the history
  • Loading branch information
jhou-pro committed Feb 5, 2015
1 parent 0422957 commit a25e415
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public EntityResourceFactory(final List<EntityMaster<? extends AbstractEntity<?>
public void handle(final Request request, final Response response) {
super.handle(request, response);

if (Method.GET == request.getMethod() || Method.POST == request.getMethod()) {
if (Method.GET == request.getMethod() || Method.POST == request.getMethod() || Method.PUT == request.getMethod()) {
final String username = (String) request.getAttributes().get("username");
injector.getInstance(IUserProvider.class).setUsername(username, injector.getInstance(IUserController.class));

Expand All @@ -69,17 +69,5 @@ public void handle(final Request request, final Response response) {
final EntityResource<AbstractEntity<?>> resource = new EntityResource<AbstractEntity<?>>((Class<AbstractEntity<?>>) entityType, (IEntityProducer<AbstractEntity<?>>) master.createEntityProducer(injector), factory, restUtil, injector.getInstance(ICompanionObjectFinder.class), getContext(), request, response);
resource.handle();
}

// if (Method.POST == request.getMethod()) {
// final String username = (String) request.getAttributes().get("username");
// injector.getInstance(IUserProvider.class).setUsername(username, injector.getInstance(IUserController.class));
//
// final String entityTypeString = (String) request.getAttributes().get("entityType");
// final Class<? extends AbstractEntity<?>> entityType = (Class<? extends AbstractEntity<?>>) ClassesRetriever.findClass(entityTypeString);
// final EntityMaster<? extends AbstractEntity<?>> master = this.masters.get(entityType);
//
// final EntityValidationResource<AbstractEntity<?>> resource = new EntityValidationResource<AbstractEntity<?>>((Class<AbstractEntity<?>>) entityType, (fetch<AbstractEntity<?>>) master.getFetchStrategy(), factory, restUtil, injector.getInstance(ICompanionObjectFinder.class), getContext(), request, response);
// resource.handle();
// }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.restlet.representation.Representation;
import org.restlet.resource.Get;
import org.restlet.resource.Post;
import org.restlet.resource.Put;
import org.restlet.resource.ResourceException;
import org.restlet.resource.ServerResource;

Expand Down Expand Up @@ -57,11 +58,30 @@ public Representation get() throws ResourceException {
}

/**
* Handles POST requests resulting from tg-entity-master <code>save()</code> method (new or persisted entity).
* Handles POST requests resulting from tg-entity-master <code>save()</code> method (persisted entity).
*/
@Post
@Override
public Representation post(final Representation envelope) throws ResourceException {
return tryToSave(envelope);
}

/**
* Handles PUT requests resulting from tg-entity-master <code>save()</code> method (new entity).
*/
@Put
@Override
public Representation put(final Representation envelope) throws ResourceException {
return tryToSave(envelope);
}

/**
* Tries to save the changes for the entity and returns it in JSON format.
*
* @param envelope
* @return
*/
private Representation tryToSave(final Representation envelope) {
final Map<String, Object> modifiedPropertiesHolder = mixin.restoreModifiedPropertiesHolderFrom(envelope, restUtil);

final T validationPrototype = mixin.createEntityForRetrieval(this.entityId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<template>
<core-ajax id="ajaxRetriever" url="/users/{{user}}/entity/{{entitytype}}/{{entityid}}" method="GET" handleas="json"></core-ajax>
<core-ajax id="ajaxSaver" url="/users/{{user}}/entity/{{entitytype}}/{{entityid}}" method="POST" handleas="json"></core-ajax>
<core-ajax id="ajaxSaverNew" url="/users/{{user}}/entity/{{entitytype}}/{{entityid}}" method="PUT" handleas="json"></core-ajax>

<tg-reflector id="reflector"></tg-reflector>
<tg-serialiser id="serialiser"></tg-serialiser>
Expand Down Expand Up @@ -57,6 +58,10 @@
var potentiallySavedEntity = self.$.serialiser.deserialise(e.detail.response).instance;
self._onSavedDefault(potentiallySavedEntity);
});
self.$.ajaxSaverNew.addEventListener('core-response', function(e) {
var potentiallySavedEntity = self.$.serialiser.deserialise(e.detail.response).instance;
self._onSavedDefault(potentiallySavedEntity);
});
},

//////////////////////////////////////// RETRIEVAL ////////////////////////////////////////
Expand Down Expand Up @@ -152,15 +157,21 @@
// console.log("save: modifiedPropertiesHolder", modifiedPropertiesHolder);
var ser = this.$.serialiser.serialise(modifiedPropertiesHolder);
// console.log("save: serialised modifiedPropertiesHolder", ser);
this.$.ajaxSaver.body = JSON.stringify(ser);
this.$.ajaxSaver.go();
if (this.entityid === "new") {
this.$.ajaxSaverNew.body = JSON.stringify(ser);
this.$.ajaxSaverNew.go();
} else {
this.$.ajaxSaver.body = JSON.stringify(ser);
this.$.ajaxSaver.go();
}
},

/**
* Cancels any unfinished saving that was requested earlier (if any).
*/
_abortSavingIfAny: function() {
this.$.ajaxSaver.abort();
this.$.ajaxSaverNew.abort();
},

/**
Expand Down

0 comments on commit a25e415

Please sign in to comment.