Statistics
| Revision:

root / branches / v10 / extensions / extPublish / src-test / org / gvsig / remoteservices / conf / mapserver / test / AddFieldToDBF.java @ 13570

History | View | Annotate | Download (4.44 KB)

1 13570 jvhigon
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.Date;
47
import java.sql.Types;
48
import java.util.Calendar;
49
50
import com.hardcode.gdbms.engine.data.DataSourceFactory;
51
import com.hardcode.gdbms.engine.data.driver.DriverException;
52
import com.hardcode.gdbms.engine.values.Value;
53
import com.hardcode.gdbms.engine.values.ValueFactory;
54
import com.iver.cit.gvsig.fmap.core.DefaultRow;
55
import com.iver.cit.gvsig.fmap.core.IRow;
56
import com.iver.cit.gvsig.fmap.drivers.FieldDescription;
57
import com.iver.cit.gvsig.fmap.drivers.ITableDefinition;
58
import com.iver.cit.gvsig.fmap.drivers.TableDefinition;
59
import com.iver.cit.gvsig.fmap.drivers.dbf.DBFDriver;
60
import com.iver.cit.gvsig.fmap.edition.DefaultRowEdited;
61
import com.iver.cit.gvsig.fmap.edition.EditionException;
62
import com.iver.cit.gvsig.fmap.layers.SelectableDataSource;
63
64
/**
65
* @author fjp
66
*
67
* Abrimos un dbf y le a?adimos un campo. Lo rellenamos con la fecha actual con formato cadena.
68
*
69
*/
70
public class AddFieldToDBF  {
71
        public String dbfFile ="c:/0libro.dbf";
72
        public String fieldName = "date";
73
74
        public static void main(String[] args) {
75
                AddFieldToDBF prueba = new AddFieldToDBF();
76
                String []fechas = {"2000-12-01", "2000-11-17"};
77
                prueba.add("prb.dbf", "date", fechas);
78
        }
79
80
        public void add(String dbfFile, String fieldName, String[] values) {
81
       DBFDriver driver = new DBFDriver();
82
       File myFile = new File(dbfFile);
83
       try {
84
               System.out.println("Adding time to "+dbfFile);
85
               // Abrimos el fichero
86
                        driver.open(myFile);
87
88
                        // Comprobamos que tenemos acceso de escritura.
89
                        if (driver.canSaveEdits())
90
                        {
91
92
                                // Creamos el nuevo campo
93
94
                                FieldDescription dateField = new FieldDescription();
95
                                dateField.setFieldName(fieldName);
96
                                dateField.setFieldType(Types.VARCHAR);
97
                                dateField.setFieldLength(20);
98
99
                                // Creamos un array con el nuevo campo
100
                                ITableDefinition tableDef = driver.getTableDefinition();
101
                                FieldDescription[] oldFields = tableDef.getFieldsDesc();
102
                                int numOldFields = oldFields.length;
103
                                FieldDescription[] newFields = new FieldDescription[numOldFields + 1];
104
                                System.arraycopy(oldFields, 0, newFields, 0, numOldFields);
105
                                newFields[numOldFields] = dateField;
106
107
                                tableDef.setFieldsDesc(newFields);
108
109
                                driver.initialize(tableDef);
110
                                driver.preProcess();
111
                                Value[] att = new Value[newFields.length];
112
                                Calendar today = Calendar.getInstance();
113
                                for (int i=0; i < driver.getRowCount(); i++)
114
                                {
115
                                        for (int j=0; j < numOldFields; j++)
116
                                        {
117
                                                att[j] = driver.getFieldValue(i, j);
118
                                        }
119
                                        att[numOldFields] = ValueFactory.createValue(values[i]);
120
                                        DefaultRow row = new DefaultRow(att);
121
                                        DefaultRowEdited edRow = new DefaultRowEdited(row,
122
                                                        DefaultRowEdited.STATUS_MODIFIED, i);
123
124
                                        driver.process(edRow);
125
                                }
126
                                driver.postProcess();
127
128
                                System.out.println("Fichero modificado");
129
130
                        }
131
                        else
132
                        {
133
                                System.err.println("El fichero no tiene permiso de edici?n");
134
                        }
135
                        driver.close();
136
                } catch (IOException e) {
137
                        // TODO Auto-generated catch block
138
                        e.printStackTrace();
139
                } catch (EditionException e) {
140
                        // TODO Auto-generated catch block
141
                        e.printStackTrace();
142
                } catch (DriverException e) {
143
                        // TODO Auto-generated catch block
144
                        e.printStackTrace();
145
                }
146
147
148
149
        }
150
151
152
}