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
17 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.sql.users;
15
 
16
import org.openconcerto.sql.Configuration;
73 ilm 17
import org.openconcerto.sql.TM;
17 ilm 18
import org.openconcerto.sql.element.BaseSQLComponent;
19
import org.openconcerto.sql.element.SQLComponent;
80 ilm 20
import org.openconcerto.sql.element.SQLElement;
132 ilm 21
import org.openconcerto.sql.model.DBRoot;
17 ilm 22
import org.openconcerto.sql.model.SQLRow;
23
import org.openconcerto.sql.model.SQLRowAccessor;
80 ilm 24
import org.openconcerto.sql.model.SQLRowValues;
25
import org.openconcerto.sql.model.SQLRowValuesListFetcher;
26
import org.openconcerto.sql.model.SQLSelect;
27
import org.openconcerto.sql.model.SQLTable;
28
import org.openconcerto.sql.model.Where;
17 ilm 29
import org.openconcerto.sql.request.ComboSQLRequest;
30
import org.openconcerto.sql.sqlobject.itemview.SimpleRowItemView;
174 ilm 31
import org.openconcerto.sql.ui.IColorChooser;
17 ilm 32
import org.openconcerto.sql.ui.Login;
80 ilm 33
import org.openconcerto.sql.view.QuickAssignPanel;
144 ilm 34
import org.openconcerto.sql.view.list.ITableModel;
80 ilm 35
import org.openconcerto.sql.view.list.RowValuesTableModel;
36
import org.openconcerto.sql.view.list.SQLTableElement;
144 ilm 37
import org.openconcerto.sql.view.list.SQLTableModelColumn;
38
import org.openconcerto.sql.view.list.SQLTableModelSource;
17 ilm 39
import org.openconcerto.ui.DefaultGridBagConstraints;
40
import org.openconcerto.ui.ISpinner;
41
import org.openconcerto.ui.ISpinnerIntegerModel;
80 ilm 42
import org.openconcerto.ui.JLabelBold;
93 ilm 43
import org.openconcerto.ui.component.InteractionMode;
67 ilm 44
import org.openconcerto.ui.valuewrapper.TextValueWrapper;
17 ilm 45
import org.openconcerto.ui.warning.JLabelWarning;
80 ilm 46
import org.openconcerto.utils.ExceptionHandler;
132 ilm 47
import org.openconcerto.utils.ListMap;
80 ilm 48
import org.openconcerto.utils.cc.ITransformer;
21 ilm 49
import org.openconcerto.utils.checks.ValidState;
17 ilm 50
import org.openconcerto.utils.text.SimpleDocumentListener;
51
 
144 ilm 52
import java.awt.Component;
53
import java.awt.Font;
17 ilm 54
import java.awt.GridBagConstraints;
55
import java.awt.GridBagLayout;
56
import java.awt.Insets;
80 ilm 57
import java.awt.event.ItemEvent;
58
import java.awt.event.ItemListener;
59
import java.beans.PropertyChangeEvent;
60
import java.beans.PropertyChangeListener;
61
import java.sql.SQLException;
17 ilm 62
import java.util.ArrayList;
63
import java.util.List;
64
 
65
import javax.swing.BorderFactory;
66
import javax.swing.JCheckBox;
67
import javax.swing.JLabel;
68
import javax.swing.JPanel;
69
import javax.swing.JPasswordField;
144 ilm 70
import javax.swing.JTable;
17 ilm 71
import javax.swing.JTextField;
72
import javax.swing.SwingConstants;
73
import javax.swing.event.DocumentEvent;
144 ilm 74
import javax.swing.table.DefaultTableCellRenderer;
17 ilm 75
 
76
// FIXME Login user unique ?
156 ilm 77
public class UserCommonSQLElement extends SQLElement {
17 ilm 78
 
79
    /**
80
     * Set this system property to "true" if this should generate old style passwords.
81
     */
82
    public static final String LEGACY_PASSWORDS = "org.openconcerto.sql.legacyPasswords";
83
 
144 ilm 84
    private final boolean familyNameFirst;
85
 
156 ilm 86
    public UserCommonSQLElement(final DBRoot root) {
87
        this(root, false);
17 ilm 88
    }
89
 
151 ilm 90
    public UserCommonSQLElement(final DBRoot root, final boolean familyNameFirst) {
156 ilm 91
        // allow subclass to keep same code
92
        super(root.findTable("USER_COMMON"), null, "sql.user");
144 ilm 93
        this.familyNameFirst = familyNameFirst;
132 ilm 94
    }
95
 
17 ilm 96
    @Override
97
    protected List<String> getListFields() {
144 ilm 98
        final List<String> l = new ArrayList<>(5);
99
        if (this.familyNameFirst) {
100
            l.add("NOM");
101
            l.add("PRENOM");
102
        } else {
103
            l.add("PRENOM");
104
            l.add("NOM");
105
        }
17 ilm 106
        l.add("LOGIN");
132 ilm 107
        if (getTable().contains("DISABLED")) {
108
            l.add("DISABLED");
109
        }
93 ilm 110
        if (getTable().contains("OUT")) {
111
            l.add("OUT");
112
        }
17 ilm 113
        return l;
114
    }
115
 
116
    @Override
151 ilm 117
    protected synchronized void _initTableSource(final SQLTableModelSource res) {
144 ilm 118
        super._initTableSource(res);
119
        // current user in bold
120
        for (final SQLTableModelColumn col : res.getColumns()) {
121
            if (col.getValueClass().equals(String.class)) {
122
                col.setRenderer(new DefaultTableCellRenderer() {
123
                    @Override
151 ilm 124
                    public Component getTableCellRendererComponent(final JTable table, final Object value, final boolean isSelected, final boolean hasFocus, final int row, final int column) {
144 ilm 125
                        final JLabel res = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
126
                        final boolean isCurrentUser = ITableModel.getLine(table.getModel(), row).getRow().getID() == UserManager.getUserID();
127
                        final int targetStyle = isCurrentUser ? Font.BOLD : Font.PLAIN;
128
                        if ((res.getFont().getStyle() & targetStyle) == 0) {
129
                            res.setFont(res.getFont().deriveFont(targetStyle));
130
                        }
131
                        return res;
132
                    }
133
                });
134
            }
135
        }
136
    }
137
 
138
    @Override
17 ilm 139
    protected List<String> getComboFields() {
144 ilm 140
        final List<String> l = new ArrayList<>(2);
141
        if (this.familyNameFirst) {
142
            l.add("NOM");
143
            l.add("PRENOM");
144
        } else {
145
            l.add("PRENOM");
146
            l.add("NOM");
147
        }
17 ilm 148
        return l;
149
    }
150
 
151
    @Override
151 ilm 152
    protected void _initComboRequest(final ComboSQLRequest req) {
142 ilm 153
        super._initComboRequest(req);
154
        req.setFieldSeparator(" ");
17 ilm 155
    }
156
 
157
    @Override
132 ilm 158
    public ListMap<String, String> getShowAs() {
159
        return ListMap.singleton(null, "PRENOM", "NOM");
17 ilm 160
    }
161
 
162
    /*
163
     * (non-Javadoc)
151 ilm 164
     *
17 ilm 165
     * @see org.openconcerto.devis.SQLElement#getComponent()
166
     */
167
    @Override
168
    public SQLComponent createComponent() {
144 ilm 169
        return new UserSQLComponent(this);
170
    }
17 ilm 171
 
144 ilm 172
    protected static class UserSQLComponent extends BaseSQLComponent {
17 ilm 173
 
144 ilm 174
        private JPasswordField passField;
175
        private JPasswordField passFieldConfirm;
176
        private JPanel panelWarning;
177
        private final SQLElement accessSocieteElem;
178
        private QuickAssignPanel table;
179
        // TODO transform into real SQLRowItemView
180
        private final JTextField encryptedPass = new JTextField();
181
        private JCheckBox noCompanyLimitCheckBox;
182
        {
183
            final SQLTable t = getTable().getDBSystemRoot().getGraph().findReferentTable(getTable(), "ACCES_SOCIETE");
184
            this.accessSocieteElem = t == null ? null : getDirectory().getElement(t);
185
        }
80 ilm 186
 
151 ilm 187
        protected UserSQLComponent(final SQLElement element) {
144 ilm 188
            super(element);
189
        }
67 ilm 190
 
144 ilm 191
        protected final JPasswordField getPassField() {
192
            return this.passField;
193
        }
67 ilm 194
 
144 ilm 195
        protected final JPasswordField getPassFieldConfirm() {
196
            return this.passFieldConfirm;
197
        }
17 ilm 198
 
144 ilm 199
        @Override
200
        public void addViews() {
201
            final GridBagConstraints c = new GridBagConstraints();
202
            c.insets = new Insets(0, 0, 0, 0);
203
            c.gridx = 0;
204
            c.gridy = 0;
205
            c.weightx = 0;
206
            c.weighty = 0;
207
            c.fill = GridBagConstraints.HORIZONTAL;
208
            c.anchor = GridBagConstraints.WEST;
17 ilm 209
 
144 ilm 210
            this.panelWarning = new JPanel(new GridBagLayout());
211
            this.panelWarning.setBorder(BorderFactory.createEmptyBorder());
212
            final JLabelWarning labelWarning = new JLabelWarning();
213
            this.panelWarning.add(labelWarning, c);
214
            final JLabel labelTextWarning = new JLabel(TM.tr("user.passwordsDontMatch.short"));
215
            c.gridx++;
216
            this.panelWarning.add(labelTextWarning, c);
17 ilm 217
 
144 ilm 218
            final GridBagLayout layout = new GridBagLayout();
219
            this.setLayout(layout);
17 ilm 220
 
144 ilm 221
            // Login
222
            c.gridx = 0;
223
            c.insets = new Insets(2, 2, 1, 2);
174 ilm 224
            final JLabel labelLogin = new JLabel(getLabelFor("LOGIN"), SwingConstants.RIGHT);
144 ilm 225
            this.add(labelLogin, c);
226
            final JTextField textLogin = new JTextField();
227
            c.gridx++;
228
            DefaultGridBagConstraints.lockMinimumSize(textLogin);
229
            c.weightx = 1;
230
            this.add(textLogin, c);
17 ilm 231
 
144 ilm 232
            // Warning
233
            c.gridwidth = GridBagConstraints.REMAINDER;
234
            c.gridx++;
235
            c.insets = new Insets(0, 0, 0, 0);
236
            this.add(this.panelWarning, c);
237
            this.panelWarning.setVisible(false);
17 ilm 238
 
144 ilm 239
            // Pass
240
            c.gridy++;
241
            c.gridwidth = 1;
242
            c.gridx = 0;
243
            c.weightx = 0;
244
            c.insets = new Insets(2, 2, 1, 2);
174 ilm 245
            final JLabel labelPass = new JLabel(getLabelFor("PASSWORD"), SwingConstants.RIGHT);
144 ilm 246
            this.add(labelPass, c);
247
            this.passField = new JPasswordField(15);
248
            c.gridx++;
249
            c.weightx = 1;
250
            DefaultGridBagConstraints.lockMinimumSize(this.getPassField());
251
            this.add(this.getPassField(), c);
252
 
253
            // Confirmation password
254
            c.gridx++;
255
            c.weightx = 0;
174 ilm 256
            final JLabel labelConfirmationPass = new JLabel(getLabelFor("PASSWORD_CONFIRM"), SwingConstants.RIGHT);
144 ilm 257
            this.add(labelConfirmationPass, c);
258
            this.passFieldConfirm = new JPasswordField(15);
259
            c.gridx++;
260
            c.weightx = 1;
261
            DefaultGridBagConstraints.lockMinimumSize(this.getPassFieldConfirm());
262
            this.add(this.getPassFieldConfirm(), c);
263
 
264
            // Nom
265
            c.gridx = 0;
266
            c.gridy++;
267
            c.weightx = 0;
174 ilm 268
            final JLabel labelNom = new JLabel(getLabelFor("NOM"), SwingConstants.RIGHT);
144 ilm 269
            this.add(labelNom, c);
270
            final JTextField textNom = new JTextField();
271
            c.gridx++;
272
            c.weightx = 1;
273
            this.add(textNom, c);
274
 
275
            // Prenom
276
            c.gridx++;
277
            c.weightx = 0;
174 ilm 278
            final JLabel labelPrenom = new JLabel(getLabelFor("PRENOM"), SwingConstants.RIGHT);
144 ilm 279
            this.add(labelPrenom, c);
280
            final JTextField textPrenom = new JTextField();
281
            c.gridx++;
282
            c.weightx = 1;
283
            this.add(textPrenom, c);
284
 
285
            // Surnom
286
            c.gridx = 0;
287
            c.gridy++;
288
            c.weightx = 0;
174 ilm 289
            final JLabel labelSurnom = new JLabel(getLabelFor("SURNOM"), SwingConstants.RIGHT);
144 ilm 290
            this.add(labelSurnom, c);
291
            final JTextField textSurnom = new JTextField();
292
            c.gridx++;
293
            c.weightx = 1;
294
            this.add(textSurnom, c);
295
 
296
            if (this.getTable().contains("ADMIN")) {
297
                final JCheckBox checkAdmin = new JCheckBox(getLabelFor("ADMIN"));
17 ilm 298
                c.gridx++;
144 ilm 299
                c.gridwidth = GridBagConstraints.REMAINDER;
17 ilm 300
                c.weightx = 0;
144 ilm 301
                this.add(checkAdmin, c);
302
                this.addView(checkAdmin, "ADMIN");
303
            }
17 ilm 304
 
144 ilm 305
            c.gridy++;
306
            c.gridwidth = 1;
307
            c.gridx = 0;
308
            c.weightx = 0;
309
 
310
            if (getTable().contains("MAIL")) {
174 ilm 311
                final JLabel labelMail = new JLabel(getLabelFor("MAIL"), SwingConstants.RIGHT);
144 ilm 312
                c.anchor = GridBagConstraints.NORTHWEST;
313
                this.add(labelMail, c);
17 ilm 314
                c.gridx++;
144 ilm 315
                final JTextField textMail = new JTextField();
316
                c.gridwidth = 1;
17 ilm 317
                c.weightx = 1;
318
 
144 ilm 319
                this.add(textMail, c);
320
                this.addView(textMail, "MAIL");
321
            }
174 ilm 322
 
144 ilm 323
            if (getTable().contains("OUT")) {
17 ilm 324
                c.gridx++;
151 ilm 325
                final JCheckBox boxOut = new JCheckBox(getLabelFor("OUT"));
144 ilm 326
                c.gridwidth = 1;
17 ilm 327
                c.weightx = 1;
144 ilm 328
                c.gridwidth = GridBagConstraints.REMAINDER;
329
                this.add(boxOut, c);
330
                this.addView(boxOut, "OUT");
331
            } else {
332
                if (getTable().contains("DISABLED")) {
333
                    c.gridx++;
151 ilm 334
                    final JCheckBox boxDisabled = new JCheckBox(getLabelFor("DISABLED"));
144 ilm 335
                    c.gridwidth = 1;
336
                    c.weightx = 1;
337
                    c.gridwidth = GridBagConstraints.REMAINDER;
338
                    this.add(boxDisabled, c);
339
                    this.addView(boxDisabled, "DISABLED");
340
                }
341
            }
342
            c.gridy++;
174 ilm 343
 
344
            if (getTable().contains("COMPTE_NUMERO")) {
345
                final JLabel labelMail = new JLabel(getLabelFor("COMPTE_NUMERO"));
346
                labelMail.setHorizontalAlignment(SwingConstants.RIGHT);
347
                c.anchor = GridBagConstraints.NORTHWEST;
348
                this.add(labelMail, c);
349
                c.gridx++;
350
                final JTextField textMail = new JTextField();
351
                c.gridwidth = 1;
352
                c.weightx = 1;
353
                c.gridy++;
354
                this.add(textMail, c);
355
                this.addView(textMail, "COMPTE_NUMERO");
356
            }
357
 
144 ilm 358
            if (getTable().contains("TEL")) {
17 ilm 359
                c.gridx = 0;
174 ilm 360
                final JLabel labelTel = new JLabel(getLabelFor("TEL"), SwingConstants.RIGHT);
144 ilm 361
                c.gridwidth = 1;
17 ilm 362
                c.weightx = 0;
144 ilm 363
                this.add(labelTel, c);
151 ilm 364
                final JTextField fieldTel = new JTextField(20);
17 ilm 365
                c.gridx++;
366
                c.weightx = 1;
144 ilm 367
                this.add(fieldTel, c);
368
                this.addView(fieldTel, "TEL");
369
            }
370
            if (getTable().contains("CALENDAR_USER")) {
371
                c.gridx = 2;
151 ilm 372
                final JCheckBox boxCalUser = new JCheckBox(getLabelFor("CALENDAR_USER"));
144 ilm 373
                c.weightx = 1;
374
                c.gridwidth = GridBagConstraints.REMAINDER;
375
                this.add(boxCalUser, c);
376
                this.addView(boxCalUser, "CALENDAR_USER");
377
            }
174 ilm 378
            if (getTable().contains("COLOR")) {
379
                c.gridy++;
380
                c.gridx = 0;
381
                final JLabel labelTel = new JLabel(getLabelFor("COLOR"), SwingConstants.RIGHT);
382
                c.gridwidth = 1;
383
                c.weightx = 0;
384
                this.add(labelTel, c);
385
                final IColorChooser colorChooser = new IColorChooser(getLabelFor("COLOR"));
386
                c.gridx++;
387
                c.weightx = 1;
388
                c.fill = GridBagConstraints.HORIZONTAL;
389
                this.add(colorChooser, c);
390
                this.addView(colorChooser, "COLOR");
391
            }
17 ilm 392
 
151 ilm 393
            final boolean gestionHoraire = false;
144 ilm 394
            if (Configuration.getInstance().getAppName().startsWith("OpenConcerto") && gestionHoraire) {
17 ilm 395
 
396
                c.gridwidth = 1;
151 ilm 397
                final JPanel panelHoraires = new JPanel(new GridBagLayout());
398
                final GridBagConstraints cH = new DefaultGridBagConstraints();
17 ilm 399
 
144 ilm 400
                createHalfDay(panelHoraires, cH, "Matin :", "MATIN", 8, 12);
401
                createHalfDay(panelHoraires, cH, "Après midi :", "MIDI", 13, 17);
17 ilm 402
 
132 ilm 403
                c.gridy++;
144 ilm 404
                c.gridx = 0;
405
                c.gridwidth = GridBagConstraints.REMAINDER;
406
                c.weightx = 1;
407
                c.weighty = 0;
408
                panelHoraires.setBorder(BorderFactory.createTitledBorder("Horaires"));
409
                this.add(panelHoraires, c);
410
            }
17 ilm 411
 
144 ilm 412
            if (this.accessSocieteElem != null) {
413
                c.gridy++;
414
                c.gridx = 0;
415
                c.gridwidth = 4;
17 ilm 416
 
144 ilm 417
                this.add(new JLabelBold("Accés aux sociétés"), c);
418
                c.gridy++;
151 ilm 419
                this.noCompanyLimitCheckBox = new JCheckBox("ne pas limiter l'accès à certaines sociétés");
420
                this.add(this.noCompanyLimitCheckBox, c);
17 ilm 421
 
144 ilm 422
                c.gridy++;
423
                c.weighty = 1;
424
                c.fill = GridBagConstraints.BOTH;
425
                final SQLElement companyElement = this.accessSocieteElem.getForeignElement("ID_SOCIETE_COMMON");
426
                final List<SQLTableElement> tableElements = new ArrayList<>();
427
                tableElements.add(new SQLTableElement(companyElement.getTable().getField("NOM")));
428
                final RowValuesTableModel model = new RowValuesTableModel(companyElement, tableElements, companyElement.getTable().getKey(), false);
429
                this.table = new QuickAssignPanel(companyElement, "ID", model);
430
                this.add(this.table, c);
17 ilm 431
 
151 ilm 432
                this.noCompanyLimitCheckBox.addItemListener(new ItemListener() {
67 ilm 433
                    @Override
151 ilm 434
                    public void itemStateChanged(final ItemEvent e) {
435
                        UserSQLComponent.this.table.setEnabled(e.getStateChange() == ItemEvent.DESELECTED);
17 ilm 436
                    }
437
                });
144 ilm 438
                getView("ADMIN").addValueListener(new PropertyChangeListener() {
17 ilm 439
                    @Override
151 ilm 440
                    public void propertyChange(final PropertyChangeEvent evt) {
144 ilm 441
                        final boolean selected = evt.getNewValue() == Boolean.TRUE;
151 ilm 442
                        UserSQLComponent.this.noCompanyLimitCheckBox.setEnabled(!selected);
144 ilm 443
                        if (selected)
151 ilm 444
                            UserSQLComponent.this.noCompanyLimitCheckBox.setSelected(true);
17 ilm 445
                    }
446
                });
151 ilm 447
                this.noCompanyLimitCheckBox.setSelected(true);
17 ilm 448
            }
449
 
144 ilm 450
            this.addRequiredSQLObject(textLogin, "LOGIN");
451
            this.addView(new SimpleRowItemView<String>(new TextValueWrapper(this.encryptedPass)) {
452
                @Override
453
                public void setEditable(final InteractionMode mode) {
454
                    mode.applyTo(getPassField());
455
                    mode.applyTo(getPassFieldConfirm());
456
                }
457
            }, "PASSWORD", REQ);
458
            this.addSQLObject(textNom, "NOM");
459
            this.addSQLObject(textPrenom, "PRENOM");
460
            this.addSQLObject(textSurnom, "SURNOM");
17 ilm 461
 
144 ilm 462
            this.getPassField().getDocument().addDocumentListener(new SimpleDocumentListener() {
463
                @Override
151 ilm 464
                public void update(final DocumentEvent e) {
144 ilm 465
                    updateEncrypted();
466
                    fireValidChange();
467
                }
468
            });
469
            this.getPassFieldConfirm().getDocument().addDocumentListener(new SimpleDocumentListener() {
470
                @Override
151 ilm 471
                public void update(final DocumentEvent e) {
144 ilm 472
                    fireValidChange();
473
                }
474
            });
17 ilm 475
 
144 ilm 476
            this.addViews(c);
477
        }
17 ilm 478
 
144 ilm 479
        protected void addViews(final GridBagConstraints c) {
480
        }
17 ilm 481
 
144 ilm 482
        // après midi arrivée 13:30
483
        // __________ départ 17:53
151 ilm 484
        private void createHalfDay(final JPanel panelHoraires, final GridBagConstraints cH, final String label, final String field, final int startHour, final int endHour) {
144 ilm 485
            panelHoraires.add(new JLabel(label), cH);
486
            cH.gridx++;
487
            createTime(panelHoraires, cH, "arrivée", field + "_A", startHour);
488
            cH.gridy++;
489
            cH.gridx = 1;
490
            createTime(panelHoraires, cH, "départ", field + "_D", endHour);
491
            cH.gridy++;
492
            cH.gridx = 0;
493
        }
17 ilm 494
 
144 ilm 495
        // départ 17:53
151 ilm 496
        private void createTime(final JPanel panelHoraires, final GridBagConstraints cH, final String label, final String field, final int hour) {
144 ilm 497
            panelHoraires.add(new JLabel(label), cH);
498
            cH.gridx++;
17 ilm 499
 
144 ilm 500
            final ISpinner spinHourMA = createSpinner(panelHoraires, cH, true, hour);
501
            final ISpinner spinMinMA = createSpinner(panelHoraires, cH, false, 0);
17 ilm 502
 
144 ilm 503
            this.addView(new SimpleRowItemView<Integer>(spinHourMA), "HEURE_" + field, null);
504
            this.addView(new SimpleRowItemView<Integer>(spinMinMA), "MINUTE_" + field, null);
505
        }
17 ilm 506
 
144 ilm 507
        // 17 h or 53 min
151 ilm 508
        private ISpinner createSpinner(final JPanel panelHoraires, final GridBagConstraints cH, final boolean hour, final int value) {
509
            final ISpinnerIntegerModel modelHourMA = new ISpinnerIntegerModel(0, hour ? 23 : 59, value);
510
            final ISpinner spinHourMA = new ISpinner(modelHourMA);
144 ilm 511
            panelHoraires.add(spinHourMA.getComp(), cH);
512
            cH.gridx++;
513
            panelHoraires.add(new JLabel(hour ? "h" : "min"), cH);
514
            cH.gridx++;
515
            return spinHourMA;
516
        }
80 ilm 517
 
144 ilm 518
        private void updateEncrypted() {
519
            final String pass = String.valueOf(this.getPassField().getPassword());
520
            final String dbPass = Boolean.getBoolean(LEGACY_PASSWORDS) ? '"' + pass + '"' : pass;
521
            this.encryptedPass.setText(Login.encodePassword(dbPass));
522
        }
523
 
524
        private boolean checkValidityPassword() {
525
            final boolean b = String.valueOf(this.getPassField().getPassword()).equalsIgnoreCase(String.valueOf(this.getPassFieldConfirm().getPassword()));
526
            this.panelWarning.setVisible(!b);
527
            return b;
528
        }
529
 
530
        @Override
531
        public synchronized ValidState getValidState() {
532
            return super.getValidState().and(ValidState.createCached(checkValidityPassword(), TM.tr("user.passwordsDontMatch")));
533
        }
534
 
535
        @Override
536
        public void select(final SQLRowAccessor row) {
537
            // show something in the user-visible text fields, but do it before the real select
538
            // so that this.encryptedPass (which will be updated by updateEncrypted() and thus
539
            // have a bogus value) will be changed to its database value and thus the user can
540
            // update any field without changing the password.
151 ilm 541
            if (this.table != null) {
542
                this.table.getModel().clearRows();
144 ilm 543
            }
544
            if (row != null) {
545
                final String bogusPass = "bogusPass!";
546
                this.getPassField().setText(bogusPass);
547
                this.getPassFieldConfirm().setText(bogusPass);
548
                // Allowed companies
549
                if (this.accessSocieteElem != null) {
550
                    final SQLTable tableCompanyAccess = this.accessSocieteElem.getTable();
151 ilm 551
                    final SQLRowValues graph = new SQLRowValues(tableCompanyAccess);
144 ilm 552
                    graph.put("ID", null);
553
                    graph.putRowValues("ID_SOCIETE_COMMON").put("NOM", null);
151 ilm 554
                    final SQLRowValuesListFetcher f = new SQLRowValuesListFetcher(graph);
144 ilm 555
                    f.setSelTransf(new ITransformer<SQLSelect, SQLSelect>() {
556
                        @Override
151 ilm 557
                        public SQLSelect transformChecked(final SQLSelect input) {
144 ilm 558
                            input.setWhere(new Where(tableCompanyAccess.getField("ID_USER_COMMON"), "=", row.getID()));
559
                            return input;
80 ilm 560
                        }
144 ilm 561
                    });
151 ilm 562
                    final List<SQLRowValues> companies = f.fetch();
144 ilm 563
 
151 ilm 564
                    for (final SQLRowValues r : companies) {
565
                        this.table.getModel().addRow(r.getForeign("ID_SOCIETE_COMMON").asRowValues());
17 ilm 566
                    }
151 ilm 567
                    this.noCompanyLimitCheckBox.setSelected(companies.isEmpty());
17 ilm 568
                }
569
            }
144 ilm 570
            super.select(row);
571
        }
17 ilm 572
 
144 ilm 573
        @Override
574
        public int insert(SQLRow order) {
575
            int id = super.insert(order);
576
            if (this.table != null && !noCompanyLimitCheckBox.isSelected()) {
577
                insertCompanyAccessForUser(id);
17 ilm 578
            }
144 ilm 579
            return id;
580
        }
17 ilm 581
 
151 ilm 582
        private void insertCompanyAccessForUser(final int id) {
144 ilm 583
            final SQLTable tableCompanyAccess = this.getDirectory().getElement("ACCES_SOCIETE").getTable();
151 ilm 584
            final int stop = this.table.getModel().getRowCount();
144 ilm 585
            for (int i = 0; i < stop; i++) {
151 ilm 586
                final SQLRowValues rCompany = this.table.getModel().getRowValuesAt(i);
587
                final SQLRowValues rAccess = new SQLRowValues(tableCompanyAccess);
144 ilm 588
                rAccess.put("ID_USER_COMMON", id);
589
                rAccess.put("ID_SOCIETE_COMMON", rCompany.getID());
590
                try {
591
                    rAccess.commit();
151 ilm 592
                } catch (final SQLException e) {
144 ilm 593
                    ExceptionHandler.handle("Unable to store company access", e);
80 ilm 594
                }
595
            }
144 ilm 596
        }
80 ilm 597
 
144 ilm 598
        @Override
599
        public void update() {
600
            super.update();
601
            if (this.table != null) {
602
                final SQLTable tableCompanyAccess = this.getDirectory().getElement("ACCES_SOCIETE").getTable();
603
                String query = "DELETE FROM " + tableCompanyAccess.getSQL() + " WHERE \"ID_USER_COMMON\" = " + getSelectedID();
604
                tableCompanyAccess.getDBSystemRoot().getDataSource().execute(query);
605
                if (!noCompanyLimitCheckBox.isSelected()) {
606
                    insertCompanyAccessForUser(getSelectedID());
17 ilm 607
                }
608
            }
144 ilm 609
        }
17 ilm 610
    }
611
}