Cambiada la libreria utilizada para manejar los archivos del informe, se usa NIO ahora y deberia estar solucionado los errores con windows.

This commit is contained in:
Daniel Cortes
2019-01-19 19:09:11 -03:00
parent 9ed451ab4f
commit 5ebc2cc7c3
7 changed files with 481 additions and 424 deletions

View File

@@ -38,6 +38,10 @@ import javax.swing.filechooser.FileNameExtensionFilter;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
@@ -64,13 +68,13 @@ public class InformesController {
String formatedName = month.format(formatter);
String capitalized = StringUtils.toUpperCase(formatedName);
File saveFile = askForFile("Libro " + capitalized);
Path saveFile = askForFile("Libro " + capitalized);
if (saveFile == null) {
return;
}
InformeLibroDeVentas informe = new InformeLibroDeVentas(month, saveFile);
File generatedFile = informe.generarInforme();
Path generatedFile = informe.generarInforme();
this.showConfirmation(generatedFile);
}
@@ -89,13 +93,13 @@ public class InformesController {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("MMMM YYYY");
String formatedMonth = month.format(formatter);
File saveFile = askForFile("Informe Egresos - " + tipoEgreso.getNombre() + " - " + StringUtils.toUpperCase(formatedMonth));
Path saveFile = askForFile("Informe Egresos - " + tipoEgreso.getNombre() + " - " + StringUtils.toUpperCase(formatedMonth));
if (saveFile == null) {
return;
}
InformeEgresos informe = new InformeEgresos(tipoEgreso.getId(), month, saveFile);
File generatedFile = informe.generarInforme();
Path generatedFile = informe.generarInforme();
this.showConfirmation(generatedFile);
}
@@ -118,28 +122,58 @@ public class InformesController {
}
}
private File askForFile(String suggestedName) {
private Path askForFile(String suggestedName) {
JFileChooser chooser = new JFileChooser();
chooser.setDialogType(JFileChooser.SAVE_DIALOG);
chooser.setSelectedFile(new File(suggestedName + ".xls"));
chooser.setFileFilter(new FileNameExtensionFilter("Excel 2007", "xls"));
if (chooser.showSaveDialog(this.view.getContentPanel()) == JFileChooser.APPROVE_OPTION) {
String filename = chooser.getSelectedFile().getPath();
if (!FileUtils.isValidPath(filename)) {
JOptionPane.showMessageDialog(this.view.getContentPanel(),"El archivo seleccionado no es valido","Archivo no valido", JOptionPane.ERROR_MESSAGE);
return null;
} else if (!filename.endsWith(".xls")) {
filename += ".xls";
String pathString = chooser.getSelectedFile().getPath();
if (!pathString.endsWith(".xls")) {
pathString = pathString + ".xls";
}
return new File(filename);
Path path = Paths.get(pathString);
try {
Files.createFile(path);
} catch (FileAlreadyExistsException e) {
int response = JOptionPane.showConfirmDialog(
this.view.getContentPanel(),
"El archivo ya existe\n¿Desea sobreescribirlo?",
"Confirmacion",
JOptionPane.YES_NO_OPTION,
JOptionPane.WARNING_MESSAGE
);
if (response != 0) {
return null;
}
} catch (IOException e) {
e.printStackTrace();
JOptionPane.showMessageDialog(
this.view.getContentPanel(),
"No a sido posible crear el archivo",
"Error!",
JOptionPane.ERROR_MESSAGE
);
return null;
}
return path;
} else {
return null;
}
}
private void showConfirmation(File file) {
private void showConfirmation(Path path) {
int result = JOptionPane.showConfirmDialog(
this.view.getContentPanel(),
"El informes se a generado\n" +
@@ -148,9 +182,10 @@ public class InformesController {
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE
);
if (result == 0) {
try {
Desktop.getDesktop().open(file);
Desktop.getDesktop().open(path.toFile());
} catch (IOException e) {
e.printStackTrace();
}

View File

@@ -38,6 +38,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
@@ -51,7 +53,7 @@ public class InformeEgresos {
};
private List<InformeEgresosContent> informe;
private File saveFile;
private Path saveFile;
//Filas donde se almacenaran los totales
private ArrayList<Row> totalRows;
@@ -68,7 +70,7 @@ public class InformeEgresos {
private CreationHelper createHelper;
private HashMap<String, CellStyle> styles;
public InformeEgresos(int tipoEgresoId, LocalDate date, File saveFile) {
public InformeEgresos(int tipoEgresoId, LocalDate date, Path saveFile) {
new SQLiteCajaDAO().createCajasForMonth(date);
this.informe = new SQLiteInformeEgresosContentDAO().getInformeEgresosFactuasMateriaPrima(date, tipoEgresoId);
this.saveFile = saveFile;
@@ -275,7 +277,7 @@ public class InformeEgresos {
return styles;
}
public File generarInforme() {
public Path generarInforme() {
fillHeaders();
fillData();
fillTotales();
@@ -283,7 +285,7 @@ public class InformeEgresos {
setStyles();
addBorders();
try (OutputStream fileOut = new FileOutputStream(this.saveFile)) {
try (OutputStream fileOut = Files.newOutputStream(this.saveFile)){
wb.write(fileOut);
return this.saveFile;
} catch (IOException e) {

View File

@@ -36,6 +36,8 @@ import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.*;
@@ -67,14 +69,14 @@ public class InformeLibroDeVentas {
private ArrayList<InformeLibroDeVentasContent> informe;
private int informeSize;
private File saveFile;
private Path saveFile;
private Workbook wb;
private Sheet sheet;
private CreationHelper createHelper;
private HashMap<String, CellStyle> styles;
public InformeLibroDeVentas(LocalDate date, File saveFile) {
public InformeLibroDeVentas(LocalDate date, Path saveFile) {
new SQLiteCajaDAO().createCajasForMonth(date);
this.informe = new ArrayList<>(new SQLiteInformeLibroDeVentasContentDAO().getInformeMensual(date));
@@ -298,7 +300,7 @@ public class InformeLibroDeVentas {
}
}
public File generarInforme() {
public Path generarInforme() {
sortInforme();
fillData();
fillHeaders();
@@ -308,7 +310,7 @@ public class InformeLibroDeVentas {
setStyles();
addBorders();
try (OutputStream fileOut = new FileOutputStream(this.saveFile)) {
try (OutputStream fileOut = Files.newOutputStream(this.saveFile)) {
wb.write(fileOut);
return this.saveFile;
} catch (IOException e) {

View File

@@ -24,15 +24,17 @@
package danielcortes.xyz.utils;
import java.nio.file.InvalidPathException;
import java.nio.file.Paths;
import java.io.File;
import java.io.IOException;
public class FileUtils {
public static boolean isValidPath(String path) {
File file = new File(path);
try {
Paths.get(path);
} catch (InvalidPathException | NullPointerException ex) {
ex.printStackTrace();
//noinspection ResultOfMethodCallIgnored
file.getCanonicalPath();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;