001: /*
002: * Created on 13/10/2004
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2004 Igor Regis da Silva Simões
007: *
008: * This program is free software; you can redistribute it and/or
009: * modify it under the terms of the GNU General Public License
010: * as published by the Free Software Foundation; either version 2
011: * of the License, or (at your option) any later version.
012: *
013: * This program is distributed in the hope that it will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
016: * GNU General Public License for more details.
017: *
018: * You should have received a copy of the GNU General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: */
022: package br.com.gfp.actions;
023:
024: import java.awt.event.ActionEvent;
025: import java.awt.event.ActionListener;
026: import java.io.File;
027: import java.io.FileInputStream;
028: import java.io.FileOutputStream;
029: import java.io.IOException;
030: import java.sql.SQLException;
031: import java.util.zip.CRC32;
032: import java.util.zip.ZipEntry;
033: import java.util.zip.ZipOutputStream;
034:
035: import javax.swing.JFileChooser;
036: import javax.swing.JFrame;
037: import javax.swing.JOptionPane;
038:
039: import br.com.gfp.dao.GFPController;
040: import br.com.gfp.internationalization.ActionsMessages;
041: import br.com.gfp.util.GFPProperties;
042: import br.com.gfp.util.SimpleLog;
043: import br.com.gfpshare.beans.MessageView;
044: import br.com.gfpshare.controllers.ArquivoDeDadosAbertoContoller;
045: import br.com.gfpshare.db.DataBaseManager;
046: import br.com.gfpshare.util.MyFileFilter;
047:
048: /**
049: * @author Igor Regis da Silva Simoes
050: */
051: public class SalvarDBAction implements ActionListener {
052: private static SalvarDBAction action = null;
053:
054: private JFileChooser chooser = null;
055:
056: /**
057: *
058: */
059: private SalvarDBAction() {
060: super ();
061: chooser = new JFileChooser();
062: chooser.setDialogTitle(ActionsMessages.getMessages().getString(
063: "tituloSalvarArquivoDialog"));
064: chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
065: chooser.setFileFilter(new MyFileFilter("gfd", ActionsMessages
066: .getMessages().getString("arquivoDeFinancas")));
067: }
068:
069: /**
070: * @return Retorna uma instancia desta action
071: */
072: public static SalvarDBAction getAction() {
073: return action;
074: }
075:
076: /**
077: *
078: */
079: public static void initializeAction() {
080: if (action == null)
081: action = new SalvarDBAction();
082: }
083:
084: /**
085: * @see java.awt.event.ActionListener#actionPerformed(java.awt.event.ActionEvent)
086: */
087: public void actionPerformed(ActionEvent e) {
088: //Se o arquivo aberto já tiver nome (incluindo o diretório em que esta)
089: if (ArquivoDeDadosAbertoContoller.getInstance(null)
090: .isArquivoAberto()
091: && ArquivoDeDadosAbertoContoller.getInstance(null)
092: .getNomeArquivo() != null) {
093: saveFile(ArquivoDeDadosAbertoContoller.getInstance(null)
094: .getNomeArquivo());
095: return;
096: }
097:
098: //Esta chamada é necessária para previnir o caso de o usuário ter mudado o LookAndFeel com a aplicação aberta
099: chooser.updateUI();
100: //Caso o arquivo aberto ainda não tenha sido salvo então abrimos a Dialog para o usuário escolher o destino
101: if (chooser.showSaveDialog((JFrame) GFPController
102: .getGFPController().getContexto().get(
103: GFPController.FRAME_PRINCIPAL)) == JFileChooser.APPROVE_OPTION) {
104: System.out.println(chooser.getSelectedFile());
105: saveFile(chooser.getSelectedFile().getAbsolutePath());
106: }
107: }
108:
109: private void saveFile(final String file) {
110: (new Thread("SalvarArquivo") {
111: @Override
112: public void run() {
113: MessageView messages = (MessageView) GFPController
114: .getGFPController().getContexto().get(
115: GFPController.RODAPE_MESSAGER);
116: messages = messages.showMessage(
117: ActionsMessages.getMessages().getString(
118: "SalvarArquivoTitulo"), ActionsMessages
119: .getMessages().getString(
120: "SalvarArquivoMessage"), true);
121: messages.getMessagePanel().getProgressBar()
122: .setIndeterminate(true);
123:
124: try {
125: messages.getMessagePanel().setMessage(
126: ActionsMessages.getMessages().getString(
127: "CompactandoDados"), true);
128: DataBaseManager.getDataBaseManager()
129: .executarAtualizacao("CHECKPOINT DEFRAG",
130: null);
131: } catch (SQLException e1) {
132: ((SimpleLog) GFPController.getGFPController()
133: .getContexto().get(GFPController.LOG))
134: .log("Erro ao compactar base de dados!");
135: ((SimpleLog) GFPController.getGFPController()
136: .getContexto().get(GFPController.LOG))
137: .log(e1.getLocalizedMessage());
138: ((SimpleLog) GFPController.getGFPController()
139: .getContexto().get(GFPController.LOG))
140: .log(e1);
141: }
142: messages.getMessagePanel().setMessage(
143: ActionsMessages.getMessages().getString(
144: "SalvarArquivoMessage"), true);
145: try {
146: ZipEntry ze = null;
147: File backup = null;
148: if (file.indexOf(".gfp") != -1)
149: backup = new File(file);
150: else
151: backup = new File(file + ".gfp");
152:
153: backup.createNewFile();
154: FileOutputStream fos = new FileOutputStream(backup);
155: ZipOutputStream destino = new ZipOutputStream(fos);
156: destino.setMethod(ZipOutputStream.DEFLATED);
157: destino.setLevel(9);
158: String[] files = { "db.script", "db.properties",
159: "db.backup", "gfd_db.properties", "db.log" };
160: String dirOrigem = System
161: .getProperty(GFPProperties.DIRETORIO_BANCO_DE_DADOS);
162:
163: for (int i = 0; i < files.length; i++) {
164: File origem = new File(dirOrigem
165: + File.separator + files[i]);
166: if (!origem.exists())
167: continue;
168: ze = new ZipEntry(files[i]);
169: FileInputStream from = new FileInputStream(
170: origem);
171: byte[] zFile = new byte[from.available()];
172: ze.setSize(from.available());
173: from.read(zFile);
174:
175: CRC32 crc = new CRC32();
176: crc.update(zFile, 0, (int) ze.getSize());
177: ze.setCrc(crc.getValue());
178:
179: destino.putNextEntry(ze);
180: destino.write(zFile, 0, (int) ze.getSize());
181: destino.closeEntry();
182: from.close();
183: }
184: destino.close();
185: } catch (IOException e) {
186: e.printStackTrace();
187: JOptionPane
188: .showMessageDialog(
189: (JFrame) GFPController
190: .getGFPController()
191: .getContexto()
192: .get(
193: GFPController.FRAME_PRINCIPAL),
194: ActionsMessages
195: .getMessages()
196: .getString("erroAoFecharDB"),
197: ActionsMessages
198: .getMessages()
199: .getString(
200: "tituloDialogPadrao"),
201: JOptionPane.WARNING_MESSAGE, null);
202:
203: }
204: ArquivoDeDadosAbertoContoller.getInstance(file)
205: .setArquivoNaoAlterado();
206: messages.getMessagePanel().setMessage(
207: ActionsMessages.getMessages().getString(
208: "ArquivoSalvo"), true);
209: messages.getMessagePanel().getProgressBar()
210: .setIndeterminate(false);
211: messages.finishUse();
212: }
213: }).start();
214: }
215: }
|