OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
182 ilm 4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
17 ilm 5
 *
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
7
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
8
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
9
 * language governing permissions and limitations under the License.
10
 *
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
13
 
14
 package org.openconcerto.sql.view.search;
15
 
16
import java.util.Date;
17
import java.util.List;
18
 
19
public class DateSearchSpec implements SearchSpec {
20
    // include or exclude filterString
182 ilm 21
    private final boolean excludeFilterString;
22
    private final int columnIndex;
23
    private final Date fromDate;
24
    private final Date toDate;
17 ilm 25
 
26
    public DateSearchSpec(Date from, Date to, int columnIndex) {
27
        this(false, from, to, columnIndex);
28
    }
29
 
30
    public DateSearchSpec(boolean excludeFilterString, Date from, Date to, int columnIndex) {
31
        this.excludeFilterString = excludeFilterString;
32
        this.fromDate = new Date(from.getTime());
33
        this.toDate = new Date(to.getTime());
34
        this.columnIndex = columnIndex;
35
    }
36
 
37
    /**
38
     * Est-ce que line contient une date entre fromDate et toDate dans la colonne index.
39
     *
40
     * @param line la ligne dans laquelle chercher.
41
     * @param fromDate la date de debut.
42
     * @param toDate la date de fin.
43
     * @param index l'index de la colonne, -1 pour toute la ligne.
44
     * @return <code>true</code> si la ligne contient.
45
     */
46
    static private boolean contains(Object line, Date startDate, Date stopDate, int index) {
182 ilm 47
        final List<?> list = (List<?>) line;
17 ilm 48
        final int start;
49
        final int stop;
50
        if (index < 0) {
51
            // Cas ou on cherche sur tout
52
            start = 0;
53
            stop = list.size();// this.getColumnCount();
54
        } else {
55
            // Cas ou on cherche sur 1 colonne
56
            start = index;
57
            stop = index + 1;
58
        }
59
 
60
        for (int i = start; i < stop; i++) {
61
            final Object cell = list.get(i);
62
            if (cell != null) {
63
                if (cell instanceof Date) {
182 ilm 64
                    final Date date = (Date) cell;
17 ilm 65
                    if (date.after(startDate) && date.before(stopDate)) {
66
                        return true;
67
                    }
68
                } else {
69
                    throw new IllegalArgumentException("The value is not a Date:" + cell + " index:" + index);
70
                }
71
 
72
            }
73
        }
74
        return false;
75
    }
76
 
182 ilm 77
    @Override
17 ilm 78
    public boolean match(Object line) {
79
        return this.excludeFilterString ^ contains(line, this.fromDate, this.toDate, this.columnIndex);
80
    }
81
 
82
    public void dump() {
83
        System.out.println(this.excludeFilterString + ":" + this.fromDate + "->" + this.toDate + " col:" + this.columnIndex);
84
    }
85
 
182 ilm 86
    @Override
17 ilm 87
    public boolean isEmpty() {
88
        return true;
89
    }
90
 
91
}