OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 112 | Go to most recent revision | 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);
75
                if (selectedRows != null && !selectedRows.isEmpty()) {
147 ilm 76
                    final Set<Long> idsCalendarItemGroup = new HashSet<>();
112 ilm 77
                    for (SQLRowValues sqlRowValues : selectedRows) {
78
                        idsCalendarItemGroup.add(Long.valueOf(sqlRowValues.getForeign("ID_CALENDAR_ITEM_GROUP").getID()));
79
                    }
80
                    final SQLTable table = ((ComptaPropsConfiguration) Configuration.getInstance()).getRootSociete().getTable("CALENDAR_ITEM");
81
                    final SQLField flag = table.getField("FLAGS");
82
                    final SQLField group = table.getField("ID_CALENDAR_ITEM_GROUP");
83
                    final SQLSelect select = new SQLSelect();
84
                    select.addSelect(flag);
85
                    select.addSelect(group);
86
                    Where where = new Where(group, idsCalendarItemGroup).and(new Where(flag, "LIKE", "%locked%"));
87
 
88
                    select.setWhere(where);
89
 
147 ilm 90
                    final SwingWorker<Boolean, String> w = new SwingWorker<Boolean, String>() {
112 ilm 91
 
92
                        @Override
93
                        protected Boolean doInBackground() throws Exception {
94
                            @SuppressWarnings("rawtypes")
95
                            final List l = b.getDataSource().execute(select.asString());
96
                            return l.isEmpty();
97
                        }
98
 
147 ilm 99
                        @Override
112 ilm 100
                        protected void done() {
101
                            Boolean b;
102
                            try {
103
                                b = get();
104
                                listePanel.setModifyVisible(b);
105
                                listePanel.setDeleteVisible(b);
106
                            } catch (InterruptedException e) {
107
                                e.printStackTrace();
108
                            } catch (ExecutionException e) {
109
                                e.printStackTrace();
110
                            }
111
 
147 ilm 112
                        }
112 ilm 113
                    };
114
                    w.execute();
115
 
116
                }
117
 
118
            }
119
        });
120
 
121
        this.add(listHistoriquePanel);
122
 
123
    }
124
 
125
    public void updateWhere(final ComboSQLRequest comboRequest, final IListe list, final int selectedYear, final ListeHistoriquePanel panel) {
126
 
127
        try {
128
            list.getRequest().setSelectTransf(new ITransformer<SQLSelect, SQLSelect>() {
129
 
130
                @Override
131
                public SQLSelect transformChecked(SQLSelect input) {
132
                    if (panel.getSelectedRow() == null) {
133
                        return input;
134
                    }
135
 
136
                    try {
137
                        Calendar cal = Calendar.getInstance();
138
                        cal.clear();
139
 
140
                        cal.set(Calendar.YEAR, selectedYear);
141
                        Date dStart = cal.getTime();
142
                        cal.set(Calendar.YEAR, selectedYear + 1);
143
                        Date dEnd = cal.getTime();
144
 
145
                        final SQLTable groupT = comboRequest.getPrimaryTable().getTable("CALENDAR_ITEM_GROUP");
146
                        final SQLTable calItemT = comboRequest.getPrimaryTable().getTable("CALENDAR_ITEM");
147
                        final List<?> dateGroupIDs;
148
                        {
149
                            final SQLSelect copy = new SQLSelect(input);
150
                            copy.clearSelect();
151
                            copy.addSelect(copy.getAlias(groupT.getKey()));
152
                            copy.setWhere(copy.getAlias(comboRequest.getPrimaryTable().getTable("OPERATION").getField("ID_SITE")), "=", panel.getSelectedRow().getID());
153
                            final List<?> allGroupIDs = calItemT.getDBSystemRoot().getDataSource().executeCol(copy.asString());
154
 
155
                            final SQLSelect selIDGroup = new SQLSelect();
156
                            selIDGroup.addSelect(calItemT.getField("ID_CALENDAR_ITEM_GROUP"));
157
                            final Where where = new Where(calItemT.getField("START"), dStart, true, dEnd, true);
158
                            selIDGroup.setWhere(where).andWhere(new Where(calItemT.getField("ID_CALENDAR_ITEM_GROUP"), allGroupIDs));
159
                            dateGroupIDs = calItemT.getDBSystemRoot().getDataSource().executeCol(selIDGroup.asString());
160
                        }
161
 
162
                        input.setWhere(new Where(input.getAlias(groupT.getKey()), dateGroupIDs));
163
                    } catch (Throwable e) {
164
                        e.printStackTrace();
165
                    }
166
                    return input;
167
                }
168
            });
169
        } catch (Throwable ex) {
170
            ex.printStackTrace();
171
        }
172
    }
173
}