summaryrefslogtreecommitdiffstats
path: root/server/src/com/vaadin/data/util/QueryContainer.java
blob: 319cf2ad0dc590d163ae305375301a5875a57177 (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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
/* 
@VaadinApache2LicenseForJavaFiles@
 */

package com.vaadin.data.util;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;

import com.vaadin.data.Container;
import com.vaadin.data.Item;
import com.vaadin.data.Property;

/**
 * <p>
 * The <code>QueryContainer</code> is the specialized form of Container which is
 * Ordered and Indexed. This is used to represent the contents of relational
 * database tables accessed through the JDBC Connection in the Vaadin Table.
 * This creates Items based on the queryStatement provided to the container.
 * </p>
 * 
 * <p>
 * The <code>QueryContainer</code> can be visualized as a representation of a
 * relational database table.Each Item in the container represents the row
 * fetched by the query.All cells in a column have same data type and the data
 * type information is retrieved from the metadata of the resultset.
 * </p>
 * 
 * <p>
 * Note : If data in the tables gets modified, Container will not get reflected
 * with the updates, we have to explicity invoke QueryContainer.refresh method.
 * {@link com.vaadin.data.util.QueryContainer#refresh() refresh()}
 * </p>
 * 
 * @see com.vaadin.data.Container
 * 
 * @author Vaadin Ltd.
 * @since 4.0
 * 
 * @deprecated will be removed in the future, use the SQLContainer add-on
 */

@Deprecated
@SuppressWarnings("serial")
public class QueryContainer implements Container, Container.Ordered,
        Container.Indexed {

    // default ResultSet type
    public static final int DEFAULT_RESULTSET_TYPE = ResultSet.TYPE_SCROLL_INSENSITIVE;

    // default ResultSet concurrency
    public static final int DEFAULT_RESULTSET_CONCURRENCY = ResultSet.CONCUR_READ_ONLY;

    private int resultSetType = DEFAULT_RESULTSET_TYPE;

    private int resultSetConcurrency = DEFAULT_RESULTSET_CONCURRENCY;

    private final String queryStatement;

    private final Connection connection;

    private ResultSet result;

    private Collection<String> propertyIds;

    private final HashMap<String, Class<?>> propertyTypes = new HashMap<String, Class<?>>();

    private int size = -1;

    private Statement statement;

    /**
     * Constructs new <code>QueryContainer</code> with the specified
     * <code>queryStatement</code>.
     * 
     * @param queryStatement
     *            Database query
     * @param connection
     *            Connection object
     * @param resultSetType
     * @param resultSetConcurrency
     * @throws SQLException
     *             when database operation fails
     */
    public QueryContainer(String queryStatement, Connection connection,
            int resultSetType, int resultSetConcurrency) throws SQLException {
        this.queryStatement = queryStatement;
        this.connection = connection;
        this.resultSetType = resultSetType;
        this.resultSetConcurrency = resultSetConcurrency;
        init();
    }

    /**
     * Constructs new <code>QueryContainer</code> with the specified
     * queryStatement using the default resultset type and default resultset
     * concurrency.
     * 
     * @param queryStatement
     *            Database query
     * @param connection
     *            Connection object
     * @see QueryContainer#DEFAULT_RESULTSET_TYPE
     * @see QueryContainer#DEFAULT_RESULTSET_CONCURRENCY
     * @throws SQLException
     *             when database operation fails
     */
    public QueryContainer(String queryStatement, Connection connection)
            throws SQLException {
        this(queryStatement, connection, DEFAULT_RESULTSET_TYPE,
                DEFAULT_RESULTSET_CONCURRENCY);
    }

    /**
     * Fills the Container with the items and properties. Invoked by the
     * constructor.
     * 
     * @throws SQLException
     *             when parameter initialization fails.
     * @see QueryContainer#QueryContainer(String, Connection, int, int).
     */
    private void init() throws SQLException {
        refresh();
        ResultSetMetaData metadata;
        metadata = result.getMetaData();
        final int count = metadata.getColumnCount();
        final ArrayList<String> list = new ArrayList<String>(count);
        for (int i = 1; i <= count; i++) {
            final String columnName = metadata.getColumnName(i);
            list.add(columnName);
            final Property<?> p = getContainerProperty(new Integer(1),
                    columnName);
            propertyTypes.put(columnName,
                    p == null ? Object.class : p.getType());
        }
        propertyIds = Collections.unmodifiableCollection(list);
    }

    /**
     * <p>
     * Restores items in the container. This method will update the latest data
     * to the container.
     * </p>
     * Note: This method should be used to update the container with the latest
     * items.
     * 
     * @throws SQLException
     *             when database operation fails
     * 
     */

    public void refresh() throws SQLException {
        close();
        statement = connection.createStatement(resultSetType,
                resultSetConcurrency);
        result = statement.executeQuery(queryStatement);
        result.last();
        size = result.getRow();
    }

    /**
     * Releases and nullifies the <code>statement</code>.
     * 
     * @throws SQLException
     *             when database operation fails
     */

    public void close() throws SQLException {
        if (statement != null) {
            statement.close();
        }
        statement = null;
    }

    /**
     * Gets the Item with the given Item ID from the Container.
     * 
     * @param id
     *            ID of the Item to retrieve
     * @return Item Id.
     */

    @Override
    public Item getItem(Object id) {
        return new Row(id);
    }

    /**
     * Gets the collection of propertyId from the Container.
     * 
     * @return Collection of Property ID.
     */

    @Override
    public Collection<String> getContainerPropertyIds() {
        return propertyIds;
    }

    /**
     * Gets an collection of all the item IDs in the container.
     * 
     * @return collection of Item IDs
     */
    @Override
    public Collection<?> getItemIds() {
        final Collection<Integer> c = new ArrayList<Integer>(size);
        for (int i = 1; i <= size; i++) {
            c.add(new Integer(i));
        }
        return c;
    }

    /**
     * Gets the property identified by the given itemId and propertyId from the
     * container. If the container does not contain the property
     * <code>null</code> is returned.
     * 
     * @param itemId
     *            ID of the Item which contains the Property
     * @param propertyId
     *            ID of the Property to retrieve
     * 
     * @return Property with the given ID if exists; <code>null</code>
     *         otherwise.
     */

    @Override
    public synchronized Property<?> getContainerProperty(Object itemId,
            Object propertyId) {
        if (!(itemId instanceof Integer && propertyId instanceof String)) {
            return null;
        }
        Object value;
        try {
            result.absolute(((Integer) itemId).intValue());
            value = result.getObject((String) propertyId);
        } catch (final Exception e) {
            return null;
        }

        // Handle also null values from the database
        return new ObjectProperty<Object>(value != null ? value
                : new String(""));
    }

    /**
     * Gets the data type of all properties identified by the given type ID.
     * 
     * @param id
     *            ID identifying the Properties
     * 
     * @return data type of the Properties
     */

    @Override
    public Class<?> getType(Object id) {
        return propertyTypes.get(id);
    }

    /**
     * Gets the number of items in the container.
     * 
     * @return the number of items in the container.
     */
    @Override
    public int size() {
        return size;
    }

    /**
     * Tests if the list contains the specified Item.
     * 
     * @param id
     *            ID the of Item to be tested.
     * @return <code>true</code> if given id is in the container;
     *         <code>false</code> otherwise.
     */
    @Override
    public boolean containsId(Object id) {
        if (!(id instanceof Integer)) {
            return false;
        }
        final int i = ((Integer) id).intValue();
        if (i < 1) {
            return false;
        }
        if (i > size) {
            return false;
        }
        return true;
    }

    /**
     * Creates new Item with the given ID into the Container.
     * 
     * @param itemId
     *            ID of the Item to be created.
     * 
     * @return Created new Item, or <code>null</code> if it fails.
     * 
     * @throws UnsupportedOperationException
     *             if the addItem method is not supported.
     */
    @Override
    public Item addItem(Object itemId) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Creates a new Item into the Container, and assign it an ID.
     * 
     * @return ID of the newly created Item, or <code>null</code> if it fails.
     * @throws UnsupportedOperationException
     *             if the addItem method is not supported.
     */
    @Override
    public Object addItem() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Removes the Item identified by ItemId from the Container.
     * 
     * @param itemId
     *            ID of the Item to remove.
     * @return <code>true</code> if the operation succeeded; <code>false</code>
     *         otherwise.
     * @throws UnsupportedOperationException
     *             if the removeItem method is not supported.
     */
    @Override
    public boolean removeItem(Object itemId)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Adds new Property to all Items in the Container.
     * 
     * @param propertyId
     *            ID of the Property
     * @param type
     *            Data type of the new Property
     * @param defaultValue
     *            The value all created Properties are initialized to.
     * @return <code>true</code> if the operation succeeded; <code>false</code>
     *         otherwise.
     * @throws UnsupportedOperationException
     *             if the addContainerProperty method is not supported.
     */
    @Override
    public boolean addContainerProperty(Object propertyId, Class<?> type,
            Object defaultValue) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Removes a Property specified by the given Property ID from the Container.
     * 
     * @param propertyId
     *            ID of the Property to remove
     * @return <code>true</code> if the operation succeeded; <code>false</code>
     *         otherwise.
     * @throws UnsupportedOperationException
     *             if the removeContainerProperty method is not supported.
     */
    @Override
    public boolean removeContainerProperty(Object propertyId)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Removes all Items from the Container.
     * 
     * @return <code>true</code> if the operation succeeded; <code>false</code>
     *         otherwise.
     * @throws UnsupportedOperationException
     *             if the removeAllItems method is not supported.
     */
    @Override
    public boolean removeAllItems() throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Adds new item after the given item.
     * 
     * @param previousItemId
     *            Id of the previous item in ordered container.
     * @param newItemId
     *            Id of the new item to be added.
     * @return Returns new item or <code>null</code> if the operation fails.
     * @throws UnsupportedOperationException
     *             if the addItemAfter method is not supported.
     */
    @Override
    public Item addItemAfter(Object previousItemId, Object newItemId)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Adds new item after the given item.
     * 
     * @param previousItemId
     *            Id of the previous item in ordered container.
     * @return Returns item id created new item or <code>null</code> if the
     *         operation fails.
     * @throws UnsupportedOperationException
     *             if the addItemAfter method is not supported.
     */
    @Override
    public Object addItemAfter(Object previousItemId)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Returns id of first item in the Container.
     * 
     * @return ID of the first Item in the list.
     */
    @Override
    public Object firstItemId() {
        if (size < 1) {
            return null;
        }
        return new Integer(1);
    }

    /**
     * Returns <code>true</code> if given id is first id at first index.
     * 
     * @param id
     *            ID of an Item in the Container.
     */
    @Override
    public boolean isFirstId(Object id) {
        return size > 0 && (id instanceof Integer)
                && ((Integer) id).intValue() == 1;
    }

    /**
     * Returns <code>true</code> if given id is last id at last index.
     * 
     * @param id
     *            ID of an Item in the Container
     * 
     */
    @Override
    public boolean isLastId(Object id) {
        return size > 0 && (id instanceof Integer)
                && ((Integer) id).intValue() == size;
    }

    /**
     * Returns id of last item in the Container.
     * 
     * @return ID of the last Item.
     */
    @Override
    public Object lastItemId() {
        if (size < 1) {
            return null;
        }
        return new Integer(size);
    }

    /**
     * Returns id of next item in container at next index.
     * 
     * @param id
     *            ID of an Item in the Container.
     * @return ID of the next Item or null.
     */
    @Override
    public Object nextItemId(Object id) {
        if (size < 1 || !(id instanceof Integer)) {
            return null;
        }
        final int i = ((Integer) id).intValue();
        if (i >= size) {
            return null;
        }
        return new Integer(i + 1);
    }

    /**
     * Returns id of previous item in container at previous index.
     * 
     * @param id
     *            ID of an Item in the Container.
     * @return ID of the previous Item or null.
     */
    @Override
    public Object prevItemId(Object id) {
        if (size < 1 || !(id instanceof Integer)) {
            return null;
        }
        final int i = ((Integer) id).intValue();
        if (i <= 1) {
            return null;
        }
        return new Integer(i - 1);
    }

    /**
     * The <code>Row</code> class implements methods of Item.
     * 
     * @author Vaadin Ltd.
     * @since 4.0
     */
    class Row implements Item {

        Object id;

        private Row(Object rowId) {
            id = rowId;
        }

        /**
         * Adds the item property.
         * 
         * @param id
         *            ID of the new Property.
         * @param property
         *            Property to be added and associated with ID.
         * @return <code>true</code> if the operation succeeded;
         *         <code>false</code> otherwise.
         * @throws UnsupportedOperationException
         *             if the addItemProperty method is not supported.
         */
        @Override
        public boolean addItemProperty(Object id, Property property)
                throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

        /**
         * Gets the property corresponding to the given property ID stored in
         * the Item.
         * 
         * @param propertyId
         *            identifier of the Property to get
         * @return the Property with the given ID or <code>null</code>
         */
        @Override
        public Property<?> getItemProperty(Object propertyId) {
            return getContainerProperty(id, propertyId);
        }

        /**
         * Gets the collection of property IDs stored in the Item.
         * 
         * @return unmodifiable collection containing IDs of the Properties
         *         stored the Item.
         */
        @Override
        public Collection<String> getItemPropertyIds() {
            return propertyIds;
        }

        /**
         * Removes given item property.
         * 
         * @param id
         *            ID of the Property to be removed.
         * @return <code>true</code> if the item property is removed;
         *         <code>false</code> otherwise.
         * @throws UnsupportedOperationException
         *             if the removeItemProperty is not supported.
         */
        @Override
        public boolean removeItemProperty(Object id)
                throws UnsupportedOperationException {
            throw new UnsupportedOperationException();
        }

    }

    /**
     * Closes the statement.
     * 
     * @see #close()
     */
    @Override
    public void finalize() {
        try {
            close();
        } catch (final SQLException ignored) {

        }
    }

    /**
     * Adds the given item at the position of given index.
     * 
     * @param index
     *            Index to add the new item.
     * @param newItemId
     *            Id of the new item to be added.
     * @return new item or <code>null</code> if the operation fails.
     * @throws UnsupportedOperationException
     *             if the addItemAt is not supported.
     */
    @Override
    public Item addItemAt(int index, Object newItemId)
            throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Adds item at the position of provided index in the container.
     * 
     * @param index
     *            Index to add the new item.
     * @return item id created new item or <code>null</code> if the operation
     *         fails.
     * 
     * @throws UnsupportedOperationException
     *             if the addItemAt is not supported.
     */

    @Override
    public Object addItemAt(int index) throws UnsupportedOperationException {
        throw new UnsupportedOperationException();
    }

    /**
     * Gets the Index id in the container.
     * 
     * @param index
     *            Index Id.
     * @return ID in the given index.
     */
    @Override
    public Object getIdByIndex(int index) {
        if (size < 1 || index < 0 || index >= size) {
            return null;
        }
        return new Integer(index + 1);
    }

    /**
     * Gets the index of the Item corresponding to id in the container.
     * 
     * @param id
     *            ID of an Item in the Container
     * @return index of the Item, or -1 if the Container does not include the
     *         Item
     */

    @Override
    public int indexOfId(Object id) {
        if (size < 1 || !(id instanceof Integer)) {
            return -1;
        }
        final int i = ((Integer) id).intValue();
        if (i >= size || i < 1) {
            return -1;
        }
        return i - 1;
    }

}