/sitebricks-acceptance-tests/src/main/java/com/google/sitebricks/example/RestfulWebServiceWithCRUDConversions.java

http://github.com/dhanji/sitebricks · Java · 213 lines · 181 code · 32 blank · 0 comment · 37 complexity · ebf6fc7811cb1a8be799331a4abd43c9 MD5 · raw file

  1. package com.google.sitebricks.example;
  2. import com.google.inject.name.Named;
  3. import com.google.sitebricks.At;
  4. import com.google.sitebricks.client.transport.Json;
  5. import com.google.sitebricks.headless.Reply;
  6. import com.google.sitebricks.headless.Request;
  7. import com.google.sitebricks.headless.Service;
  8. import com.google.sitebricks.http.Delete;
  9. import com.google.sitebricks.http.Get;
  10. import com.google.sitebricks.http.Patch;
  11. import com.google.sitebricks.http.Post;
  12. import com.google.sitebricks.http.Put;
  13. import java.io.Serializable;
  14. import java.text.SimpleDateFormat;
  15. import java.util.ArrayList;
  16. import java.util.Date;
  17. import java.util.List;
  18. @At(RestfulWebServiceWithCRUDConversions.AT_ME)
  19. @Service
  20. public class RestfulWebServiceWithCRUDConversions {
  21. public static final String AT_ME = "/jsonConversion";
  22. public static List<Widget> widgets = new ArrayList<Widget>();
  23. static {
  24. populate();
  25. }
  26. private static void populate() {
  27. SimpleDateFormat sdf = new SimpleDateFormat("dd-MMM-yyyy HH:mm");
  28. try {
  29. addWidget(new Widget(1, "Widget 1", sdf.parse("01-JAN-1990 12:00"), 1.50));
  30. addWidget(new Widget(2, "Widget 2", sdf.parse("02-FEB-2000 18:00"), 21.75));
  31. addWidget(new Widget(3, "Widget 3", sdf.parse("03-MAR-2010 23:00"), 19.99));
  32. } catch (Exception e) {
  33. }
  34. }
  35. @Post
  36. public Reply<Widget> post(Request request) {
  37. Widget widget = request.read(Widget.class).as(Json.class);
  38. addWidget(widget);
  39. return Reply.with(widget).as(Json.class).type("application/json");
  40. }
  41. @Put
  42. public Reply<?> put(Request request) {
  43. Widget widget = request.read(Widget.class).as(Json.class);
  44. addWidget(widget);
  45. return Reply.with(widget).as(Json.class).type("application/json");
  46. }
  47. @Patch
  48. public Reply<?> patch(Request request) {
  49. Widget widget = request.read(Widget.class).as(Json.class);
  50. updateWidget(widget);
  51. return Reply.with(widget).as(Json.class).type("application/json");
  52. }
  53. @Get
  54. public Reply<List<Widget>> getAll() {
  55. return Reply.with(widgets).as(Json.class).type("application/json");
  56. }
  57. @At("/:id")
  58. @Get
  59. public Reply<?> get(@Named("id") int id) {
  60. Widget widget = findWidget(id);
  61. if (widget != null)
  62. return Reply.with(widget).as(Json.class).type("application/json");
  63. return Reply.saying().error();
  64. }
  65. @At("/:id")
  66. @Delete
  67. public Reply<?> delete(@Named("id") int id) {
  68. Widget widget = removeWidget(id);
  69. if (widget != null)
  70. return Reply.with(widget).as(Json.class).type("application/json");
  71. return Reply.saying().error();
  72. }
  73. public static Widget removeWidget(Widget widget) {
  74. return removeWidget(widget.getId());
  75. }
  76. public static Widget removeWidget(int id) {
  77. Widget oldWidget = findWidget(id);
  78. if (oldWidget != null)
  79. widgets.remove(oldWidget);
  80. return oldWidget;
  81. }
  82. public static void addWidget(Widget widget) {
  83. Widget oldWidget = findWidget(widget.getId());
  84. if (oldWidget != null)
  85. widgets.remove(oldWidget);
  86. widgets.add(widget);
  87. }
  88. public static void updateWidget(Widget widget) {
  89. Widget staleWidget = findWidget(widget.getId());
  90. if (staleWidget != null) {
  91. staleWidget.setName(widget.getName());
  92. staleWidget.setAvailable(widget.getAvailable());
  93. staleWidget.setPrice(widget.getPrice());
  94. }
  95. }
  96. public static Widget findWidget(int id) {
  97. for (Widget widget : widgets) {
  98. if (widget.getId() == id)
  99. return widget;
  100. }
  101. return null;
  102. }
  103. public static class Widget implements Serializable {
  104. int id;
  105. String name;
  106. Date available;
  107. double price;
  108. public Widget() {
  109. }
  110. public Widget(int id, String name, Date available, double price) {
  111. this.id = id;
  112. this.name = name;
  113. this.available = available;
  114. this.price = price;
  115. }
  116. public int getId() {
  117. return id;
  118. }
  119. public void setId(int id) {
  120. this.id = id;
  121. }
  122. public String getName() {
  123. return name;
  124. }
  125. public void setName(String name) {
  126. this.name = name;
  127. }
  128. public Date getAvailable() {
  129. return available;
  130. }
  131. public void setAvailable(Date available) {
  132. this.available = available;
  133. }
  134. public double getPrice() {
  135. return price;
  136. }
  137. public void setPrice(double price) {
  138. this.price = price;
  139. }
  140. public Widget clone() {
  141. return new Widget (id, name, available, price);
  142. }
  143. @Override
  144. public int hashCode() {
  145. final int prime = 31;
  146. int result = 1;
  147. result = prime * result + ((available == null) ? 0 : available.hashCode());
  148. result = prime * result + id;
  149. result = prime * result + ((name == null) ? 0 : name.hashCode());
  150. long temp;
  151. temp = Double.doubleToLongBits(price);
  152. result = prime * result + (int) (temp ^ (temp >>> 32));
  153. return result;
  154. }
  155. @Override
  156. public boolean equals(Object obj) {
  157. if (this == obj)
  158. return true;
  159. if (obj == null)
  160. return false;
  161. if (getClass() != obj.getClass())
  162. return false;
  163. Widget other = (Widget) obj;
  164. if (available == null) {
  165. if (other.available != null)
  166. return false;
  167. } else if (!available.equals(other.available))
  168. return false;
  169. if (id != other.id)
  170. return false;
  171. if (name == null) {
  172. if (other.name != null)
  173. return false;
  174. } else if (!name.equals(other.name))
  175. return false;
  176. if (Double.doubleToLongBits(price) != Double.doubleToLongBits(other.price))
  177. return false;
  178. return true;
  179. }
  180. }
  181. }