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 | 4143 | caballero | 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 | 4328 | fjp | import java.sql.Types; |
9 | 6127 | fjp | import java.util.ArrayList; |
10 | 4143 | caballero | |
11 | import com.iver.cit.gvsig.fmap.core.IRow; |
||
12 | 6127 | fjp | import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
13 | 5558 | fjp | import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
14 | 4143 | caballero | import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileHeaderNIO; |
15 | import com.iver.cit.gvsig.fmap.drivers.shp.DbaseFileWriterNIO; |
||
16 | 12910 | caballero | import com.iver.cit.gvsig.fmap.drivers.shp.SHP; |
17 | 4143 | caballero | import com.iver.cit.gvsig.fmap.edition.EditionException; |
18 | 6127 | fjp | import com.iver.cit.gvsig.fmap.edition.IFieldManager; |
19 | 4143 | caballero | import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
20 | 6127 | fjp | 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 | 4328 | fjp | import com.iver.cit.gvsig.fmap.edition.writers.AbstractWriter; |
25 | 4143 | caballero | import com.iver.cit.gvsig.fmap.layers.FBitSet; |
26 | |||
27 | 6579 | fjp | public class DbfWriter extends AbstractWriter { |
28 | 5558 | fjp | private String dbfPath = null; |
29 | 4143 | caballero | |
30 | private DbaseFileWriterNIO dbfWrite;
|
||
31 | |||
32 | private DbaseFileHeaderNIO myHeader;
|
||
33 | |||
34 | private int numRows; |
||
35 | 6127 | fjp | |
36 | 4143 | caballero | private Object[] record; |
37 | |||
38 | 6212 | fjp | private ArrayList fieldCommands = new ArrayList(); |
39 | |||
40 | 4143 | caballero | // private FLyrVect lyrVect;
|
41 | private FBitSet selection = null; |
||
42 | |||
43 | 6127 | fjp | private FieldDescription[] originalFields; |
44 | 12910 | caballero | |
45 | 10046 | jmvivo | public DbfWriter() {
|
46 | super();
|
||
47 | this.capabilities.setProperty("FieldNameMaxLength","10"); |
||
48 | } |
||
49 | 6127 | fjp | |
50 | public void setFile(File f) { |
||
51 | 12910 | caballero | String absolutePath=f.getAbsolutePath();
|
52 | if (absolutePath.toUpperCase().endsWith("DBF")){ |
||
53 | dbfPath=absolutePath; |
||
54 | } else {
|
||
55 | dbfPath = SHP.getDbfFile(f).getAbsolutePath(); |
||
56 | } |
||
57 | 4143 | caballero | } |
58 | |||
59 | 6127 | fjp | private WritableByteChannel getWriteChannel(String path) throws IOException { |
60 | 4143 | caballero | 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 | 6127 | fjp | System.err.print("Error al crear el fichero " |
69 | + f.getAbsolutePath()); |
||
70 | 4143 | caballero | 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 | 6212 | fjp | alterTable(); |
86 | 6127 | fjp | if (selection == null) { |
87 | 4143 | caballero | |
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 | 8765 | jjdelcerro | // System.err.println("DBFWriter: " + row.getStatus() + " numRows = "
|
106 | // + numRows);
|
||
107 | 6212 | fjp | switch (row.getStatus()) {
|
108 | 6127 | fjp | case IRowEdited.STATUS_ADDED:
|
109 | case IRowEdited.STATUS_ORIGINAL:
|
||
110 | case IRowEdited.STATUS_MODIFIED:
|
||
111 | try {
|
||
112 | 6212 | fjp | |
113 | 6127 | fjp | for (int i = 0; i < record.length; i++) |
114 | record[i] = rowEdit.getAttribute(i); |
||
115 | dbfWrite.write(record); |
||
116 | numRows++; |
||
117 | 4143 | caballero | |
118 | 6127 | fjp | } catch (IOException e) { |
119 | e.printStackTrace(); |
||
120 | throw new EditionException(e); |
||
121 | } |
||
122 | 6212 | fjp | |
123 | 4143 | caballero | } |
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 | 6127 | fjp | |
133 | 4143 | caballero | } 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 | 4328 | fjp | public boolean canWriteAttribute(int sqlType) { |
145 | 6127 | fjp | switch (sqlType) {
|
146 | 4328 | fjp | case Types.DOUBLE: |
147 | 5389 | caballero | case Types.FLOAT: |
148 | 4328 | fjp | 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 | 5389 | caballero | return true; |
156 | 4328 | fjp | case Types.VARCHAR: |
157 | 5389 | caballero | case Types.CHAR: |
158 | 4328 | fjp | case Types.LONGVARCHAR: |
159 | 6127 | fjp | return true; // TODO: Revisar esto, porque no creo que admita |
160 | 6212 | fjp | // campos muy grandes
|
161 | 4328 | fjp | |
162 | } |
||
163 | 5389 | caballero | |
164 | 4328 | fjp | return false; |
165 | } |
||
166 | |||
167 | 6127 | fjp | public void initialize(ITableDefinition tableDefinition) |
168 | throws EditionException {
|
||
169 | 6259 | fjp | super.initialize(tableDefinition);
|
170 | 6127 | fjp | 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 | 5389 | caballero | |
178 | 6127 | fjp | } |
179 | |||
180 | public FieldDescription[] getOriginalFields() { |
||
181 | return originalFields;
|
||
182 | } |
||
183 | |||
184 | 6212 | fjp | public boolean alterTable() throws EditionException { |
185 | 6227 | fjp | FieldDescription[] fieldsDesc =getFields();
|
186 | 6212 | fjp | |
187 | 6127 | fjp | myHeader = DbaseFileHeaderNIO.createDbaseHeader(fieldsDesc); |
188 | try {
|
||
189 | dbfWrite = new DbaseFileWriterNIO(myHeader,
|
||
190 | (FileChannel) getWriteChannel(dbfPath));
|
||
191 | 6212 | fjp | // 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 | 6127 | fjp | } catch (IOException e) { |
200 | throw new EditionException(e); |
||
201 | } |
||
202 | 6227 | fjp | return true; |
203 | 6212 | fjp | } |
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 | 12910 | caballero | |
230 | 6212 | fjp | } |
231 | |||
232 | public FieldDescription[] getFields() { |
||
233 | ArrayList aux = new ArrayList(); |
||
234 | 6227 | fjp | for (int i=0; i < getOriginalFields().length; i++) |
235 | 6212 | fjp | { |
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 | 12910 | caballero | } |
270 | |||
271 | 6212 | fjp | } |
272 | return (FieldDescription[]) aux.toArray(new FieldDescription[0]); |
||
273 | } |
||
274 | |||
275 | 6621 | fjp | public boolean canAlterTable() { |
276 | return true; |
||
277 | } |
||
278 | |||
279 | 6856 | fjp | public boolean canSaveEdits() { |
280 | 12910 | caballero | File aux = new File(dbfPath); |
281 | 6856 | fjp | if (aux.canWrite()) return true; |
282 | return false; |
||
283 | } |
||
284 | |||
285 | 6212 | fjp | } |