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