Statistics
| Revision:

root / branches / v2_0_0_prep / extensions / extSextanteGvsigBindings / src / es / unex / sextante / gvsig / core / gvTable.java @ 26840

History | View | Annotate | Download (7.12 KB)

1
package es.unex.sextante.gvsig.core;
2

    
3
import java.io.File;
4
import java.io.IOException;
5
import java.sql.Types;
6

    
7
import org.gvsig.fmap.dal.DataTypes;
8
import org.gvsig.fmap.dal.exception.DataException;
9
import org.gvsig.fmap.dal.exception.ReadException;
10
import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor;
11
import org.gvsig.fmap.dal.feature.FeatureSet;
12
import org.gvsig.fmap.dal.feature.FeatureStore;
13
import org.gvsig.fmap.dal.feature.FeatureType;
14
import org.gvsig.project.document.table.FeatureTableDocument;
15

    
16
import com.iver.cit.gvsig.project.ProjectFactory;
17

    
18

    
19
import es.unex.sextante.dataObjects.AbstractTable;
20
import es.unex.sextante.dataObjects.IRecordsetIterator;
21

    
22
public class gvTable extends AbstractTable {
23

    
24
        private Object m_BaseDataObject;
25
        private String m_sName;
26
        private String m_sFilename;
27
        private FeatureSet featureSet;
28
        private FeatureType featureType;
29
        private FeatureStore featureStore;
30

    
31
        public String getName() {
32

    
33
                if (m_BaseDataObject instanceof FeatureTableDocument){
34
                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
35
                        return table.getName();
36
                }
37
                else{
38
                        return m_sName;
39
                }
40

    
41
        }
42

    
43
        public void create(Object obj) {
44

    
45
                if (obj instanceof FeatureTableDocument){
46
                        try {
47
                                featureStore =((FeatureTableDocument)obj).getStore();
48
                                featureSet = featureStore.getFeatureSet(((FeatureTableDocument)obj).getQuery());
49
                                featureType = featureStore.getDefaultFeatureType();
50

    
51
                        } catch (DataException e) {
52
                                e.printStackTrace();
53
                        }
54
                }
55

    
56
        }
57

    
58
        public void create(String sName, String sFilename, Class[] types, String[] sFields) {
59

    
60
//                m_sFilename = sFilename;
61
//                TableMemoryDriver table = new TableMemoryDriver(sFields, DataTools.getgvSIGTypes(types));
62
//                m_sName = sName;
63
//                m_BaseDataObject = table;
64

    
65
        }
66

    
67
        public void addRecord(Object[] record) {
68

    
69
//                if (m_BaseDataObject instanceof TableMemoryDriver) {
70
//                        TableMemoryDriver table = (TableMemoryDriver) m_BaseDataObject;
71
//                        table.addRow(DataTools.getGVSIGValues(record));
72
//                }
73

    
74
        }
75

    
76
        public IRecordsetIterator iterator() {
77

    
78
                return new gvRecordsetIterator(featureSet);
79

    
80
        }
81

    
82
        public String getFieldName(int i) {
83
                return ((FeatureAttributeDescriptor)featureType.get(i)).getName();
84
//                if (m_BaseDataObject instanceof FeatureTableDocument) {
85
//                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
86
//                        try {
87
//                                return table.getModelo().getRecordset().getFieldName(i);
88
//                        } catch (Exception e) {
89
//                                return "";
90
//                        }
91
//                }
92
//                else{
93
//                        TableMemoryDriver table = (TableMemoryDriver) m_BaseDataObject;
94
//                        return table.getFieldName(i);
95
//                }
96

    
97
        }
98

    
99
        public Class getFieldType(int i) {
100
                return DataTools.getTypeClass(((FeatureAttributeDescriptor)featureType.get(i)).getDataType());
101
//                if (m_BaseDataObject instanceof FeatureTableDocument) {
102
//                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
103
//                        try {
104
//                                int iType = table.getModelo().getRecordset().getFieldType(i);
105
//                                return DataTools.getTypeClass(iType);
106
//                        } catch (Exception e) {
107
//                                return String.class;
108
//                        }
109
//                }
110
//                else{
111
//                        TableMemoryDriver table = (TableMemoryDriver) m_BaseDataObject;
112
//                        int iType;
113
//                        iType = table.getFieldType(i);
114
//                        return DataTools.getTypeClass(iType);
115
//                }
116

    
117
        }
118

    
119
        public int getFieldCount() {
120
                return featureType.size();
121
//                if (m_BaseDataObject instanceof FeatureTableDocument) {
122
//                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
123
//                        try {
124
//                                return table.getModelo().getRecordset().getFieldCount();
125
//                        } catch (Exception e) {
126
//                                return 0;
127
//                        }
128
//                }
129
//                else{
130
//                        TableMemoryDriver table = (TableMemoryDriver) m_BaseDataObject;
131
//                        return table.getFieldCount();
132
//                }
133

    
134
        }
135

    
136
        public long getRecordCount() {
137
                try {
138
                        return featureSet.getSize();
139
                } catch (DataException e) {
140
                        e.printStackTrace();
141
                }
142
                return 0;
143
//                if (m_BaseDataObject instanceof FeatureTableDocument) {
144
//                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
145
//                        try {
146
//                                return table.getModelo().getRecordset().getRowCount();
147
//                        } catch (Exception e) {
148
//                                return 0;
149
//                        }
150
//                }
151
//                else{
152
//                        TableMemoryDriver table = (TableMemoryDriver) m_BaseDataObject;
153
//                        return table.getRowCount();
154
//                }
155

    
156
        }
157

    
158
        public void postProcess() {
159

    
160
//                SelectableDataSource source;
161
//                ITableDefinition orgDef;
162
//                FileDriver driver;
163
                File file;
164
                try {
165
//                        LayerFactory.getDataSourceFactory().addDataSource((TableMemoryDriver)m_BaseDataObject,
166
//                                        m_sName);
167
//                        DataSource dataSource = LayerFactory.getDataSourceFactory().
168
//                        createRandomDataSource(m_sName);
169
//                        dataSource.start();
170
//                        SelectableDataSource sds = new SelectableDataSource(dataSource);
171
//                        EditableAdapter auxea = new EditableAdapter();
172
//                        auxea.setOriginalDataSource(sds);
173
                        FeatureTableDocument table = ProjectFactory.createTable(m_sName, featureStore);
174
                        file = new File(m_sFilename);
175
//                        driver = (FileDriver) LayerFactory.getDM().getDriver("gdbms dbf driver");
176
//                        source = table.getModelo().getRecordset();
177
//                        source.start();
178
//                        orgDef = table.getModelo().getTableDefinition();
179
                } catch (Exception e) {
180
                        return;
181
                }
182

    
183
//                try {
184
//                        if (!file.exists()){
185
//                                driver.createSource(file.getAbsolutePath(),new String[] {"0"},new int[] {Types.INTEGER} );
186
//                                file.createNewFile();
187
//                        }
188
//                        driver.open(file);
189
//                } catch (IOException e) {
190
//                        e.printStackTrace();
191
//                        return;
192
//                } catch (ReadException ex) {
193
//                        ex.printStackTrace();
194
//                        return;
195
//                }
196

    
197
//                IWriter writer = ((IWriteable)driver).getWriter();
198
//                try {
199
//                        writer.initialize(orgDef);
200
//                        writer.preProcess();
201
//                        SourceIterator sourceIter = new SourceIterator(source);
202
//                        IFeature feature;
203
//                        int i=0;
204
//                        while (sourceIter.hasNext()){
205
//                                 feature = sourceIter.nextFeature();
206
//
207
//                                 DefaultRowEdited edRow = new DefaultRowEdited(feature,
208
//                                                 DefaultRowEdited.STATUS_ADDED, i);
209
//                                 writer.process(edRow);
210
//                                 i++;
211
//                        }
212
//                        writer.postProcess();
213
//                } catch (Exception e) {
214
//                        return;
215
//                }
216

    
217
                FeatureTableDocument table = FileTools.openTable(m_sFilename, m_sName);
218
                create(table);
219

    
220
        }
221

    
222
        public void open() {
223

    
224
//                if (m_BaseDataObject instanceof FeatureTableDocument){
225
//                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
226
//                        try {
227
//                                table.getModelo().getRecordset().start();
228
//                        } catch (Exception e) {
229
//                                e.printStackTrace();
230
//                        }
231
//                }
232

    
233
        }
234

    
235
        public void close() {
236

    
237
//                if (m_BaseDataObject instanceof FeatureTableDocument){
238
//                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
239
//                        try {
240
//                                table.getModelo().getRecordset().stop();
241
//                        } catch (Exception e) {
242
//                                e.printStackTrace();
243
//                        }
244
//                }
245

    
246
        }
247

    
248
        public String getFilename() {
249

    
250
                return m_sFilename;
251

    
252
        }
253

    
254
        public void setName(String name) {
255

    
256
                if (m_BaseDataObject instanceof FeatureTableDocument){
257
                        FeatureTableDocument table = (FeatureTableDocument) m_BaseDataObject;
258
                        table.setName(name);
259
                }
260
                else{
261
                        m_sName = name;
262
                }
263

    
264
        }
265

    
266
        public Object getBaseDataObject() {
267

    
268
                return m_BaseDataObject;
269

    
270
        }
271

    
272

    
273
}