package be.itoffice.mbt.ws;
import java.util.Collection;
import java.util.List;
import javax.ws.rs.Consumes;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.GenericEntity;
import javax.ws.rs.core.MediaType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import be.itoffice.mbt.persist.dao.CalendarEventDao;
import be.itoffice.mbt.persist.entity.CalendarEvent;
import com.sun.jersey.api.json.JSONWithPadding;
@Component
@Path("/test")
public class TestWService {
@Autowired
CalendarEventDao calendarDao;
@GET
@Path("/populateEvents")
@Produces("text/html")
public String populateEvents() {
try {
CalendarEvent e = new CalendarEvent();
e.setTitle("Event A");
e.setAllDay(true);
e.setStart("12/02/2012");
e.setDescription("Description event A");
calendarDao.persistEvent(e);
CalendarEvent e1 = new CalendarEvent();
e1.setTitle("Event B");
e1.setAllDay(true);
e1.setStart("21/02/2012");
e1.setDescription("Description event B");
calendarDao.persistEvent(e1);
CalendarEvent e2 = new CalendarEvent();
e2.setTitle("Event C");
e2.setAllDay(false);
e2.setStart("21/02/2012 13:00");
e2.setEnd("21/02/2012 14:30");
e1.setDescription("Description event c");
calendarDao.persistEvent(e2);
} catch (Exception e) {
e.printStackTrace();
return "Insert data failed: " + e.getMessage();
}
return "Insert data succeed";
}
@GET
@Produces({
"application/x-javascript", MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
@Path("/allEvents")
public JSONWithPadding getEvents(@QueryParam("callback") @DefaultValue("event") String callback) {
List<CalendarEvent> resultList = calendarDao.getAllEvents();
return new JSONWithPadding(new GenericEntity<Collection<CalendarEvent>>(resultList) {}, callback);
}
@POST
@Path("/createEvent")
@Produces(MediaType.TEXT_HTML)
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public void createEvent(@FormParam("title") String title, @FormParam("dtstart") String dtstart, @FormParam("dtend") String dtend,
@FormParam("allday") String allDay, @FormParam("description") String description, @FormParam("important") String important) {
System.out.println("Create event:");
System.out.println("- title: " + title);
System.out.println("- dtstart: " + dtstart);
System.out.println("- dtend: " + dtend);
System.out.println("- allDay: " + allDay);
System.out.println("- description: " + description);
System.out.println("- important: " + important);
CalendarEvent e = new CalendarEvent();
e.setTitle(title);
e.setStart(dtstart);
if (dtend != null) {
e.setEnd(dtend);
}
e.setAllDay(allDay != null ? Boolean.parseBoolean(allDay) : false);
e.setDescription(description);
e.setImportant(important != null ? Boolean.parseBoolean(important) : false);
calendarDao.persistEvent(e);
}
}