OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
18 ilm 1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
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.erp.core.common.ui;
15
 
73 ilm 16
import org.openconcerto.erp.config.ComptaPropsConfiguration;
156 ilm 17
import org.openconcerto.erp.utils.TM;
61 ilm 18
import org.openconcerto.sql.Configuration;
18 ilm 19
import org.openconcerto.sql.model.SQLField;
73 ilm 20
import org.openconcerto.sql.model.SQLRow;
18 ilm 21
import org.openconcerto.sql.model.SQLSelect;
22
import org.openconcerto.sql.model.Where;
23
import org.openconcerto.sql.view.list.IListe;
24
import org.openconcerto.ui.DefaultGridBagConstraints;
25
import org.openconcerto.ui.JDate;
26
import org.openconcerto.utils.Tuple2;
27
import org.openconcerto.utils.cc.ITransformer;
28
 
29
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
31
import java.awt.event.ActionEvent;
32
import java.awt.event.ActionListener;
33
import java.beans.PropertyChangeEvent;
34
import java.beans.PropertyChangeListener;
73 ilm 35
import java.io.File;
18 ilm 36
import java.util.Calendar;
37
import java.util.Date;
41 ilm 38
import java.util.HashMap;
18 ilm 39
import java.util.LinkedHashMap;
40
import java.util.Map;
41
 
42
import javax.swing.BorderFactory;
43
import javax.swing.DefaultComboBoxModel;
44
import javax.swing.JComboBox;
45
import javax.swing.JLabel;
46
import javax.swing.JPanel;
73 ilm 47
import javax.swing.event.EventListenerList;
18 ilm 48
 
49
public class IListFilterDatePanel extends JPanel {
50
 
41 ilm 51
    private JDate dateDu, dateAu;
156 ilm 52
    private static final String CUSTOM_COMBO_ITEM = TM.tr("dateFilter.custom");
41 ilm 53
    private Map<IListe, SQLField> mapList;
57 ilm 54
    // Cache des transformers initiaux
55
    private Map<IListe, ITransformer<SQLSelect, SQLSelect>> mapListTransformer;
41 ilm 56
    // Liste des filtres
57
    private Map<String, Tuple2<Date, Date>> map;
61 ilm 58
    private static LinkedHashMap<String, Tuple2<Date, Date>> mapDefault;
93 ilm 59
    public static boolean LOAD_STATE = true;
174 ilm 60
    private static Tuple2<Date, Date> exerciceTuple;
18 ilm 61
 
61 ilm 62
    private JComboBox combo;
63
 
73 ilm 64
    private EventListenerList listeners = new EventListenerList();
65
 
80 ilm 66
    private final PropertyChangeListener listener = new PropertyChangeListener() {
73 ilm 67
 
18 ilm 68
        @Override
80 ilm 69
        public void propertyChange(PropertyChangeEvent evt) {
73 ilm 70
            combo.setSelectedItem(CUSTOM_COMBO_ITEM);
18 ilm 71
            fireDateChanged();
72
        }
73
    };
74
 
75
    // FIXME Cacher pour ne pas recharger la liste si il n'y a aucune modif
76
    public IListFilterDatePanel(IListe l, SQLField fieldDate) {
77
        this(l, fieldDate, null);
61 ilm 78
        if (l.getRequest() == Configuration.getInstance().getDirectory().getElement(l.getSource().getPrimaryTable()).getListRequest()) {
93 ilm 79
            System.err.println("Attention il ne faut pas utiliser la ListRequest par défaut sinon les filtres restent !!!");
61 ilm 80
            Thread.dumpStack();
81
        }
18 ilm 82
    }
83
 
174 ilm 84
    private static void initDefaultMap() {
85
        mapDefault = new LinkedHashMap<String, Tuple2<Date, Date>>();
86
 
87
        // Exercice courant
88
        SQLRow rowEx = ComptaPropsConfiguration.getInstanceCompta().getRowSociete().getForeignRow("ID_EXERCICE_COMMON");
89
        Calendar c1 = rowEx.getDate("DATE_DEB");
90
        clearTimeSchedule(c1);
91
        Calendar c2 = rowEx.getDate("DATE_FIN");
92
        setEndTimeSchedule(c2);
93
        exerciceTuple = Tuple2.create(c1.getTime(), c2.getTime());
94
    }
95
 
18 ilm 96
    public static Map<String, Tuple2<Date, Date>> getDefaultMap() {
61 ilm 97
        if (mapDefault == null) {
98
            initDefaultMap();
99
        }
18 ilm 100
        Map<String, Tuple2<Date, Date>> m = new LinkedHashMap<String, Tuple2<Date, Date>>();
174 ilm 101
        m.putAll(getDefaultDateFilter());
61 ilm 102
        m.putAll(mapDefault);
103
        return m;
104
    }
18 ilm 105
 
174 ilm 106
    public static Map<String, Tuple2<Date, Date>> getDefaultDateFilter() {
107
        Map<String, Tuple2<Date, Date>> mapDefaultDate = new LinkedHashMap<>();
73 ilm 108
 
109
        // ALL
110
        Date emptyDate = null;
174 ilm 111
        mapDefaultDate.put(TM.tr("dateFilter.none"), Tuple2.create(emptyDate, emptyDate));
73 ilm 112
 
61 ilm 113
        Calendar c = Calendar.getInstance();
18 ilm 114
        // Année courante
73 ilm 115
        clearTimeSchedule(c);
18 ilm 116
        c.set(Calendar.DATE, 1);
117
        c.set(Calendar.MONTH, 0);
118
        Date d1 = c.getTime();
73 ilm 119
        setEndTimeSchedule(c);
18 ilm 120
        c.set(Calendar.DATE, 31);
121
        c.set(Calendar.MONTH, 11);
122
        Date d2 = c.getTime();
174 ilm 123
        mapDefaultDate.put(TM.tr("dateFilter.currentYear"), Tuple2.create(d1, d2));
18 ilm 124
 
125
        // Année précedente
73 ilm 126
        clearTimeSchedule(c);
18 ilm 127
        c.set(Calendar.DATE, 1);
128
        c.set(Calendar.MONTH, 0);
129
        c.add(Calendar.YEAR, -1);
130
        Date d3 = c.getTime();
73 ilm 131
 
132
        setEndTimeSchedule(c);
18 ilm 133
        c.set(Calendar.DATE, 31);
134
        c.set(Calendar.MONTH, 11);
135
        Date d4 = c.getTime();
174 ilm 136
        mapDefaultDate.put(TM.tr("dateFilter.previousYear"), Tuple2.create(d3, d4));
18 ilm 137
 
138
        // Mois courant
139
        c = Calendar.getInstance();
73 ilm 140
        clearTimeSchedule(c);
18 ilm 141
        c.set(Calendar.DATE, 1);
142
        Date d5 = c.getTime();
143
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
73 ilm 144
        setEndTimeSchedule(c);
18 ilm 145
        Date d6 = c.getTime();
174 ilm 146
        mapDefaultDate.put(TM.tr("dateFilter.currentMonth"), Tuple2.create(d5, d6));
18 ilm 147
 
148
        // Mois précédent
149
        c = Calendar.getInstance();
73 ilm 150
        clearTimeSchedule(c);
18 ilm 151
        c.set(Calendar.DATE, 1);
152
        c.add(Calendar.MONTH, -1);
153
        Date d7 = c.getTime();
154
        c.set(Calendar.DATE, c.getActualMaximum(Calendar.DATE));
73 ilm 155
        setEndTimeSchedule(c);
18 ilm 156
        Date d8 = c.getTime();
174 ilm 157
        mapDefaultDate.put(TM.tr("dateFilter.previousMonth"), Tuple2.create(d7, d8));
18 ilm 158
 
159
        // semaine courante
160
        c = Calendar.getInstance();
73 ilm 161
        clearTimeSchedule(c);
18 ilm 162
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
163
        Date d9 = c.getTime();
164
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
73 ilm 165
        setEndTimeSchedule(c);
18 ilm 166
        Date d10 = c.getTime();
174 ilm 167
        mapDefaultDate.put(TM.tr("dateFilter.currentWeek"), Tuple2.create(d9, d10));
18 ilm 168
 
169
        // semaine précédente
170
        c = Calendar.getInstance();
73 ilm 171
        clearTimeSchedule(c);
18 ilm 172
        c.add(Calendar.DATE, -7);
173
        c.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
174
        Date d11 = c.getTime();
175
        c.set(Calendar.DAY_OF_WEEK, Calendar.SUNDAY);
73 ilm 176
        setEndTimeSchedule(c);
18 ilm 177
        Date d12 = c.getTime();
174 ilm 178
        mapDefaultDate.put(TM.tr("dateFilter.previousWeek"), Tuple2.create(d11, d12));
73 ilm 179
 
180
        if (findItem(exerciceTuple, mapDefault).equals(CUSTOM_COMBO_ITEM)) {
174 ilm 181
            mapDefaultDate.put(TM.tr("dateFilter.currentFiscalYear"), exerciceTuple);
73 ilm 182
        }
183
 
184
        // Custom
174 ilm 185
        mapDefaultDate.put(CUSTOM_COMBO_ITEM, null);
186
 
187
        return mapDefaultDate;
188
 
61 ilm 189
    }
18 ilm 190
 
174 ilm 191
    private static void addCustomToMap(Map<String, Tuple2<Date, Date>> map) {
192
 
193
    }
194
 
61 ilm 195
    public static void addDefaultValue(String label, Tuple2<Date, Date> period) {
196
        if (mapDefault == null)
197
            initDefaultMap();
198
        mapDefault.put(label, period);
18 ilm 199
    }
200
 
201
    public IListFilterDatePanel(IListe l, SQLField fieldDate, Map<String, Tuple2<Date, Date>> m) {
202
        super(new GridBagLayout());
61 ilm 203
        setOpaque(false);
41 ilm 204
        Map<IListe, SQLField> map = new HashMap<IListe, SQLField>();
205
        map.put(l, fieldDate);
206
 
207
        init(map, m);
208
 
209
    }
210
 
211
    public IListFilterDatePanel(Map<IListe, SQLField> l, Map<String, Tuple2<Date, Date>> m) {
212
        super(new GridBagLayout());
213
        init(l, m);
214
 
215
    }
216
 
217
    public void init(Map<IListe, SQLField> mapList, Map<String, Tuple2<Date, Date>> m) {
218
 
156 ilm 219
        this.setBorder(BorderFactory.createTitledBorder(TM.tr("dateFilter.range")));
41 ilm 220
        this.mapList = mapList;
57 ilm 221
 
222
        this.mapListTransformer = new HashMap<IListe, ITransformer<SQLSelect, SQLSelect>>();
223
        for (IListe l : mapList.keySet()) {
73 ilm 224
 
57 ilm 225
            this.mapListTransformer.put(l, l.getRequest().getSelectTransf());
226
        }
227
 
18 ilm 228
        this.dateDu = new JDate();
229
        this.dateAu = new JDate();
230
        this.map = m;
231
 
232
        GridBagConstraints c = new DefaultGridBagConstraints();
233
        c.gridx = GridBagConstraints.RELATIVE;
234
        c.weightx = 0;
90 ilm 235
        c.fill = GridBagConstraints.NONE;
18 ilm 236
 
237
        // Période pédéfini
238
        if (map != null && map.keySet().size() > 0) {
239
            DefaultComboBoxModel model = new DefaultComboBoxModel();
73 ilm 240
 
18 ilm 241
            for (String s : this.map.keySet()) {
242
                model.addElement(s);
243
            }
244
 
61 ilm 245
            this.combo = new JComboBox(model);
246
 
18 ilm 247
            c.weightx = 0;
248
            this.add(combo, c);
249
 
250
            combo.addActionListener(new ActionListener() {
251
                @Override
252
                public void actionPerformed(ActionEvent e) {
253
                    String s = (String) combo.getSelectedItem();
254
                    setPeriode(map.get(s));
255
                }
256
            });
257
        }
258
        // Filtre
156 ilm 259
        this.add(new JLabel(TM.tr("dateFilter.from")), c);
18 ilm 260
        this.add(this.dateDu, c);
156 ilm 261
        this.add(new JLabel(TM.tr("dateFilter.to")), c);
18 ilm 262
        this.add(this.dateAu, c);
80 ilm 263
        this.dateAu.addValueListener(this.listener);
264
        this.dateDu.addValueListener(this.listener);
73 ilm 265
 
266
        IListFilterDateStateManager stateManager = new IListFilterDateStateManager(this, getConfigFile(mapList), true);
93 ilm 267
        if (LOAD_STATE) {
268
            stateManager.loadState();
269
        }
18 ilm 270
    }
271
 
272
    public void setDateDu(Date d) {
273
        this.dateDu.setValue(d);
274
    }
275
 
276
    public void setDateAu(Date d) {
277
        this.dateAu.setValue(d);
278
    }
279
 
73 ilm 280
    public Date getFromValue() {
281
        return this.dateDu.getValue();
282
    }
283
 
284
    public Date getToValue() {
285
        return this.dateAu.getValue();
286
    }
287
 
61 ilm 288
    private static Tuple2<String, Tuple2<Date, Date>> DEFAULT_FILTER = null;
289
 
290
    public static void setDefaultFilter(Tuple2<String, Tuple2<Date, Date>> t) {
291
        DEFAULT_FILTER = t;
41 ilm 292
    }
293
 
61 ilm 294
    public void setFilterOnDefault() {
295
 
296
        if (DEFAULT_FILTER != null) {
297
 
298
            if (this.combo != null) {
299
                this.combo.setSelectedItem(DEFAULT_FILTER.get0());
300
            } else {
301
                setPeriode(DEFAULT_FILTER.get1().get0(), DEFAULT_FILTER.get1().get1());
302
            }
303
        }
304
    }
305
 
73 ilm 306
    /**
307
     *
308
     * @param t do nothing if t is null
309
     */
18 ilm 310
    public void setPeriode(Tuple2<Date, Date> t) {
73 ilm 311
        if (t != null) {
18 ilm 312
            setPeriode(t.get0(), t.get1());
313
        }
314
    }
315
 
316
    public void setPeriode(Date du, Date au) {
317
        setDateAu(au);
318
        setDateDu(du);
319
 
320
        fireDateChanged();
321
    }
322
 
323
    public void fireDateChanged() {
324
        if (this.dateAu.getValue() == null && this.dateDu.getValue() == null) {
57 ilm 325
            for (IListe list : this.mapList.keySet()) {
326
                list.getRequest().setSelectTransf(this.mapListTransformer.get(list));
18 ilm 327
            }
73 ilm 328
        } else if (this.dateAu.getValue() == null) {
18 ilm 329
            final Calendar c = Calendar.getInstance();
330
            c.setTime(this.dateDu.getValue());
73 ilm 331
            clearTimeSchedule(c);
18 ilm 332
 
57 ilm 333
            for (final IListe list : this.mapList.keySet()) {
41 ilm 334
                final SQLField filterField = this.mapList.get(list);
335
                list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
336
                    @Override
337
                    public SQLSelect transformChecked(SQLSelect input) {
73 ilm 338
                        Where w = new Where(input.getAlias(filterField), ">=", c.getTime());
339
                        return setWhere(input, w, mapListTransformer.get(list));
18 ilm 340
                    }
41 ilm 341
                });
342
            }
18 ilm 343
 
73 ilm 344
        } else if (this.dateDu.getValue() == null) {
18 ilm 345
            final Calendar c = Calendar.getInstance();
346
            c.setTime(this.dateAu.getValue());
73 ilm 347
            setEndTimeSchedule(c);
57 ilm 348
            for (final IListe list : this.mapList.keySet()) {
41 ilm 349
                final SQLField filterField = this.mapList.get(list);
18 ilm 350
 
41 ilm 351
                list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
174 ilm 352
 
41 ilm 353
                    @Override
354
                    public SQLSelect transformChecked(SQLSelect input) {
73 ilm 355
                        Where w = new Where(input.getAlias(filterField), "<=", c.getTime());
356
                        return setWhere(input, w, mapListTransformer.get(list));
18 ilm 357
                    }
41 ilm 358
                });
359
            }
73 ilm 360
 
361
        } else {
174 ilm 362
 
73 ilm 363
            final Calendar c = Calendar.getInstance();
364
            c.setTime(this.dateAu.getValue());
174 ilm 365
 
73 ilm 366
            setEndTimeSchedule(c);
367
 
368
            final Calendar c2 = Calendar.getInstance();
369
            c2.setTime(this.dateDu.getValue());
370
            clearTimeSchedule(c2);
371
 
372
            for (final IListe list : this.mapList.keySet()) {
373
                final SQLField filterField = this.mapList.get(list);
374
 
375
                list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
376
                    @Override
377
                    public SQLSelect transformChecked(SQLSelect input) {
378
                        final Where w = new Where(input.getAlias(filterField), c2.getTime(), c.getTime());
379
                        return setWhere(input, w, mapListTransformer.get(list));
380
                    }
381
                });
382
            }
18 ilm 383
        }
384
 
73 ilm 385
        final Tuple2<Date, Date> selectedTuple = Tuple2.create(this.dateDu.getValue(), this.dateAu.getValue());
386
        this.combo.setSelectedItem(findItem(selectedTuple, this.map));
387
        for (PropertyChangeListener l : this.listeners.getListeners(PropertyChangeListener.class)) {
388
            l.propertyChange(new PropertyChangeEvent(this, "valueChanged", null, selectedTuple));
389
        }
390
    }
391
 
392
    public void addValueListener(PropertyChangeListener l) {
393
        this.listeners.add(PropertyChangeListener.class, l);
394
    }
395
 
396
    public void rmValueListener(PropertyChangeListener l) {
397
        this.listeners.remove(PropertyChangeListener.class, l);
398
    }
399
 
400
    private SQLSelect setWhere(SQLSelect input, Where w, ITransformer<SQLSelect, SQLSelect> t) {
401
        if (t != null) {
402
            input = t.transformChecked(input);
403
        }
404
        input.andWhere(w);
405
        return input;
406
    }
407
 
408
    public static String findItem(Tuple2<Date, Date> t, Map<String, Tuple2<Date, Date>> mapItem) {
409
 
410
        Date d1 = t.get0();
411
        Date d2 = t.get1();
412
        Calendar c1 = getCalendarFromDate(d1);
413
        Calendar c2 = getCalendarFromDate(d2);
414
 
415
        for (String label : mapItem.keySet()) {
416
            Tuple2<Date, Date> t2 = mapItem.get(label);
417
            if (t2 != null) {
418
                final Date get0 = t2.get0();
419
                final Date get1 = t2.get1();
420
                Calendar cGet0 = getCalendarFromDate(get0);
421
                Calendar cGet1 = getCalendarFromDate(get1);
422
 
423
                if (isDateEquals(c1, cGet0) && isDateEquals(c2, cGet1)) {
424
                    return label;
425
                }
426
            }
427
        }
428
        return CUSTOM_COMBO_ITEM;
429
    }
430
 
431
    private static Calendar getCalendarFromDate(final Date d) {
432
        Calendar cal = null;
433
        if (d != null) {
434
            cal = Calendar.getInstance();
435
            cal.setTime(d);
436
        }
437
        return cal;
438
    }
439
 
440
    public static boolean isDateEquals(Calendar d1, Calendar d2) {
441
        boolean b = false;
442
        if (d1 == null && d2 == null) {
443
            b = true;
444
        } else if (d1 != null && d2 != null) {
445
            b = d1.get(Calendar.DAY_OF_MONTH) == d2.get(Calendar.DAY_OF_MONTH) && d2.get(Calendar.YEAR) == d1.get(Calendar.YEAR) && d1.get(Calendar.MONTH) == d2.get(Calendar.MONTH);
446
        } else {
447
            b = false;
448
        }
449
        return b;
450
    }
451
 
452
    private static void clearTimeSchedule(final Calendar c) {
453
        c.set(Calendar.HOUR_OF_DAY, 0);
454
        c.set(Calendar.MINUTE, 0);
455
        c.set(Calendar.SECOND, 0);
456
        c.set(Calendar.MILLISECOND, 0);
457
    }
458
 
459
    private static void setEndTimeSchedule(final Calendar c) {
18 ilm 460
        c.set(Calendar.HOUR_OF_DAY, 23);
461
        c.set(Calendar.MINUTE, 59);
73 ilm 462
        c.set(Calendar.SECOND, 59);
18 ilm 463
        c.set(Calendar.MILLISECOND, 59);
73 ilm 464
    }
18 ilm 465
 
73 ilm 466
    static private final File getConfigFile(Map<IListe, SQLField> mapList) {
467
        final Configuration conf = Configuration.getInstance();
468
        if (conf == null)
469
            return null;
41 ilm 470
 
73 ilm 471
        String name = null;
472
        for (IListe l : mapList.keySet()) {
90 ilm 473
            final File configFile = l.getConfigFile();
474
            if (configFile != null) {
475
                String confName = configFile.getName();
476
                if (name == null) {
477
                    name = confName;
478
                } else if (name.compareTo(confName) > 0) {
479
                    name = confName;
480
                }
481
            } else {
482
                name = l.getSource().getPrimaryTable().getName();
73 ilm 483
            }
41 ilm 484
        }
73 ilm 485
        return new File(conf.getConfDir(), "DateRanges" + File.separator + name);
18 ilm 486
    }
487
}