OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 17 | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 17 Rev 25
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 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.
10
 * 
10
 * 
11
 * When distributing the software, include this License Header Notice in each file.
11
 * When distributing the software, include this License Header Notice in each file.
12
 */
12
 */
13
 
13
 
14
 package org.openconcerto.sql.users.rights;
14
 package org.openconcerto.sql.users.rights;
15
 
15
 
16
import org.openconcerto.sql.Configuration;
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.model.SQLRow;
17
import org.openconcerto.sql.model.SQLRow;
18
import org.openconcerto.sql.model.SQLRowListRSH;
18
import org.openconcerto.sql.model.SQLRowListRSH;
19
import org.openconcerto.sql.model.SQLRowValues;
19
import org.openconcerto.sql.model.SQLRowValues;
20
import org.openconcerto.sql.model.SQLSelect;
20
import org.openconcerto.sql.model.SQLSelect;
21
import org.openconcerto.sql.model.SQLTable;
21
import org.openconcerto.sql.model.SQLTable;
22
import org.openconcerto.sql.model.SQLTableListener;
22
import org.openconcerto.sql.model.SQLTableEvent;
-
 
23
import org.openconcerto.sql.model.SQLTableEvent.Mode;
-
 
24
import org.openconcerto.sql.model.SQLTableModifiedListener;
23
import org.openconcerto.sql.model.Where;
25
import org.openconcerto.sql.model.Where;
24
 
26
 
25
import java.sql.SQLException;
27
import java.sql.SQLException;
26
import java.util.ArrayList;
28
import java.util.ArrayList;
27
import java.util.Arrays;
29
import java.util.Arrays;
28
import java.util.HashMap;
30
import java.util.HashMap;
29
import java.util.List;
31
import java.util.List;
30
import java.util.Map;
32
import java.util.Map;
31
import java.util.Vector;
33
import java.util.Vector;
32
 
34
 
33
import javax.swing.table.AbstractTableModel;
35
import javax.swing.table.AbstractTableModel;
34
 
36
 
35
public class UserRightsManagerModel extends AbstractTableModel {
37
public class UserRightsManagerModel extends AbstractTableModel {
36
 
38
 
37
    private SQLTable tableRight = Configuration.getInstance().getRoot().findTable("RIGHTS");
39
    private SQLTable tableRight = Configuration.getInstance().getRoot().findTable("RIGHTS");
38
    private SQLTable tableUserRight = Configuration.getInstance().getRoot().findTable("USER_RIGHT");
40
    private SQLTable tableUserRight = Configuration.getInstance().getRoot().findTable("USER_RIGHT");
39
 
41
 
40
    private int idUser = -1;
42
    private int idUser = -1;
41
 
43
 
42
    private Vector<SQLRowValues> listRowValues = new Vector<SQLRowValues>(this.tableRight.getRowCount());
44
    private Vector<SQLRowValues> listRowValues = new Vector<SQLRowValues>(this.tableRight.getRowCount());
43
 
45
 
44
    private List<String> columns = Arrays.asList("Actif", "Libellé");
46
    private List<String> columns = Arrays.asList("Actif", "Libellé");
45
 
47
 
46
    List<SQLRow> cache = new ArrayList<SQLRow>();
48
    List<SQLRow> cache = new ArrayList<SQLRow>();
47
 
49
 
48
    public UserRightsManagerModel() {
50
    public UserRightsManagerModel() {
49
        super();
51
        super();
50
 
52
 
51
        // On recupere l'ensemble des droits
53
        // On recupere l'ensemble des droits
52
        SQLSelect sel2 = new SQLSelect(Configuration.getInstance().getBase());
54
        SQLSelect sel2 = new SQLSelect(Configuration.getInstance().getBase());
53
        sel2.addSelect(this.tableRight.getKey());
55
        sel2.addSelect(this.tableRight.getKey());
54
        sel2.addFieldOrder(this.tableRight.getField("CODE"));
56
        sel2.addFieldOrder(this.tableRight.getField("CODE"));
55
        List<SQLRow> rowsRights = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel2.asString(), new SQLRowListRSH(this.tableRight, true));
57
        List<SQLRow> rowsRights = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel2.asString(), new SQLRowListRSH(this.tableRight, true));
56
        this.cache.addAll(rowsRights);
58
        this.cache.addAll(rowsRights);
57
 
59
 
58
        this.tableRight.addTableListener(new SQLTableListener() {
60
        this.tableRight.addTableModifiedListener(new SQLTableModifiedListener() {
59
            @Override
61
            @Override
60
            public void rowAdded(SQLTable table, int id) {
62
            public void tableModified(SQLTableEvent evt) {
61
 
-
 
62
                UserRightsManagerModel.this.cache.add(table.getRow(id));
63
                if (evt.getMode() == Mode.ROW_ADDED) {
63
            }
-
 
64
 
-
 
65
            @Override
-
 
66
            public void rowDeleted(SQLTable table, int id) {
64
                    UserRightsManagerModel.this.cache.add(evt.getRow());
67
 
-
 
68
            }
-
 
69
 
-
 
70
            @Override
65
                } else {
71
            public void rowModified(SQLTable table, int id) {
-
 
72
                SQLRow row = table.getRow(id);
66
                    final SQLRow row = evt.getRow();
73
 
-
 
74
                for (int i = 0; i < UserRightsManagerModel.this.cache.size(); i++) {
67
                    for (int i = 0; i < UserRightsManagerModel.this.cache.size(); i++) {
75
                    SQLRow row2 = UserRightsManagerModel.this.cache.get(i);
68
                        final SQLRow row2 = UserRightsManagerModel.this.cache.get(i);
76
                    if (row2.getID() == id) {
69
                        if (row2.getID() == row.getID()) {
77
                        if (row.isArchived()) {
70
                            if (!row.isValid()) {
78
                            UserRightsManagerModel.this.cache.remove(i);
71
                                UserRightsManagerModel.this.cache.remove(i);
79
                        } else {
72
                            } else {
80
                            UserRightsManagerModel.this.cache.set(i, row2);
73
                                UserRightsManagerModel.this.cache.set(i, row2);
81
                        }
74
                            }
82
                        break;
75
                            break;
83
                    }
76
                        }
84
                }
77
                    }
85
            }
78
                }
-
 
79
            }
86
        });
80
        });
87
 
81
 
88
        this.tableUserRight.addTableListener(new SQLTableListener() {
82
        this.tableUserRight.addTableModifiedListener(new SQLTableModifiedListener() {
-
 
83
 
89
            @Override
84
            @Override
90
            public void rowAdded(SQLTable table, int id) {
85
            public void tableModified(SQLTableEvent evt) {
-
 
86
                if (evt.getMode() == Mode.ROW_ADDED) {
-
 
87
                    rowAdded(evt);
-
 
88
                } else {
-
 
89
                    rowModified(evt);
-
 
90
                }
-
 
91
            }
91
 
92
 
-
 
93
            public void rowAdded(SQLTableEvent evt) {
92
                SQLRow row = table.getRow(id);
94
                final SQLRow row = evt.getRow();
93
                if (row.getInt("ID_USER_COMMON") == UserRightsManagerModel.this.idUser) {
95
                if (row.getInt("ID_USER_COMMON") == UserRightsManagerModel.this.idUser) {
94
                    SQLRowValues rowVals = getSQLRowValuesForID(id);
96
                    SQLRowValues rowVals = getSQLRowValuesFor(row);
95
                    if (rowVals == null) {
97
                    if (rowVals == null) {
96
                        UserRightsManagerModel.this.listRowValues.add(row.createUpdateRow());
98
                        UserRightsManagerModel.this.listRowValues.add(row.createUpdateRow());
97
                        fireTableRowsInserted(UserRightsManagerModel.this.listRowValues.size() - 2, UserRightsManagerModel.this.listRowValues.size() - 1);
99
                        fireTableRowsInserted(UserRightsManagerModel.this.listRowValues.size() - 2, UserRightsManagerModel.this.listRowValues.size() - 1);
98
                    }
100
                    }
99
                }
101
                }
100
            }
102
            }
101
 
103
 
102
            @Override
-
 
103
            public void rowDeleted(SQLTable table, int id) {
-
 
104
            }
-
 
105
 
-
 
106
            @Override
-
 
107
            public void rowModified(SQLTable table, int id) {
104
            public void rowModified(SQLTableEvent evt) {
108
                SQLRow row = table.getRow(id);
105
                final SQLRow row = evt.getRow();
109
                if (row.getInt("ID_USER_COMMON") == UserRightsManagerModel.this.idUser) {
106
                if (row.getInt("ID_USER_COMMON") == UserRightsManagerModel.this.idUser) {
110
                    SQLRowValues rowVals = getSQLRowValuesForID(id);
107
                    SQLRowValues rowVals = getSQLRowValuesFor(row);
111
                    int index = UserRightsManagerModel.this.listRowValues.indexOf(rowVals);
108
                    int index = UserRightsManagerModel.this.listRowValues.indexOf(rowVals);
112
                    if (row.isArchived()) {
109
                    if (!row.isValid()) {
113
                        UserRightsManagerModel.this.listRowValues.removeElement(rowVals);
110
                        UserRightsManagerModel.this.listRowValues.removeElement(rowVals);
114
                        fireTableRowsDeleted(index - 1, index + 1);
111
                        fireTableRowsDeleted(index - 1, index + 1);
115
                    } else {
112
                    } else {
116
                        rowVals.loadAbsolutelyAll(row);
113
                        rowVals.loadAbsolutelyAll(row);
117
                        fireTableRowsUpdated(index - 1, index + 1);
114
                        fireTableRowsUpdated(index - 1, index + 1);
118
                    }
115
                    }
119
                }
116
                }
120
            }
117
            }
121
        });
118
        });
122
    }
119
    }
123
 
120
 
124
    private SQLRowValues getSQLRowValuesForID(int id) {
121
    private SQLRowValues getSQLRowValuesFor(final SQLRow row) {
125
        SQLRow row = this.tableUserRight.getRow(id);
-
 
126
        final String string2 = row.getString("CODE");
122
        final String string2 = row.getString("CODE");
127
        for (SQLRowValues rowVals : this.listRowValues) {
123
        for (SQLRowValues rowVals : this.listRowValues) {
128
            final String string = rowVals.getString("CODE");
124
            final String string = rowVals.getString("CODE");
129
            if (rowVals.getID() == id || (string != null && string2 != null && string.equalsIgnoreCase(string2))) {
125
            if (rowVals.getID() == row.getID() || (string != null && string2 != null && string.equalsIgnoreCase(string2))) {
130
                return rowVals;
126
                return rowVals;
131
            }
127
            }
132
        }
128
        }
133
        return null;
129
        return null;
134
    }
130
    }
135
 
131
 
136
    @Override
132
    @Override
137
    public String getColumnName(int column) {
133
    public String getColumnName(int column) {
138
        return this.columns.get(column);
134
        return this.columns.get(column);
139
    }
135
    }
140
 
136
 
141
    @Override
137
    @Override
142
    public int getColumnCount() {
138
    public int getColumnCount() {
143
        return this.columns.size();
139
        return this.columns.size();
144
    }
140
    }
145
 
141
 
146
    @Override
142
    @Override
147
    public int getRowCount() {
143
    public int getRowCount() {
148
        return this.listRowValues.size();
144
        return this.listRowValues.size();
149
    }
145
    }
150
 
146
 
151
    @Override
147
    @Override
152
    public Class<?> getColumnClass(int columnIndex) {
148
    public Class<?> getColumnClass(int columnIndex) {
153
        if (columnIndex == 0) {
149
        if (columnIndex == 0) {
154
 
150
 
155
            return Boolean.class;
151
            return Boolean.class;
156
        } else {
152
        } else {
157
            return String.class;
153
            return String.class;
158
        }
154
        }
159
    }
155
    }
160
 
156
 
161
    @Override
157
    @Override
162
    public boolean isCellEditable(int rowIndex, int columnIndex) {
158
    public boolean isCellEditable(int rowIndex, int columnIndex) {
163
 
159
 
164
        return (columnIndex == 0);
160
        return (columnIndex == 0);
165
    }
161
    }
166
 
162
 
167
    @Override
163
    @Override
168
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
164
    public void setValueAt(Object value, int rowIndex, int columnIndex) {
169
 
165
 
170
        this.listRowValues.get(rowIndex).put("HAVE_RIGHT", value);
166
        this.listRowValues.get(rowIndex).put("HAVE_RIGHT", value);
171
        fireTableCellUpdated(rowIndex, columnIndex);
167
        fireTableCellUpdated(rowIndex, columnIndex);
172
    }
168
    }
173
 
169
 
174
    @Override
170
    @Override
175
    public Object getValueAt(int rowIndex, int columnIndex) {
171
    public Object getValueAt(int rowIndex, int columnIndex) {
176
 
172
 
177
        if (columnIndex == 0) {
173
        if (columnIndex == 0) {
178
            Boolean b = this.listRowValues.get(rowIndex).getBoolean("HAVE_RIGHT");
174
            Boolean b = this.listRowValues.get(rowIndex).getBoolean("HAVE_RIGHT");
179
            return (b == null) ? true : b;
175
            return (b == null) ? true : b;
180
        } else {
176
        } else {
181
            return this.listRowValues.get(rowIndex).getString("NOM");
177
            return this.listRowValues.get(rowIndex).getString("NOM");
182
        }
178
        }
183
 
179
 
184
    }
180
    }
185
 
181
 
186
    public SQLRowValues getRowValuesAt(int index) {
182
    public SQLRowValues getRowValuesAt(int index) {
187
        return this.listRowValues.get(index);
183
        return this.listRowValues.get(index);
188
    }
184
    }
189
 
185
 
190
    /**
186
    /**
191
     * Valide les modifications dans la base
187
     * Valide les modifications dans la base
192
     */
188
     */
193
    public void commitData() {
189
    public void commitData() {
194
 
190
 
195
        List<SQLRowValues> listRowVals = new ArrayList<SQLRowValues>(this.listRowValues);
191
        List<SQLRowValues> listRowVals = new ArrayList<SQLRowValues>(this.listRowValues);
196
 
192
 
197
        for (SQLRowValues rowVals : listRowVals) {
193
        for (SQLRowValues rowVals : listRowVals) {
198
 
194
 
199
            try {
195
            try {
200
                SQLRow row = rowVals.commit();
196
                SQLRow row = rowVals.commit();
201
                rowVals.loadAbsolutelyAll(row);
197
                rowVals.loadAbsolutelyAll(row);
202
            } catch (SQLException e) {
198
            } catch (SQLException e) {
203
                e.printStackTrace();
199
                e.printStackTrace();
204
            }
200
            }
205
        }
201
        }
206
    }
202
    }
207
 
203
 
208
    /**
204
    /**
209
     * Charge les droits de l'utilisateur, vide la table si il n'existe pas
205
     * Charge les droits de l'utilisateur, vide la table si il n'existe pas
210
     * 
206
     * 
211
     * @param idUser
207
     * @param idUser
212
     */
208
     */
213
    public void loadRightsForUser(int idUser) {
209
    public void loadRightsForUser(int idUser) {
214
        this.idUser = idUser;
210
        this.idUser = idUser;
215
        if (idUser > 1) {
211
        if (idUser > 1) {
216
 
212
 
217
            // On recupere les droits deja définit pour cet utilisateur
213
            // On recupere les droits deja définit pour cet utilisateur
218
            SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
214
            SQLSelect sel = new SQLSelect(Configuration.getInstance().getBase());
219
            sel.addSelect(this.tableUserRight.getKey());
215
            sel.addSelect(this.tableUserRight.getKey());
220
            sel.setWhere(new Where(this.tableUserRight.getField("ID_USER_COMMON"), "=", idUser));
216
            sel.setWhere(new Where(this.tableUserRight.getField("ID_USER_COMMON"), "=", idUser));
221
            List<SQLRow> rows = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new SQLRowListRSH(this.tableUserRight, true));
217
            List<SQLRow> rows = (List<SQLRow>) Configuration.getInstance().getBase().getDataSource().execute(sel.asString(), new SQLRowListRSH(this.tableUserRight, true));
222
            Map<String, SQLRowValues> map = new HashMap<String, SQLRowValues>(rows.size());
218
            Map<String, SQLRowValues> map = new HashMap<String, SQLRowValues>(rows.size());
223
            for (SQLRow row : rows) {
219
            for (SQLRow row : rows) {
224
                map.put(row.getString("CODE"), row.createUpdateRow());
220
                map.put(row.getString("CODE"), row.createUpdateRow());
225
            }
221
            }
226
 
222
 
227
            this.listRowValues.clear();
223
            this.listRowValues.clear();
228
 
224
 
229
            for (SQLRow row : this.cache) {
225
            for (SQLRow row : this.cache) {
230
                final SQLRowValues e = map.get(row.getString("CODE"));
226
                final SQLRowValues e = map.get(row.getString("CODE"));
231
                if (e != null) {
227
                if (e != null) {
232
                    e.put("NOM", row.getString("NOM"));
228
                    e.put("NOM", row.getString("NOM"));
233
                    e.put("DESCRIPTION", row.getString("DESCRIPTION"));
229
                    e.put("DESCRIPTION", row.getString("DESCRIPTION"));
234
                    this.listRowValues.add(e);
230
                    this.listRowValues.add(e);
235
                } else {
231
                } else {
236
                    SQLRowValues rowVals = new SQLRowValues(this.tableUserRight);
232
                    SQLRowValues rowVals = new SQLRowValues(this.tableUserRight);
237
                    rowVals.put("ID_USER_COMMON", idUser);
233
                    rowVals.put("ID_USER_COMMON", idUser);
238
                    rowVals.put("CODE", row.getString("CODE"));
234
                    rowVals.put("CODE", row.getString("CODE"));
239
                    rowVals.put("NOM", row.getString("NOM"));
235
                    rowVals.put("NOM", row.getString("NOM"));
240
                    rowVals.put("DESCRIPTION", row.getString("DESCRIPTION"));
236
                    rowVals.put("DESCRIPTION", row.getString("DESCRIPTION"));
241
                    this.listRowValues.add(rowVals);
237
                    this.listRowValues.add(rowVals);
242
                }
238
                }
243
            }
239
            }
244
        } else {
240
        } else {
245
            this.listRowValues.clear();
241
            this.listRowValues.clear();
246
        }
242
        }
247
        fireTableDataChanged();
243
        fireTableDataChanged();
248
    }
244
    }
249
}
245
}