Guest User

Untitled

a guest
Jul 18th, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.29 KB | None | 0 0
  1. package org.sonatype.sitebricks;
  2.  
  3. import java.util.Collection;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6.  
  7. import javax.inject.Inject;
  8.  
  9. import org.mvel2.MVEL;
  10. import org.sonatype.addressbook.AddressBook;
  11. import org.sonatype.sitebricks.form.FormDefinitionGenerator;
  12.  
  13. import com.google.inject.name.Named;
  14. import com.google.sitebricks.At;
  15. import com.google.sitebricks.client.transport.Json;
  16. import com.google.sitebricks.headless.Reply;
  17. import com.google.sitebricks.headless.Request;
  18. import com.google.sitebricks.headless.Service;
  19. import com.google.sitebricks.http.Delete;
  20. import com.google.sitebricks.http.Get;
  21. import com.google.sitebricks.http.Post;
  22. import com.google.sitebricks.http.Put;
  23.  
  24. // ------------------------------
  25. // Method URL Action
  26. // ------------------------------
  27. // POST /users create
  28. // GET /users read
  29. // PUT /users/23 update
  30. // DESTROY /users/23 delete
  31.  
  32. @At( "/json" )
  33. @Service
  34. public class EntityService
  35. {
  36. @Inject
  37. ApplicationMapping mapping;
  38.  
  39. @Inject
  40. FormDefinitionGenerator formDefinitionGenerator;
  41.  
  42. @Inject
  43. AddressBook application;
  44.  
  45. @At( "/:type" )
  46. @Post
  47. public Reply<?> post( Request request, @Named( "type" ) String type )
  48. {
  49. System.out.println( "POST" );
  50. Object entity = request.read( mapping.getType() ).as( Json.class );
  51. Map<String, Object> m = new HashMap<String, Object>();
  52. m.put( "entity", entity );
  53. MVEL.eval( mapping.getCreate(), application, m );
  54. // return Reply.with( "{ 'success': true} " );
  55. return Reply.with( "OK" );
  56. }
  57.  
  58. @At( "/:type" )
  59. @Get
  60. public Reply<?> get( @Named( "type" ) String type )
  61. {
  62. System.out.println( "GET/collection" );
  63. Collection<Object> collection = (Collection<Object>) MVEL.eval( mapping.getReadCollection(), application );
  64. return Reply.with( collection ).type( "application/json" ).as( Json.class );
  65. }
  66.  
  67. @At( "/type:/:id" )
  68. @Get
  69. public Reply<?> get( @Named( "type" ) String type, @Named( "id" ) String id )
  70. {
  71. System.out.println( "GET/id" );
  72. Map m = new HashMap();
  73. m.put( "id", id );
  74. return Reply.with( MVEL.eval( mapping.getRead(), application, m ) ).type( "application/json" ).as( Json.class );
  75. }
  76.  
  77. @At( "/:type/:id" )
  78. @Put
  79. public Reply<String> put( Request request, @Named( "type" ) String type, @Named( "id" ) String id )
  80. {
  81. System.out.println( "PUT" );
  82. Object entity = request.read( mapping.getType() ).as( Json.class );
  83. System.out.println( entity );
  84. Map<String, Object> m = new HashMap<String, Object>();
  85. m.put( "entity", entity );
  86. MVEL.eval( mapping.getUpdate(), application, m );
  87. return Reply.with( "OK" );
  88. }
  89.  
  90. @At( "/:type/:id" )
  91. @Delete
  92. public Reply<String> delete( @Named( "type" ) String type, @Named( "id" ) String id )
  93. {
  94. System.out.println( "DELETE" );
  95. System.out.println( "we are attempting to delete record: " + id );
  96. //
  97. // With ExtJs the store will have been updated to remove the record so we just need to run
  98. // to perform the delete operation.
  99. //
  100. Map m = new HashMap();
  101. m.put( "id", id );
  102. MVEL.eval( mapping.getDelete(), application, m );
  103.  
  104. return Reply.with( "{'success': true }" );
  105. }
  106. }
Add Comment
Please, Sign In to add comment