OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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