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.view.list;
|
|
|
15 |
|
|
|
16 |
import org.openconcerto.sql.model.FieldPath;
|
73 |
ilm |
17 |
import org.openconcerto.sql.model.SQLField;
|
17 |
ilm |
18 |
import org.openconcerto.sql.model.SQLRowValues;
|
73 |
ilm |
19 |
import org.openconcerto.sql.model.SQLRowValues.CreateMode;
|
17 |
ilm |
20 |
import org.openconcerto.sql.model.SQLTable;
|
|
|
21 |
import org.openconcerto.sql.model.graph.Path;
|
73 |
ilm |
22 |
import org.openconcerto.sql.view.list.search.SearchQueue;
|
17 |
ilm |
23 |
import org.openconcerto.utils.CollectionUtils;
|
|
|
24 |
|
|
|
25 |
import java.util.ArrayList;
|
|
|
26 |
import java.util.Collection;
|
|
|
27 |
import java.util.Collections;
|
|
|
28 |
import java.util.HashSet;
|
|
|
29 |
import java.util.List;
|
|
|
30 |
import java.util.Set;
|
|
|
31 |
|
|
|
32 |
/**
|
|
|
33 |
* A line used by SQLTableModelSource, posessing an order and an id. Compare is done on the order.
|
|
|
34 |
*
|
|
|
35 |
* @author Sylvain
|
|
|
36 |
*/
|
|
|
37 |
public final class ListSQLLine implements Comparable<ListSQLLine> {
|
|
|
38 |
|
|
|
39 |
public static final int indexFromID(final List<ListSQLLine> l, final int id) {
|
|
|
40 |
int foundIndex = -1;
|
|
|
41 |
final int size = l.size();
|
|
|
42 |
for (int i = 0; i < size; i++) {
|
|
|
43 |
final int currentID = l.get(i).getID();
|
|
|
44 |
if (currentID == id) {
|
|
|
45 |
foundIndex = i;
|
|
|
46 |
break;
|
|
|
47 |
}
|
|
|
48 |
}
|
|
|
49 |
return foundIndex;
|
|
|
50 |
}
|
|
|
51 |
|
|
|
52 |
private final SQLTableModelLinesSource src;
|
|
|
53 |
private final SQLRowValues row;
|
|
|
54 |
private final int id;
|
|
|
55 |
// lists are accessed by Swing (model.getValueAt()) and
|
|
|
56 |
// by the search queue (SearchRunnable#matchFilter(ListSQLLine line))
|
|
|
57 |
private final List<Object> list;
|
|
|
58 |
// count of column values loaded in this.list
|
|
|
59 |
// (to avoid loading debug columns, which took more time than the regular columns, ie more than
|
|
|
60 |
// half the time was passed on almost never displayed values)
|
|
|
61 |
private int loadedCol;
|
|
|
62 |
private final List<Object> pubList;
|
|
|
63 |
|
|
|
64 |
public ListSQLLine(SQLTableModelLinesSource src, SQLRowValues row, int id) {
|
|
|
65 |
super();
|
|
|
66 |
this.src = src;
|
|
|
67 |
this.row = row;
|
|
|
68 |
this.id = id;
|
|
|
69 |
this.list = new ArrayList<Object>();
|
|
|
70 |
this.pubList = Collections.unmodifiableList(this.list);
|
|
|
71 |
this.loadedCol = 0;
|
|
|
72 |
}
|
|
|
73 |
|
|
|
74 |
// load at least columnCount values
|
|
|
75 |
private synchronized void loadCache(int columnCount) {
|
|
|
76 |
if (this.loadedCol >= columnCount)
|
|
|
77 |
return;
|
|
|
78 |
|
|
|
79 |
try {
|
|
|
80 |
final List<SQLTableModelColumn> allCols = this.src.getParent().getAllColumns();
|
|
|
81 |
for (int i = this.loadedCol; i < columnCount; i++)
|
|
|
82 |
this.list.add(allCols.get(i).show(this.row));
|
|
|
83 |
this.loadedCol = columnCount;
|
|
|
84 |
} catch (RuntimeException e) {
|
|
|
85 |
// the list length must be equal to the column count
|
|
|
86 |
// if we're interrupted, come back to a safe state
|
|
|
87 |
this.list.clear();
|
|
|
88 |
this.loadedCol = 0;
|
|
|
89 |
throw e;
|
|
|
90 |
}
|
|
|
91 |
}
|
|
|
92 |
|
|
|
93 |
public final SQLTableModelLinesSource getSrc() {
|
|
|
94 |
return this.src;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public final SQLRowValues getRow() {
|
|
|
98 |
return this.row;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
@Override
|
|
|
102 |
public int compareTo(ListSQLLine o) {
|
|
|
103 |
if (this.src != o.src)
|
|
|
104 |
throw new IllegalArgumentException(this.src + " != " + o.src);
|
|
|
105 |
return this.src.compare(this, o);
|
|
|
106 |
}
|
|
|
107 |
|
|
|
108 |
public int getID() {
|
|
|
109 |
return this.id;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
public synchronized List<Object> getList(int columnCount) {
|
|
|
113 |
this.loadCache(columnCount);
|
|
|
114 |
return this.pubList;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public final void setValueAt(Object obj, int colIndex) {
|
|
|
118 |
this.src.getParent().getColumn(colIndex).put(this, obj);
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public final void updateValueAt(Set<Integer> colIndexes) {
|
80 |
ilm |
122 |
if (colIndexes.size() == 0)
|
|
|
123 |
return;
|
|
|
124 |
final int max = Collections.max(colIndexes).intValue();
|
17 |
ilm |
125 |
synchronized (this) {
|
|
|
126 |
final int alreadyLoaded = this.loadedCol;
|
|
|
127 |
this.loadCache(max);
|
|
|
128 |
for (final int colIndex : colIndexes) {
|
|
|
129 |
// no need to update twice colIndex
|
|
|
130 |
if (colIndex < alreadyLoaded)
|
|
|
131 |
// MAYBE first iterate to fetch the new values and then merge them to list,
|
|
|
132 |
// otherwise if there's an exn list will be half updated
|
|
|
133 |
this.list.set(colIndex, this.src.getParent().getColumn(colIndex).show(this.getRow()));
|
|
|
134 |
}
|
|
|
135 |
}
|
|
|
136 |
this.src.fireLineChanged(this.getID(), this, colIndexes);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
public void clearCache() {
|
|
|
140 |
synchronized (this) {
|
|
|
141 |
this.list.clear();
|
|
|
142 |
this.loadedCol = 0;
|
|
|
143 |
}
|
|
|
144 |
this.src.fireLineChanged(this.getID(), this, null);
|
|
|
145 |
}
|
|
|
146 |
|
|
|
147 |
/**
|
|
|
148 |
* Load the passed values into this row at the passed path.
|
|
|
149 |
*
|
73 |
ilm |
150 |
* @param id ID of vals, needed when vals is <code>null</code>.
|
17 |
ilm |
151 |
* @param vals values to load, eg CONTACT.NOM = "Dupont".
|
|
|
152 |
* @param p where to load the values, eg "SITE.ID_CONTACT_CHEF".
|
|
|
153 |
*/
|
73 |
ilm |
154 |
void loadAt(int id, SQLRowValues vals, Path p) {
|
|
|
155 |
final String lastReferentField = SearchQueue.getLastReferentField(p);
|
17 |
ilm |
156 |
// load() empties vals, so getFields() before
|
73 |
ilm |
157 |
final Set<Integer> indexes = lastReferentField == null ? this.pathToIndex(p, vals.getFields()) : null;
|
17 |
ilm |
158 |
// replace our values with the new ones
|
73 |
ilm |
159 |
if (lastReferentField == null) {
|
|
|
160 |
for (final SQLRowValues v : this.getRow().followPath(p, CreateMode.CREATE_NONE, false)) {
|
|
|
161 |
v.load(vals.deepCopy(), null);
|
|
|
162 |
}
|
|
|
163 |
} else {
|
|
|
164 |
// e.g. if p is SITE <- BATIMENT <- LOCAL, lastField is LOCAL.ID_BATIMENT
|
|
|
165 |
// if p is SITE -> CLIENT <- SITE (i.e. siblings of a site), lastField is SITE.ID_CLIENT
|
|
|
166 |
final SQLField lastField = p.getStep(-1).getSingleField();
|
|
|
167 |
final Collection<SQLRowValues> previous;
|
|
|
168 |
if (p.length() > 1 && p.getStep(-2).reverse().equals(p.getStep(-1)))
|
|
|
169 |
previous = this.getRow().followPath(p.minusLast(2), CreateMode.CREATE_NONE, false);
|
|
|
170 |
else
|
|
|
171 |
previous = null;
|
|
|
172 |
// the rows that vals should point to, e.g. BATIMENT or CLIENT
|
|
|
173 |
final Collection<SQLRowValues> targets = this.getRow().followPath(p.minusLast(), CreateMode.CREATE_NONE, false);
|
|
|
174 |
for (final SQLRowValues target : targets) {
|
|
|
175 |
// remove existing referent with the updated ID
|
|
|
176 |
SQLRowValues toRemove = null;
|
|
|
177 |
for (final SQLRowValues toUpdate : target.getReferentRows(lastField)) {
|
|
|
178 |
// don't back track (in the example a given SITE will be at the primary location
|
|
|
179 |
// and a second time along its siblings)
|
|
|
180 |
if ((previous == null || !previous.contains(toUpdate)) && toUpdate.getID() == id) {
|
|
|
181 |
if (toRemove != null)
|
|
|
182 |
throw new IllegalStateException("Duplicate IDs " + id + " : " + System.identityHashCode(toRemove) + " and " + System.identityHashCode(toUpdate) + "\n"
|
|
|
183 |
+ this.getRow().printGraph());
|
|
|
184 |
toRemove = toUpdate;
|
|
|
185 |
}
|
|
|
186 |
}
|
|
|
187 |
if (toRemove != null)
|
|
|
188 |
toRemove.remove(lastField.getName());
|
|
|
189 |
// attach updated values
|
|
|
190 |
if (vals != null && vals.getLong(lastField.getName()) == target.getIDNumber().longValue())
|
|
|
191 |
vals.deepCopy().put(lastField.getName(), target);
|
|
|
192 |
}
|
|
|
193 |
}
|
17 |
ilm |
194 |
// update our cache
|
|
|
195 |
if (indexes == null)
|
|
|
196 |
this.clearCache();
|
|
|
197 |
else
|
|
|
198 |
this.updateValueAt(indexes);
|
|
|
199 |
}
|
|
|
200 |
|
|
|
201 |
/**
|
|
|
202 |
* Find the columns that use the modifiedFields for their value.
|
|
|
203 |
*
|
|
|
204 |
* @param p the path to the modified fields, eg "CPI.ID_LOCAL".
|
|
|
205 |
* @param modifiedFields the field modified, eg "DESIGNATION".
|
|
|
206 |
* @return the index of columns using "CPI.ID_LOCAL.DESIGNATION", or null for every columns.
|
|
|
207 |
*/
|
|
|
208 |
private Set<Integer> pathToIndex(final Path p, final Collection<String> modifiedFields) {
|
73 |
ilm |
209 |
if (containsFK(p.getLast(), modifiedFields)) {
|
|
|
210 |
// e.g. CPI.ID_LOCAL, easier to just refresh the whole line, than to search for each
|
|
|
211 |
// column affected (that would mean expanding the FK)
|
17 |
ilm |
212 |
return null;
|
73 |
ilm |
213 |
} else {
|
17 |
ilm |
214 |
final Set<Integer> res = new HashSet<Integer>();
|
|
|
215 |
final Set<FieldPath> modifiedPaths = FieldPath.create(p, modifiedFields);
|
|
|
216 |
final List<? extends SQLTableModelColumn> cols = this.src.getParent().getAllColumns();
|
|
|
217 |
for (int i = 0; i < cols.size(); i++) {
|
|
|
218 |
final SQLTableModelColumn col = cols.get(i);
|
|
|
219 |
if (CollectionUtils.containsAny(col.getPaths(), modifiedPaths))
|
|
|
220 |
res.add(i);
|
|
|
221 |
}
|
|
|
222 |
return res;
|
|
|
223 |
}
|
|
|
224 |
}
|
|
|
225 |
|
|
|
226 |
private static boolean containsFK(final SQLTable t, Collection<String> fields) {
|
|
|
227 |
for (final String f : fields) {
|
|
|
228 |
if (t.getForeignKeys().contains(t.getField(f)))
|
|
|
229 |
return true;
|
|
|
230 |
}
|
|
|
231 |
return false;
|
|
|
232 |
}
|
|
|
233 |
|
|
|
234 |
@Override
|
|
|
235 |
public String toString() {
|
|
|
236 |
return this.getClass().getSimpleName() + " on " + this.row;
|
|
|
237 |
}
|
|
|
238 |
}
|