Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.sonatype.sitebricks;
- import java.util.Collection;
- import java.util.HashMap;
- import java.util.Map;
- import javax.inject.Inject;
- import org.mvel2.MVEL;
- import org.sonatype.addressbook.AddressBook;
- import org.sonatype.sitebricks.form.FormDefinitionGenerator;
- import com.google.inject.name.Named;
- import com.google.sitebricks.At;
- import com.google.sitebricks.client.transport.Json;
- import com.google.sitebricks.headless.Reply;
- import com.google.sitebricks.headless.Request;
- import com.google.sitebricks.headless.Service;
- import com.google.sitebricks.http.Delete;
- import com.google.sitebricks.http.Get;
- import com.google.sitebricks.http.Post;
- import com.google.sitebricks.http.Put;
- // ------------------------------
- // Method URL Action
- // ------------------------------
- // POST /users create
- // GET /users read
- // PUT /users/23 update
- // DESTROY /users/23 delete
- @At( "/json" )
- @Service
- public class EntityService
- {
- @Inject
- ApplicationMapping mapping;
- @Inject
- FormDefinitionGenerator formDefinitionGenerator;
- @Inject
- AddressBook application;
- @At( "/:type" )
- @Post
- public Reply<?> post( Request request, @Named( "type" ) String type )
- {
- System.out.println( "POST" );
- Object entity = request.read( mapping.getType() ).as( Json.class );
- Map<String, Object> m = new HashMap<String, Object>();
- m.put( "entity", entity );
- MVEL.eval( mapping.getCreate(), application, m );
- // return Reply.with( "{ 'success': true} " );
- return Reply.with( "OK" );
- }
- @At( "/:type" )
- @Get
- public Reply<?> get( @Named( "type" ) String type )
- {
- System.out.println( "GET/collection" );
- Collection<Object> collection = (Collection<Object>) MVEL.eval( mapping.getReadCollection(), application );
- return Reply.with( collection ).type( "application/json" ).as( Json.class );
- }
- @At( "/type:/:id" )
- @Get
- public Reply<?> get( @Named( "type" ) String type, @Named( "id" ) String id )
- {
- System.out.println( "GET/id" );
- Map m = new HashMap();
- m.put( "id", id );
- return Reply.with( MVEL.eval( mapping.getRead(), application, m ) ).type( "application/json" ).as( Json.class );
- }
- @At( "/:type/:id" )
- @Put
- public Reply<String> put( Request request, @Named( "type" ) String type, @Named( "id" ) String id )
- {
- System.out.println( "PUT" );
- Object entity = request.read( mapping.getType() ).as( Json.class );
- System.out.println( entity );
- Map<String, Object> m = new HashMap<String, Object>();
- m.put( "entity", entity );
- MVEL.eval( mapping.getUpdate(), application, m );
- return Reply.with( "OK" );
- }
- @At( "/:type/:id" )
- @Delete
- public Reply<String> delete( @Named( "type" ) String type, @Named( "id" ) String id )
- {
- System.out.println( "DELETE" );
- System.out.println( "we are attempting to delete record: " + id );
- //
- // With ExtJs the store will have been updated to remove the record so we just need to run
- // to perform the delete operation.
- //
- Map m = new HashMap();
- m.put( "id", id );
- MVEL.eval( mapping.getDelete(), application, m );
- return Reply.with( "{'success': true }" );
- }
- }
Add Comment
Please, Sign In to add comment