The Schedule
component provides a calendar to manage events, such as Outlook Calendar and iCal. By default, a whole set of events is eagerly provided via the ScheduleModel
class. That means all events are loaded at once on page load. The lazy loading feature helps to improve performance if we have a huge dataset of events or if events take too much time to load. In the lazy loading mode, only events that belong to the displayed timeframe are fetched.
In this recipe, we will implement a small example for the Schedule
component's lazy loading feature.
A basic definition of schedule
would be as shown here:
<p:schedule id="lazySchedule" value="#{scheduleBean.lazyScheduleModel}"/>
The visual output of the component with two randomly generated events will be as shown here:
By default, the month view is displayed, so the user sees a whole month and can switch between months. Assume that we have to load events for 12 months and every month requires, on average, 1.5 seconds for event loading. The default eager mode would take 18 seconds (12 x 1.5) to load all events. This is too long, so using the lazy loading feature is recommended to improve performance. The ScheduleBean
bean definition with lazy model construction is given next; for the sake of simplicity, the loading time is simulated with Thread.sleep(1500)
:
@Named @ViewScoped public class ScheduleBean implements Serializable { private ScheduleModel lazyEventModel; @PostConstruct public void initialize() { lazyEventModel = new LazyScheduleModel() { @Override public void loadEvents(Date start, Date end) { try { // simulate a long running task Thread.sleep(1500); } catch (Exception e) { } clear(); Date random = getRandomDate(start); addEvent(new DefaultScheduleEvent("Lazy Event 1", random, random)); random = getRandomDate(start); addEvent(new DefaultScheduleEvent("Lazy Event 2", random, random)); } }; } public Date getRandomDate(Date base) { Calendar date = Calendar.getInstance(); date.setTime(base); date.add(Calendar.DATE, ((int) (Math.random() * 30)) + 1); return date.getTime(); } public ScheduleModel getLazyScheduleModel() { return lazyEventModel; } }
To enable lazy loading of the schedule
component's events, we need to provide an instance of org.primefaces.model.LazyScheduleModel
and implement the loadEvents
method. This method is called with new date boundaries every time the displayed timeframe is changed. Events are now loaded on demand on the initial page load or during switching between months. That means the maximum delay for the event's loading is not longer than 1.5 seconds.
The Schedule
component offers five different views, which are month
, agendaWeek
, agendaDay
, basicWeek
, and basicDay
. The default view is month
, and the rest of the modes are given here with their visual outputs:
The schedule
component supports AJAX behavior events in order to handle the interactions of the user by date selection, event selection/deselection, or movement and view change. The definitions of the AJAX behaviors are shown in the following code snippet (they should be defined inside the schedule
component):
<p:ajax event="dateSelect" listener="#{scheduleBean.onDateSelect}" update=":mainForm:growl" /> <p:ajax event="eventSelect" update=":mainForm:growl" listener="#{scheduleBean.onEventSelect}"/> <p:ajax event="eventMove" listener="#{scheduleBean.onEventMove}" update=":mainForm:growl" /> <p:ajax event="eventResize" update=":mainForm:growl" listener="#{scheduleBean.onEventResize}" /> <p:ajax event="viewChange" listener="#{scheduleBean.onViewChange}" update=":mainForm:growl" />
The method definitions are listed here:
public void onDateSelect(SelectEvent event) { MessageUtil.addInfoMessage("date.selected", event.getObject()); } public void onEventSelect(SelectEvent event) { MessageUtil.addInfoMessage("event.selected", ((DefaultScheduleEvent)event.getObject()).getTitle()); } public void onEventMove(ScheduleEntryMoveEvent event) { MessageUtil.addInfoMessage("event.moved", event.getScheduleEvent().getTitle(), event.getDayDelta(), event.getMinuteDelta()); } public void onEventResize(ScheduleEntryResizeEvent event) { MessageUtil.addInfoMessage("event.resized", event.getScheduleEvent().getTitle(), event.getDayDelta(), event. getMinuteDelta()); } public void onViewChange(SelectEvent event) { MessageUtil.addInfoMessage("view.changed", event.getObject()); }
Defining the locale value to the locale
attribute provides the localization of schedule
. The locale
attribute can either take a String
or java.util.Locale
instance as the key. By default, all labels provided by the schedule are in English, so you need to provide the other translations manually. The PrimeFaces community implements the translations, and they are available as JavaScript objects. Please refer to https://code.google.com/p/primefaces/wiki/PrimeFacesLocales to access the translations.
This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.
The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter5/scheduleLazyLoad.jsf
.
For details about the MessageUtil
class, see the Internationalization (i18n) and Localization (L10n) recipe in Chapter 1, Getting Started with PrimeFaces.
18.189.178.237