OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

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