root / branches / v2_0_0_prep / applications / appgvSIG / src / org / gvsig / app / project / documents / table / ExportStatisticsFile.java @ 31496
History | View | Annotate | Download (10.8 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2005 IVER T.I. and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*/
|
19 |
|
20 |
|
21 |
package org.gvsig.app.project.documents.table; |
22 |
|
23 |
import java.awt.Component; |
24 |
import java.io.File; |
25 |
import java.io.FileWriter; |
26 |
import java.io.IOException; |
27 |
import java.util.Hashtable; |
28 |
import java.util.Iterator; |
29 |
import java.util.List; |
30 |
|
31 |
import javax.swing.JFileChooser; |
32 |
import javax.swing.JOptionPane; |
33 |
import javax.swing.filechooser.FileFilter; |
34 |
|
35 |
import org.gvsig.andami.PluginServices; |
36 |
import org.gvsig.andami.messages.NotificationManager; |
37 |
import org.gvsig.app.project.documents.table.gui.CSVSeparatorOptionsPanel; |
38 |
import org.gvsig.app.project.documents.table.gui.Statistics.MyObjectStatistics; |
39 |
import org.gvsig.fmap.dal.DALLocator; |
40 |
import org.gvsig.fmap.dal.DataManager; |
41 |
import org.gvsig.fmap.dal.DataTypes; |
42 |
import org.gvsig.fmap.dal.exception.DataException; |
43 |
import org.gvsig.fmap.dal.exception.ValidateDataParametersException; |
44 |
import org.gvsig.fmap.dal.feature.EditableFeature; |
45 |
import org.gvsig.fmap.dal.feature.EditableFeatureType; |
46 |
import org.gvsig.fmap.dal.feature.FeatureStore; |
47 |
import org.gvsig.fmap.dal.feature.NewFeatureStoreParameters; |
48 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorer; |
49 |
import org.gvsig.fmap.dal.serverexplorer.filesystem.FilesystemServerExplorerParameters; |
50 |
import org.slf4j.Logger; |
51 |
import org.slf4j.LoggerFactory; |
52 |
|
53 |
|
54 |
/**
|
55 |
* Class to create dbf and csv files at disk with the statistics group generated from a table.
|
56 |
*
|
57 |
* dbf -> Data Base File
|
58 |
* csv -> Comma Separated Value
|
59 |
*
|
60 |
* @author ?ngel Fraile Gri??n e-mail: angel.fraile@iver.es
|
61 |
*
|
62 |
*/
|
63 |
|
64 |
|
65 |
public class ExportStatisticsFile { |
66 |
private static final Logger logger = LoggerFactory |
67 |
.getLogger(ExportStatisticsFile.class); |
68 |
|
69 |
private String lastPath = null; |
70 |
private Hashtable<String, MyFileFilter> dbfExtensionsSupported; // Supported extensions. |
71 |
private Hashtable<String, MyFileFilter> csvExtensionsSupported; |
72 |
|
73 |
public ExportStatisticsFile(List<MyObjectStatistics> valores) { |
74 |
|
75 |
JFileChooser jfc = new JFileChooser(lastPath); |
76 |
jfc.removeChoosableFileFilter(jfc.getAcceptAllFileFilter()); |
77 |
|
78 |
// Adding required extensions (dbf, csv)
|
79 |
dbfExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
80 |
csvExtensionsSupported = new Hashtable<String, MyFileFilter>(); |
81 |
dbfExtensionsSupported.put("dbf", new MyFileFilter("dbf",PluginServices.getText(this, "Ficheros_dbf"), "dbf")); |
82 |
csvExtensionsSupported.put("csv", new MyFileFilter("csv",PluginServices.getText(this, "Ficheros_csv"), "csv")); |
83 |
|
84 |
Iterator<MyFileFilter> iter = csvExtensionsSupported.values().iterator();
|
85 |
while (iter.hasNext()) {
|
86 |
jfc.addChoosableFileFilter(iter.next()); |
87 |
} |
88 |
|
89 |
iter = dbfExtensionsSupported.values().iterator(); |
90 |
while (iter.hasNext()) {
|
91 |
jfc.addChoosableFileFilter(iter.next()); |
92 |
} |
93 |
|
94 |
// Opening a JFileCooser
|
95 |
if (jfc.showSaveDialog((Component) PluginServices.getMainFrame()) == JFileChooser.APPROVE_OPTION) { |
96 |
File endFile = jfc.getSelectedFile();
|
97 |
if (endFile.exists()){// File exists in the directory. |
98 |
int resp = JOptionPane.showConfirmDialog( |
99 |
(Component) PluginServices.getMainFrame(),
|
100 |
PluginServices.getText(this,
|
101 |
"fichero_ya_existe_seguro_desea_guardarlo")+"\n"+endFile.getAbsolutePath(), |
102 |
PluginServices.getText(this,"guardar"), JOptionPane.YES_NO_OPTION);// Informing the user |
103 |
if (resp != JOptionPane.YES_OPTION) {//cancel pressed. |
104 |
return;
|
105 |
} |
106 |
}//end if exits.
|
107 |
MyFileFilter filter = (MyFileFilter)jfc.getFileFilter();// dbf, csv
|
108 |
endFile = filter.normalizeExtension(endFile);//"name" + "." + "dbf", "name" + "." + "csv"
|
109 |
|
110 |
if(filter.getExtensionOfAFile(endFile).toLowerCase().compareTo("csv") == 0) { // csv file |
111 |
exportToCSVFile(valores,endFile); // export to csv format
|
112 |
} |
113 |
else if(filter.getExtensionOfAFile(endFile).toLowerCase().compareTo("dbf") == 0) {// dbf file |
114 |
try {
|
115 |
exportToDBFFile(valores,endFile); |
116 |
} catch (Exception e) { |
117 |
NotificationManager.addError(e); |
118 |
} // export to dbf format
|
119 |
} |
120 |
}//end if aprove option.
|
121 |
} |
122 |
|
123 |
/**
|
124 |
* Creating cvs format file with the statistics.
|
125 |
* Option to select the two columns separator.
|
126 |
*
|
127 |
* Example with semicolon: Name;data\n
|
128 |
* Name2;data2\n
|
129 |
*
|
130 |
* @param valores
|
131 |
* - Pairs: String name (key) + Double value (
|
132 |
* @param endFile
|
133 |
* - File to write the information
|
134 |
*/
|
135 |
|
136 |
private void exportToCSVFile(List<MyObjectStatistics> valores, File endFile) { |
137 |
|
138 |
try {
|
139 |
CSVSeparatorOptionsPanel csvSeparatorOptions = new CSVSeparatorOptionsPanel();
|
140 |
PluginServices.getMDIManager().addWindow(csvSeparatorOptions); |
141 |
|
142 |
String separator = csvSeparatorOptions.getSeparator();
|
143 |
|
144 |
if(separator != null) { |
145 |
|
146 |
FileWriter fileCSV = new FileWriter(endFile); |
147 |
|
148 |
fileCSV.write(PluginServices.getText(this, "Nombre") + separator + PluginServices.getText(this, "Valor")+ "\n"); |
149 |
|
150 |
Iterator<MyObjectStatistics> iterador = valores.listIterator();
|
151 |
|
152 |
while (iterador.hasNext()) {// Writing value,value\n |
153 |
MyObjectStatistics data= iterador.next(); |
154 |
fileCSV.write(data.getKey() + separator + (data.getValue())+ "\n");
|
155 |
} |
156 |
fileCSV.close(); |
157 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, "fichero_creado_en") + " " + endFile.getAbsolutePath(), |
158 |
PluginServices.getText(this, "fichero_creado_en_formato")+ " csv "+ |
159 |
PluginServices.getText(this, "mediante_el_separador")+ |
160 |
" \""+ separator + "\"", JOptionPane.INFORMATION_MESSAGE);// Informing the user |
161 |
} else {
|
162 |
return;
|
163 |
} |
164 |
|
165 |
} catch (IOException e) {// Informing the user |
166 |
logger.error("Error exportando a formato csv");
|
167 |
JOptionPane.showMessageDialog(null, PluginServices.getText(this, "Error_exportando_las_estadisticas") + " " + endFile.getAbsolutePath(), |
168 |
PluginServices.getText(this, "Error"), JOptionPane.ERROR_MESSAGE); |
169 |
} |
170 |
|
171 |
} |
172 |
|
173 |
public void exportToDBFFile(List<MyObjectStatistics> valores, File endFile) |
174 |
throws DataException, ValidateDataParametersException {
|
175 |
DataManager datamanager=DALLocator.getDataManager(); |
176 |
FilesystemServerExplorerParameters explorerParams=(FilesystemServerExplorerParameters) datamanager.createServerExplorerParameters(FilesystemServerExplorer.NAME); |
177 |
explorerParams.setRoot(endFile.getParent()); |
178 |
FilesystemServerExplorer explorer=(FilesystemServerExplorer) datamanager.createServerExplorer(explorerParams); |
179 |
NewFeatureStoreParameters newParams = (NewFeatureStoreParameters) explorer.getAddParameters(endFile); |
180 |
try {
|
181 |
EditableFeatureType type = newParams.getDefaultFeatureType() |
182 |
.getEditable(); |
183 |
type.add(PluginServices.getText(this, "Nombre"), DataTypes.STRING, 50); |
184 |
type.add(PluginServices.getText(this, "Valor"), DataTypes.DOUBLE, 100).setPrecision(25); |
185 |
newParams.setDefaultFeatureType(type); |
186 |
explorer.add(newParams, true);
|
187 |
DataManager manager = DALLocator.getDataManager(); |
188 |
FeatureStore target = (FeatureStore) manager.createStore(newParams); |
189 |
// FeatureType targetType = target.getDefaultFeatureType();
|
190 |
target.edit(FeatureStore.MODE_APPEND); |
191 |
Iterator<MyObjectStatistics> iterador = valores.listIterator();
|
192 |
while (iterador.hasNext()) {
|
193 |
MyObjectStatistics data= iterador.next(); |
194 |
EditableFeature ef=target.createNewFeature().getEditable();//new DefaultFeature(target).getEditable();//EditableFeature();
|
195 |
ef.set(PluginServices.getText(this, "Nombre"), data.getKey()); |
196 |
ef.set(PluginServices.getText(this, "Valor"), data.getValue()); |
197 |
target.insert(ef); |
198 |
} |
199 |
target.finishEditing(); |
200 |
target.dispose(); |
201 |
JOptionPane.showMessageDialog((Component)PluginServices.getMainFrame(), PluginServices.getText(this, "fichero_creado_en") + " " + endFile.getAbsolutePath(), |
202 |
PluginServices.getText(this, "fichero_creado_en_formato")+ " dbf", JOptionPane.INFORMATION_MESSAGE);// Informing the user |
203 |
} catch (Exception e) { |
204 |
e.printStackTrace(); |
205 |
} |
206 |
} |
207 |
} |
208 |
|
209 |
/**
|
210 |
* @author ?ngel Fraile Gri??n e-mail: angel.fraile@iver.es
|
211 |
*
|
212 |
* Class to work with the file extensions.
|
213 |
*/
|
214 |
class MyFileFilter extends FileFilter { |
215 |
|
216 |
private String[] extensiones = new String[1]; |
217 |
private String description; |
218 |
private boolean dirs = true; |
219 |
private String info = null; |
220 |
|
221 |
public MyFileFilter(String[] ext, String desc) { |
222 |
extensiones = ext; |
223 |
description = desc; |
224 |
} |
225 |
|
226 |
public MyFileFilter(String[] ext, String desc, String info) { |
227 |
extensiones = ext; |
228 |
description = desc; |
229 |
this.info = info;
|
230 |
} |
231 |
|
232 |
public MyFileFilter(String ext, String desc) { |
233 |
extensiones[0] = ext;
|
234 |
description = desc; |
235 |
} |
236 |
|
237 |
public MyFileFilter(String ext, String desc, String info) { |
238 |
extensiones[0] = ext;
|
239 |
description = desc; |
240 |
this.info = info;
|
241 |
} |
242 |
|
243 |
public MyFileFilter(String ext, String desc, boolean dirs) { |
244 |
extensiones[0] = ext;
|
245 |
description = desc; |
246 |
this.dirs = dirs;
|
247 |
} |
248 |
|
249 |
public MyFileFilter(String ext, String desc, boolean dirs, String info) { |
250 |
extensiones[0] = ext;
|
251 |
description = desc; |
252 |
this.dirs = dirs;
|
253 |
this.info = info;
|
254 |
} |
255 |
|
256 |
public boolean accept(File f) { |
257 |
if (f.isDirectory()) {
|
258 |
if (dirs) {
|
259 |
return true; |
260 |
} else {
|
261 |
return false; |
262 |
} |
263 |
} |
264 |
for (int i = 0; i < extensiones.length; i++) { |
265 |
if (extensiones[i].equals("")) { |
266 |
continue;
|
267 |
} |
268 |
if (getExtensionOfAFile(f).equalsIgnoreCase(extensiones[i])) {
|
269 |
return true; |
270 |
} |
271 |
} |
272 |
return false; |
273 |
} |
274 |
|
275 |
public String getDescription() { |
276 |
return description;
|
277 |
} |
278 |
|
279 |
public String[] getExtensions() { |
280 |
return extensiones;
|
281 |
} |
282 |
|
283 |
public boolean isDirectory() { |
284 |
return dirs;
|
285 |
} |
286 |
|
287 |
public String getExtensionOfAFile(File file) { |
288 |
String name;
|
289 |
int dotPos;
|
290 |
name = file.getName(); |
291 |
dotPos = name.lastIndexOf(".");
|
292 |
if (dotPos < 1) { |
293 |
return ""; |
294 |
} |
295 |
return name.substring(dotPos + 1); |
296 |
} |
297 |
|
298 |
public File normalizeExtension(File file) { |
299 |
String ext = getExtensionOfAFile(file);
|
300 |
if (ext.equals("") || !(this.accept(file))) { |
301 |
return new File(file.getAbsolutePath() + "." + extensiones[0]); |
302 |
} |
303 |
return file;
|
304 |
} |
305 |
|
306 |
public String getInfo() { |
307 |
return this.info; |
308 |
} |
309 |
|
310 |
public void setInfo(String info) { |
311 |
this.info = info;
|
312 |
} |
313 |
} |