Last month I posted a Small Talk discussing the use of ZK Calendar with a database. Since then I have been working closely with ZK Calendar and thought I would share another way of achieving the same effect.

Extending ZK Calendar’s AbstractCalendarModel

The key is to extend the AbstractCalendarModel and override the get function. This function is used to retrieve the events from the selected model, in this case the underlying model architecture is a database.

The ‘get’ function

The get function takes the following parameters:

public List get(Date arg0, Date arg1, RenderContext arg2)

The two Date objects denote the start and end dates respectively whereas the RenderContext contains a function getTimeZone(). This function is used to retrieve the calendar’s TimeZone which is used to align the Date object’s time with the database’s timezone.

The code

public final class DatabaseCalendarModel extends AbstractCalendarModel {

	private NewsDAO _newsDAO = new NewsDAO();

	@Override
	public List get(Date arg0, Date arg1, RenderContext arg2) {

		long startDate = arg0.getTime();
		long endDate = arg1.getTime();
                
                //retrieve the events from the database between the two times
		List result =
			_newsDAO.selectAllBetween(startDate, endDate);

		return result;
	}
}

The above code demonstrates how we have implemented the retrieval. You can download the Small Talk’s code to give you a better understanding of how the database interaction was implemented.

In this case I added a new function which will retrieve all the database entries between the two dates, I will not show how to do this in this blog post. However, by using the Small Talk’s code as an example you can see that the time is stored as an integer value (the time since Unix Epoch), thus we can easily retrieve the events between two dates using integer comparisons.

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.

Leave a Reply