Calendar component: Exception in adding event

I want to use the newly added calendar component to perform the following:

  1. Like Vaadin demo, select a range of dates by dragging on the calendar that automatically pop-ups event editor and when saved, it saves to Event Entity and updates the calendar.
  2. Updates my own google or MS outlook calendar in parallel.

I’m stuck at the beginning to create an event. I have a very simple interface called Task to add events.
Here is the exception i am getting:


java.lang.UnsupportedOperationException: Use datasource for changing data items of EntityCalendarEventProvider
	at com.haulmont.cuba.gui.components.calendar.EntityCalendarEventProvider.addEvent(EntityCalendarEventProvider.java:83)
	at com.company.calendarandgoogle.web.task.TaskEdit.addEvent(TaskEdit.java:72)
	at com.company.calendarandgoogle.web.task.TaskEdit.postCommit(TaskEdit.java:56)
	at com.haulmont.cuba.gui.components.EditorWindowDelegate.commit(EditorWindowDelegate.java:269)
	at com.haulmont.cuba.web.gui.WebWindow$Editor.commitAndClose(WebWindow.java:1578)
	at com.haulmont.cuba.gui.components.AbstractEditor.commitAndClose(AbstractEditor.java:110)
	at com.haulmont.cuba.gui.components.EditorWindowDelegate$2.actionPerform(EditorWindowDelegate.java:93)
	at com.haulmont.cuba.web.gui.components.WebButton.performAction(WebButton.java:44)
	at com.haulmont.cuba.web.gui.components.WebButton.lambda$new$61446b05$1(WebButton.java:36)

Here is my Task entity


@Table(name = "CALENDARANDGOOGLE_TASK")
@Entity(name = "calendarandgoogle$Task")
public class Task extends StandardEntity {
    private static final long serialVersionUID = -4292269014751733171L;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "EMPLOYEE_ID")
    protected Employee employee;

    @Column(name = "CAPTION")
    protected String caption;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "START_DATE")
    protected Date startDate;

    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "END_DATE")
    protected Date endDate;

    @Column(name = "DESCRIPTION")
    protected String description;

    @Column(name = "STYLE_NAME")
    protected String styleName;

    @Column(name = "ALL_DAY")
    protected Boolean allDay;

..........

Here is the Task controller


public class TaskEdit extends AbstractEditor<Task> {

    @Named("fieldGroup.allDay")
    private CheckBox allDayField;
    @Named("fieldGroup.caption")
    private TextField captionField;
    @Named("fieldGroup.description")
    private TextField descriptionField;
    @Named("fieldGroup.employee")
    private PickerField employeeField;
    @Named("fieldGroup.endDate")
    private DateField endDateField;
    @Named("fieldGroup.startDate")
    private DateField startDateField;
    @Named("fieldGroup.styleName")
    private TextField styleNameField;

    @Inject
    Calendar calendar;

    @Override
    public void init(Map<String, Object> params) {
        super.init(params);

        startDateField.setValue(new Date());
        endDateField.setValue(new Date());
        captionField.setValue(getMessage("caption"));
        descriptionField.setValue(getMessage("description"));
        allDayField.setValue(Boolean.FALSE);
    }

    @Override
    protected boolean postCommit(boolean committed, boolean close) {
        if (committed) {
           addEvent();
        }
        return super.postCommit(committed, close);
    }
    
    public void addEvent() {
        SimpleCalendarEvent calendarEvent = new SimpleCalendarEvent();
        calendarEvent.setCaption(captionField.getValue());
        calendarEvent.setDescription(descriptionField.getValue());
        calendarEvent.setStart(startDateField.getValue());
        calendarEvent.setEnd(endDateField.getValue());
        calendarEvent.setAllDay(allDayField.getValue());
        calendarEvent.setStyleName("event-blue");

        calendar.getEventProvider().addEvent(calendarEvent);
    }
}

Hello, Mortoza!

  1. The stack trace of your exception says that you set datasource for Calendar but try to add CalendarEvent directly. When you use datasource with Calendar, all manipulations with events must be via datasource, because events represented with entities. It’s not clear from the documentation.

  2. Currently, Calendar doesn’t have RangeSelectListener, but we have plans to add it in future releases. Now you can unwrap Calendar and add RangeSelectHandler via Vaadin API. For example:


com.vaadin.ui.Calendar vaadinCalendar = (com.vaadin.ui.Calendar) WebComponentsHelper.unwrap(calendar);
        vaadinCalendar.setHandler((CalendarComponentEvents.RangeSelectHandler) event ->
                handleRangeSelect(event.getStart(), event.getEnd()));

I’ve prepared more detailed demo project and you can get it from github.

Regards,

Gleb

Thank you so much Gleb.
with regards to the 2nd point, Is it possible we can update MS Outlook calendar with this event ?

Hi Gleb
The sample is awesome. I have explored the Calendar sample app and tried the work on a weekly display of the Calendar. It is displaying the weekly view, events are saved to database but not displayed in the weekly view that I can see in monthly view. Any clue why it is not displayed in weekly view using time?

I have attached the modified sample app.

calendar-demo-master.zip (328.0K)

Hello, Mortoza!

In your sample, you have missed endDateProperty and startDateProperty, so Calendar doesn’t know which entity fields use to display events.

About external calendars, sorry, but I don’t familiar with they API and I don’t have any advice. But when you commit Event entity (In your sample Task entity) you have all necessary information which you can use to add event to external calendars, just place your code in addCloseWithCommitListener of Task editor (where I just refresh the datasource) or add datasource listeners and update external calendars according to ds changes.

Regards,

Gleb

Thank you Gleb

:ticket: See the following issue in our bug tracker:

https://youtrack.cuba-platform.com/issue/PL-8366