17 |
ilm |
1 |
/*
|
|
|
2 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
|
|
|
3 |
*
|
182 |
ilm |
4 |
* Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
|
17 |
ilm |
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.view.list;
|
|
|
15 |
|
73 |
ilm |
16 |
import org.openconcerto.sql.model.ConnectionHandlerNoSetup;
|
|
|
17 |
import org.openconcerto.sql.model.SQLDataSource;
|
17 |
ilm |
18 |
import org.openconcerto.sql.model.SQLRow;
|
73 |
ilm |
19 |
import org.openconcerto.sql.model.SQLRowAccessor;
|
17 |
ilm |
20 |
import org.openconcerto.sql.model.SQLRowValues;
|
156 |
ilm |
21 |
import org.openconcerto.sql.model.SQLSelect;
|
|
|
22 |
import org.openconcerto.sql.model.SQLSelect.LockStrength;
|
73 |
ilm |
23 |
import org.openconcerto.sql.model.SQLSyntax;
|
17 |
ilm |
24 |
import org.openconcerto.sql.model.SQLTable;
|
156 |
ilm |
25 |
import org.openconcerto.sql.model.Where;
|
73 |
ilm |
26 |
import org.openconcerto.sql.request.UpdateBuilder;
|
|
|
27 |
import org.openconcerto.sql.utils.ReOrder;
|
|
|
28 |
import org.openconcerto.sql.utils.SQLUtils;
|
17 |
ilm |
29 |
import org.openconcerto.utils.ExceptionUtils;
|
156 |
ilm |
30 |
import org.openconcerto.utils.NumberUtils;
|
17 |
ilm |
31 |
import org.openconcerto.utils.SleepingQueue;
|
|
|
32 |
|
73 |
ilm |
33 |
import java.math.BigDecimal;
|
17 |
ilm |
34 |
import java.sql.SQLException;
|
73 |
ilm |
35 |
import java.util.ArrayList;
|
|
|
36 |
import java.util.Arrays;
|
|
|
37 |
import java.util.Collections;
|
|
|
38 |
import java.util.List;
|
63 |
ilm |
39 |
import java.util.concurrent.Callable;
|
93 |
ilm |
40 |
import java.util.concurrent.Future;
|
63 |
ilm |
41 |
import java.util.concurrent.FutureTask;
|
17 |
ilm |
42 |
|
93 |
ilm |
43 |
import javax.swing.SwingUtilities;
|
|
|
44 |
|
73 |
ilm |
45 |
public final class MoveQueue extends SleepingQueue {
|
17 |
ilm |
46 |
|
|
|
47 |
private final ITableModel tableModel;
|
|
|
48 |
|
73 |
ilm |
49 |
MoveQueue(ITableModel model) {
|
17 |
ilm |
50 |
super(MoveQueue.class.getSimpleName() + " on " + model);
|
|
|
51 |
this.tableModel = model;
|
|
|
52 |
}
|
|
|
53 |
|
93 |
ilm |
54 |
public Future<?> move(final List<? extends SQLRowAccessor> rows, final int inc) {
|
73 |
ilm |
55 |
if (inc == 0 || rows.size() == 0)
|
93 |
ilm |
56 |
return null;
|
73 |
ilm |
57 |
|
|
|
58 |
final boolean after = inc > 0;
|
93 |
ilm |
59 |
SQLRowAccessor outerR = null;
|
|
|
60 |
for (final SQLRowAccessor r : rows) {
|
|
|
61 |
if (outerR == null) {
|
|
|
62 |
outerR = r;
|
|
|
63 |
} else {
|
|
|
64 |
final int compare = r.getOrder().compareTo(outerR.getOrder());
|
|
|
65 |
if (after && compare > 0 || !after && compare < 0) {
|
|
|
66 |
outerR = r;
|
|
|
67 |
}
|
|
|
68 |
}
|
|
|
69 |
}
|
|
|
70 |
final int id = outerR.getID();
|
|
|
71 |
return this.put(new Runnable() {
|
17 |
ilm |
72 |
public void run() {
|
73 |
ilm |
73 |
final FutureTask<ListSQLLine> destID = new FutureTask<ListSQLLine>(new Callable<ListSQLLine>() {
|
63 |
ilm |
74 |
@Override
|
73 |
ilm |
75 |
public ListSQLLine call() {
|
|
|
76 |
return MoveQueue.this.tableModel.getDestLine(id, inc);
|
17 |
ilm |
77 |
}
|
|
|
78 |
});
|
63 |
ilm |
79 |
MoveQueue.this.tableModel.invokeLater(destID);
|
17 |
ilm |
80 |
try {
|
63 |
ilm |
81 |
if (destID.get() != null) {
|
93 |
ilm |
82 |
SQLUtils.executeAtomic(getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, Exception>() {
|
73 |
ilm |
83 |
@Override
|
|
|
84 |
public Object handle(SQLDataSource ds) throws Exception {
|
182 |
ilm |
85 |
moveQuick(rows, after, destID.get().getRowAccessor().asRow());
|
73 |
ilm |
86 |
return null;
|
|
|
87 |
}
|
|
|
88 |
});
|
17 |
ilm |
89 |
}
|
63 |
ilm |
90 |
} catch (Exception e) {
|
|
|
91 |
throw ExceptionUtils.createExn(IllegalStateException.class, "move failed", e);
|
17 |
ilm |
92 |
}
|
|
|
93 |
}
|
|
|
94 |
});
|
|
|
95 |
}
|
|
|
96 |
|
80 |
ilm |
97 |
// row index as returned by JTable.DropLocation.getRow()
|
93 |
ilm |
98 |
public Future<?> moveTo(final List<? extends Number> rows, final int rowIndex) {
|
80 |
ilm |
99 |
if (rows.size() == 0)
|
93 |
ilm |
100 |
return null;
|
80 |
ilm |
101 |
|
93 |
ilm |
102 |
// the user just dropped rows to move on rowIndex, find out right away which row that is.
|
|
|
103 |
// (we used to go through the UpdateQueue which could result in the destination row the user
|
|
|
104 |
// saw being replaced by a different row)
|
|
|
105 |
assert rowIndex >= 0;
|
|
|
106 |
assert SwingUtilities.isEventDispatchThread();
|
|
|
107 |
final int rowCount = this.tableModel.getRowCount();
|
|
|
108 |
final boolean after = rowIndex >= rowCount;
|
|
|
109 |
final int index = after ? rowCount - 1 : rowIndex;
|
182 |
ilm |
110 |
final SQLRowAccessor line = this.tableModel.getRow(index).getRowAccessor();
|
93 |
ilm |
111 |
assert line.isFrozen() : "row could change by the time move() is called";
|
|
|
112 |
|
|
|
113 |
return this.put(new Runnable() {
|
|
|
114 |
@Override
|
80 |
ilm |
115 |
public void run() {
|
|
|
116 |
try {
|
93 |
ilm |
117 |
SQLUtils.executeAtomic(getTable().getDBSystemRoot().getDataSource(), new ConnectionHandlerNoSetup<Object, Exception>() {
|
80 |
ilm |
118 |
@Override
|
|
|
119 |
public Object handle(SQLDataSource ds) throws Exception {
|
93 |
ilm |
120 |
moveQuick(rows, after, line.asRow());
|
80 |
ilm |
121 |
return null;
|
|
|
122 |
}
|
|
|
123 |
});
|
|
|
124 |
} catch (Exception e) {
|
|
|
125 |
throw ExceptionUtils.createExn(IllegalStateException.class, "move failed", e);
|
|
|
126 |
}
|
|
|
127 |
}
|
|
|
128 |
});
|
|
|
129 |
}
|
|
|
130 |
|
93 |
ilm |
131 |
final void moveQuick(final List<?> srcRows, final boolean after, final SQLRow destRow) throws SQLException {
|
73 |
ilm |
132 |
final int rowCount = srcRows.size();
|
|
|
133 |
// if only some rows are moved, update one by one (avoids refreshing the whole list)
|
93 |
ilm |
134 |
// (getRowCount() is not thread-safe, so use getTotalRowCount())
|
|
|
135 |
if (rowCount < 5 && rowCount < (this.tableModel.getTotalRowCount() / 3)) {
|
|
|
136 |
final SQLTable t = getTable();
|
156 |
ilm |
137 |
final String orderName = t.getOrderField().getName();
|
93 |
ilm |
138 |
final SQLRowValues vals = new SQLRowValues(t);
|
156 |
ilm |
139 |
final List<BigDecimal> orders = ReOrder.getFreeOrderValuesFor(rowCount, after, destRow).get0();
|
|
|
140 |
for (int i = 0; i < rowCount; i++) {
|
|
|
141 |
final Object src = srcRows.get(i);
|
|
|
142 |
vals.put(orderName, orders.get(i)).update(getID(src, t).intValue());
|
93 |
ilm |
143 |
}
|
73 |
ilm |
144 |
} else {
|
|
|
145 |
// update all rows at once and refresh the whole list
|
93 |
ilm |
146 |
moveAtOnce(srcRows, rowCount, after, destRow);
|
73 |
ilm |
147 |
}
|
17 |
ilm |
148 |
}
|
|
|
149 |
|
93 |
ilm |
150 |
private static Number getID(final Object o, final SQLTable t) {
|
|
|
151 |
final Number res;
|
|
|
152 |
if (o instanceof SQLRowAccessor) {
|
|
|
153 |
final SQLRowAccessor row = (SQLRowAccessor) o;
|
|
|
154 |
if (row.getTable() != t)
|
|
|
155 |
throw new IllegalArgumentException("Not from the same table : " + row + " != " + t);
|
|
|
156 |
res = row.getIDNumber();
|
|
|
157 |
} else {
|
|
|
158 |
res = (Number) o;
|
|
|
159 |
}
|
|
|
160 |
return res;
|
|
|
161 |
}
|
|
|
162 |
|
17 |
ilm |
163 |
private SQLTable getTable() {
|
|
|
164 |
return this.tableModel.getTable();
|
|
|
165 |
}
|
|
|
166 |
|
93 |
ilm |
167 |
/**
|
|
|
168 |
* Move the passed rows just before or just after the destination row. The current database
|
|
|
169 |
* order of the rows is not used, i.e. after this method returns the order will be that of the
|
|
|
170 |
* passed list, with <code>destRow</code> coming either before or after them all.
|
|
|
171 |
*
|
|
|
172 |
* @param srcRows rows to reorder.
|
|
|
173 |
* @param after <code>true</code> if the rows should be placed after <code>destRow</code>,
|
|
|
174 |
* <code>false</code> otherwise.
|
|
|
175 |
* @param destRow <code>srcRows</code> will be placed relative to the order of this row.
|
|
|
176 |
* @throws SQLException if an error occurs.
|
|
|
177 |
*/
|
73 |
ilm |
178 |
static public void moveAtOnce(final List<? extends SQLRowAccessor> srcRows, final boolean after, final SQLRow destRow) throws SQLException {
|
93 |
ilm |
179 |
moveAtOnce(srcRows, srcRows.size(), after, destRow);
|
|
|
180 |
}
|
|
|
181 |
|
156 |
ilm |
182 |
/**
|
|
|
183 |
* Move the passed rows just before or just after the destination row if needed.
|
|
|
184 |
*
|
|
|
185 |
* @param ids rows to order.
|
|
|
186 |
* @param after <code>true</code> if the rows should be placed after <code>destRow</code>,
|
|
|
187 |
* <code>false</code> otherwise.
|
|
|
188 |
* @param destRow <code>srcRows</code> will be placed relative to the order of this row.
|
|
|
189 |
* @param checkBefore if <code>true</code> the current DB order will be checked and if it
|
|
|
190 |
* matched the passed rows, no change will be made to the DB, <code>false</code> to
|
|
|
191 |
* always change the orders in the DB.
|
|
|
192 |
* @return if the DB was changed.
|
|
|
193 |
* @throws SQLException if an error occurs.
|
|
|
194 |
*/
|
|
|
195 |
static public boolean moveIDsAtOnce(final List<? extends Number> ids, final boolean after, final SQLRow destRow, final boolean checkBefore) throws SQLException {
|
|
|
196 |
final int size = ids.size();
|
|
|
197 |
if (checkBefore) {
|
|
|
198 |
final SQLTable table = destRow.getTable();
|
|
|
199 |
final SQLSelect sel = new SQLSelect(true);
|
|
|
200 |
sel.addSelect(table.getKey());
|
|
|
201 |
sel.addOrder(table);
|
|
|
202 |
sel.setWhere(new Where(table.getKey(), ids));
|
|
|
203 |
// needed since we might update them just after in moveAtOnce(), and it's safer to lock
|
|
|
204 |
// with the most restrictive mode first.
|
|
|
205 |
sel.setLockStrength(LockStrength.UPDATE);
|
|
|
206 |
final List<?> dbOrderedIDs = table.getDBSystemRoot().getDataSource().executeCol(sel.asString());
|
|
|
207 |
if (dbOrderedIDs.size() != size)
|
|
|
208 |
throw new IllegalStateException("Missing rows");
|
|
|
209 |
boolean orderOK = true;
|
|
|
210 |
for (int i = 0; i < size && orderOK; i++) {
|
|
|
211 |
final Number passedID = ids.get(i);
|
|
|
212 |
final Number dbID = (Number) dbOrderedIDs.get(i);
|
|
|
213 |
orderOK = NumberUtils.areNumericallyEqual(passedID, dbID);
|
|
|
214 |
}
|
|
|
215 |
if (orderOK)
|
|
|
216 |
return false;
|
|
|
217 |
}
|
|
|
218 |
moveAtOnce(ids, size, after, destRow);
|
|
|
219 |
return true;
|
|
|
220 |
}
|
|
|
221 |
|
93 |
ilm |
222 |
static private void moveAtOnce(final List<?> srcRows, final int rowCount, final boolean after, final SQLRow destRow) throws SQLException {
|
73 |
ilm |
223 |
if (rowCount == 0)
|
|
|
224 |
return;
|
93 |
ilm |
225 |
final SQLTable t = destRow.getTable();
|
73 |
ilm |
226 |
|
156 |
ilm |
227 |
final List<BigDecimal> freeOrderValues = ReOrder.getFreeOrderValuesFor(rowCount, after, destRow).get0();
|
73 |
ilm |
228 |
|
|
|
229 |
final List<List<String>> newOrdersAndIDs = new ArrayList<List<String>>(rowCount);
|
156 |
ilm |
230 |
int i = 0;
|
|
|
231 |
final List<Number> ids = rowCount < 10 ? new ArrayList<Number>(rowCount) : null;
|
93 |
ilm |
232 |
// we go from newOrder and up, so that the passed rows are in ascending order
|
|
|
233 |
for (final Object src : srcRows) {
|
|
|
234 |
final Number srcID = getID(src, t);
|
156 |
ilm |
235 |
if (ids != null)
|
|
|
236 |
ids.add(srcID);
|
|
|
237 |
final BigDecimal newOrder = freeOrderValues.get(i++);
|
93 |
ilm |
238 |
newOrdersAndIDs.add(Arrays.asList(srcID.toString(), newOrder.toPlainString()));
|
73 |
ilm |
239 |
}
|
|
|
240 |
|
|
|
241 |
final SQLSyntax syntax = SQLSyntax.get(t);
|
|
|
242 |
final UpdateBuilder update = new UpdateBuilder(t);
|
|
|
243 |
final String constantTableAlias = "newOrdersAndIDs";
|
|
|
244 |
update.addVirtualJoin(syntax.getConstantTable(newOrdersAndIDs, constantTableAlias, Arrays.asList("ID", "newOrder")), constantTableAlias, true, "ID", t.getKey().getName());
|
|
|
245 |
update.setFromVirtualJoinField(t.getOrderField().getName(), constantTableAlias, "newOrder");
|
|
|
246 |
t.getDBSystemRoot().getDataSource().execute(update.asString());
|
|
|
247 |
|
156 |
ilm |
248 |
final List<String> fieldsChanged = Collections.singletonList(t.getOrderField().getName());
|
|
|
249 |
if (ids == null) {
|
|
|
250 |
t.fireTableModified(SQLRow.NONEXISTANT_ID, fieldsChanged);
|
|
|
251 |
} else {
|
|
|
252 |
for (final Number id : ids)
|
|
|
253 |
t.fireTableModified(id.intValue(), fieldsChanged);
|
|
|
254 |
}
|
73 |
ilm |
255 |
}
|
17 |
ilm |
256 |
}
|