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