gvsig-geoprocess / org.gvsig.geoprocess / trunk / org.gvsig.geoprocess / org.gvsig.geoprocess.lib / org.gvsig.geoprocess.lib.sextante / src / main / java / org / gvsig / geoprocess / lib / sextante / dataObjects / TableDocumentITable.java @ 657
History | View | Annotate | Download (8.14 KB)
1 | 237 | cordinyana | /**
|
---|---|---|---|
2 | * gvSIG. Desktop Geographic Information System.
|
||
3 | *
|
||
4 | 245 | cordinyana | * Copyright (C) 2007-2012 gvSIG Association.
|
5 | 237 | cordinyana | *
|
6 | * This program is free software; you can redistribute it and/or
|
||
7 | * modify it under the terms of the GNU General Public License
|
||
8 | * as published by the Free Software Foundation; either version 2
|
||
9 | * of the License, or (at your option) any later version.
|
||
10 | *
|
||
11 | * This program is distributed in the hope that it will be useful,
|
||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
14 | * GNU General Public License for more details.
|
||
15 | *
|
||
16 | * You should have received a copy of the GNU General Public License
|
||
17 | * along with this program; if not, write to the Free Software
|
||
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
||
19 | * MA 02110-1301, USA.
|
||
20 | 245 | cordinyana | *
|
21 | * For any additional information, do not hesitate to contact us
|
||
22 | * at info AT gvsig.com, or visit our website www.gvsig.com.
|
||
23 | 237 | cordinyana | */
|
24 | 220 | cordinyana | package org.gvsig.geoprocess.lib.sextante.dataObjects; |
25 | |||
26 | import java.io.File; |
||
27 | import java.util.Iterator; |
||
28 | |||
29 | import es.unex.sextante.core.Sextante; |
||
30 | import es.unex.sextante.dataObjects.AbstractTable; |
||
31 | import es.unex.sextante.dataObjects.IRecordsetIterator; |
||
32 | import es.unex.sextante.outputs.FileOutputChannel; |
||
33 | import es.unex.sextante.outputs.IOutputChannel; |
||
34 | |||
35 | import org.gvsig.app.project.ProjectManager; |
||
36 | import org.gvsig.app.project.documents.table.TableDocument; |
||
37 | import org.gvsig.app.project.documents.table.TableManager; |
||
38 | import org.gvsig.fmap.dal.DALLocator; |
||
39 | import org.gvsig.fmap.dal.DataManager; |
||
40 | import org.gvsig.fmap.dal.exception.DataException; |
||
41 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
42 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
43 | import org.gvsig.fmap.dal.feature.FeatureAttributeDescriptor; |
||
44 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
45 | import org.gvsig.fmap.dal.feature.FeatureType; |
||
46 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
47 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
48 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
||
49 | import org.gvsig.tools.ToolsLocator; |
||
50 | import org.gvsig.tools.dataTypes.DataTypesManager; |
||
51 | |||
52 | 225 | cordinyana | public class TableDocumentITable extends AbstractTable { |
53 | 220 | cordinyana | |
54 | private final int PRECISION = 5; |
||
55 | private TableDocument m_Table;
|
||
56 | private String m_sName; |
||
57 | private String m_sFilename; |
||
58 | private FeatureType featureType;
|
||
59 | private FeatureStore featureStore;
|
||
60 | |||
61 | public String getName() { |
||
62 | |||
63 | if (m_Table != null) { |
||
64 | return m_Table.getName();
|
||
65 | } else {
|
||
66 | return m_sName;
|
||
67 | } |
||
68 | |||
69 | } |
||
70 | |||
71 | public void create(final TableDocument table) { |
||
72 | |||
73 | try {
|
||
74 | m_Table = table; |
||
75 | featureStore = table.getStore(); |
||
76 | featureType = featureStore.getDefaultFeatureType(); |
||
77 | } catch (final DataException e) { |
||
78 | Sextante.addErrorToLog(e); |
||
79 | } |
||
80 | |||
81 | } |
||
82 | |||
83 | public void create(final String sName, final String sFilename, |
||
84 | final Class<?>[] types, final String[] sFields) { |
||
85 | |||
86 | try {
|
||
87 | m_sName = sName; |
||
88 | |||
89 | final DataManager datamanager = DALLocator.getDataManager();
|
||
90 | final FilesystemServerExplorerParameters explorerParams =
|
||
91 | (FilesystemServerExplorerParameters) datamanager |
||
92 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
93 | explorerParams.setRoot(new File(sFilename).getParent()); |
||
94 | final FilesystemServerExplorer explorer =
|
||
95 | (FilesystemServerExplorer) datamanager.openServerExplorer( |
||
96 | FilesystemServerExplorer.NAME, explorerParams); |
||
97 | final NewFeatureStoreParameters newParams =
|
||
98 | (NewFeatureStoreParameters) explorer.getAddParameters(new File( |
||
99 | sFilename)); |
||
100 | |||
101 | EditableFeatureType editableFeatureType = |
||
102 | newParams.getDefaultFeatureType(); |
||
103 | featureType = editableFeatureType; |
||
104 | loadFeatureType(sFields, types, editableFeatureType); |
||
105 | |||
106 | newParams.setDefaultFeatureType(featureType); |
||
107 | |||
108 | explorer.add(newParams.getDataStoreName(), newParams, true);
|
||
109 | final DataManager manager = DALLocator.getDataManager();
|
||
110 | featureStore = |
||
111 | (FeatureStore) manager.openStore(newParams.getDataStoreName(), |
||
112 | newParams); |
||
113 | featureStore.edit(FeatureStore.MODE_APPEND); |
||
114 | featureType = featureStore.getDefaultFeatureType(); |
||
115 | m_sFilename = sFilename; |
||
116 | |||
117 | } catch (final Exception e) { |
||
118 | Sextante.addErrorToLog(e); |
||
119 | } |
||
120 | |||
121 | } |
||
122 | |||
123 | private void loadFeatureType(final String[] fields, final Class<?>[] types, |
||
124 | final EditableFeatureType featureType) {
|
||
125 | final int[] iTypes = getTypes(types); |
||
126 | |||
127 | for (int i = 0; i < fields.length; i++) { |
||
128 | featureType.add(fields[i], iTypes[i]).setPrecision(PRECISION); |
||
129 | } |
||
130 | } |
||
131 | |||
132 | private int[] getTypes(Class<?>[] classes) { |
||
133 | DataTypesManager typesManager = ToolsLocator.getDataTypesManager(); |
||
134 | int[] types = new int[classes.length]; |
||
135 | for (int i = 0; i < classes.length; i++) { |
||
136 | types[i] = typesManager.getDataType(classes[i]).getType(); |
||
137 | } |
||
138 | return types;
|
||
139 | } |
||
140 | |||
141 | public void addRecord(final Object[] record) { |
||
142 | try {
|
||
143 | final EditableFeature ef = featureStore.createNewFeature();
|
||
144 | @SuppressWarnings("unchecked") |
||
145 | final Iterator<FeatureAttributeDescriptor> features = |
||
146 | featureType.iterator(); |
||
147 | |||
148 | int i = 0; |
||
149 | while (features.hasNext()) {
|
||
150 | final FeatureAttributeDescriptor featureAttributeDescriptor =
|
||
151 | features.next(); |
||
152 | ef.set(featureAttributeDescriptor.getName(), record[i]); |
||
153 | i++; |
||
154 | } |
||
155 | featureStore.insert(ef); |
||
156 | } catch (final Exception e) { |
||
157 | e.printStackTrace(); |
||
158 | } |
||
159 | |||
160 | } |
||
161 | |||
162 | public IRecordsetIterator iterator() {
|
||
163 | try {
|
||
164 | 225 | cordinyana | return new FeatureSetIRecordsetIterator(featureStore.getFeatureSet(m_Table |
165 | 220 | cordinyana | .getQuery())); |
166 | } catch (final DataException e) { |
||
167 | throw new RuntimeException(e); |
||
168 | } |
||
169 | } |
||
170 | |||
171 | public String getFieldName(final int i) { |
||
172 | return ((FeatureAttributeDescriptor) featureType.get(i)).getName();
|
||
173 | } |
||
174 | |||
175 | public Class<?> getFieldType(final int i) { |
||
176 | return featureType.getAttributeDescriptor(i).getDataType()
|
||
177 | .getDefaultClass(); |
||
178 | } |
||
179 | |||
180 | public int getFieldCount() { |
||
181 | return featureType.size();
|
||
182 | } |
||
183 | |||
184 | public long getRecordCount() { |
||
185 | try {
|
||
186 | return featureStore.getFeatureSet(m_Table.getQuery()).getSize();
|
||
187 | } catch (final DataException e) { |
||
188 | e.printStackTrace(); |
||
189 | } |
||
190 | return 0; |
||
191 | } |
||
192 | |||
193 | public void postProcess() { |
||
194 | 657 | fdiaz | if(featureStore.isAppending() || featureStore.isEditing()) {
|
195 | try {
|
||
196 | featureStore.finishEditing(); |
||
197 | } catch (DataException e) {
|
||
198 | return;
|
||
199 | } |
||
200 | } |
||
201 | 220 | cordinyana | try {
|
202 | final TableDocument table =
|
||
203 | (TableDocument) ProjectManager.getInstance().createDocument( |
||
204 | TableManager.TYPENAME, m_sName); |
||
205 | table.setStore(featureStore); |
||
206 | } catch (final Exception e) { |
||
207 | return;
|
||
208 | } |
||
209 | |||
210 | final TableDocument table = FileTools.openTable(m_sFilename, m_sName);
|
||
211 | create(table); |
||
212 | } |
||
213 | |||
214 | public void open() { |
||
215 | |||
216 | } |
||
217 | |||
218 | public void close() { |
||
219 | // Nothing to do
|
||
220 | } |
||
221 | |||
222 | public String getFilename() { |
||
223 | |||
224 | return m_sFilename;
|
||
225 | |||
226 | } |
||
227 | |||
228 | public void setName(final String name) { |
||
229 | |||
230 | if (m_Table != null) { |
||
231 | m_Table.setName(name); |
||
232 | } else {
|
||
233 | m_sName = name; |
||
234 | } |
||
235 | |||
236 | } |
||
237 | |||
238 | public Object getBaseDataObject() { |
||
239 | |||
240 | return m_Table;
|
||
241 | |||
242 | } |
||
243 | |||
244 | public void free() { |
||
245 | |||
246 | m_Table = null;
|
||
247 | |||
248 | } |
||
249 | |||
250 | public IOutputChannel getOutputChannel() {
|
||
251 | |||
252 | return new FileOutputChannel(m_sFilename); |
||
253 | |||
254 | } |
||
255 | } |