svn-gvsig-desktop / branches / v2_0_0_prep / extensions / org.gvsig.app.document.table.app / org.gvsig.app.document.table.app.mainplugin / src / main / java / org / gvsig / app / project / documents / table / ExportStatisticsFile.java @ 38413
History | View | Annotate | Download (13.6 KB)
1 | 36443 | cordinyana | /* gvSIG. Geographic Information System of the Valencian Government
|
---|---|---|---|
2 | *
|
||
3 | * Copyright (C) 2007-2008 Infrastructures and Transports Department
|
||
4 | * of the Valencian Government (CIT)
|
||
5 | *
|
||
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 | *
|
||
21 | */
|
||
22 | package org.gvsig.app.project.documents.table; |
||
23 | |||
24 | import java.awt.Component; |
||
25 | import java.io.File; |
||
26 | import java.io.FileWriter; |
||
27 | import java.io.IOException; |
||
28 | import java.util.Hashtable; |
||
29 | import java.util.Iterator; |
||
30 | import java.util.List; |
||
31 | |||
32 | import javax.swing.JFileChooser; |
||
33 | import javax.swing.JOptionPane; |
||
34 | import javax.swing.filechooser.FileFilter; |
||
35 | |||
36 | import org.slf4j.Logger; |
||
37 | import org.slf4j.LoggerFactory; |
||
38 | |||
39 | import org.gvsig.andami.PluginServices; |
||
40 | import org.gvsig.andami.messages.NotificationManager; |
||
41 | import org.gvsig.app.project.documents.table.gui.CSVSeparatorOptionsPanel; |
||
42 | import org.gvsig.app.project.documents.table.gui.Statistics.MyObjectStatistics; |
||
43 | import org.gvsig.fmap.dal.DALLocator; |
||
44 | import org.gvsig.fmap.dal.DataManager; |
||
45 | 38413 | jldominguez | import org.gvsig.fmap.dal.DataServerExplorerParameters; |
46 | import org.gvsig.fmap.dal.DataStoreParameters; |
||
47 | 36443 | cordinyana | import org.gvsig.fmap.dal.DataTypes; |
48 | import org.gvsig.fmap.dal.exception.DataException; |
||
49 | import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
||
50 | import org.gvsig.fmap.dal.feature.EditableFeature; |
||
51 | import org.gvsig.fmap.dal.feature.EditableFeatureType; |
||
52 | import org.gvsig.fmap.dal.feature.FeatureStore; |
||
53 | import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
||
54 | import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
||
55 | |||
56 | /**
|
||
57 | * Class to create dbf and csv files at disk with the statistics group generated
|
||
58 | * from a table.
|
||
59 | *
|
||
60 | * dbf -> Data Base File
|
||
61 | * csv -> Comma Separated Value
|
||
62 | *
|
||
63 | * @author ?ngel Fraile Gri??n e-mail: angel.fraile@iver.es
|
||
64 | *
|
||
65 | */
|
||
66 | |||
67 | public class ExportStatisticsFile { |
||
68 | |||
69 | private static final Logger logger = LoggerFactory |
||
70 | .getLogger(ExportStatisticsFile.class); |
||
71 | |||
72 | private String lastPath = null; |
||
73 | private Hashtable<String, MyFileFilter> dbfExtensionsSupported; // Supported |
||
74 | // extensions.
|
||
75 | private Hashtable<String, MyFileFilter> csvExtensionsSupported; |
||
76 | |||
77 | public ExportStatisticsFile(List<MyObjectStatistics> valores) { |
||
78 | |||
79 | JFileChooser jfc = new JFileChooser(lastPath); |
||
80 | jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); |
||
81 | |||
82 | // Adding required extensions (dbf, csv)
|
||
83 | dbfExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
||
84 | csvExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
||
85 | dbfExtensionsSupported.put("dbf", new MyFileFilter("dbf", |
||
86 | PluginServices.getText(this, "Ficheros_dbf"), "dbf")); |
||
87 | csvExtensionsSupported.put("csv", new MyFileFilter("csv", |
||
88 | PluginServices.getText(this, "Ficheros_csv"), "csv")); |
||
89 | |||
90 | Iterator<MyFileFilter> iter =
|
||
91 | csvExtensionsSupported.values().iterator(); |
||
92 | while (iter.hasNext()) {
|
||
93 | jfc.addChoosableFileFilter(iter.next()); |
||
94 | } |
||
95 | |||
96 | iter = dbfExtensionsSupported.values().iterator(); |
||
97 | while (iter.hasNext()) {
|
||
98 | jfc.addChoosableFileFilter(iter.next()); |
||
99 | } |
||
100 | |||
101 | // Opening a JFileCooser
|
||
102 | if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
||
103 | File endFile = jfc.getSelectedFile();
|
||
104 | if (endFile.exists()) {// File exists in the directory. |
||
105 | int resp =
|
||
106 | JOptionPane.showConfirmDialog(
|
||
107 | (Component) PluginServices.getMainFrame(),
|
||
108 | PluginServices.getText(this,
|
||
109 | "fichero_ya_existe_seguro_desea_guardarlo")
|
||
110 | + "\n"
|
||
111 | + endFile.getAbsolutePath(), PluginServices |
||
112 | .getText(this, "guardar"), |
||
113 | JOptionPane.YES_NO_OPTION);// Informing the user |
||
114 | if (resp != JOptionPane.YES_OPTION) {// cancel pressed. |
||
115 | return;
|
||
116 | } |
||
117 | }// end if exits.
|
||
118 | MyFileFilter filter = (MyFileFilter) jfc.getFileFilter();// dbf, csv
|
||
119 | endFile = filter.normalizeExtension(endFile);// "name" + "." +
|
||
120 | // "dbf", "name" + "."
|
||
121 | // + "csv"
|
||
122 | |||
123 | if (filter.getExtensionOfAFile(endFile).toLowerCase()
|
||
124 | .compareTo("csv") == 0) { // csv file |
||
125 | exportToCSVFile(valores, endFile); // export to csv format
|
||
126 | } else
|
||
127 | if (filter.getExtensionOfAFile(endFile).toLowerCase()
|
||
128 | .compareTo("dbf") == 0) {// dbf file |
||
129 | try {
|
||
130 | exportToDBFFile(valores, endFile); |
||
131 | } catch (Exception e) { |
||
132 | NotificationManager.addError(e); |
||
133 | } // export to dbf format
|
||
134 | } |
||
135 | }// end if aprove option.
|
||
136 | } |
||
137 | |||
138 | /**
|
||
139 | * Creating cvs format file with the statistics.
|
||
140 | * Option to select the two columns separator.
|
||
141 | *
|
||
142 | * Example with semicolon: Name;data\n
|
||
143 | * Name2;data2\n
|
||
144 | *
|
||
145 | * @param valores
|
||
146 | * - Pairs: String name (key) + Double value (
|
||
147 | * @param endFile
|
||
148 | * - File to write the information
|
||
149 | */
|
||
150 | |||
151 | private void exportToCSVFile(List<MyObjectStatistics> valores, File endFile) { |
||
152 | |||
153 | try {
|
||
154 | CSVSeparatorOptionsPanel csvSeparatorOptions = |
||
155 | new CSVSeparatorOptionsPanel();
|
||
156 | PluginServices.getMDIManager().addWindow(csvSeparatorOptions); |
||
157 | |||
158 | String separator = csvSeparatorOptions.getSeparator();
|
||
159 | |||
160 | if (separator != null) { |
||
161 | |||
162 | FileWriter fileCSV = new FileWriter(endFile); |
||
163 | |||
164 | fileCSV.write(PluginServices.getText(this, "Nombre") |
||
165 | + separator + PluginServices.getText(this, "Valor") + "\n"); |
||
166 | |||
167 | Iterator<MyObjectStatistics> iterador = valores.listIterator();
|
||
168 | |||
169 | while (iterador.hasNext()) {// Writing value,value\n |
||
170 | MyObjectStatistics data = iterador.next(); |
||
171 | fileCSV.write(data.getKey() + separator + (data.getValue()) |
||
172 | + "\n");
|
||
173 | } |
||
174 | fileCSV.close(); |
||
175 | JOptionPane.showMessageDialog(
|
||
176 | null,
|
||
177 | PluginServices.getText(this, "fichero_creado_en") + " " |
||
178 | + endFile.getAbsolutePath(), |
||
179 | PluginServices.getText(this, "fichero_creado_en_formato") |
||
180 | + " csv "
|
||
181 | + PluginServices.getText(this, "mediante_el_separador") |
||
182 | + " \"" + separator + "\"", |
||
183 | JOptionPane.INFORMATION_MESSAGE);// Informing the user |
||
184 | } else {
|
||
185 | return;
|
||
186 | } |
||
187 | |||
188 | } catch (IOException e) {// Informing the user |
||
189 | logger.error("Error exportando a formato csv");
|
||
190 | JOptionPane.showMessageDialog(
|
||
191 | null,
|
||
192 | PluginServices.getText(this,
|
||
193 | "Error_exportando_las_estadisticas")
|
||
194 | + " "
|
||
195 | + endFile.getAbsolutePath(), PluginServices.getText(this,
|
||
196 | "Error"), JOptionPane.ERROR_MESSAGE); |
||
197 | } |
||
198 | |||
199 | } |
||
200 | |||
201 | public void exportToDBFFile(List<MyObjectStatistics> valores, File endFile) |
||
202 | throws DataException, ValidateDataParametersException {
|
||
203 | DataManager datamanager = DALLocator.getDataManager(); |
||
204 | |||
205 | //
|
||
206 | // Averigua el proveedor en funcion del fichero
|
||
207 | // preguntandoselo al FilesystemServerExplorer.
|
||
208 | 38413 | jldominguez | DataServerExplorerParameters no_params = |
209 | (DataServerExplorerParameters) datamanager |
||
210 | .createServerExplorerParameters(FilesystemServerExplorer.NAME); |
||
211 | |||
212 | 36443 | cordinyana | FilesystemServerExplorer explorer = |
213 | (FilesystemServerExplorer) datamanager.openServerExplorer( |
||
214 | 38413 | jldominguez | FilesystemServerExplorer.NAME, no_params // empty params
|
215 | 36443 | cordinyana | ); |
216 | String providerName = explorer.getProviderName(endFile);
|
||
217 | |||
218 | try {
|
||
219 | 38413 | jldominguez | DataStoreParameters dsp = |
220 | explorer.createStoreParameters(endFile, providerName); |
||
221 | 36443 | cordinyana | NewFeatureStoreParameters parameters = |
222 | (NewFeatureStoreParameters) datamanager |
||
223 | .createNewStoreParameters(FilesystemServerExplorer.NAME, |
||
224 | providerName); |
||
225 | 38413 | jldominguez | |
226 | parameters.delegate(dsp); |
||
227 | |||
228 | 36443 | cordinyana | EditableFeatureType type = |
229 | 38413 | jldominguez | parameters.getDefaultFeatureType(); // .getEditable();
|
230 | 36443 | cordinyana | type.add(PluginServices.getText(this, "Nombre"), DataTypes.STRING, |
231 | 50);
|
||
232 | type.add(PluginServices.getText(this, "Valor"), DataTypes.DOUBLE, |
||
233 | 100).setPrecision(25); |
||
234 | |||
235 | parameters.setDefaultFeatureType(type); |
||
236 | datamanager.newStore(FilesystemServerExplorer.NAME, providerName, |
||
237 | parameters, true);
|
||
238 | |||
239 | FeatureStore target = |
||
240 | (FeatureStore) datamanager.openStore(providerName, parameters); |
||
241 | target.edit(FeatureStore.MODE_APPEND); |
||
242 | Iterator<MyObjectStatistics> iterador = valores.listIterator();
|
||
243 | while (iterador.hasNext()) {
|
||
244 | MyObjectStatistics data = iterador.next(); |
||
245 | EditableFeature ef = target.createNewFeature().getEditable(); |
||
246 | ef.set(PluginServices.getText(this, "Nombre"), data.getKey()); |
||
247 | ef.set(PluginServices.getText(this, "Valor"), data.getValue()); |
||
248 | target.insert(ef); |
||
249 | } |
||
250 | target.finishEditing(); |
||
251 | target.dispose(); |
||
252 | JOptionPane.showMessageDialog(
|
||
253 | (Component) PluginServices.getMainFrame(),
|
||
254 | PluginServices.getText(this, "fichero_creado_en") + " " |
||
255 | + endFile.getAbsolutePath(), |
||
256 | PluginServices.getText(this, "fichero_creado_en_formato") |
||
257 | + " dbf", JOptionPane.INFORMATION_MESSAGE);// Informing the |
||
258 | // user
|
||
259 | } catch (Exception e) { |
||
260 | e.printStackTrace(); |
||
261 | } |
||
262 | } |
||
263 | } |
||
264 | |||
265 | /**
|
||
266 | * @author ?ngel Fraile Gri??n e-mail: angel.fraile@iver.es
|
||
267 | *
|
||
268 | * Class to work with the file extensions.
|
||
269 | */
|
||
270 | class MyFileFilter extends FileFilter { |
||
271 | |||
272 | private String[] extensiones = new String[1]; |
||
273 | private String description; |
||
274 | private boolean dirs = true; |
||
275 | private String info = null; |
||
276 | |||
277 | public MyFileFilter(String[] ext, String desc) { |
||
278 | extensiones = ext; |
||
279 | description = desc; |
||
280 | } |
||
281 | |||
282 | public MyFileFilter(String[] ext, String desc, String info) { |
||
283 | extensiones = ext; |
||
284 | description = desc; |
||
285 | this.info = info;
|
||
286 | } |
||
287 | |||
288 | public MyFileFilter(String ext, String desc) { |
||
289 | extensiones[0] = ext;
|
||
290 | description = desc; |
||
291 | } |
||
292 | |||
293 | public MyFileFilter(String ext, String desc, String info) { |
||
294 | extensiones[0] = ext;
|
||
295 | description = desc; |
||
296 | this.info = info;
|
||
297 | } |
||
298 | |||
299 | public MyFileFilter(String ext, String desc, boolean dirs) { |
||
300 | extensiones[0] = ext;
|
||
301 | description = desc; |
||
302 | this.dirs = dirs;
|
||
303 | } |
||
304 | |||
305 | public MyFileFilter(String ext, String desc, boolean dirs, String info) { |
||
306 | extensiones[0] = ext;
|
||
307 | description = desc; |
||
308 | this.dirs = dirs;
|
||
309 | this.info = info;
|
||
310 | } |
||
311 | |||
312 | public boolean accept(File f) { |
||
313 | if (f.isDirectory()) {
|
||
314 | if (dirs) {
|
||
315 | return true; |
||
316 | } else {
|
||
317 | return false; |
||
318 | } |
||
319 | } |
||
320 | for (int i = 0; i < extensiones.length; i++) { |
||
321 | if (extensiones[i].equals("")) { |
||
322 | continue;
|
||
323 | } |
||
324 | if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])) {
|
||
325 | return true; |
||
326 | } |
||
327 | } |
||
328 | return false; |
||
329 | } |
||
330 | |||
331 | public String getDescription() { |
||
332 | return description;
|
||
333 | } |
||
334 | |||
335 | public String[] getExtensions() { |
||
336 | return extensiones;
|
||
337 | } |
||
338 | |||
339 | public boolean isDirectory() { |
||
340 | return dirs;
|
||
341 | } |
||
342 | |||
343 | public String getExtensionOfAFile(File file) { |
||
344 | String name;
|
||
345 | int dotPos;
|
||
346 | name = file.getName(); |
||
347 | dotPos = name.lastIndexOf(".");
|
||
348 | if (dotPos < 1) { |
||
349 | return ""; |
||
350 | } |
||
351 | return name.substring(dotPos + 1); |
||
352 | } |
||
353 | |||
354 | public File normalizeExtension(File file) { |
||
355 | String ext = getExtensionOfAFile(file);
|
||
356 | if (ext.equals("") || !(this.accept(file))) { |
||
357 | return new File(file.getAbsolutePath() + "." + extensiones[0]); |
||
358 | } |
||
359 | return file;
|
||
360 | } |
||
361 | |||
362 | public String getInfo() { |
||
363 | return this.info; |
||
364 | } |
||
365 | |||
366 | public void setInfo(String info) { |
||
367 | this.info = info;
|
||
368 | } |
||
369 | } |