summaryrefslogtreecommitdiffstats
path: root/src/com/vaadin/demo/reservation/ReservationApplication.java
blob: baba6c11a29aec7bf47d5e738eaacbbe4156e6ab (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
/* 
@ITMillApache2LicenseForJavaFiles@
 */

package com.vaadin.demo.reservation;

import java.awt.geom.Point2D;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import com.vaadin.Application;
import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.Property.ValueChangeListener;
import com.vaadin.ui.Alignment;
import com.vaadin.ui.Button;
import com.vaadin.ui.Label;
import com.vaadin.ui.OrderedLayout;
import com.vaadin.ui.Panel;
import com.vaadin.ui.TabSheet;
import com.vaadin.ui.Table;
import com.vaadin.ui.TextField;
import com.vaadin.ui.Window;
import com.vaadin.ui.TabSheet.SelectedTabChangeEvent;
import com.vaadin.ui.Window.Notification;

public class ReservationApplication extends Application {

    private SampleDB db;

    ResourceSelectorPanel resourcePanel;

    private CalendarField reservedFrom;

    private static final long DEFAULT_GAP_MILLIS = 3600000; // (almost) one

    // hour
    private long currentGapMillis = DEFAULT_GAP_MILLIS; // current length of

    // reservation
    private CalendarField reservedTo;

    private Label resourceName;

    private TextField description;

    private Button reservationButton;

    private Table allTable;

    private GoogleMap map;

    @Override
    public void init() {
        //
        db = new SampleDB(getProperty("jdbcUrl"));

        final Window mainWindow = new Window("Reservr ");
        setMainWindow(mainWindow);
        setTheme("reservr");
        mainWindow.getLayout().setWidth("100%");

        Label logo = new Label("Reservr");
        logo.setStyleName("logo");
        mainWindow.addComponent(logo);

        Label slogan = new Label("Representational vehicles on-the-road");
        slogan.setStyleName("slogan");
        mainWindow.addComponent(slogan);

        final TabSheet mainTabs = new TabSheet();
        mainWindow.addComponent(mainTabs);

        final OrderedLayout reservationTab = new OrderedLayout();
        reservationTab.setWidth("100%");
        mainTabs.addTab(reservationTab, "Make reservation", null);

        resourcePanel = new ResourceSelectorPanel("Resources");
        resourcePanel.setResourceContainer(db.getResources(null));
        resourcePanel.addListener(
                ResourceSelectorPanel.SelectedResourcesChangedEvent.class,
                this, "selectedResourcesChanged");
        reservationTab.addComponent(resourcePanel);

        final Panel reservationPanel = new Panel("Reservation",
                new OrderedLayout(OrderedLayout.ORIENTATION_HORIZONTAL));
        reservationPanel.addStyleName(Panel.STYLE_LIGHT);
        reservationPanel.getLayout().setMargin(true);
        reservationTab.addComponent(reservationPanel);

        final OrderedLayout infoLayout = new OrderedLayout();
        infoLayout.setSpacing(true);
        infoLayout.setSizeUndefined();
        infoLayout.setMargin(false, true, false, false);
        reservationPanel.addComponent(infoLayout);
        resourceName = new Label("From the list above");
        resourceName.setCaption("Choose resource");
        infoLayout.addComponent(resourceName);
        description = new TextField();
        description.setWidth("250px");
        description.setRows(5);
        infoLayout.addComponent(description);
        reservationButton = new Button("Make reservation", this,
                "makeReservation");
        infoLayout.addComponent(reservationButton);
        infoLayout.setComponentAlignment(reservationButton, Alignment.MIDDLE_CENTER);

        map = new GoogleMap();
        map.setWidth("250px");
        map.setHeight("250px");
        map.setItemMarkerHtmlPropertyId(SampleDB.Resource.PROPERTY_ID_NAME);
        map.setItemMarkerXPropertyId(SampleDB.Resource.PROPERTY_ID_LOCATIONX);
        map.setItemMarkerYPropertyId(SampleDB.Resource.PROPERTY_ID_LOCATIONY);
        map.setContainerDataSource(db.getResources(null));
        infoLayout.addComponent(map);

        final Calendar from = Calendar.getInstance();
        from.add(Calendar.HOUR, 1);
        from.set(Calendar.MINUTE, 0);
        from.set(Calendar.SECOND, 0);
        from.set(Calendar.MILLISECOND, 0);
        reservedFrom = new CalendarField("From");
        reservedFrom.setMinimumDate(from.getTime());
        reservedFrom.setValue(from.getTime());
        reservedFrom.setImmediate(true);
        initCalendarFieldPropertyIds(reservedFrom);
        reservationPanel.addComponent(reservedFrom);

        final Label arrowLabel = new Label("»");
        arrowLabel.setContentMode(Label.CONTENT_XHTML);
        arrowLabel.setStyleName("arrow");
        reservationPanel.addComponent(arrowLabel);

        final Calendar to = Calendar.getInstance();
        to.setTime(from.getTime());
        to.add(Calendar.MILLISECOND, (int) DEFAULT_GAP_MILLIS);
        reservedTo = new CalendarField("To");
        reservedTo.setMinimumDate(to.getTime());
        reservedTo.setValue(to.getTime());
        reservedTo.setImmediate(true);
        initCalendarFieldPropertyIds(reservedTo);
        reservationPanel.addComponent(reservedTo);

        reservedFrom.addListener(new ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                final Date fd = (Date) reservedFrom.getValue();
                if (fd == null) {
                    reservedTo.setValue(null);
                    reservedTo.setEnabled(false);
                    refreshSelectedResources(true);
                    return;
                } else {
                    reservedTo.setEnabled(true);
                }
                reservedTo.setMinimumDate(new Date(fd.getTime()
                        + DEFAULT_GAP_MILLIS));
                final Calendar to = Calendar.getInstance();
                to.setTime(fd);
                to.add(Calendar.MILLISECOND, (int) currentGapMillis);
                reservedTo.setValue(to.getTime());
                refreshSelectedResources(true);
            }
        });
        reservedTo.addListener(new ValueChangeListener() {
            public void valueChange(ValueChangeEvent event) {
                final Date from = (Date) reservedFrom.getValue();
                final Date to = (Date) reservedTo.getValue();
                currentGapMillis = to.getTime() - from.getTime();
                if (currentGapMillis <= 0) {
                    final Calendar t = Calendar.getInstance();
                    t.setTime(from);
                    t.add(Calendar.MILLISECOND, (int) DEFAULT_GAP_MILLIS);
                    reservedTo.setValue(t.getTime());
                }
                refreshSelectedResources(true);
            }
        });

        allTable = new Table();
        allTable.setWidth("100%");
        allTable.setHeight("450px");
        allTable.setColumnCollapsingAllowed(true);
        allTable.setColumnReorderingAllowed(true);
        mainTabs.addTab(allTable, "All reservations", null);
        mainTabs.addListener(new TabSheet.SelectedTabChangeListener() {
            public void selectedTabChange(SelectedTabChangeEvent event) {
                refreshReservations();
            }
        });

        resourcePanel.selectFirstCategory();
        refreshReservations();
        refreshSelectedResources(true);
    }

    public void makeReservation() {
        try {
            final Item resource = getActiveResource();
            if (resource != null) {
                db.addReservation(resource, 0, (Date) reservedFrom.getValue(),
                        (Date) reservedTo.getValue(), (String) description
                                .getValue());
                getMainWindow()
                        .showNotification(
                                "Success!",
                                "You have reserved the resource for the selected period.",
                                Notification.TYPE_WARNING_MESSAGE);
                refreshReservations();
                refreshSelectedResources(false);
            } else {
                getMainWindow().showNotification("Oops!",
                        "Please select a resource (or category) to reserve.",
                        Notification.TYPE_WARNING_MESSAGE);
            }
        } catch (final ResourceNotAvailableException e) {
            getMainWindow()
                    .showNotification(
                            "Not available!",
                            "The selected resource is already reserved for the selected period.",
                            Notification.TYPE_ERROR_MESSAGE);
            refreshReservations();
        }
    }

    private Item getActiveResource() throws ResourceNotAvailableException {
        final List rids = resourcePanel.getSelectedResources();
        if (rids != null && rids.size() > 0) {
            for (final Iterator it = rids.iterator(); it.hasNext();) {
                final Item resource = (Item) it.next();
                final int id = ((Integer) resource.getItemProperty(
                        SampleDB.Resource.PROPERTY_ID_ID).getValue())
                        .intValue();
                if (db.isAvailableResource(id, (Date) reservedFrom.getValue(),
                        (Date) reservedTo.getValue())) {
                    return resource;
                }
            }
            throw new ResourceNotAvailableException("No available resources");
        } else {
            return null;
        }
    }

    private void refreshReservations() {
        final Container reservations = db.getReservations(resourcePanel
                .getSelectedResources());
        reservedFrom.setContainerDataSource(reservations);
        reservedTo.setContainerDataSource(reservations);
        final Container allReservations = db.getReservations(null);
        allTable.setContainerDataSource(allReservations);
        if (allReservations != null && allReservations.size() > 0) {
            allTable.setVisibleColumns(new Object[] {
                    SampleDB.Reservation.PROPERTY_ID_RESERVED_FROM,
                    SampleDB.Reservation.PROPERTY_ID_RESERVED_TO,
                    SampleDB.Resource.PROPERTY_ID_NAME,
                    SampleDB.Resource.PROPERTY_ID_DESCRIPTION,
                    SampleDB.Reservation.PROPERTY_ID_DESCRIPTION });
            allTable.setColumnHeaders(new String[] { "From", "To", "Resource",
                    "Description", "Message" });
        }
    }

    private void refreshSelectedResources(boolean alertIfNotAvailable) {
        Item resource = null;
        try {
            resource = getActiveResource();
        } catch (final ResourceNotAvailableException e) {
            if (alertIfNotAvailable) {
                getMainWindow().showNotification("Not available",
                        "Please choose another resource or time period.",
                        Notification.TYPE_HUMANIZED_MESSAGE);
            }
            refreshReservations();
            return;
        }
        map.clear();
        if (resource == null) {
            resourceName.setCaption("Choose resource above");
            resourceName.setValue("");
            map.setContainerDataSource(db.getResources(null));
            map.setZoomLevel(1);

        } else {
            // Display active resource name + desc
            String name = (String) resource.getItemProperty(
                    SampleDB.Resource.PROPERTY_ID_NAME).getValue();
            String desc = (String) resource.getItemProperty(
                    SampleDB.Resource.PROPERTY_ID_DESCRIPTION).getValue();
            resourceName.setCaption(name);
            resourceName.setValue(desc);
            // Put all resources on map (may be many if category was selected)
            final LinkedList srs = resourcePanel.getSelectedResources();
            for (final Iterator it = srs.iterator(); it.hasNext();) {
                resource = (Item) it.next();
                name = (String) resource.getItemProperty(
                        SampleDB.Resource.PROPERTY_ID_NAME).getValue();
                desc = (String) resource.getItemProperty(
                        SampleDB.Resource.PROPERTY_ID_DESCRIPTION).getValue();
                final Double x = (Double) resource.getItemProperty(
                        SampleDB.Resource.PROPERTY_ID_LOCATIONX).getValue();
                final Double y = (Double) resource.getItemProperty(
                        SampleDB.Resource.PROPERTY_ID_LOCATIONY).getValue();
                if (x != null && y != null) {
                    map.addMarker(name + "<br/>" + desc, new Point2D.Double(x
                            .doubleValue(), y.doubleValue()));

                }

            }
            map.setZoomLevel((srs.size() == 1 ? 14 : 9));
        }

    }

    private void initCalendarFieldPropertyIds(CalendarField cal) {
        cal.setItemStyleNamePropertyId(SampleDB.Resource.PROPERTY_ID_STYLENAME);
        cal
                .setItemStartPropertyId(SampleDB.Reservation.PROPERTY_ID_RESERVED_FROM);
        cal.setItemEndPropertyId(SampleDB.Reservation.PROPERTY_ID_RESERVED_TO);
        cal.setItemTitlePropertyId(SampleDB.Resource.PROPERTY_ID_NAME);
        cal
                .setItemDescriptionPropertyId(SampleDB.Reservation.PROPERTY_ID_DESCRIPTION);
    }

    public void selectedResourcesChanged(
            ResourceSelectorPanel.SelectedResourcesChangedEvent event) {
        refreshReservations();
        refreshSelectedResources(true);
    }

}