PageRenderTime 28ms CodeModel.GetById 10ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

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