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