svn-gvsig-desktop / trunk / extensions / extPublish / src / org / gvsig / remoteservices / conf / mapserver / test / AddFieldToDBF.java @ 10626
History | View | Annotate | Download (4.96 KB)
1 |
package org.gvsig.remoteservices.conf.mapserver.test; |
---|---|
2 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generali
|
3 |
* tat Valenciana
|
4 |
*
|
5 |
* Copyright (C) 2004 IVER T.I. and Generalitat Valenciana.
|
6 |
*
|
7 |
* This program is free software; you can redistribute it and/or
|
8 |
* modify it under the terms of the GNU General Public License
|
9 |
* as published by the Free Software Foundation; either version 2
|
10 |
* of the License, or (at your option) any later version.
|
11 |
*
|
12 |
* This program is distributed in the hope that it will be useful,
|
13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
15 |
* GNU General Public License for more details.
|
16 |
*
|
17 |
* You should have received a copy of the GNU General Public License
|
18 |
* along with this program; if not, write to the Free Software
|
19 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
20 |
*
|
21 |
* For more information, contact:
|
22 |
*
|
23 |
* Generalitat Valenciana
|
24 |
* Conselleria d'Infraestructures i Transport
|
25 |
* Av. Blasco Ib??ez, 50
|
26 |
* 46010 VALENCIA
|
27 |
* SPAIN
|
28 |
*
|
29 |
* +34 963862235
|
30 |
* gvsig@gva.es
|
31 |
* www.gvsig.gva.es
|
32 |
*
|
33 |
* or
|
34 |
*
|
35 |
* IVER T.I. S.A
|
36 |
* Salamanca 50
|
37 |
* 46005 Valencia
|
38 |
* Spain
|
39 |
*
|
40 |
* +34 963163400
|
41 |
* dac@iver.es
|
42 |
*/
|
43 |
|
44 |
import java.io.File; |
45 |
import java.io.IOException; |
46 |
import java.sql.Types; |
47 |
import java.util.Calendar; |
48 |
|
49 |
import com.hardcode.gdbms.driver.exceptions.CloseDriverException; |
50 |
import com.hardcode.gdbms.driver.exceptions.InitializeWriterException; |
51 |
import com.hardcode.gdbms.driver.exceptions.OpenDriverException; |
52 |
import com.hardcode.gdbms.driver.exceptions.ReadDriverException; |
53 |
import com.hardcode.gdbms.engine.data.driver.DriverException; |
54 |
import com.hardcode.gdbms.engine.values.Value; |
55 |
import com.hardcode.gdbms.engine.values.ValueFactory; |
56 |
import com.iver.cit.gvsig.exceptions.visitors.ProcessWriterVisitorException; |
57 |
import com.iver.cit.gvsig.exceptions.visitors.StartWriterVisitorException; |
58 |
import com.iver.cit.gvsig.exceptions.visitors.StopWriterVisitorException; |
59 |
import com.iver.cit.gvsig.fmap.core.DefaultRow; |
60 |
import com.iver.cit.gvsig.fmap.drivers.FieldDescription; |
61 |
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition; |
62 |
import com.iver.cit.gvsig.fmap.drivers.dbf.DBFDriver; |
63 |
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited; |
64 |
import com.iver.cit.gvsig.fmap.edition.IRowEdited; |
65 |
|
66 |
/**
|
67 |
* @author fjp
|
68 |
*
|
69 |
* Abrimos un dbf y le a?adimos un campo. Lo rellenamos con la fecha actual con formato cadena.
|
70 |
*
|
71 |
*/
|
72 |
public class AddFieldToDBF { |
73 |
public String dbfFile ="c:/0libro.dbf"; |
74 |
public String fieldName = "date"; |
75 |
|
76 |
public static void main(String[] args) { |
77 |
AddFieldToDBF prueba = new AddFieldToDBF();
|
78 |
String []fechas = {"2000-12-01", "2000-11-17"}; |
79 |
prueba.add("prb.dbf", "date", fechas); |
80 |
} |
81 |
|
82 |
public void add(String dbfFile, String fieldName, String[] values) { |
83 |
DBFDriver driver = new DBFDriver();
|
84 |
File myFile = new File(dbfFile); |
85 |
try {
|
86 |
System.out.println("Adding time to "+dbfFile); |
87 |
// Abrimos el fichero
|
88 |
driver.open(myFile); |
89 |
|
90 |
// Comprobamos que tenemos acceso de escritura.
|
91 |
if (driver.canSaveEdits())
|
92 |
{ |
93 |
|
94 |
// Creamos el nuevo campo
|
95 |
|
96 |
FieldDescription dateField = new FieldDescription();
|
97 |
dateField.setFieldName(fieldName); |
98 |
dateField.setFieldType(Types.VARCHAR);
|
99 |
dateField.setFieldLength(20);
|
100 |
|
101 |
// Creamos un array con el nuevo campo
|
102 |
ITableDefinition tableDef = driver.getTableDefinition(); |
103 |
FieldDescription[] oldFields = tableDef.getFieldsDesc();
|
104 |
int numOldFields = oldFields.length;
|
105 |
FieldDescription[] newFields = new FieldDescription[numOldFields + 1]; |
106 |
System.arraycopy(oldFields, 0, newFields, 0, numOldFields); |
107 |
newFields[numOldFields] = dateField; |
108 |
|
109 |
tableDef.setFieldsDesc(newFields); |
110 |
|
111 |
driver.initialize(tableDef); |
112 |
driver.preProcess(); |
113 |
Value[] att = new Value[newFields.length]; |
114 |
Calendar today = Calendar.getInstance(); |
115 |
for (int i=0; i < driver.getRowCount(); i++) |
116 |
{ |
117 |
for (int j=0; j < numOldFields; j++) |
118 |
{ |
119 |
att[j] = driver.getFieldValue(i, j); |
120 |
} |
121 |
att[numOldFields] = ValueFactory.createValue(values[i]); |
122 |
DefaultRow row = new DefaultRow(att);
|
123 |
DefaultRowEdited edRow = new DefaultRowEdited(row,
|
124 |
IRowEdited.STATUS_MODIFIED, i); |
125 |
|
126 |
driver.process(edRow); |
127 |
} |
128 |
driver.postProcess(); |
129 |
|
130 |
System.out.println("Fichero modificado"); |
131 |
|
132 |
} |
133 |
else
|
134 |
{ |
135 |
System.err.println("El fichero no tiene permiso de edici?n"); |
136 |
} |
137 |
driver.close(); |
138 |
} catch (OpenDriverException e) {
|
139 |
// TODO Auto-generated catch block
|
140 |
e.printStackTrace(); |
141 |
} catch (InitializeWriterException e) {
|
142 |
// TODO Auto-generated catch block
|
143 |
e.printStackTrace(); |
144 |
} catch (ReadDriverException e) {
|
145 |
// TODO Auto-generated catch block
|
146 |
e.printStackTrace(); |
147 |
} catch (StartWriterVisitorException e) {
|
148 |
// TODO Auto-generated catch block
|
149 |
e.printStackTrace(); |
150 |
} catch (ProcessWriterVisitorException e) {
|
151 |
// TODO Auto-generated catch block
|
152 |
e.printStackTrace(); |
153 |
} catch (StopWriterVisitorException e) {
|
154 |
// TODO Auto-generated catch block
|
155 |
e.printStackTrace(); |
156 |
} |
157 |
|
158 |
|
159 |
|
160 |
} |
161 |
|
162 |
|
163 |
} |