1. HTML
We will assume that your HTML file (either .html or .jsp) is located in Calendar directory of your web application. If you use a
different location you must change the relative URLs accordingly.
Add the references to the required JavaScript libraries to the header or
to the body:
<script type="text/javascript" src="../js/common.js"></script>
<script type="text/javascript" src="../js/calendar.js"></script>
Put a placeholder for the Calendar into the <body> :
<div id="dpc">
</div>
Add the JavaScript initialization code:
<script type="text/javascript">
var dpc = new DayPilot.Calendar("dpc");
dpc.backendUrl = '${pageContext.request.contextPath}/dps';
dpc.viewType = "Week";
dpc.Init();
</script>It is necessary to specify the backendUrl property.
That's the URL which handles the AJAX callback requests.
The complete API reference for the Scheduler client-side object can be
found here:
2. Libraries
JavaScript libraries
- Copy common.js and calendar.js to
js directory of your web application.
Java libraries
- Copy daypilot-x.x.x.jar (e.g. daypilot-1.0.30.jar)
to WEB-INF/lib directory of your web application.
3. Servlet
Create a new servlet and map it to "/dpc" Url. You can do it by adding
the following declaration to web.xml file:
<servlet>
<description />
<display-name>DpcServlet</display-name>
<servlet-name>DpcServlet</servlet-name>
<servlet-class>org.daypilot.demo.DpcServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DpcServlet</servlet-name>
<url-pattern>/dpc</url-pattern>
</servlet-mapping>
The servlet class will be very simple:
package org.daypilot.demo;
import java.io.IOException;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.daypilot.data.Column;
import org.daypilot.date.DateTime;
import org.daypilot.date.Week;
import org.daypilot.demo.db.Db;
import org.daypilot.ui.DayPilotCalendar;
import org.daypilot.ui.args.calendar.BeforeCellRenderArgs;
import org.daypilot.ui.args.calendar.BeforeEventRenderArgs;
import org.daypilot.ui.args.calendar.BeforeHeaderRenderArgs;
import org.daypilot.ui.args.calendar.CommandArgs;
import org.daypilot.ui.args.calendar.EventBubbleArgs;
import org.daypilot.ui.args.calendar.EventMenuClickArgs;
import org.daypilot.ui.args.calendar.EventMoveArgs;
import org.daypilot.ui.args.calendar.EventResizeArgs;
import org.daypilot.ui.args.calendar.TimeRangeSelectedArgs;
import org.daypilot.ui.enums.UpdateType;
import org.daypilot.ui.enums.ViewType;
public class DpcServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { }
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
DayPilotCalendar dpc = new Dpc();
dpc.process(request, response);
}
}This will forward the POST request to a special Dpc
class which we will create in the next step.
4. Dpc Class
Create a new Dpc class (it must inherit from
org.daypilot.ui.DayPilotCalendar):
public class Dpc extends DayPilotCalendar {
@Override
public void init() throws Exception {
// map the database column names
setDataIdField("event_id");
setDataTextField("event_name");
setDataStartField("event_start");
setDataEndField("event_end");
// assign the collection of events
setEvents(Db.getEvents(getRequest(), getStartDate().toDate(), getStartDate().addDays(getDays()).toDate()));
// request an update of the event set on the client side
update();
}
} The init() method is called using an AJAX callback
right after the Calendar is initialized on the client side.
5. Event Moving
In order to handle event move action, we need to
override eventMove() method. The event arguments are
available in EventMoveArgs class (ea parameter).
@Override
public void eventMove(EventMoveArgs ea) throws Exception {
// update the DB
Db.moveEvent(getRequest(), ea.getValue(), ea.getNewStart().toTimeStamp(), ea.getNewEnd().toTimeStamp(), ea.getNewResource());
update();
}It has to be enabled on the client-side as well:
dpc.eventMoveHandling = "CallBack";
6. Prepare and Finish
There are two special methods available for
overriding:
They are called during every AJAX callback request
(before and after the main event method).
We will use prepare() to initialize an in-memory
instance of a HSQLDB embedded database
(which we will use for testing purposes):
public void prepare() throws Exception {
// create the in-memory DB if it's not ready
if (!Db.tableExists("EVENTS")) {
Db.createTable();
}
}We will also move the event loading code there to the
finish() method:
@Override
public void finish() throws Exception {
if (getUpdateType() == UpdateType.NONE) {
return;
}
// set the database fields
setDataIdField("event_id");
setDataTextField("event_name");
setDataStartField("event_start");
setDataEndField("event_end");
// reload events
setEvents(Db.getEvents(getRequest(), getStartDate().toDate(), getStartDate().addDays(getDays()).toDate()));
}7. Handling Other
Events
Implementing event handlers is very similar to
eventMove. Just add the required database changes. Since we already added
finish() method, you don't need to care about refreshing the events.
Our new eventResize handler:
@Override
public void eventResize(EventResizeArgs ea) throws Exception {
Db.resizeEvent(getRequest(), ea.getValue(), ea.getNewStart().toTimeStamp(), ea.getNewEnd().toTimeStamp());
update();
}And timeRangeSelected handler:
@Override
public void timeRangeSelected(TimeRangeSelectedArgs ea) throws Exception {
Db.insertEvent(getRequest(), "New event", ea.getStart().toTimeStamp(), ea.getEnd().toTimeStamp(), ea.getResource());
update();
}Both events need to be enabled on the client side:
dpc.eventResizeHandling = "CallBack";
dpc.timeRangeSelectedHandling = "CallBack";