OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 93 | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 93 Rev 182
Line 1... Line 1...
1
/*
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 * 
3
 * 
4
 * Copyright 2011 OpenConcerto, by ILM Informatique. All rights reserved.
4
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
5
 * 
5
 * 
6
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
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
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
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.
9
 * language governing permissions and limitations under the License.
Line 15... Line 15...
15
 
15
 
16
import org.openconcerto.sql.Configuration;
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.TM;
17
import org.openconcerto.sql.TM;
18
import org.openconcerto.sql.element.SQLElement;
18
import org.openconcerto.sql.element.SQLElement;
19
import org.openconcerto.sql.element.SQLElementDirectory;
19
import org.openconcerto.sql.element.SQLElementDirectory;
-
 
20
import org.openconcerto.sql.model.SQLInsert;
-
 
21
import org.openconcerto.sql.model.SQLRow;
-
 
22
import org.openconcerto.sql.model.SQLRowValues;
20
import org.openconcerto.sql.model.SQLTable;
23
import org.openconcerto.sql.model.SQLTable;
-
 
24
import org.openconcerto.sql.model.SQLTableEvent;
-
 
25
import org.openconcerto.sql.model.SQLTableEvent.Mode;
21
import org.openconcerto.sql.model.Where;
26
import org.openconcerto.sql.model.Where;
22
import org.openconcerto.sql.request.ListSQLRequest;
27
import org.openconcerto.sql.request.ListSQLRequest;
23
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
28
import org.openconcerto.sql.sqlobject.IComboSelectionItem;
24
import org.openconcerto.sql.users.UserManager;
29
import org.openconcerto.sql.users.UserManager;
25
import org.openconcerto.sql.view.ListeModifyPanel;
30
import org.openconcerto.sql.view.ListeModifyPanel;
-
 
31
import org.openconcerto.sql.view.list.RowAction;
26
import org.openconcerto.ui.DefaultGridBagConstraints;
32
import org.openconcerto.ui.DefaultGridBagConstraints;
-
 
33
import org.openconcerto.utils.ExceptionHandler;
-
 
34
import org.openconcerto.utils.Tuple2;
27
import org.openconcerto.utils.cc.IClosure;
35
import org.openconcerto.utils.cc.IClosure;
28
 
36
 
29
import java.awt.GridBagConstraints;
37
import java.awt.GridBagConstraints;
30
import java.awt.GridBagLayout;
38
import java.awt.GridBagLayout;
-
 
39
import java.awt.Toolkit;
-
 
40
import java.awt.datatransfer.Clipboard;
-
 
41
import java.awt.datatransfer.Transferable;
-
 
42
import java.awt.datatransfer.UnsupportedFlavorException;
31
import java.awt.event.ActionEvent;
43
import java.awt.event.ActionEvent;
-
 
44
import java.io.IOException;
-
 
45
import java.math.BigDecimal;
-
 
46
import java.sql.SQLException;
-
 
47
import java.util.ArrayList;
-
 
48
import java.util.HashMap;
-
 
49
import java.util.List;
-
 
50
import java.util.Map;
32
 
51
 
-
 
52
import javax.swing.AbstractAction;
33
import javax.swing.JButton;
53
import javax.swing.JButton;
34
import javax.swing.JLabel;
54
import javax.swing.JLabel;
35
import javax.swing.JPanel;
55
import javax.swing.JPanel;
36
import javax.swing.JScrollPane;
56
import javax.swing.JScrollPane;
37
import javax.swing.JSplitPane;
57
import javax.swing.JSplitPane;
38
import javax.swing.event.ListSelectionEvent;
58
import javax.swing.event.ListSelectionEvent;
39
import javax.swing.event.ListSelectionListener;
59
import javax.swing.event.ListSelectionListener;
40
 
60
 
-
 
61
import net.minidev.json.JSONArray;
-
 
62
import net.minidev.json.JSONObject;
-
 
63
 
41
public class UserRightsPanel extends JPanel {
64
public class UserRightsPanel extends JPanel {
42
 
65
 
43
    // Liste des utilisateurs
66
    // Liste des utilisateurs
44
    private final JListSQLTablePanel list;
67
    private final JListSQLTablePanel list;
45
    private final ListeModifyPanel modifPanel;
68
    private final ListeModifyPanel modifPanel;
Line 128... Line 151...
128
        GridBagConstraints c3 = new GridBagConstraints();
151
        GridBagConstraints c3 = new GridBagConstraints();
129
        c3.weightx = 1;
152
        c3.weightx = 1;
130
        c3.weighty = 1;
153
        c3.weighty = 1;
131
        c3.fill = GridBagConstraints.BOTH;
154
        c3.fill = GridBagConstraints.BOTH;
132
        this.add(pane, c3);
155
        this.add(pane, c3);
-
 
156
 
-
 
157
        RowAction actionPaste = new RowAction(new AbstractAction("Coller") {
-
 
158
 
-
 
159
            @Override
-
 
160
            public void actionPerformed(ActionEvent e) {
-
 
161
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
-
 
162
                Transferable clipboardContent = clipboard.getContents(this);
-
 
163
 
-
 
164
                if (clipboardContent.isDataFlavorSupported(UserRightCopySelection.rightsFlavor)) {
-
 
165
                    JSONObject rights;
-
 
166
                    try {
-
 
167
                        rights = (JSONObject) clipboardContent.getTransferData(UserRightCopySelection.rightsFlavor);
-
 
168
 
-
 
169
                        addRights(rights);
-
 
170
 
-
 
171
                    } catch (UnsupportedFlavorException | IOException e1) {
-
 
172
 
-
 
173
                        e1.printStackTrace();
-
 
174
                    }
-
 
175
                }
-
 
176
            }
-
 
177
 
-
 
178
        }, true) {
-
 
179
            @Override
-
 
180
            public boolean enabledFor(List<SQLRowValues> selection) {
-
 
181
                Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
-
 
182
                Transferable clipboardContent = clipboard.getContents(this);
-
 
183
 
-
 
184
                return clipboardContent.isDataFlavorSupported(UserRightCopySelection.rightsFlavor);
-
 
185
            }
-
 
186
        };
-
 
187
        this.modifPanel.getListe().addIListeAction(actionPaste);
133
    }
188
    }
134
 
189
 
135
    private void updateListFromSelection() {
190
    private void updateListFromSelection() {
136
        final int selectedIndex = this.list.getSelectedIndex();
191
        final int selectedIndex = this.list.getSelectedIndex();
137
 
192
 
Line 151... Line 206...
151
        // enforce the limitation
206
        // enforce the limitation
152
        ((UserRightSQLComponent) this.modifPanel.getModifComp()).setUserID(userID);
207
        ((UserRightSQLComponent) this.modifPanel.getModifComp()).setUserID(userID);
153
        ((UserRightSQLComponent) this.modifPanel.getAddComp()).setUserID(userID);
208
        ((UserRightSQLComponent) this.modifPanel.getAddComp()).setUserID(userID);
154
    }
209
    }
155
 
210
 
-
 
211
    private void addRights(JSONObject rights) {
-
 
212
        // Récupération des droits courants
-
 
213
        final ListSQLRequest req = this.modifPanel.getListe().getRequest();
-
 
214
        List<SQLRowValues> vals = req.getValues();
-
 
215
        Map<Tuple2<Number, String>, SQLRowValues> currentUserRight = new HashMap<>();
-
 
216
        for (SQLRowValues sqlRowValues : vals) {
-
 
217
            currentUserRight.put(Tuple2.create(sqlRowValues.getForeignIDNumber("ID_RIGHT"), sqlRowValues.getString("OBJECT")), sqlRowValues);
-
 
218
        }
-
 
219
 
-
 
220
        try {
-
 
221
            List<SQLInsert> inserts = new ArrayList<SQLInsert>();
-
 
222
 
-
 
223
            final int selectedIndex = this.list.getSelectedIndex();
-
 
224
            final boolean b = selectedIndex >= 0;
-
 
225
 
-
 
226
            final int userID;
-
 
227
            if (b) {
-
 
228
                userID = this.list.getModel().getRowAt(selectedIndex).getID();
-
 
229
            } else {
-
 
230
                // since we don't display user in the list (to avoid undef)
-
 
231
                // we need to always display at most one user
-
 
232
                userID = this.list.getModel().getTable().getUndefinedID();
-
 
233
            }
-
 
234
 
-
 
235
            BigDecimal order = getTable().getMaxOrder().add(BigDecimal.ONE);
-
 
236
            for (Object a : rights.values()) {
-
 
237
                JSONArray array = (JSONArray) a;
-
 
238
                Number right = (Number) array.get(0);
-
 
239
                String obj = (String) array.get(1);
-
 
240
                Boolean hasRight = (Boolean) array.get(2);
-
 
241
 
-
 
242
                final Tuple2<Number, String> key = Tuple2.create(right, obj);
-
 
243
                if (currentUserRight.containsKey(key)) {
-
 
244
                    SQLRowValues rowVals = currentUserRight.get(key);
-
 
245
                    if (!rowVals.getBoolean("HAVE_RIGHT").equals(hasRight)) {
-
 
246
                        rowVals.createEmptyUpdateRow().put("HAVE_RIGHT", hasRight).commit();
-
 
247
                    }
-
 
248
                } else {
-
 
249
                    SQLInsert insert = new SQLInsert();
-
 
250
                    insert.add(getTable().getField("ID_RIGHT"), right);
-
 
251
                    insert.add(getTable().getField("HAVE_RIGHT"), hasRight);
-
 
252
                    insert.add(getTable().getField("OBJECT"), obj);
-
 
253
                    insert.add(getTable().getField("ID_USER_COMMON"), userID);
-
 
254
                    insert.add(getTable().getOrderField(), order);
-
 
255
 
-
 
256
                    inserts.add(insert);
-
 
257
                    order = order.add(BigDecimal.ONE);
-
 
258
                }
-
 
259
            }
-
 
260
            if (!inserts.isEmpty()) {
-
 
261
                SQLInsert.executeMultiple(getTable().getDBSystemRoot(), inserts);
-
 
262
                getTable().fire(new SQLTableEvent(getTable(), SQLRow.NONEXISTANT_ID, Mode.ROW_ADDED));
-
 
263
            }
-
 
264
        } catch (SQLException e1) {
-
 
265
            ExceptionHandler.handle("Erreur lors de la duplication des droits", e1);
-
 
266
        }
-
 
267
    }
-
 
268
 
156
    public final SQLTable getTable() {
269
    public final SQLTable getTable() {
157
        return this.modifPanel.getElement().getTable();
270
        return this.modifPanel.getElement().getTable();
158
    }
271
    }
159
}
272
}