root / 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 @ 38536
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.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 |
import org.gvsig.fmap.dal.DataServerExplorerParameters; |
46 |
import org.gvsig.fmap.dal.DataStoreParameters; |
47 |
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 |
DataServerExplorerParameters no_params = |
209 |
(DataServerExplorerParameters) datamanager |
210 |
.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
211 |
|
212 |
FilesystemServerExplorer explorer = |
213 |
(FilesystemServerExplorer) datamanager.openServerExplorer( |
214 |
FilesystemServerExplorer.NAME, no_params // empty params
|
215 |
); |
216 |
String providerName = explorer.getProviderName(endFile);
|
217 |
|
218 |
try {
|
219 |
if (endFile.exists()) {
|
220 |
endFile.delete(); |
221 |
} |
222 |
endFile.createNewFile(); |
223 |
|
224 |
DataStoreParameters dsp = |
225 |
explorer.createStoreParameters(endFile, providerName); |
226 |
NewFeatureStoreParameters parameters = |
227 |
(NewFeatureStoreParameters) datamanager |
228 |
.createNewStoreParameters(FilesystemServerExplorer.NAME, |
229 |
providerName); |
230 |
|
231 |
parameters.delegate(dsp); |
232 |
|
233 |
EditableFeatureType type = |
234 |
parameters.getDefaultFeatureType(); // .getEditable();
|
235 |
type.add(PluginServices.getText(this, "Nombre"), DataTypes.STRING, |
236 |
50);
|
237 |
type.add(PluginServices.getText(this, "Valor"), DataTypes.DOUBLE, |
238 |
100).setPrecision(25); |
239 |
|
240 |
parameters.setDefaultFeatureType(type); |
241 |
datamanager.newStore(FilesystemServerExplorer.NAME, providerName, |
242 |
parameters, true);
|
243 |
|
244 |
FeatureStore target = |
245 |
(FeatureStore) datamanager.openStore(providerName, parameters); |
246 |
target.edit(FeatureStore.MODE_APPEND); |
247 |
Iterator<MyObjectStatistics> iterador = valores.listIterator();
|
248 |
while (iterador.hasNext()) {
|
249 |
MyObjectStatistics data = iterador.next(); |
250 |
EditableFeature ef = target.createNewFeature().getEditable(); |
251 |
ef.set(PluginServices.getText(this, "Nombre"), data.getKey()); |
252 |
ef.set(PluginServices.getText(this, "Valor"), data.getValue()); |
253 |
target.insert(ef); |
254 |
} |
255 |
target.finishEditing(); |
256 |
target.dispose(); |
257 |
JOptionPane.showMessageDialog(
|
258 |
(Component) PluginServices.getMainFrame(),
|
259 |
PluginServices.getText(this, "fichero_creado_en") + " " |
260 |
+ endFile.getAbsolutePath(), |
261 |
PluginServices.getText(this, "fichero_creado_en_formato") |
262 |
+ " dbf", JOptionPane.INFORMATION_MESSAGE);// Informing the |
263 |
// user
|
264 |
} catch (Exception e) { |
265 |
e.printStackTrace(); |
266 |
} |
267 |
} |
268 |
} |
269 |
|
270 |
/**
|
271 |
* @author ?ngel Fraile Gri??n e-mail: angel.fraile@iver.es
|
272 |
*
|
273 |
* Class to work with the file extensions.
|
274 |
*/
|
275 |
class MyFileFilter extends FileFilter { |
276 |
|
277 |
private String[] extensiones = new String[1]; |
278 |
private String description; |
279 |
private boolean dirs = true; |
280 |
private String info = null; |
281 |
|
282 |
public MyFileFilter(String[] ext, String desc) { |
283 |
extensiones = ext; |
284 |
description = desc; |
285 |
} |
286 |
|
287 |
public MyFileFilter(String[] ext, String desc, String info) { |
288 |
extensiones = ext; |
289 |
description = desc; |
290 |
this.info = info;
|
291 |
} |
292 |
|
293 |
public MyFileFilter(String ext, String desc) { |
294 |
extensiones[0] = ext;
|
295 |
description = desc; |
296 |
} |
297 |
|
298 |
public MyFileFilter(String ext, String desc, String info) { |
299 |
extensiones[0] = ext;
|
300 |
description = desc; |
301 |
this.info = info;
|
302 |
} |
303 |
|
304 |
public MyFileFilter(String ext, String desc, boolean dirs) { |
305 |
extensiones[0] = ext;
|
306 |
description = desc; |
307 |
this.dirs = dirs;
|
308 |
} |
309 |
|
310 |
public MyFileFilter(String ext, String desc, boolean dirs, String info) { |
311 |
extensiones[0] = ext;
|
312 |
description = desc; |
313 |
this.dirs = dirs;
|
314 |
this.info = info;
|
315 |
} |
316 |
|
317 |
public boolean accept(File f) { |
318 |
if (f.isDirectory()) {
|
319 |
if (dirs) {
|
320 |
return true; |
321 |
} else {
|
322 |
return false; |
323 |
} |
324 |
} |
325 |
for (int i = 0; i < extensiones.length; i++) { |
326 |
if (extensiones[i].equals("")) { |
327 |
continue;
|
328 |
} |
329 |
if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])) {
|
330 |
return true; |
331 |
} |
332 |
} |
333 |
return false; |
334 |
} |
335 |
|
336 |
public String getDescription() { |
337 |
return description;
|
338 |
} |
339 |
|
340 |
public String[] getExtensions() { |
341 |
return extensiones;
|
342 |
} |
343 |
|
344 |
public boolean isDirectory() { |
345 |
return dirs;
|
346 |
} |
347 |
|
348 |
public String getExtensionOfAFile(File file) { |
349 |
String name;
|
350 |
int dotPos;
|
351 |
name = file.getName(); |
352 |
dotPos = name.lastIndexOf(".");
|
353 |
if (dotPos < 1) { |
354 |
return ""; |
355 |
} |
356 |
return name.substring(dotPos + 1); |
357 |
} |
358 |
|
359 |
public File normalizeExtension(File file) { |
360 |
String ext = getExtensionOfAFile(file);
|
361 |
if (ext.equals("") || !(this.accept(file))) { |
362 |
return new File(file.getAbsolutePath() + "." + extensiones[0]); |
363 |
} |
364 |
return file;
|
365 |
} |
366 |
|
367 |
public String getInfo() { |
368 |
return this.info; |
369 |
} |
370 |
|
371 |
public void setInfo(String info) { |
372 |
this.info = info;
|
373 |
} |
374 |
} |