OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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

Rev Author Line No. Line
112 ilm 1
package org.openconcerto.modules.operation;
2
 
3
import java.awt.GridLayout;
4
import java.beans.PropertyChangeEvent;
5
import java.beans.PropertyChangeListener;
6
import java.util.Arrays;
7
import java.util.Calendar;
8
import java.util.Date;
9
import java.util.HashMap;
10
import java.util.HashSet;
11
import java.util.List;
12
import java.util.Map;
13
import java.util.Set;
14
import java.util.concurrent.ExecutionException;
15
 
16
import javax.swing.JLabel;
17
import javax.swing.JPanel;
18
import javax.swing.JSpinner;
19
import javax.swing.SpinnerNumberModel;
147 ilm 20
import javax.swing.SwingWorker;
112 ilm 21
import javax.swing.event.ChangeEvent;
22
import javax.swing.event.ChangeListener;
23
 
24
import org.openconcerto.erp.config.ComptaPropsConfiguration;
25
import org.openconcerto.erp.core.reports.history.ui.ListeHistoriquePanel;
26
import org.openconcerto.sql.Configuration;
27
import org.openconcerto.sql.model.SQLBase;
28
import org.openconcerto.sql.model.SQLField;
29
import org.openconcerto.sql.model.SQLRowValues;
30
import org.openconcerto.sql.model.SQLSelect;
31
import org.openconcerto.sql.model.SQLTable;
32
import org.openconcerto.sql.model.Where;
33
import org.openconcerto.sql.request.ComboSQLRequest;
34
import org.openconcerto.sql.users.rights.JListSQLTablePanel;
35
import org.openconcerto.sql.view.IListPanel;
36
import org.openconcerto.sql.view.list.IListe;
37
import org.openconcerto.utils.cc.ITransformer;
38
 
39
public class OperationHistoryPanel extends JPanel {
40
    OperationHistoryPanel() {
41
        this.setLayout(new GridLayout(1, 1));
42
        final SQLBase b = ((ComptaPropsConfiguration) Configuration.getInstance()).getSQLBaseSociete();
147 ilm 43
        final Map<String, List<String>> mapList = new HashMap<>();
112 ilm 44
        mapList.put("Interventions", Arrays.asList("OPERATION"));
45
 
147 ilm 46
        final Map<SQLTable, SQLField> map = new HashMap<>();
112 ilm 47
        final ComboSQLRequest comboRequest = JListSQLTablePanel.createComboRequest(Configuration.getInstance().getDirectory().getElement(b.getTable("SITE")), true);
48
 
49
        JPanel panel = new JPanel();
50
        panel.add(new JLabel("Année"));
51
        int year = Calendar.getInstance().get(Calendar.YEAR);
52
        final JSpinner spiner = new JSpinner(new SpinnerNumberModel(year, 1000, year + 20, 1));
53
        panel.add(spiner);
54
 
55
        final ListeHistoriquePanel listHistoriquePanel = new ListeHistoriquePanel("Interventions", comboRequest, mapList, panel, map, null);
56
        final IListe list = listHistoriquePanel.getListe(0);
57
        updateWhere(comboRequest, list, year, listHistoriquePanel);
58
 
59
        spiner.addChangeListener(new ChangeListener() {
60
 
61
            @Override
62
            public void stateChanged(ChangeEvent e) {
63
                final int selectedYear = ((Number) spiner.getValue()).intValue();
64
                updateWhere(comboRequest, list, selectedYear, listHistoriquePanel);
65
            }
66
 
67
        });
68
 
69
        list.addSelectionDataListener(new PropertyChangeListener() {
70
 
71
            @Override
72
            public void propertyChange(PropertyChangeEvent evt) {
73
                final List<SQLRowValues> selectedRows = list.getSelectedRows();
74
                final IListPanel listePanel = listHistoriquePanel.getListePanel(0);
181 ilm 75
                // Activation/Desactivation des boutons Mofifier/Supprimer
112 ilm 76
                if (selectedRows != null && !selectedRows.isEmpty()) {
147 ilm 77
                    final Set<Long> idsCalendarItemGroup = new HashSet<>();
112 ilm 78
                    for (SQLRowValues sqlRowValues : selectedRows) {
79
                        idsCalendarItemGroup.add(Long.valueOf(sqlRowValues.getForeign("ID_CALENDAR_ITEM_GROUP").getID()));
80
                    }
81
                    final SQLTable table = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("CALENDAR_ITEM");
82
                    final SQLField flag = table.getField("FLAGS");
83
                    final SQLField group = table.getField("ID_CALENDAR_ITEM_GROUP");
84
                    final SQLSelect select = new SQLSelect();
85
                    select.addSelect(flag);
86
                    select.addSelect(group);
87
                    Where where = new Where(group, idsCalendarItemGroup).and(new Where(flag, "LIKE", "%locked%"));
88
 
89
                    select.setWhere(where);
90
 
147 ilm 91
                    final SwingWorker<Boolean, String> w = new SwingWorker<Boolean, String>() {
112 ilm 92
 
93
                        @Override
94
                        protected Boolean doInBackground() throws Exception {
95
                            @SuppressWarnings("rawtypes")
96
                            final List l = b.getDataSource().execute(select.asString());
97
                            return l.isEmpty();
98
                        }
99
 
147 ilm 100
                        @Override
112 ilm 101
                        protected void done() {
102
                            Boolean b;
103
                            try {
104
                                b = get();
105
                                listePanel.setModifyVisible(b);
106
                                listePanel.setDeleteVisible(b);
107
                            } catch (InterruptedException e) {
108
                                e.printStackTrace();
109
                            } catch (ExecutionException e) {
110
                                e.printStackTrace();
111
                            }
112
 
147 ilm 113
                        }
112 ilm 114
                    };
115
                    w.execute();
116
 
117
                }
118
 
119
            }
120
        });
121
 
122
        this.add(listHistoriquePanel);
123
 
124
    }
125
 
126
    public void updateWhere(final ComboSQLRequest comboRequest, final IListe list, final int selectedYear, final ListeHistoriquePanel panel) {
127
 
128
        try {
129
            list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
130
 
131
                @Override
132
                public SQLSelect transformChecked(SQLSelect input) {
133
                    if (panel.getSelectedRow() == null) {
134
                        return input;
135
                    }
136
 
137
                    try {
138
                        Calendar cal = Calendar.getInstance();
139
                        cal.clear();
140
 
141
                        cal.set(Calendar.YEAR, selectedYear);
142
                        Date dStart = cal.getTime();
143
                        cal.set(Calendar.YEAR, selectedYear + 1);
144
                        Date dEnd = cal.getTime();
145
 
181 ilm 146
                        final SQLTable itemGroupTable = comboRequest.getPrimaryTable().getTable("CALENDAR_ITEM_GROUP");
147
                        final SQLTable itemTable = comboRequest.getPrimaryTable().getTable("CALENDAR_ITEM");
148
                        final SQLTable operationTable = comboRequest.getPrimaryTable().getTable("OPERATION");
112 ilm 149
                        final List<?> dateGroupIDs;
150
                        {
151
                            final SQLSelect copy = new SQLSelect(input);
152
                            copy.clearSelect();
181 ilm 153
                            copy.addSelect(copy.getAlias(itemGroupTable.getKey()));
154
                            copy.setWhere(copy.getAlias(operationTable.getField("ID_SITE")), "=", panel.getSelectedRow().getID());
155
                            final List<?> allGroupIDs = itemTable.getDBSystemRoot().getDataSource().executeCol(copy.asString());
112 ilm 156
                            final SQLSelect selIDGroup = new SQLSelect();
181 ilm 157
                            selIDGroup.addSelect(itemTable.getField("ID_CALENDAR_ITEM_GROUP"));
158
                            final Where where = new Where(itemTable.getField("START"), dStart, true, dEnd, true);
159
                            selIDGroup.setWhere(where).andWhere(new Where(itemTable.getField("ID_CALENDAR_ITEM_GROUP"), allGroupIDs));
160
                            dateGroupIDs = itemTable.getDBSystemRoot().getDataSource().executeCol(selIDGroup.asString());
112 ilm 161
                        }
162
 
181 ilm 163
                        Where w = new Where(input.getAlias(itemGroupTable.getKey()), dateGroupIDs);
164
                        input.setWhere(w);
112 ilm 165
                    } catch (Throwable e) {
166
                        e.printStackTrace();
167
                    }
168
                    return input;
169
                }
170
            });
171
        } catch (Throwable ex) {
172
            ex.printStackTrace();
173
        }
174
    }
175
}