root / trunk / libraries / libFMap / src / com / iver / cit / gvsig / fmap / edition / writers / dbf / DbfWriter.java @ 13044
History | View | Annotate | Download (7.43 KB)
1 |
package com.iver.cit.gvsig.fmap.edition.writers.dbf; |
---|---|
2 |
|
3 |
import java.io.File; |
4 |
import java.io.IOException; |
5 |
import java.io.RandomAccessFile; |
6 |
import java.nio.channels.FileChannel; |
7 |
import java.nio.channels.WritableByteChannel; |
8 |
import java.sql.Types; |
9 |
import java.util.ArrayList; |
10 |
|
11 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
12 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException; |
13 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
14 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
15 |
import com.iver.cit.gvsig.fmap.core.IRow; |
16 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
17 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
18 |
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileHeaderNIO; |
19 |
import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileWriterNIO; |
20 |
import com.iver.cit.gvsig.fmap.drivers.shp.SHP; |
21 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
22 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.AddFieldCommand; |
23 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.FieldCommand; |
24 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.RemoveFieldCommand; |
25 |
import com.iver.cit.gvsig.fmap.edition.fieldmanagers.RenameFieldCommand; |
26 |
import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
27 |
import com.iver.cit.gvsig.fmap.layers.FBitSet; |
28 |
|
29 |
public class DbfWriter extends AbstractWriter { |
30 |
private String dbfPath = null; |
31 |
|
32 |
private DbaseFileWriterNIO dbfWrite;
|
33 |
|
34 |
private DbaseFileHeaderNIO myHeader;
|
35 |
|
36 |
private int numRows; |
37 |
|
38 |
private Object[] record; |
39 |
|
40 |
private ArrayList fieldCommands = new ArrayList(); |
41 |
|
42 |
// private FLyrVect lyrVect;
|
43 |
private FBitSet selection = null; |
44 |
|
45 |
private FieldDescription[] originalFields; |
46 |
|
47 |
public DbfWriter() {
|
48 |
super();
|
49 |
this.capabilities.setProperty("FieldNameMaxLength","10"); |
50 |
} |
51 |
|
52 |
public void setFile(File f) { |
53 |
String absolutePath=f.getAbsolutePath();
|
54 |
if (absolutePath.toUpperCase().endsWith("DBF")){ |
55 |
dbfPath=absolutePath; |
56 |
} else {
|
57 |
dbfPath = SHP.getDbfFile(f).getAbsolutePath(); |
58 |
} |
59 |
} |
60 |
|
61 |
private WritableByteChannel getWriteChannel(String path) throws IOException { |
62 |
WritableByteChannel channel;
|
63 |
|
64 |
File f = new File(path); |
65 |
|
66 |
if (!f.exists()) {
|
67 |
System.out.println("Creando fichero " + f.getAbsolutePath()); |
68 |
|
69 |
if (!f.createNewFile()) {
|
70 |
System.err.print("Error al crear el fichero " |
71 |
+ f.getAbsolutePath()); |
72 |
throw new IOException("Cannot create file " + f); |
73 |
} |
74 |
} |
75 |
|
76 |
RandomAccessFile raf = new RandomAccessFile(f, "rw"); |
77 |
channel = raf.getChannel(); |
78 |
|
79 |
return channel;
|
80 |
} |
81 |
|
82 |
public void preProcess() throws StartWriterVisitorException { |
83 |
// Por ahora solo escribimos los primeros bytes
|
84 |
// de las cabeceras. Luego en el postprocess los escribiremos
|
85 |
// correctamente, con el fullExtent y el numero de
|
86 |
// registros que tocan.
|
87 |
alterTable(); |
88 |
if (selection == null) { |
89 |
|
90 |
try {
|
91 |
myHeader.setNumRecords(0);
|
92 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
93 |
(FileChannel) getWriteChannel(dbfPath));
|
94 |
record = new Object[myHeader.getNumFields()]; |
95 |
numRows = 0;
|
96 |
|
97 |
} catch (IOException e) { |
98 |
throw new StartWriterVisitorException(getName(),e); |
99 |
} |
100 |
} |
101 |
|
102 |
} |
103 |
|
104 |
public void process(IRowEdited row) throws ProcessWriterVisitorException { |
105 |
IRow rowEdit = row.getLinkedRow(); |
106 |
// System.err.println("DBFWriter: " + row.getStatus() + " numRows = "
|
107 |
// + numRows);
|
108 |
switch (row.getStatus()) {
|
109 |
case IRowEdited.STATUS_ADDED:
|
110 |
case IRowEdited.STATUS_ORIGINAL:
|
111 |
case IRowEdited.STATUS_MODIFIED:
|
112 |
try {
|
113 |
|
114 |
for (int i = 0; i < record.length; i++) |
115 |
record[i] = rowEdit.getAttribute(i); |
116 |
dbfWrite.write(record); |
117 |
numRows++; |
118 |
|
119 |
} catch (IOException e) { |
120 |
throw new ProcessWriterVisitorException(getName(),e); |
121 |
} |
122 |
|
123 |
} |
124 |
|
125 |
} |
126 |
|
127 |
public void postProcess() throws StopWriterVisitorException { |
128 |
try {
|
129 |
myHeader.setNumRecords(numRows); |
130 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
131 |
(FileChannel) getWriteChannel(dbfPath));
|
132 |
|
133 |
} catch (IOException e) { |
134 |
throw new StopWriterVisitorException(getName(),e); |
135 |
} |
136 |
|
137 |
} |
138 |
|
139 |
public String getName() { |
140 |
return "DBF Writer"; |
141 |
} |
142 |
|
143 |
public boolean canWriteAttribute(int sqlType) { |
144 |
switch (sqlType) {
|
145 |
case Types.DOUBLE: |
146 |
case Types.FLOAT: |
147 |
case Types.INTEGER: |
148 |
case Types.BIGINT: |
149 |
return true; |
150 |
case Types.DATE: |
151 |
return true; |
152 |
case Types.BIT: |
153 |
case Types.BOOLEAN: |
154 |
return true; |
155 |
case Types.VARCHAR: |
156 |
case Types.CHAR: |
157 |
case Types.LONGVARCHAR: |
158 |
return true; // TODO: Revisar esto, porque no creo que admita |
159 |
// campos muy grandes
|
160 |
|
161 |
} |
162 |
|
163 |
return false; |
164 |
} |
165 |
|
166 |
public void initialize(ITableDefinition tableDefinition) |
167 |
throws InitializeWriterException {
|
168 |
super.initialize(tableDefinition);
|
169 |
originalFields = tableDefinition.getFieldsDesc(); |
170 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(tableDefinition |
171 |
.getFieldsDesc()); |
172 |
if (dbfPath == null) { |
173 |
throw new InitializeWriterException(getName(),null); |
174 |
} |
175 |
|
176 |
} |
177 |
|
178 |
public FieldDescription[] getOriginalFields() { |
179 |
return originalFields;
|
180 |
} |
181 |
|
182 |
public boolean alterTable() throws StartWriterVisitorException { |
183 |
FieldDescription[] fieldsDesc =getFields();
|
184 |
|
185 |
myHeader = DbaseFileHeaderNIO.createDbaseHeader(fieldsDesc); |
186 |
try {
|
187 |
dbfWrite = new DbaseFileWriterNIO(myHeader,
|
188 |
(FileChannel) getWriteChannel(dbfPath));
|
189 |
// if (bNeedRewrite) {
|
190 |
// int aux = (int) (Math.random() * 1000);
|
191 |
// File fTemp = new File(System.getProperty("java.io.tmpdir")
|
192 |
// + "/tmpDbf" + aux + ".dbf");
|
193 |
//
|
194 |
// // TODO: TERMINAR!!
|
195 |
//
|
196 |
// }
|
197 |
} catch (IOException e) { |
198 |
throw new StartWriterVisitorException(getName(),e); |
199 |
} |
200 |
return true; |
201 |
} |
202 |
|
203 |
public void addField(FieldDescription fieldDesc) { |
204 |
AddFieldCommand c = new AddFieldCommand(fieldDesc);
|
205 |
fieldCommands.add(c); |
206 |
} |
207 |
|
208 |
public FieldDescription removeField(String fieldName) { |
209 |
RemoveFieldCommand c = new RemoveFieldCommand(fieldName);
|
210 |
FieldDescription[] act = getFields();
|
211 |
FieldDescription found = null;
|
212 |
for (int i=0; i < act.length; i++) |
213 |
{ |
214 |
if (act[i].getFieldAlias().compareToIgnoreCase(fieldName) == 0) |
215 |
{ |
216 |
found = act[i]; |
217 |
break;
|
218 |
} |
219 |
} |
220 |
fieldCommands.add(c); |
221 |
return found;
|
222 |
} |
223 |
|
224 |
public void renameField(String antName, String newName) { |
225 |
RenameFieldCommand c = new RenameFieldCommand(antName, newName);
|
226 |
fieldCommands.add(c); |
227 |
|
228 |
} |
229 |
|
230 |
public FieldDescription[] getFields() { |
231 |
ArrayList aux = new ArrayList(); |
232 |
for (int i=0; i < getOriginalFields().length; i++) |
233 |
{ |
234 |
aux.add(getOriginalFields()[i]); |
235 |
} |
236 |
// procesamos comandos para devolver los campos reales.
|
237 |
for (int j=0; j < fieldCommands.size(); j++) |
238 |
{ |
239 |
FieldCommand fc = (FieldCommand) fieldCommands.get(j); |
240 |
if (fc instanceof AddFieldCommand) |
241 |
{ |
242 |
AddFieldCommand ac = (AddFieldCommand) fc; |
243 |
aux.add(ac.getFieldDesc()); |
244 |
} |
245 |
if (fc instanceof RemoveFieldCommand) |
246 |
{ |
247 |
RemoveFieldCommand rc = (RemoveFieldCommand) fc; |
248 |
for (int k = 0; k < aux.size(); k++) { |
249 |
FieldDescription fAux = (FieldDescription) aux.get(k); |
250 |
if (fAux.getFieldAlias().compareTo(rc.getFieldName()) == 0) { |
251 |
aux.remove(k); |
252 |
break;
|
253 |
} |
254 |
} |
255 |
} |
256 |
if (fc instanceof RenameFieldCommand) |
257 |
{ |
258 |
RenameFieldCommand renc = (RenameFieldCommand) fc; |
259 |
for (int k = 0; k < aux.size(); k++) { |
260 |
FieldDescription fAux = (FieldDescription) aux.get(k); |
261 |
if (fAux.getFieldAlias().compareTo(renc.getAntName()) == 0) { |
262 |
fAux.setFieldAlias(renc.getNewName()); |
263 |
break;
|
264 |
} |
265 |
} |
266 |
|
267 |
} |
268 |
|
269 |
} |
270 |
return (FieldDescription[]) aux.toArray(new FieldDescription[0]); |
271 |
} |
272 |
|
273 |
public boolean canAlterTable() { |
274 |
return true; |
275 |
} |
276 |
|
277 |
public boolean canSaveEdits() { |
278 |
File aux = new File(dbfPath); |
279 |
if (aux.canWrite()) return true; |
280 |
return false; |
281 |
} |
282 |
|
283 |
} |