root / branches / v10 / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / EditableAdapter.java @ 8913
History | View | Annotate | Download (45.8 KB)
1 |
package com.iver.cit.gvsig.fmap.edition; |
---|---|
2 |
|
3 |
import java.io.IOException; |
4 |
import java.util.ArrayList; |
5 |
import java.util.Collection; |
6 |
import java.util.HashMap; |
7 |
import java.util.Iterator; |
8 |
import java.util.TreeMap; |
9 |
|
10 |
import com.hardcode.driverManager.Driver; |
11 |
import com.hardcode.driverManager.DriverLoadException; |
12 |
import com.hardcode.gdbms.engine.data.DataSourceFactory; |
13 |
import com.hardcode.gdbms.engine.data.NoSuchTableException; |
14 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
15 |
import com.hardcode.gdbms.engine.data.driver.ObjectDriver; |
16 |
import com.hardcode.gdbms.engine.data.edition.DataWare; |
17 |
import com.hardcode.gdbms.engine.values.Value; |
18 |
import com.iver.cit.gvsig.fmap.core.DefaultRow; |
19 |
import com.iver.cit.gvsig.fmap.core.IRow; |
20 |
import com.iver.cit.gvsig.fmap.drivers.DriverIOException; |
21 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
22 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
23 |
import com.iver.cit.gvsig.fmap.drivers.TableDefinition; |
24 |
import com.iver.cit.gvsig.fmap.edition.commands.AddFieldCommand; |
25 |
import com.iver.cit.gvsig.fmap.edition.commands.AddRowCommand; |
26 |
import com.iver.cit.gvsig.fmap.edition.commands.Command; |
27 |
import com.iver.cit.gvsig.fmap.edition.commands.CommandCollection; |
28 |
import com.iver.cit.gvsig.fmap.edition.commands.CommandRecord; |
29 |
import com.iver.cit.gvsig.fmap.edition.commands.MemoryCommandRecord; |
30 |
import com.iver.cit.gvsig.fmap.edition.commands.ModifyRowCommand; |
31 |
import com.iver.cit.gvsig.fmap.edition.commands.RemoveFieldCommand; |
32 |
import com.iver.cit.gvsig.fmap.edition.commands.RemoveRowCommand; |
33 |
import com.iver.cit.gvsig.fmap.edition.commands.RenameFieldCommand; |
34 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.AbstractFieldManager; |
35 |
import com.iver.cit.gvsig.fmap.edition.rules.IRule; |
36 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
37 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
38 |
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource; |
39 |
import com.iver.cit.gvsig.fmap.operations.Cancel; |
40 |
|
41 |
/**
|
42 |
* DOCUMENT ME!
|
43 |
*
|
44 |
* @author Vicente Caballero Navarro
|
45 |
*/
|
46 |
public class EditableAdapter implements IEditableSource, IWriteable { |
47 |
protected boolean isEditing = false; |
48 |
|
49 |
private SelectableDataSource ds = null; |
50 |
|
51 |
protected FBitSet delRows = new FBitSet(); |
52 |
|
53 |
private CommandRecord cr;
|
54 |
|
55 |
protected IWriter writer;
|
56 |
|
57 |
/**
|
58 |
* Flag que indica que hay que tomar las siguientes operaciones como una
|
59 |
* operaci?n at?mica
|
60 |
*/
|
61 |
private boolean complex = false; |
62 |
|
63 |
private CommandCollection commands = null; |
64 |
|
65 |
protected ArrayList listFields = new ArrayList(); |
66 |
|
67 |
protected ArrayList listInternalFields = new ArrayList(); |
68 |
|
69 |
protected boolean bFieldsHasBeenChanged = false; |
70 |
|
71 |
/**
|
72 |
* La clave ser? el fieldId. Para buscar si un value de una row ha de ser
|
73 |
* rellenado con defaultValue o con lo que venga del expansion file,
|
74 |
* miraremos si existe en este hash. Si existe, usamos el value del
|
75 |
* expansion file. Si no existe, usamos el defaultValue del campo busc?ndolo
|
76 |
* en la lista internalFields. Por cierto, en listInternalFields NO se
|
77 |
* borran campos. Solo se van a?adiendo nuevos actualFields.
|
78 |
*/
|
79 |
protected TreeMap actualFields; // la clave ser? el fieldId. |
80 |
|
81 |
protected ArrayList fastAccessFields = new ArrayList(); |
82 |
|
83 |
protected class MyFieldManager extends AbstractFieldManager { |
84 |
|
85 |
public boolean alterTable() throws EditionException { |
86 |
return getFieldManager().alterTable();
|
87 |
} |
88 |
|
89 |
public void addField(FieldDescription fieldDesc) { |
90 |
super.addField(fieldDesc);
|
91 |
} |
92 |
|
93 |
public FieldDescription removeField(String fieldName) { |
94 |
// TODO Auto-generated method stub
|
95 |
return super.removeField(fieldName); |
96 |
} |
97 |
|
98 |
public void renameField(String antName, String newName) { |
99 |
// TODO Auto-generated method stub
|
100 |
super.renameField(antName, newName);
|
101 |
} |
102 |
|
103 |
} |
104 |
|
105 |
/*
|
106 |
* Establece una relaci?n entre los ?ndices de las geometr?as en el
|
107 |
* EditableFeatureSource y los ?ndices en el fichero de expansi?n FJP:
|
108 |
* CAMBIO: NECESITAMOS TRABAJAR CON FEATURE Y FEATUREITERATOR PARA IR
|
109 |
* PREPARANDO EL CAMINO, GUARDAMOS EL FEATUREID (STRING) COMO CLAVE, Y COMO
|
110 |
* VALOR, EL INDICE DENTRO DEL FICHERO DE EXPANSION (Integer). Lo de que
|
111 |
* FeatureId sea un String es por compatibilidad con OGC. Seg?n OGC, una
|
112 |
* Feature tiene que tener un Id string En el caso de los randomaccess,
|
113 |
* ser?n el id de registro En los casos de base de datos espaciales, supongo
|
114 |
* que siempre ser? num?rico tambi?n, pero lo tendremos que convertir a
|
115 |
* string. Lo que est? claro es que NO se puede confiar nunca en que sea
|
116 |
* algo correlativo (1, 2, 3, 4, 5, ... => FALSO!!)
|
117 |
*/
|
118 |
protected HashMap relations = new HashMap(); |
119 |
|
120 |
/*
|
121 |
* Fichero en el que se guardan las nuevas geometr?as, producto de adiciones
|
122 |
* o de modificaciones
|
123 |
*/
|
124 |
protected ExpansionFile expansionFile;
|
125 |
|
126 |
protected int numAdd = 0; |
127 |
|
128 |
private ObjectDriver editingDriver = new myObjectDriver(); |
129 |
|
130 |
private SelectableDataSource ods;
|
131 |
|
132 |
private ArrayList editionListeners = new ArrayList(); |
133 |
|
134 |
private ArrayList rules = new ArrayList(); |
135 |
|
136 |
protected int actualIndexFields; |
137 |
|
138 |
protected boolean isFullExtentDirty = false; |
139 |
|
140 |
/**
|
141 |
* Crea un nuevo EditableAdapter.
|
142 |
*/
|
143 |
public EditableAdapter() {
|
144 |
expansionFile = new MemoryExpansionFile(this); |
145 |
cr = new MemoryCommandRecord();
|
146 |
} |
147 |
|
148 |
/**
|
149 |
* DOCUMENT ME!
|
150 |
*
|
151 |
* @param ds
|
152 |
* DOCUMENT ME!
|
153 |
* @throws DriverException
|
154 |
*/
|
155 |
public void setOriginalDataSource(SelectableDataSource ds) throws DriverException { |
156 |
this.ods = ds;
|
157 |
initalizeFields(ds); |
158 |
Driver drv = ods.getDriver();
|
159 |
if (drv instanceof IWriteable) { |
160 |
setWriter(((IWriteable) drv).getWriter()); |
161 |
} |
162 |
|
163 |
|
164 |
} |
165 |
|
166 |
/**
|
167 |
* @param ds
|
168 |
* @throws DriverException
|
169 |
*/
|
170 |
private void initalizeFields(SelectableDataSource ds) throws DriverException { |
171 |
FieldDescription[] fields = ds.getFieldsDescription();
|
172 |
listInternalFields.clear(); |
173 |
actualIndexFields = 0;
|
174 |
actualFields = new TreeMap(); |
175 |
// fastAccessFields = new ArrayList();
|
176 |
for (int i=0; i < fields.length; i++) |
177 |
{ |
178 |
InternalField field = new InternalField(fields[i], InternalField.ORIGINAL, new Integer(i)); |
179 |
listFields.add(field); |
180 |
// field.setFieldIndex(i);
|
181 |
actualFields.put(field.getFieldId(), field); |
182 |
// fastAccessFields.add(fields[i]);
|
183 |
System.out.println("INITIALIZEFIELDS: FIELD " + field.getFieldDesc().getFieldAlias()); |
184 |
} |
185 |
try {
|
186 |
fieldsChanged(); |
187 |
bFieldsHasBeenChanged = false;
|
188 |
} catch (EditionException e) {
|
189 |
e.printStackTrace(); |
190 |
throw new DriverException(e); |
191 |
} |
192 |
} |
193 |
|
194 |
private TreeMap deepCloneInternalFields(TreeMap col) |
195 |
{ |
196 |
TreeMap clonedFields = new TreeMap(); |
197 |
for (Iterator iter = col.values().iterator(); iter.hasNext();) { |
198 |
InternalField fld = (InternalField) iter.next(); |
199 |
InternalField clonedField = fld.cloneInternalField(); |
200 |
clonedFields.put(clonedField.getFieldId(), clonedField); |
201 |
} |
202 |
|
203 |
return clonedFields;
|
204 |
} |
205 |
private void fieldsChanged() throws EditionException { |
206 |
fastAccessFields= new ArrayList(); |
207 |
int index = 0; |
208 |
for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) { |
209 |
InternalField fld = (InternalField) iter.next(); |
210 |
fastAccessFields.add(fld.getFieldDesc()); |
211 |
fld.setFieldIndex(index++); |
212 |
} |
213 |
|
214 |
listInternalFields.add(deepCloneInternalFields(actualFields)); |
215 |
actualIndexFields = listInternalFields.size()-1;
|
216 |
try {
|
217 |
ds = null;
|
218 |
getRecordset().mapExternalFields(); |
219 |
bFieldsHasBeenChanged = true;
|
220 |
} catch (DriverLoadException e) {
|
221 |
e.printStackTrace(); |
222 |
throw new EditionException(e); |
223 |
} catch (DriverException e) {
|
224 |
e.printStackTrace(); |
225 |
throw new EditionException(e); |
226 |
} |
227 |
} |
228 |
|
229 |
/**
|
230 |
* DOCUMENT ME!
|
231 |
*
|
232 |
* @throws EditionException
|
233 |
* DOCUMENT ME!
|
234 |
*/
|
235 |
public void startEdition(int sourceType) throws EditionException { |
236 |
isEditing = true;
|
237 |
|
238 |
fireStartEditionEvent(sourceType); |
239 |
} |
240 |
|
241 |
/**
|
242 |
* Se ejecuta preProcess() del IWriter, luego se itera por los registros
|
243 |
* borrados por si el IWriter los quiere borrar (solo ser? necesario cuando
|
244 |
* escribimos sobre la misma tabla) y luego se itera por los nuevos
|
245 |
* registros llamando a process con el registro correcto. (A?adidos,
|
246 |
* modificados). Para finalizar, se ejecuta PostProcess
|
247 |
*
|
248 |
* @param writer
|
249 |
* IWriter que recibir? las llamadas.
|
250 |
*
|
251 |
* @throws EditionException
|
252 |
* DOCUMENT ME!
|
253 |
*
|
254 |
*/
|
255 |
public void stopEdition(IWriter writer, int sourceType) |
256 |
throws EditionException {
|
257 |
saveEdits(writer, sourceType); |
258 |
isEditing = false;
|
259 |
cr.clearAll(); |
260 |
fireStopEditionEvent(sourceType); |
261 |
} |
262 |
|
263 |
public void saveEdits(IWriter writer, int sourceType) |
264 |
throws EditionException {
|
265 |
|
266 |
// TODO: ARREGLAR ESTO PARA QUE CUANDO HA HABIDO CAMBIOS
|
267 |
// EN LOS CAMPOS, PODAMOS CAMBIAR LO QUE TOQUE (A SER POSIBLE
|
268 |
// SIN TENER QUE REESCRIBIR TODA LA TABLA CON POSTGIS)
|
269 |
if (bFieldsHasBeenChanged)
|
270 |
{ |
271 |
// Para cada campo de los originales, miramos si no est? en
|
272 |
// los actuales. Si no est?, le decimos al fieldManager
|
273 |
// que lo borre. Si est?, pero le hemos cambiado el nombre
|
274 |
// le pedimos al fieldManager que le cambie el nombre.
|
275 |
// Luego recorremos los campos actuales para ver cuales
|
276 |
// son nuevos, y los a?adimos.
|
277 |
|
278 |
TreeMap ancientFields = (TreeMap) listInternalFields |
279 |
.get(0);
|
280 |
Collection aux = ancientFields.values();
|
281 |
Iterator it = aux.iterator();
|
282 |
while (it.hasNext()) {
|
283 |
InternalField fld = (InternalField) it.next(); |
284 |
// System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() + " id=" + fld.getFieldId());
|
285 |
if (actualFields.containsKey(fld.getFieldId())) {
|
286 |
// Es un original
|
287 |
String f1 = fld.getFieldDesc().getFieldName();
|
288 |
String f2 = fld.getFieldDesc().getFieldAlias();
|
289 |
if (f1.compareTo(f2) != 0) |
290 |
{ |
291 |
getFieldManager().renameField(f1, f2); |
292 |
} |
293 |
} |
294 |
else
|
295 |
{ // No est?, hay que borrarlo
|
296 |
getFieldManager().removeField(fld.getFieldDesc().getFieldAlias()); |
297 |
} |
298 |
} |
299 |
Collection aux2= actualFields.values();
|
300 |
Iterator it2 = aux2.iterator();
|
301 |
while (it2.hasNext()) {
|
302 |
InternalField fld = (InternalField) it2.next(); |
303 |
// System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() + " id=" + fld.getFieldId());
|
304 |
if (!ancientFields.containsKey(fld.getFieldId())) {
|
305 |
// Es uno a?adido
|
306 |
getFieldManager().addField(fld.getFieldDesc()); |
307 |
} |
308 |
} |
309 |
// getFieldManager().alterTable(); // Se llama dentro del preprocess()
|
310 |
} |
311 |
|
312 |
writer.preProcess(); |
313 |
|
314 |
try {
|
315 |
|
316 |
// Procesamos primero los borrados.
|
317 |
// Cuando se genere un tema nuevo, no se les debe hacer caso
|
318 |
// a estos registros
|
319 |
|
320 |
for (int i = delRows.nextSetBit(0); i >= 0; i = delRows |
321 |
.nextSetBit(i + 1)) {
|
322 |
int calculatedIndex = i;
|
323 |
Integer integer = new Integer(calculatedIndex); |
324 |
// Si no est? en el fichero de expansi?n, es de los originales
|
325 |
// y hay que borrarlo
|
326 |
DefaultRowEdited edRow = null;
|
327 |
if (!relations.containsKey(integer)) {
|
328 |
edRow = new DefaultRowEdited(new DefaultRow(ods |
329 |
.getRow(calculatedIndex)), |
330 |
DefaultRowEdited.STATUS_DELETED, calculatedIndex); |
331 |
writer.process(edRow); |
332 |
} else {
|
333 |
int num = ((Integer) relations.get(integer)).intValue(); |
334 |
|
335 |
// return expansionFile.getRow(num);
|
336 |
IRowEdited rowFromExpansion = expansionFile.getRow(num); |
337 |
// ?Habr?a que hacer aqu? setID(index + "")?
|
338 |
edRow = new DefaultRowEdited(rowFromExpansion
|
339 |
.getLinkedRow().cloneRow(), |
340 |
DefaultRowEdited.STATUS_DELETED, calculatedIndex); |
341 |
writer.process(edRow); |
342 |
} |
343 |
|
344 |
} |
345 |
|
346 |
int rowCount = getRowCount();
|
347 |
if (writer.isWriteAll())
|
348 |
{ |
349 |
for (int i = 0; i < rowCount; i++) { |
350 |
IRowEdited rowEdited = getRow(i); |
351 |
|
352 |
if (rowEdited != null) { |
353 |
writer.process(rowEdited); |
354 |
} |
355 |
} |
356 |
} |
357 |
else
|
358 |
{ |
359 |
// Escribimos solo aquellos registros que han cambiado
|
360 |
for (int i = 0; i < rowCount; i++) { |
361 |
int calculatedIndex = getCalculatedIndex(i);
|
362 |
Integer integer = new Integer(calculatedIndex); |
363 |
DefaultRowEdited edRow = null;
|
364 |
// Si est? en el fichero de expansi?n hay que modificar
|
365 |
if (relations.containsKey(integer)) {
|
366 |
int num = ((Integer) relations.get(integer)).intValue(); |
367 |
|
368 |
// return expansionFile.getRow(num);
|
369 |
// ExpansionFile ya entrega el registro formateado como debe
|
370 |
IRowEdited rowFromExpansion = expansionFile.getRow(num); |
371 |
// ?Habr?a que hacer aqu? setID(index + "")?
|
372 |
edRow = new DefaultRowEdited(rowFromExpansion.getLinkedRow()
|
373 |
.cloneRow(), rowFromExpansion.getStatus(), i); |
374 |
writer.process(edRow); |
375 |
} |
376 |
} |
377 |
} |
378 |
writer.postProcess(); |
379 |
|
380 |
// Hacemos que el escritor se entere de los nuevos campos
|
381 |
// que tiene. El escritor debe guardar una referencia
|
382 |
// a los campos con los que trabaja el drier, de forma
|
383 |
// que al recargar la capa (por ejemplo en el caso de
|
384 |
// PostGIS, se haga un setData con los campos que se hayan
|
385 |
// a?adido, borrado o renombrado).
|
386 |
writer.getTableDefinition().setFieldsDesc(getRecordset().getFieldsDescription()); |
387 |
|
388 |
ods.reload(); |
389 |
ds = null;
|
390 |
clean(); |
391 |
|
392 |
} catch (DriverIOException e) {
|
393 |
e.printStackTrace(); |
394 |
throw new EditionException(e); |
395 |
} catch (IOException e) { |
396 |
e.printStackTrace(); |
397 |
throw new EditionException(e); |
398 |
} catch (DriverException e) {
|
399 |
e.printStackTrace(); |
400 |
throw new EditionException(e); |
401 |
} |
402 |
|
403 |
} |
404 |
|
405 |
/**
|
406 |
* DOCUMENT ME!
|
407 |
*
|
408 |
* @throws IOException
|
409 |
* DOCUMENT ME!
|
410 |
*/
|
411 |
public void cancelEdition(int sourceType) throws IOException { |
412 |
isEditing = false;
|
413 |
try {
|
414 |
ds= null;
|
415 |
clean(); |
416 |
cr.clearAll(); |
417 |
} catch (DriverException e) {
|
418 |
e.printStackTrace(); |
419 |
throw new IOException("Error: " + e.getMessage()); |
420 |
} |
421 |
fireCancelEditionEvent(sourceType); |
422 |
} |
423 |
|
424 |
/*
|
425 |
* (non-Javadoc)
|
426 |
*
|
427 |
* @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getRow(int)
|
428 |
*/
|
429 |
public IRowEdited getRow(int index) throws DriverIOException, IOException { |
430 |
int calculatedIndex = getCalculatedIndex(index);
|
431 |
Integer integer = new Integer(calculatedIndex); |
432 |
DefaultRowEdited edRow = null;
|
433 |
// Si no est? en el fichero de expansi?n
|
434 |
if (!relations.containsKey(integer)) {
|
435 |
try {
|
436 |
/*
|
437 |
* edRow = new DefaultRowEdited(new
|
438 |
* DefaultRow(ods.getRow(calculatedIndex), "" + index),
|
439 |
* DefaultRowEdited.STATUS_ORIGINAL, index);
|
440 |
*/
|
441 |
DefaultRow auxR = new DefaultRow(ods.getRow(calculatedIndex));
|
442 |
edRow = new DefaultRowEdited(auxR,
|
443 |
DefaultRowEdited.STATUS_ORIGINAL, index); |
444 |
return createExternalRow(edRow, 0); |
445 |
// edRow = new DefaultRowEdited(new DefaultRow(ods
|
446 |
// .getRow(calculatedIndex)),
|
447 |
// DefaultRowEdited.STATUS_ORIGINAL, index);
|
448 |
} catch (DriverException e) {
|
449 |
throw new DriverIOException(e); |
450 |
} |
451 |
} else {
|
452 |
int num = ((Integer) relations.get(integer)).intValue(); |
453 |
|
454 |
// return expansionFile.getRow(num);
|
455 |
// ExpansionFile ya entrega el registro formateado como debe
|
456 |
IRowEdited rowFromExpansion = expansionFile.getRow(num); |
457 |
// ?Habr?a que hacer aqu? setID(index + "")?
|
458 |
edRow = new DefaultRowEdited(rowFromExpansion.getLinkedRow()
|
459 |
.cloneRow(), rowFromExpansion.getStatus(), index); |
460 |
return edRow;
|
461 |
} |
462 |
|
463 |
|
464 |
|
465 |
} |
466 |
|
467 |
/**
|
468 |
* DOCUMENT ME!
|
469 |
*
|
470 |
* @return DOCUMENT ME!
|
471 |
*
|
472 |
* @throws DriverIOException
|
473 |
* DOCUMENT ME!
|
474 |
* @throws IOException
|
475 |
* DOCUMENT ME!
|
476 |
*/
|
477 |
public int getRowCount() throws DriverIOException, IOException { |
478 |
try {
|
479 |
return (int) (ods.getRowCount() + numAdd) - delRows.cardinality();// - |
480 |
// expansionFile.getInvalidRows().cardinality();
|
481 |
} catch (DriverException e) {
|
482 |
throw new DriverIOException(e); |
483 |
} |
484 |
|
485 |
} |
486 |
|
487 |
/*
|
488 |
* (non-Javadoc)
|
489 |
*
|
490 |
* @see com.iver.cit.gvsig.fmap.edition.IEditableSource#addRow(com.iver.cit.gvsig.fmap.core.IRow,
|
491 |
* java.lang.String)
|
492 |
*/
|
493 |
public int addRow(IRow row, String descrip, int sourceType) |
494 |
throws DriverIOException, IOException { |
495 |
|
496 |
try {
|
497 |
validateRow(row); |
498 |
} catch (EditionException e) {
|
499 |
e.printStackTrace(); |
500 |
throw new IOException(e.getMessage()); |
501 |
} |
502 |
|
503 |
int calculatedIndex = doAddRow(row, sourceType);
|
504 |
Command command = new AddRowCommand(this, row, calculatedIndex, |
505 |
sourceType); |
506 |
command.setDescription(descrip); |
507 |
if (complex) {
|
508 |
commands.add(command); |
509 |
} else {
|
510 |
cr.pushCommand(command); |
511 |
} |
512 |
|
513 |
return calculatedIndex;
|
514 |
} |
515 |
|
516 |
/**
|
517 |
* DOCUMENT ME!
|
518 |
*
|
519 |
* @throws DriverIOException
|
520 |
* DOCUMENT ME!
|
521 |
* @throws IOException
|
522 |
* DOCUMENT ME!
|
523 |
*/
|
524 |
public void undo() throws DriverIOException, IOException { |
525 |
// seleccion.clear();
|
526 |
if (cr.moreUndoCommands()) {
|
527 |
cr.undoCommand(); |
528 |
} |
529 |
} |
530 |
|
531 |
/**
|
532 |
* DOCUMENT ME!
|
533 |
*
|
534 |
* @throws DriverIOException
|
535 |
* DOCUMENT ME!
|
536 |
* @throws IOException
|
537 |
* DOCUMENT ME!
|
538 |
*/
|
539 |
public void redo() throws DriverIOException, IOException { |
540 |
// seleccion.clear();
|
541 |
if (cr.moreRedoCommands()) {
|
542 |
cr.redoCommand(); |
543 |
} |
544 |
} |
545 |
|
546 |
/*
|
547 |
* (non-Javadoc)
|
548 |
*
|
549 |
* @see com.iver.cit.gvsig.fmap.edition.IEditableSource#removeRow(int)
|
550 |
*/
|
551 |
public void removeRow(int index, String descrip, int sourceType) |
552 |
throws IOException, DriverIOException { |
553 |
|
554 |
int calculatedIndex = getCalculatedIndex(index);
|
555 |
Command command = new RemoveRowCommand(this, calculatedIndex, |
556 |
sourceType); |
557 |
command.setDescription(descrip); |
558 |
if (complex) {
|
559 |
commands.add(command); |
560 |
} else {
|
561 |
cr.pushCommand(command); |
562 |
} |
563 |
doRemoveRow(calculatedIndex, sourceType); |
564 |
|
565 |
} |
566 |
|
567 |
/*
|
568 |
* (non-Javadoc)
|
569 |
*
|
570 |
* @see com.iver.cit.gvsig.fmap.edition.IEditableSource#modifyRow(int,
|
571 |
* com.iver.cit.gvsig.fmap.core.IRow)
|
572 |
*/
|
573 |
public int modifyRow(int index, IRow row, String descrip, int sourceType) |
574 |
throws IOException, DriverIOException { |
575 |
|
576 |
try {
|
577 |
validateRow(row); |
578 |
} catch (EditionException e) {
|
579 |
e.printStackTrace(); |
580 |
throw new IOException(e.getMessage()); |
581 |
} |
582 |
|
583 |
int calculatedIndex = getCalculatedIndex(index);
|
584 |
int pos = doModifyRow(calculatedIndex, row, sourceType);
|
585 |
Command command = new ModifyRowCommand(this, calculatedIndex, pos, row, |
586 |
sourceType); |
587 |
command.setDescription(descrip); |
588 |
if (complex) {
|
589 |
commands.add(command); |
590 |
} else {
|
591 |
cr.pushCommand(command); |
592 |
} |
593 |
|
594 |
return pos;
|
595 |
} |
596 |
|
597 |
/**
|
598 |
* DOCUMENT ME!
|
599 |
*/
|
600 |
public void compact() { |
601 |
expansionFile.compact(relations); |
602 |
} |
603 |
|
604 |
/**
|
605 |
* DOCUMENT ME!
|
606 |
*/
|
607 |
public void startComplexRow() { |
608 |
complex = true;
|
609 |
commands = new CommandCollection();
|
610 |
} |
611 |
|
612 |
/**
|
613 |
* DOCUMENT ME!
|
614 |
*
|
615 |
* @throws IOException
|
616 |
* DOCUMENT ME!
|
617 |
* @throws DriverIOException
|
618 |
* DOCUMENT ME!
|
619 |
*/
|
620 |
public void endComplexRow(String description) throws IOException, |
621 |
DriverIOException { |
622 |
commands.setDescription(description); |
623 |
cr.pushCommand(commands); |
624 |
complex = false;
|
625 |
} |
626 |
|
627 |
/**
|
628 |
* Actualiza en el mapa de ?ndices, la posici?n en la que estaba la
|
629 |
* geometr?a antes de ser modificada. Se marca como v?lida, en caso de que
|
630 |
* fuera una modificaci?n de una geometr?a que estuviese en el fichero de
|
631 |
* expansi?n antes de ser modificada y se pone el puntero de escritura del
|
632 |
* expansion file a justo despues de la penultima geometr?a
|
633 |
*
|
634 |
* @param geometryIndex
|
635 |
* ?ndice de la geometr?a que se quiere deshacer su modificaci?n
|
636 |
* @param previousExpansionFileIndex
|
637 |
* ?ndice que ten?a antes la geometr?a en el expansionFile. Si
|
638 |
* vale -1 quiere decir que es una modificaci?n de una geometr?a
|
639 |
* original y por tanto no hay que actualizar el mapa de indices
|
640 |
* sino eliminar su entrada.
|
641 |
*
|
642 |
* @throws IOException
|
643 |
* @throws DriverIOException
|
644 |
*/
|
645 |
public void undoModifyRow(int geometryIndex, |
646 |
int previousExpansionFileIndex, int sourceType) throws IOException, |
647 |
DriverIOException { |
648 |
|
649 |
if (previousExpansionFileIndex == -1) { |
650 |
DefaultRowEdited edRow = null;
|
651 |
try {
|
652 |
edRow = new DefaultRowEdited(new DefaultRow(ods |
653 |
.getRow(geometryIndex)), |
654 |
DefaultRowEdited.STATUS_ORIGINAL, geometryIndex); |
655 |
} catch (DriverException e) {
|
656 |
e.printStackTrace(); |
657 |
} |
658 |
boolean cancel = fireBeforeModifyRow(edRow, geometryIndex,
|
659 |
sourceType); |
660 |
if (cancel)
|
661 |
return;
|
662 |
// Se elimina de las relaciones y del fichero de expansi?n
|
663 |
relations.remove(new Integer(geometryIndex)); |
664 |
expansionFile.deleteLastRow(); |
665 |
} else {
|
666 |
boolean cancel = fireBeforeModifyRow(expansionFile
|
667 |
.getRow(previousExpansionFileIndex), geometryIndex, |
668 |
sourceType); |
669 |
if (cancel)
|
670 |
return;
|
671 |
// Se actualiza la relaci?n de ?ndices
|
672 |
relations.put(new Integer(geometryIndex), new Integer( |
673 |
previousExpansionFileIndex)); |
674 |
} |
675 |
fireAfterModifyRow(geometryIndex, sourceType); |
676 |
} |
677 |
|
678 |
/**
|
679 |
* Elimina una geometria. Si es una geometr?a original de la capa en edici?n
|
680 |
* se marca como eliminada (haya sido modificada o no). Si es una geometr?a
|
681 |
* a?adida posteriormente se invalida en el fichero de expansi?n, para que
|
682 |
* una futura compactaci?n termine con ella.
|
683 |
*
|
684 |
* @param index
|
685 |
* ?ndice de la geometr?a.
|
686 |
*
|
687 |
* @throws DriverIOException
|
688 |
* @throws IOException
|
689 |
*/
|
690 |
public IRow doRemoveRow(int index, int sourceType) |
691 |
throws DriverIOException, IOException { |
692 |
boolean cancel = fireBeforeRemoveRow(index, sourceType);
|
693 |
if (cancel)
|
694 |
return null; |
695 |
// Llega un calculatedIndex
|
696 |
delRows.set(index, true);
|
697 |
System.err.println("Elimina una Row en la posici?n: " + index); |
698 |
// TODO: Con tablas no es necesario devolver la anterior feature. Por
|
699 |
// ahora.
|
700 |
isFullExtentDirty = true;
|
701 |
fireAfterRemoveRow(index, sourceType); |
702 |
return null; |
703 |
} |
704 |
|
705 |
/**
|
706 |
* Si se intenta modificar una geometr?a original de la capa en edici?n se
|
707 |
* a?ade al fichero de expansi?n y se registra la posici?n en la que se
|
708 |
* a?adi?. Si se intenta modificar una geometria que se encuentra en el
|
709 |
* fichero de expansi?n (por ser nueva o original pero modificada) se invoca
|
710 |
* el m?todo modifyGeometry y se actualiza el ?ndice de la geometria en el
|
711 |
* fichero.
|
712 |
*
|
713 |
* @param index
|
714 |
* DOCUMENT ME!
|
715 |
* @param feat
|
716 |
* DOCUMENT ME!
|
717 |
*
|
718 |
* @return DOCUMENT ME!
|
719 |
*
|
720 |
* @throws IOException
|
721 |
* @throws DriverIOException
|
722 |
*/
|
723 |
public int doModifyRow(int index, IRow feat, int sourceType) |
724 |
throws IOException, DriverIOException { |
725 |
boolean cancel = fireBeforeModifyRow(feat, index, sourceType);
|
726 |
if (cancel)
|
727 |
return -1; |
728 |
|
729 |
int pos = -1; |
730 |
Integer integer = new Integer(index); |
731 |
System.err.println("Modifica una Row en la posici?n: " + index); |
732 |
// Si la geometr?a no ha sido modificada
|
733 |
if (!relations.containsKey(integer)) {
|
734 |
int expansionIndex = expansionFile.addRow(feat,
|
735 |
IRowEdited.STATUS_MODIFIED, actualIndexFields); |
736 |
relations.put(integer, new Integer(expansionIndex)); |
737 |
} else {
|
738 |
// Obtenemos el ?ndice en el fichero de expansi?n
|
739 |
int num = ((Integer) relations.get(integer)).intValue(); |
740 |
pos = num; |
741 |
|
742 |
/*
|
743 |
* Se modifica la geometr?a y nos guardamos el ?ndice dentro del
|
744 |
* fichero de expansi?n en el que se encuentra la geometr?a
|
745 |
* modificada
|
746 |
*/
|
747 |
num = expansionFile.modifyRow(num, feat, actualIndexFields); |
748 |
|
749 |
/*
|
750 |
* Actualiza la relaci?n del ?ndice de la geometr?a al ?ndice en el
|
751 |
* fichero de expansi?n.
|
752 |
*/
|
753 |
relations.put(integer, new Integer(num)); |
754 |
} |
755 |
isFullExtentDirty = true;
|
756 |
fireAfterModifyRow(index, sourceType); |
757 |
return pos;
|
758 |
} |
759 |
|
760 |
/**
|
761 |
* A?ade una geometria al fichero de expansi?n y guarda la correspondencia
|
762 |
* en la tabla relations.
|
763 |
*
|
764 |
* @param feat
|
765 |
* geometr?a a guardar.
|
766 |
*
|
767 |
* @return calculatedIndex
|
768 |
*
|
769 |
* @throws DriverIOException
|
770 |
* @throws IOException
|
771 |
*/
|
772 |
public int doAddRow(IRow feat, int sourceType) throws DriverIOException, |
773 |
IOException {
|
774 |
boolean cancel = fireBeforeRowAdded(sourceType);
|
775 |
if (cancel)
|
776 |
return -1; |
777 |
// A?ade la geometr?a
|
778 |
// int virtualIndex = 0;
|
779 |
int calculatedIndex = -1; |
780 |
|
781 |
try {
|
782 |
calculatedIndex = (int) ods.getRowCount() + numAdd;
|
783 |
// int externalIndex = getRowCount();
|
784 |
// calculatedIndex = getCalculatedIndex(externalIndex);
|
785 |
} catch (DriverException e) {
|
786 |
throw new DriverIOException(e); |
787 |
} |
788 |
|
789 |
int pos = expansionFile.addRow(feat, IRowEdited.STATUS_ADDED, actualIndexFields);
|
790 |
relations.put(new Integer(calculatedIndex), new Integer(pos)); |
791 |
numAdd++; |
792 |
System.err.println("A?ade una Row en la posici?n: " + calculatedIndex); |
793 |
isFullExtentDirty = true;
|
794 |
fireAfterRowAdded(feat,calculatedIndex, sourceType); |
795 |
return calculatedIndex;
|
796 |
} |
797 |
|
798 |
/**
|
799 |
* Se desmarca como invalidada en el fichero de expansion o como eliminada
|
800 |
* en el fichero original
|
801 |
*
|
802 |
* @param index
|
803 |
* DOCUMENT ME!
|
804 |
*
|
805 |
* @throws IOException
|
806 |
* @throws DriverIOException
|
807 |
*/
|
808 |
public void undoRemoveRow(int index, int sourceType) throws IOException, |
809 |
DriverIOException { |
810 |
boolean cancel = fireBeforeRowAdded(sourceType);
|
811 |
if (cancel)
|
812 |
return;
|
813 |
delRows.set(index, false);
|
814 |
fireAfterRowAdded(null,index, sourceType);
|
815 |
} |
816 |
|
817 |
/**
|
818 |
* Se elimina del final del fichero de expansi?n poniendo el puntero de
|
819 |
* escritura apuntando al final de la pen?ltima geometr?a. Deber? quitar la
|
820 |
* relaci?n del mapa de relaciones
|
821 |
*
|
822 |
* @param index
|
823 |
* ?ndice de la geometr?a que se a?adi?
|
824 |
*
|
825 |
* @throws DriverIOException
|
826 |
* @throws IOException
|
827 |
*/
|
828 |
public void undoAddRow(int calculatedIndex, int sourceType) |
829 |
throws DriverIOException, IOException { |
830 |
boolean cancel = fireBeforeRemoveRow(calculatedIndex, sourceType);
|
831 |
if (cancel)
|
832 |
return;
|
833 |
expansionFile.deleteLastRow(); |
834 |
relations.remove(new Integer(calculatedIndex)); |
835 |
numAdd--; |
836 |
fireAfterRemoveRow(calculatedIndex, sourceType); |
837 |
} |
838 |
|
839 |
/*
|
840 |
* (non-Javadoc)
|
841 |
*
|
842 |
* @see com.iver.cit.gvsig.fmap.layers.VectorialAdapter#getRecordset()
|
843 |
*/
|
844 |
public SelectableDataSource getRecordset() throws DriverLoadException { |
845 |
if (isEditing) {
|
846 |
if (ds == null) { |
847 |
String name = LayerFactory.getDataSourceFactory()
|
848 |
.addDataSource((ObjectDriver) editingDriver); |
849 |
|
850 |
try {
|
851 |
|
852 |
ds = new SelectableDataSource(LayerFactory
|
853 |
.getDataSourceFactory().createRandomDataSource( |
854 |
name, DataSourceFactory.MANUAL_OPENING)); |
855 |
ds.start(); |
856 |
ds.setSelectionSupport(ods.getSelectionSupport()); |
857 |
|
858 |
} catch (NoSuchTableException e) {
|
859 |
throw new RuntimeException(e); |
860 |
} catch (com.hardcode.gdbms.engine.data.driver.DriverException e) {
|
861 |
throw new RuntimeException(e); |
862 |
} |
863 |
} |
864 |
|
865 |
return ds;
|
866 |
} else {
|
867 |
return ods;
|
868 |
} |
869 |
} |
870 |
|
871 |
|
872 |
/**
|
873 |
* DOCUMENT ME!
|
874 |
*
|
875 |
* @return
|
876 |
*/
|
877 |
public FBitSet getSelection() {
|
878 |
/*
|
879 |
* try { return getRecordset().getSelection(); } catch
|
880 |
* (DriverLoadException e) { // TODO Auto-generated catch block
|
881 |
* e.printStackTrace(); } return null;
|
882 |
*/
|
883 |
return getRecordset().getSelection();
|
884 |
} |
885 |
|
886 |
public void setSelection(FBitSet selection) { |
887 |
/*
|
888 |
* try { getRecordset().setSelection(selection); } catch
|
889 |
* (DriverLoadException e) { // TODO Auto-generated catch block
|
890 |
* e.printStackTrace(); }
|
891 |
*/
|
892 |
getRecordset().setSelection(selection); |
893 |
} |
894 |
|
895 |
/**
|
896 |
* DOCUMENT ME!
|
897 |
*
|
898 |
* @return DOCUMENT ME!
|
899 |
*/
|
900 |
public boolean isEditing() { |
901 |
return isEditing;
|
902 |
} |
903 |
|
904 |
public int getInversedIndex(long rowIndex) { |
905 |
int intervalNotDeleted = 0; |
906 |
int antDeleted = -1; |
907 |
int idPedido = (int) rowIndex; |
908 |
int numNotDeleted = 0; |
909 |
int numBorradosAnt = 0; |
910 |
|
911 |
for (int i = delRows.nextSetBit(0); i >= 0; i = delRows |
912 |
.nextSetBit(i + 1)) {
|
913 |
intervalNotDeleted = i - antDeleted - 1;
|
914 |
numNotDeleted += intervalNotDeleted; |
915 |
if (i > idPedido) {
|
916 |
numNotDeleted = numNotDeleted + (i - idPedido); |
917 |
break;
|
918 |
} |
919 |
numBorradosAnt++; |
920 |
antDeleted = i; |
921 |
} |
922 |
numNotDeleted = idPedido - numBorradosAnt; |
923 |
// System.out.println("Piden Viejo : "+ rowIndex + " y devuelvo como
|
924 |
// nuevo " + (numNotDeleted));
|
925 |
return numNotDeleted;
|
926 |
} |
927 |
|
928 |
/**
|
929 |
* DOCUMENT ME!
|
930 |
*
|
931 |
* @param rowIndex
|
932 |
* DOCUMENT ME!
|
933 |
*
|
934 |
* @return DOCUMENT ME!
|
935 |
*/
|
936 |
public int getCalculatedIndex(long rowIndex) { |
937 |
int numNotDeleted = 0; |
938 |
int intervalNotDeleted = 0; |
939 |
int antDeleted = -1; |
940 |
int calculatedIndex;
|
941 |
int idPedido = (int) rowIndex; |
942 |
int numBorradosAnt = 0; |
943 |
|
944 |
for (int i = delRows.nextSetBit(0); i >= 0; i = delRows |
945 |
.nextSetBit(i + 1)) {
|
946 |
intervalNotDeleted = i - antDeleted - 1;
|
947 |
numNotDeleted += intervalNotDeleted; |
948 |
if (numNotDeleted > idPedido) {
|
949 |
break;
|
950 |
} |
951 |
numBorradosAnt++; |
952 |
antDeleted = i; |
953 |
} |
954 |
calculatedIndex = numBorradosAnt + idPedido; |
955 |
// System.out.println("Piden Registro : "+ rowIndex + " y devuelvo el "
|
956 |
// + (calculatedIndex));
|
957 |
return calculatedIndex;
|
958 |
} |
959 |
|
960 |
/**
|
961 |
* DOCUMENT ME!
|
962 |
*
|
963 |
* @author Vicente Caballero Navarro
|
964 |
*/
|
965 |
private class myObjectDriver implements ObjectDriver { |
966 |
/*
|
967 |
* (non-Javadoc)
|
968 |
*
|
969 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#getPrimaryKeys()
|
970 |
*/
|
971 |
public int[] getPrimaryKeys() throws DriverException { |
972 |
return ods.getPrimaryKeys();
|
973 |
// int[] pk=new int[1];
|
974 |
/*
|
975 |
* for (int i=0;i<getRowCount();i++){ pk[i]=i; }
|
976 |
*/
|
977 |
// pk[0]=1;
|
978 |
// return pk;
|
979 |
} |
980 |
|
981 |
/*
|
982 |
* (non-Javadoc)
|
983 |
*
|
984 |
* @see com.hardcode.gdbms.engine.data.driver.ObjectDriver#write(com.hardcode.gdbms.engine.data.edition.DataWare)
|
985 |
*/
|
986 |
public void write(DataWare dataWare) throws DriverException { |
987 |
DataWare dataWareOrig = ods |
988 |
.getDataWare(DataSourceFactory.DATA_WARE_DIRECT_MODE); |
989 |
dataWareOrig.commitTrans(); |
990 |
} |
991 |
|
992 |
/*
|
993 |
* (non-Javadoc)
|
994 |
*
|
995 |
* @see com.hardcode.gdbms.engine.data.driver.GDBMSDriver#setDataSourceFactory(com.hardcode.gdbms.engine.data.DataSourceFactory)
|
996 |
*/
|
997 |
public void setDataSourceFactory(DataSourceFactory dsf) { |
998 |
ods.setDataSourceFactory(dsf); |
999 |
} |
1000 |
|
1001 |
/*
|
1002 |
* (non-Javadoc)
|
1003 |
*
|
1004 |
* @see com.hardcode.driverManager.Driver#getName()
|
1005 |
*/
|
1006 |
public String getName() { |
1007 |
return ods.getName();
|
1008 |
} |
1009 |
|
1010 |
/*
|
1011 |
* (non-Javadoc)
|
1012 |
*
|
1013 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldValue(long,
|
1014 |
* int)
|
1015 |
*/
|
1016 |
public Value getFieldValue(long rowIndex, int fieldId) |
1017 |
throws DriverException {
|
1018 |
// Si no est? en el fichero de expansi?n
|
1019 |
// Integer integer = new Integer(getCalculatedIndex(rowIndex));
|
1020 |
|
1021 |
|
1022 |
try {
|
1023 |
IRow row = getRow((int)rowIndex);
|
1024 |
return row.getAttribute(fieldId);
|
1025 |
// if (!relations.containsKey(integer)) {
|
1026 |
// return ods.getFieldValue(rowIndex, fieldId);
|
1027 |
// } else {
|
1028 |
// int num = ((Integer) relations.get(integer)).intValue();
|
1029 |
// DefaultRowEdited feat = (DefaultRowEdited) expansionFile
|
1030 |
// .getRow(num);
|
1031 |
//
|
1032 |
// if (feat == null) {
|
1033 |
// return null;
|
1034 |
// }
|
1035 |
//
|
1036 |
// return feat.getAttribute(fieldId);
|
1037 |
// }
|
1038 |
// } catch (DriverException e) {
|
1039 |
// e.printStackTrace();
|
1040 |
// throw new DriverException(e);
|
1041 |
} catch (IOException e) { |
1042 |
e.printStackTrace(); |
1043 |
throw new DriverException(e); |
1044 |
} catch (DriverIOException e) {
|
1045 |
e.printStackTrace(); |
1046 |
throw new DriverException(e); |
1047 |
} |
1048 |
|
1049 |
/**
|
1050 |
* try { if (!relations.containsKey(integer)) { // Si ha sido
|
1051 |
* eliminada if (delRows.get(integer.intValue())) { return null; }
|
1052 |
* else { return ods.getFieldValue(rowIndex, fieldId); }} else { int
|
1053 |
* num = ((Integer) relations.get(integer)).intValue();
|
1054 |
* DefaultRowEdited feat = (DefaultRowEdited)
|
1055 |
* expansionFile.getRow(num); if (feat==null)return null; return
|
1056 |
* feat.getAttribute(fieldId); }} catch (DriverException e) {
|
1057 |
* e.printStackTrace(); throw new DriverException(e); } catch
|
1058 |
* (IOException e) { e.printStackTrace(); throw new
|
1059 |
* DriverException(e); }
|
1060 |
*/
|
1061 |
} |
1062 |
|
1063 |
/*
|
1064 |
* (non-Javadoc)
|
1065 |
*
|
1066 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldCount()
|
1067 |
*/
|
1068 |
public int getFieldCount() throws DriverException { |
1069 |
return fastAccessFields.size();
|
1070 |
} |
1071 |
|
1072 |
/*
|
1073 |
* (non-Javadoc)
|
1074 |
*
|
1075 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldName(int)
|
1076 |
*/
|
1077 |
public String getFieldName(int fieldId) throws DriverException { |
1078 |
// int i=0;
|
1079 |
// for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
|
1080 |
// InternalField fld = (InternalField) iter.next();
|
1081 |
// if (i == fieldId)
|
1082 |
// return fld.getFieldDesc().getFieldAlias();
|
1083 |
// i++;
|
1084 |
//
|
1085 |
// }
|
1086 |
// throw new DriverException("FieldId " + fieldId + " not found ");
|
1087 |
FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId); |
1088 |
return aux.getFieldAlias();
|
1089 |
// return null;
|
1090 |
// return ods.getFieldName(fieldId);
|
1091 |
} |
1092 |
|
1093 |
/*
|
1094 |
* (non-Javadoc)
|
1095 |
*
|
1096 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getRowCount()
|
1097 |
*/
|
1098 |
public long getRowCount() { |
1099 |
try {
|
1100 |
return (int) (ods.getRowCount() + numAdd) |
1101 |
- delRows.cardinality();// -
|
1102 |
// expansionFile.getInvalidRows().cardinality();
|
1103 |
} catch (DriverException e) {
|
1104 |
// TODO Auto-generated catch block
|
1105 |
e.printStackTrace(); |
1106 |
} |
1107 |
|
1108 |
return 0; |
1109 |
} |
1110 |
|
1111 |
/*
|
1112 |
* (non-Javadoc)
|
1113 |
*
|
1114 |
* @see com.hardcode.gdbms.engine.data.driver.ReadAccess#getFieldType(int)
|
1115 |
*/
|
1116 |
public int getFieldType(int fieldId) throws DriverException { |
1117 |
// int i=0;
|
1118 |
// for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
|
1119 |
// InternalField fld = (InternalField) iter.next();
|
1120 |
// if (i == fieldId)
|
1121 |
// return fld.getFieldDesc().getFieldType();
|
1122 |
// i++;
|
1123 |
//
|
1124 |
// }
|
1125 |
FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId); |
1126 |
return aux.getFieldType();
|
1127 |
|
1128 |
// return ods.getFieldType(i);
|
1129 |
} |
1130 |
|
1131 |
public int getFieldWidth(int fieldId) throws DriverException { |
1132 |
// int i=0;
|
1133 |
// for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
|
1134 |
// InternalField fld = (InternalField) iter.next();
|
1135 |
//// if (fld.getFieldIndex() == i)
|
1136 |
//// return fld.getFieldDesc().getFieldLength();
|
1137 |
// if (i == fieldId)
|
1138 |
// return fld.getFieldDesc().getFieldLength();
|
1139 |
// i++;
|
1140 |
//
|
1141 |
// }
|
1142 |
//
|
1143 |
// return ods.getFieldWidth(i);
|
1144 |
FieldDescription aux = (FieldDescription) fastAccessFields.get(fieldId); |
1145 |
return aux.getFieldLength();
|
1146 |
|
1147 |
} |
1148 |
|
1149 |
public void reload() throws IOException, DriverException { |
1150 |
ods.reload(); |
1151 |
|
1152 |
} |
1153 |
} |
1154 |
|
1155 |
public CommandRecord getCommandRecord() {
|
1156 |
return cr;
|
1157 |
} |
1158 |
|
1159 |
protected void fireAfterRemoveRow(int index, int sourceType) { |
1160 |
AfterRowEditEvent event = new AfterRowEditEvent(this, index, |
1161 |
EditionEvent.CHANGE_TYPE_DELETE, sourceType); |
1162 |
for (int i = 0; i < editionListeners.size(); i++) { |
1163 |
IEditionListener listener = (IEditionListener) editionListeners |
1164 |
.get(i); |
1165 |
listener.afterRowEditEvent(null, event);
|
1166 |
} |
1167 |
|
1168 |
} |
1169 |
|
1170 |
protected boolean fireBeforeRemoveRow(int index, int sourceType) { |
1171 |
Cancel cancel = new Cancel();
|
1172 |
BeforeRowEditEvent event = new BeforeRowEditEvent(this, index, |
1173 |
EditionEvent.CHANGE_TYPE_DELETE, cancel, sourceType); |
1174 |
for (int i = 0; i < editionListeners.size(); i++) { |
1175 |
IEditionListener listener = (IEditionListener) editionListeners |
1176 |
.get(i); |
1177 |
listener.beforeRowEditEvent(null, event);
|
1178 |
if (cancel.isCanceled())
|
1179 |
return true; |
1180 |
} |
1181 |
return false; |
1182 |
} |
1183 |
|
1184 |
protected void fireAfterRowAdded(IRow feat,int calculatedIndex, int sourceType) { |
1185 |
AfterRowEditEvent event = new AfterRowEditEvent(this, calculatedIndex, |
1186 |
EditionEvent.CHANGE_TYPE_ADD, sourceType); |
1187 |
for (int i = 0; i < editionListeners.size(); i++) { |
1188 |
IEditionListener listener = (IEditionListener) editionListeners |
1189 |
.get(i); |
1190 |
listener.afterRowEditEvent(feat, event); |
1191 |
} |
1192 |
} |
1193 |
|
1194 |
protected void fireAfterFieldAdded(FieldDescription field) { |
1195 |
AfterFieldEditEvent event = new AfterFieldEditEvent(this,field, |
1196 |
EditionEvent.CHANGE_TYPE_ADD); |
1197 |
for (int i = 0; i < editionListeners.size(); i++) { |
1198 |
IEditionListener listener = (IEditionListener) editionListeners |
1199 |
.get(i); |
1200 |
listener.afterFieldEditEvent(event); |
1201 |
} |
1202 |
} |
1203 |
|
1204 |
protected void fireAfterFieldRemoved(FieldDescription field) { |
1205 |
AfterFieldEditEvent event = new AfterFieldEditEvent(this,field, |
1206 |
EditionEvent.CHANGE_TYPE_DELETE); |
1207 |
for (int i = 0; i < editionListeners.size(); i++) { |
1208 |
IEditionListener listener = (IEditionListener) editionListeners |
1209 |
.get(i); |
1210 |
listener.afterFieldEditEvent(event); |
1211 |
} |
1212 |
} |
1213 |
|
1214 |
protected void fireAfterFieldModified(FieldDescription field) { |
1215 |
AfterFieldEditEvent event = new AfterFieldEditEvent(this,field, |
1216 |
EditionEvent.CHANGE_TYPE_MODIFY); |
1217 |
for (int i = 0; i < editionListeners.size(); i++) { |
1218 |
IEditionListener listener = (IEditionListener) editionListeners |
1219 |
.get(i); |
1220 |
listener.afterFieldEditEvent(event); |
1221 |
} |
1222 |
} |
1223 |
|
1224 |
|
1225 |
protected boolean fireBeforeRowAdded(int sourceType) |
1226 |
throws DriverIOException, IOException { |
1227 |
Cancel cancel = new Cancel();
|
1228 |
BeforeRowEditEvent event = new BeforeRowEditEvent(this, getRowCount(), |
1229 |
EditionEvent.CHANGE_TYPE_ADD, cancel, sourceType); |
1230 |
for (int i = 0; i < editionListeners.size(); i++) { |
1231 |
IEditionListener listener = (IEditionListener) editionListeners |
1232 |
.get(i); |
1233 |
listener.beforeRowEditEvent(null, event);
|
1234 |
if (cancel.isCanceled())
|
1235 |
return true; |
1236 |
} |
1237 |
return false; |
1238 |
} |
1239 |
|
1240 |
protected boolean fireBeforeFieldAdded(FieldDescription field) |
1241 |
throws EditionException {
|
1242 |
Cancel cancel = new Cancel();
|
1243 |
BeforeFieldEditEvent event = new BeforeFieldEditEvent(this, field, |
1244 |
EditionEvent.CHANGE_TYPE_ADD, cancel); |
1245 |
for (int i = 0; i < editionListeners.size(); i++) { |
1246 |
IEditionListener listener = (IEditionListener) editionListeners |
1247 |
.get(i); |
1248 |
listener.beforeFieldEditEvent(event); |
1249 |
if (cancel.isCanceled())
|
1250 |
return true; |
1251 |
} |
1252 |
return false; |
1253 |
} |
1254 |
|
1255 |
protected boolean fireBeforeRemoveField(FieldDescription field) |
1256 |
throws EditionException {
|
1257 |
Cancel cancel = new Cancel();
|
1258 |
BeforeFieldEditEvent event = new BeforeFieldEditEvent(this, field, |
1259 |
EditionEvent.CHANGE_TYPE_DELETE, cancel); |
1260 |
for (int i = 0; i < editionListeners.size(); i++) { |
1261 |
IEditionListener listener = (IEditionListener) editionListeners |
1262 |
.get(i); |
1263 |
listener.beforeFieldEditEvent(event); |
1264 |
if (cancel.isCanceled())
|
1265 |
return true; |
1266 |
} |
1267 |
return false; |
1268 |
} |
1269 |
|
1270 |
|
1271 |
protected boolean fireBeforeModifyRow(IRow feat, int index, int sourceType) { |
1272 |
Cancel cancel = new Cancel();
|
1273 |
BeforeRowEditEvent event = new BeforeRowEditEvent(this, index, |
1274 |
EditionEvent.CHANGE_TYPE_MODIFY, cancel, sourceType); |
1275 |
for (int i = 0; i < editionListeners.size(); i++) { |
1276 |
IEditionListener listener = (IEditionListener) editionListeners |
1277 |
.get(i); |
1278 |
listener.beforeRowEditEvent(feat, event); |
1279 |
if (cancel.isCanceled())
|
1280 |
return true; |
1281 |
} |
1282 |
return false; |
1283 |
} |
1284 |
|
1285 |
protected void fireAfterModifyRow(int index, int sourceType) { |
1286 |
AfterRowEditEvent event = new AfterRowEditEvent(this, index, |
1287 |
EditionEvent.CHANGE_TYPE_MODIFY, sourceType); |
1288 |
for (int i = 0; i < editionListeners.size(); i++) { |
1289 |
IEditionListener listener = (IEditionListener) editionListeners |
1290 |
.get(i); |
1291 |
listener.afterRowEditEvent(null, event);
|
1292 |
} |
1293 |
|
1294 |
} |
1295 |
|
1296 |
protected void fireStartEditionEvent(int sourceType) { |
1297 |
EditionEvent ev = new EditionEvent(this, EditionEvent.START_EDITION, |
1298 |
sourceType); |
1299 |
for (int i = 0; i < editionListeners.size(); i++) { |
1300 |
IEditionListener listener = (IEditionListener) editionListeners |
1301 |
.get(i); |
1302 |
listener.processEvent(ev); |
1303 |
} |
1304 |
|
1305 |
} |
1306 |
|
1307 |
protected void fireStopEditionEvent(int sourceType) { |
1308 |
EditionEvent ev = new EditionEvent(this, EditionEvent.STOP_EDITION, |
1309 |
sourceType); |
1310 |
for (int i = 0; i < editionListeners.size(); i++) { |
1311 |
IEditionListener listener = (IEditionListener) editionListeners |
1312 |
.get(i); |
1313 |
listener.processEvent(ev); |
1314 |
} |
1315 |
|
1316 |
} |
1317 |
|
1318 |
protected void fireCancelEditionEvent(int sourceType) { |
1319 |
EditionEvent ev = new EditionEvent(this, EditionEvent.CANCEL_EDITION, |
1320 |
sourceType); |
1321 |
for (int i = 0; i < editionListeners.size(); i++) { |
1322 |
IEditionListener listener = (IEditionListener) editionListeners |
1323 |
.get(i); |
1324 |
listener.processEvent(ev); |
1325 |
} |
1326 |
|
1327 |
} |
1328 |
|
1329 |
public void addEditionListener(IEditionListener listener) { |
1330 |
if (!editionListeners.contains(listener))
|
1331 |
editionListeners.add(listener); |
1332 |
} |
1333 |
|
1334 |
public void removeEditionListener(IEditionListener listener) { |
1335 |
editionListeners.remove(listener); |
1336 |
} |
1337 |
|
1338 |
public IWriter getWriter() {
|
1339 |
return writer;
|
1340 |
} |
1341 |
|
1342 |
protected void setWriter(IWriter writer) { |
1343 |
this.writer = writer;
|
1344 |
|
1345 |
} |
1346 |
|
1347 |
public ITableDefinition getTableDefinition() throws DriverLoadException, |
1348 |
DriverException { |
1349 |
TableDefinition tableDef = new TableDefinition();
|
1350 |
tableDef.setFieldsDesc(getRecordset().getFieldsDescription()); |
1351 |
tableDef.setName(getRecordset().getSourceInfo().name); |
1352 |
return tableDef;
|
1353 |
} |
1354 |
|
1355 |
public void validateRow(IRow row) throws EditionException { |
1356 |
for (int i = 0; i < rules.size(); i++) { |
1357 |
IRule rule = (IRule) rules.get(i); |
1358 |
boolean bAux = rule.validate(row);
|
1359 |
if (bAux == false) { |
1360 |
EditionException ex = new EditionException(
|
1361 |
"NOT follow the rule: " + rule.getDescription());
|
1362 |
// TODO: Lanzar una RuleException con datos como el registro
|
1363 |
// que no cumple, la regla que no lo ha cumplido, etc.
|
1364 |
throw ex;
|
1365 |
} |
1366 |
} |
1367 |
} |
1368 |
|
1369 |
public ArrayList getRules() { |
1370 |
return rules;
|
1371 |
} |
1372 |
|
1373 |
public void setRules(ArrayList rules) { |
1374 |
this.rules = rules;
|
1375 |
} |
1376 |
|
1377 |
private void clean() throws IOException, DriverException { |
1378 |
expansionFile.close(); |
1379 |
relations.clear(); |
1380 |
numAdd = 0;
|
1381 |
delRows.clear(); |
1382 |
// TODO: Es muy probable que necesitemos un reload de los datasources, al
|
1383 |
// igual que lo tenemos en las capas. Por ahora, basta con retocar
|
1384 |
// listInternalFields, pero casi seguro que lo correcto ser?a hacer un
|
1385 |
// reload completo.
|
1386 |
initalizeFields(ods); |
1387 |
|
1388 |
// listInternalFields.clear();
|
1389 |
// listInternalFields.add(actualFields);
|
1390 |
} |
1391 |
|
1392 |
/*
|
1393 |
* (non-Javadoc)
|
1394 |
*
|
1395 |
* @see com.iver.cit.gvsig.fmap.edition.IEditableSource#getFieldManager()
|
1396 |
*/
|
1397 |
public IFieldManager getFieldManager() {
|
1398 |
if (ods.getDriver() instanceof IWriteable) |
1399 |
{ |
1400 |
IWriter writer = ((IWriteable)ods.getDriver()).getWriter(); |
1401 |
if ((writer != null) && (writer instanceof IFieldManager)) |
1402 |
{ |
1403 |
IFieldManager fldManager = (IFieldManager) writer; |
1404 |
return fldManager;
|
1405 |
} |
1406 |
} |
1407 |
return null; |
1408 |
} |
1409 |
|
1410 |
/**
|
1411 |
* Tiene en cuenta los campos actuales para formatear una row con ellos. Le
|
1412 |
* pasamos los campos que hab?a en el momento en que se cre? esa row.
|
1413 |
*
|
1414 |
* @param edRow
|
1415 |
* @param indexInternalFields
|
1416 |
* @return
|
1417 |
*/
|
1418 |
public IRowEdited createExternalRow(IRowEdited edRow,
|
1419 |
int indexInternalFields) {
|
1420 |
|
1421 |
// para acelerar
|
1422 |
if (bFieldsHasBeenChanged == false) |
1423 |
return edRow;
|
1424 |
|
1425 |
Value[] att = edRow.getAttributes();
|
1426 |
TreeMap ancientFields = (TreeMap) listInternalFields |
1427 |
.get(indexInternalFields); |
1428 |
Value[] newAtt = new Value[actualFields.size()]; |
1429 |
Collection aux = actualFields.values();
|
1430 |
Iterator it = aux.iterator();
|
1431 |
int i = 0; |
1432 |
Value val = null;
|
1433 |
while (it.hasNext()) {
|
1434 |
// Para cada campo de los actuales, miramos si ya estaba cuando
|
1435 |
// el registro estaba guardado.
|
1436 |
// Si estaba, cogemos el valor de ese campo en el registro
|
1437 |
// guardado. Si no estaba, ha sido a?adido despu?s y ponemos
|
1438 |
// su valor por defecto.
|
1439 |
// Nota importante: fieldIndex es el ?ndice del campo cuando
|
1440 |
// se guard?. NO es el ?ndice actual dentro de actualFields.
|
1441 |
// Se usa SOLO para recuperar el valor de los atributos
|
1442 |
// antiguos. Por eso no nos preocupamos de mantener actuallizados
|
1443 |
// el resto de campos cuando se borra o a?ade un nuevo campo.
|
1444 |
InternalField fld = (InternalField) it.next(); |
1445 |
// System.err.println("fld = " + fld.getFieldDesc().getFieldAlias() + " id=" + fld.getFieldId());
|
1446 |
if (ancientFields.containsKey(fld.getFieldId())) {
|
1447 |
InternalField ancientField = (InternalField) ancientFields |
1448 |
.get(fld.getFieldId()); |
1449 |
val = att[ancientField.getFieldIndex()]; |
1450 |
// val = att[ancientField.getFieldId().intValue()];
|
1451 |
// System.out.println("fld: " + fld.getFieldDesc().getFieldAlias() + " ancient:" + " val" + val);
|
1452 |
} else
|
1453 |
val = fld.getFieldDesc().getDefaultValue(); |
1454 |
newAtt[i++] = val; |
1455 |
} |
1456 |
IRowEdited newRow = (IRowEdited) edRow.cloneRow(); |
1457 |
newRow.setAttributes(newAtt); |
1458 |
return newRow;
|
1459 |
} |
1460 |
|
1461 |
public void removeField(String fieldName) throws EditionException { |
1462 |
|
1463 |
InternalField fld = findFieldByName(fieldName); |
1464 |
if (fld == null) |
1465 |
throw new EditionException("Field " + fieldName + " not found when removing field"); |
1466 |
Command command = new RemoveFieldCommand(this, fld); |
1467 |
if (complex) {
|
1468 |
commands.add(command); |
1469 |
} else {
|
1470 |
cr.pushCommand(command); |
1471 |
} |
1472 |
doRemoveField(fld); |
1473 |
|
1474 |
} |
1475 |
|
1476 |
private InternalField findFieldByName(String fieldName) { |
1477 |
Collection aux = actualFields.values();
|
1478 |
Iterator it = aux.iterator();
|
1479 |
while (it.hasNext()) {
|
1480 |
InternalField fld = (InternalField) it.next(); |
1481 |
if (fld.getFieldDesc().getFieldAlias().compareToIgnoreCase(fieldName) == 0) |
1482 |
return fld;
|
1483 |
} |
1484 |
|
1485 |
return null; |
1486 |
} |
1487 |
|
1488 |
public void undoRemoveField(InternalField field) throws EditionException { |
1489 |
// field.setDeleted(false);
|
1490 |
// field.setFieldIndex(actualFields.size());
|
1491 |
actualFields.put(field.getFieldId(), field); |
1492 |
fieldsChanged(); |
1493 |
fireAfterFieldAdded(field.getFieldDesc()); |
1494 |
} |
1495 |
|
1496 |
public void doRemoveField(InternalField field) throws EditionException { |
1497 |
boolean cancel = fireBeforeRemoveField(field.getFieldDesc());
|
1498 |
if (cancel) return; |
1499 |
actualFields.remove(field.getFieldId()); |
1500 |
fieldsChanged(); |
1501 |
fireAfterFieldRemoved(field.getFieldDesc()); |
1502 |
} |
1503 |
|
1504 |
public void renameField(String antName, String newName) throws EditionException { |
1505 |
|
1506 |
InternalField fld = findFieldByName(antName); |
1507 |
Command command = new RenameFieldCommand(this, fld, newName); |
1508 |
if (complex) {
|
1509 |
commands.add(command); |
1510 |
} else {
|
1511 |
cr.pushCommand(command); |
1512 |
} |
1513 |
doRenameField(fld, newName); |
1514 |
|
1515 |
} |
1516 |
|
1517 |
public void undoRenameField(InternalField field, String antName) throws EditionException { |
1518 |
field.getFieldDesc().setFieldAlias(antName); |
1519 |
fieldsChanged(); |
1520 |
fireAfterFieldModified(field.getFieldDesc()); |
1521 |
|
1522 |
} |
1523 |
|
1524 |
public void doRenameField(InternalField field, String newName) throws EditionException { |
1525 |
field.getFieldDesc().setFieldAlias(newName); |
1526 |
fieldsChanged(); |
1527 |
fireAfterFieldModified(field.getFieldDesc()); |
1528 |
|
1529 |
} |
1530 |
|
1531 |
|
1532 |
public void addField(FieldDescription field) throws EditionException { |
1533 |
|
1534 |
InternalField fld = new InternalField(field, InternalField.ADDED, new Integer(listFields.size())); |
1535 |
Command command = new AddFieldCommand(this, fld); |
1536 |
if (complex) {
|
1537 |
commands.add(command); |
1538 |
} else {
|
1539 |
cr.pushCommand(command); |
1540 |
} |
1541 |
listFields.add(fld); |
1542 |
doAddField(fld); |
1543 |
|
1544 |
} |
1545 |
|
1546 |
public void undoAddField(InternalField field) throws EditionException { |
1547 |
boolean cancel = fireBeforeRemoveField(field.getFieldDesc());
|
1548 |
if (cancel)
|
1549 |
return;
|
1550 |
|
1551 |
// field.setDeleted(true);
|
1552 |
actualFields.remove(field.getFieldId()); |
1553 |
fieldsChanged(); |
1554 |
fireAfterFieldRemoved(field.getFieldDesc()); |
1555 |
|
1556 |
} |
1557 |
|
1558 |
public int doAddField(InternalField field) throws EditionException { |
1559 |
boolean cancel;
|
1560 |
cancel = fireBeforeFieldAdded(field.getFieldDesc()); |
1561 |
if (cancel)
|
1562 |
return -1; |
1563 |
|
1564 |
// field.setDeleted(false);
|
1565 |
// field.setFieldIndex(actualFields.size());
|
1566 |
actualFields.put(field.getFieldId(), field); |
1567 |
fieldsChanged(); |
1568 |
fireAfterFieldAdded(field.getFieldDesc()); |
1569 |
// return field.getFieldIndex();
|
1570 |
return field.getFieldId().intValue();
|
1571 |
} |
1572 |
|
1573 |
public Driver getOriginalDriver() |
1574 |
{ |
1575 |
return ods.getDriver();
|
1576 |
} |
1577 |
|
1578 |
/**
|
1579 |
* Use it to be sure the recordset will have the right fields. It forces a new SelectableDataSource
|
1580 |
* to be created next time it is needed
|
1581 |
*/
|
1582 |
public void cleanSelectableDatasource() { |
1583 |
ds = null;
|
1584 |
} |
1585 |
|
1586 |
public FieldDescription[] getFieldsDescription() { |
1587 |
return (FieldDescription[]) fastAccessFields.toArray(new FieldDescription[0]); |
1588 |
} |
1589 |
|
1590 |
|
1591 |
// private InternalField getInternalFieldByIndex(int fieldId)
|
1592 |
// {
|
1593 |
// for (Iterator iter = actualFields.values().iterator(); iter.hasNext();) {
|
1594 |
// InternalField fld = (InternalField) iter.next();
|
1595 |
// if (fld.getFieldIndex() == fieldId)
|
1596 |
// return fld;
|
1597 |
// }
|
1598 |
// return null;
|
1599 |
// }
|
1600 |
|
1601 |
} |