OpenConcerto

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

svn://code.openconcerto.org/openconcerto

Rev

Rev 144 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
132 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.ui.light;
15
 
16
import org.openconcerto.sql.Configuration;
17
import org.openconcerto.sql.model.SQLRowAccessor;
142 ilm 18
import org.openconcerto.sql.model.SQLSelect;
19
import org.openconcerto.sql.view.list.ITableModel;
20
import org.openconcerto.sql.view.list.ListSQLLine;
132 ilm 21
import org.openconcerto.sql.view.list.SQLTableModelColumn;
22
import org.openconcerto.sql.view.list.SQLTableModelSourceOnline;
142 ilm 23
import org.openconcerto.ui.SwingThreadUtils;
24
import org.openconcerto.ui.light.ColumnSpec;
25
import org.openconcerto.ui.light.ColumnsSpec;
132 ilm 26
import org.openconcerto.ui.light.LightUIElement;
27
import org.openconcerto.ui.light.LightUITable;
28
import org.openconcerto.ui.light.Row;
142 ilm 29
import org.openconcerto.ui.light.SearchSpec;
30
import org.openconcerto.utils.NumberUtils;
31
import org.openconcerto.utils.cc.ITransformer;
132 ilm 32
 
33
import java.util.ArrayList;
34
import java.util.List;
142 ilm 35
import java.util.concurrent.Callable;
132 ilm 36
 
142 ilm 37
import javax.swing.SwingUtilities;
38
import javax.swing.event.TableModelListener;
39
 
40
import org.jdom2.Document;
41
import org.jdom2.Element;
42
import org.jdom2.input.DOMBuilder;
43
 
132 ilm 44
import net.minidev.json.JSONObject;
45
 
142 ilm 46
public abstract class LightRowValuesTable extends LightUITable {
132 ilm 47
 
142 ilm 48
    public static final int MAX_LINE_TO_SEND = 100;
132 ilm 49
 
142 ilm 50
    private int totalRowCount = -1;
51
 
52
    private int offset = 0;
53
 
156 ilm 54
    private transient ITableModel model;
142 ilm 55
 
156 ilm 56
    private transient ITransformer<SQLSelect, SQLSelect> orginTransformer;
142 ilm 57
 
156 ilm 58
    private transient List<TableModelListener> tableModelListeners = new ArrayList<TableModelListener>();
142 ilm 59
 
156 ilm 60
    public LightRowValuesTable() {
61
        // Serialization
62
    }
63
 
142 ilm 64
    public LightRowValuesTable(final Configuration configuration, final Number userId, final String id, final ITableModel model) {
65
        super(id);
66
 
67
        this.model = model;
68
        final ColumnsSpec columnsSpec = this.createColumnsSpecFromModelSource(configuration, userId);
69
 
70
        this.getTableSpec().setColumns(columnsSpec);
71
 
72
        if (model.getReq() instanceof SQLTableModelSourceOnline) {
73
            final SQLTableModelSourceOnline source = (SQLTableModelSourceOnline) model.getReq();
74
            this.orginTransformer = source.getReq().getSelectTransf();
75
        } else {
76
            this.orginTransformer = null;
77
        }
78
 
79
        this.setDynamicLoad(true);
132 ilm 80
    }
81
 
82
    // Clone constructor
83
    public LightRowValuesTable(final LightRowValuesTable tableElement) {
84
        super(tableElement);
142 ilm 85
 
86
        this.model = tableElement.model;
87
        this.totalRowCount = tableElement.totalRowCount;
88
        this.orginTransformer = tableElement.orginTransformer;
132 ilm 89
    }
90
 
142 ilm 91
    // Json constructor
92
    public LightRowValuesTable(final JSONObject json) {
93
        super(json);
94
        this.orginTransformer = null;
132 ilm 95
    }
96
 
142 ilm 97
    public final int getTotalRowsCount() {
98
        return this.totalRowCount;
132 ilm 99
    }
100
 
142 ilm 101
    public final int getOffset() {
102
        return this.offset;
132 ilm 103
    }
104
 
142 ilm 105
    public final void setOffset(final int offset) {
106
        this.offset = offset;
132 ilm 107
    }
108
 
142 ilm 109
    public final void addTableModelListener(final TableModelListener tableModelListener) {
110
        this.tableModelListeners.add(tableModelListener);
111
        this.model.addTableModelListener(tableModelListener);
132 ilm 112
    }
113
 
142 ilm 114
    public final void removeTableModelListener(final TableModelListener tableModelListener) {
115
        this.tableModelListeners.remove(tableModelListener);
116
        this.model.removeTableModelListener(tableModelListener);
132 ilm 117
    }
118
 
142 ilm 119
    public ITableModel getModel() {
120
        return this.model;
121
    }
132 ilm 122
 
142 ilm 123
    public final LightListSqlRow getRowFromSqlID(final Number sqlID) {
124
        if (this.hasRow()) {
125
            final int size = this.getTableSpec().getContent().getRowsCount();
126
            for (int i = 0; i < size; i++) {
127
                final LightListSqlRow row = (LightListSqlRow) this.getRow(i);
128
                if (NumberUtils.areNumericallyEqual(row.getSqlRow().getIDNumber(), sqlID)) {
129
                    return row;
130
                }
131
            }
132
        }
133
        return null;
132 ilm 134
    }
135
 
142 ilm 136
    public final LightListSqlRow createLightListRowFromListLine(final ListSQLLine listSqlLine, final int index) throws IllegalStateException {
137
        final ColumnsSpec columnsSpec = this.getTableSpec().getColumns();
138
        final List<SQLTableModelColumn> sqlColumns = this.getModelColumns();
139
        final int colSize = sqlColumns.size();
132 ilm 140
 
142 ilm 141
        final LightListSqlRow row = new LightListSqlRow(listSqlLine.getRow(), listSqlLine.getID());
142
        final List<Object> values = new ArrayList<Object>();
143
        for (int i = 0; i < colSize; i++) {
144
            final String columnId = columnsSpec.getColumn(i).getId();
145
            final SQLTableModelColumn col = getColumnFromId(sqlColumns, columnId);
132 ilm 146
 
142 ilm 147
            if (col != null) {
148
                Object value = col.show(row.getSqlRow());
149
                if (col.getLightUIrenderer() != null) {
150
                    value = col.getLightUIrenderer().getLightUIElement(value, 0, i);
151
                }
152
                values.add(value);
153
            } else {
154
                throw new IllegalArgumentException("column " + columnId + " is in ColumnsSpec but it is not found in SQLTableModelColumn");
132 ilm 155
            }
156
        }
142 ilm 157
        row.setValues(values);
158
 
159
        return row;
132 ilm 160
    }
161
 
142 ilm 162
    public final SQLRowAccessor getFirstSelectedSqlRow() {
163
        final List<Row> selectedRows = this.getSelectedRows();
164
        if (selectedRows.isEmpty()) {
165
            return null;
166
        } else {
167
            return ((LightListSqlRow) selectedRows.get(0)).getSqlRow();
168
        }
132 ilm 169
    }
170
 
142 ilm 171
    private final List<SQLTableModelColumn> getModelColumns() {
172
        try {
173
            // TODO: clean swing
174
            return SwingThreadUtils.call(new Callable<List<SQLTableModelColumn>>() {
175
                @Override
176
                public List<SQLTableModelColumn> call() throws Exception {
177
                    return LightRowValuesTable.this.getModel().getReq().getColumns();
178
                }
179
            });
180
        } catch (final Exception ex) {
181
            throw new IllegalStateException(ex);
182
        }
132 ilm 183
    }
142 ilm 184
 
185
    private final SQLTableModelColumn getColumnFromId(final List<SQLTableModelColumn> allCols, final String columnId) {
186
        final int columnSize = allCols.size();
187
        for (int i = 0; i < columnSize; i++) {
188
            final SQLTableModelColumn tableModelColumn = allCols.get(i);
189
            if (tableModelColumn.getIdentifier().equals(columnId)) {
190
                return tableModelColumn;
191
            }
192
        }
193
        return null;
132 ilm 194
    }
195
 
142 ilm 196
    /**
197
     * Get columns user preferences for a specific table
198
     *
199
     * @param configuration - The user SQL configuration
200
     * @param userId - Id of the user who want view the table
201
     *
202
     * @return the XML which contains user preferences
203
     *
204
     * @throws IllegalArgumentException
205
     * @throws IllegalStateException
206
     *
207
     */
208
    // TODO: move in LightUITable, maybe move LightUITable in FrameWork_SQL
209
    private final Document getColumnsSpecUserPerfs(final Configuration configuration, final Number userId) throws IllegalArgumentException, IllegalStateException {
210
        Document columnsPrefs = null;
211
        final DOMBuilder in = new DOMBuilder();
212
        org.w3c.dom.Document w3cDoc = null;
132 ilm 213
 
142 ilm 214
        w3cDoc = configuration.getXMLConf(userId, this.getId());
215
        if (w3cDoc != null) {
216
            columnsPrefs = in.build(w3cDoc);
217
        }
218
        return columnsPrefs;
132 ilm 219
    }
220
 
142 ilm 221
    /**
222
     * Create ColumnsSpec from list of SQLTableModelColumn and apply user preferences
223
     *
224
     * @param configuration - current SQL configuration of user
225
     * @param userId - Id of user
226
     *
227
     * @return New ColumnsSpec with user preferences application
228
     *
229
     * @throws IllegalArgumentException
230
     * @throws IllegalStateException
231
     */
232
    private final ColumnsSpec createColumnsSpecFromModelSource(final Configuration configuration, final Number userId) throws IllegalArgumentException, IllegalStateException {
233
        final List<String> possibleColumnIds = new ArrayList<String>();
234
        final List<String> sortedIds = new ArrayList<String>();
235
        final List<ColumnSpec> columnsSpec = new ArrayList<ColumnSpec>();
132 ilm 236
 
142 ilm 237
        final List<SQLTableModelColumn> columns = this.getModelColumns();
238
        final int columnsCount = columns.size();
239
 
240
        for (int i = 0; i < columnsCount; i++) {
241
            final SQLTableModelColumn sqlColumn = columns.get(i);
242
            // TODO : creer la notion d'ID un peu plus dans le l'esprit sales.invoice.amount
243
            final String columnId = sqlColumn.getIdentifier();
244
 
245
            possibleColumnIds.add(columnId);
246
            Class<?> valueClass = sqlColumn.getValueClass();
247
            if (sqlColumn.getLightUIrenderer() != null) {
248
                valueClass = LightUIElement.class;
132 ilm 249
            }
250
 
142 ilm 251
            columnsSpec.add(new ColumnSpec(columnId, valueClass, sqlColumn.getName(), null, false, null));
132 ilm 252
        }
253
 
142 ilm 254
        // TODO : recuperer l'info sauvegardée sur le serveur par user (à coder)
255
        sortedIds.add(columnsSpec.get(0).getId());
132 ilm 256
 
142 ilm 257
        final ColumnsSpec cSpec = new ColumnsSpec(this.getId(), columnsSpec, possibleColumnIds, sortedIds);
258
        cSpec.setAllowMove(true);
259
        cSpec.setAllowResize(true);
144 ilm 260
        try {
261
            final Document xmlColumnsPref = this.getColumnsSpecUserPerfs(configuration, userId);
262
            if (!cSpec.setUserPrefs(xmlColumnsPref)) {
263
                configuration.removeXMLConf(userId, this.getId());
264
            }
265
        } catch (Exception e) {
266
            System.err.println("LightRowValuesTable.createColumnsSpecFromModelSource() no column pref for conf : " + configuration.getRoot().getName() + " , user id : " + userId);
132 ilm 267
        }
142 ilm 268
 
269
        return cSpec;
132 ilm 270
    }
271
 
142 ilm 272
    // TODO: merge with OpenConcerto List search system
273
    public abstract void doSearch(final Configuration configuration, final SearchSpec searchSpec, final int offset);
274
 
132 ilm 275
    @Override
142 ilm 276
    public Row getRowById(Number rowId) {
277
        for (int i = 0; i < this.model.getRowCount(); i++) {
278
            if (NumberUtils.areNumericallyEqual(this.model.getRow(i).getID(), rowId)) {
279
                return this.createLightListRowFromListLine(this.model.getRow(i), i);
280
            }
281
        }
282
        return super.getRowById(rowId);
132 ilm 283
    }
284
 
142 ilm 285
    /**
286
     * Create default columns preferences from the SQLTableModelLinesSourceOnline
287
     *
288
     * @return XML document with columns preferences
289
     */
132 ilm 290
    @Override
142 ilm 291
    public Document createDefaultXmlPreferences() {
292
        final Element rootElement = new Element("list");
293
        final List<SQLTableModelColumn> columns = this.getModelColumns();
132 ilm 294
 
142 ilm 295
        final int sqlColumnsCount = columns.size();
296
        for (int i = 0; i < sqlColumnsCount; i++) {
297
            final SQLTableModelColumn sqlColumn = columns.get(i);
298
            final String columnId = sqlColumn.getIdentifier();
299
            final ColumnSpec columnSpec = new ColumnSpec(columnId, sqlColumn.getValueClass(), sqlColumn.getName(), null, false, null);
300
            final Element columnElement = this.createXmlColumn(columnId, columnSpec.getMaxWidth(), columnSpec.getMinWidth(), columnSpec.getWidth());
301
            rootElement.addContent(columnElement);
132 ilm 302
        }
142 ilm 303
        final Document xmlConf = new Document(rootElement);
132 ilm 304
 
142 ilm 305
        return xmlConf;
132 ilm 306
    }
307
 
308
    @Override
142 ilm 309
    public void destroy() {
310
        super.destroy();
132 ilm 311
 
142 ilm 312
        // TODO: clean swing
313
        try {
314
            SwingUtilities.invokeAndWait(new Runnable() {
315
                @Override
316
                public void run() {
317
                    final List<TableModelListener> tableModelListeners = LightRowValuesTable.this.tableModelListeners;
318
                    for (int i = tableModelListeners.size() - 1; i > -1; i--) {
319
                        LightRowValuesTable.this.removeTableModelListener(tableModelListeners.get(i));
320
                    }
321
                }
322
            });
323
        } catch (final Exception ex) {
324
            throw new IllegalStateException(ex);
132 ilm 325
        }
326
 
142 ilm 327
        this.clearRows();
132 ilm 328
    }
329
}