001: /*
002: * Created on 21/10/2005
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2005 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: */
023: package br.com.gfp.db.migration;
024:
025: import java.awt.Frame;
026: import java.awt.GridBagConstraints;
027: import java.awt.GridBagLayout;
028: import java.io.BufferedReader;
029: import java.io.IOException;
030: import java.io.InputStream;
031: import java.io.InputStreamReader;
032: import java.io.LineNumberReader;
033: import java.sql.SQLException;
034: import java.util.Enumeration;
035:
036: import javax.swing.JPanel;
037: import javax.swing.JTextArea;
038:
039: import br.com.gfp.internationalization.BasicWizardMessages;
040: import br.com.gfp.internationalization.TipoDeLancamentosMessages;
041: import br.com.gfp.wizard.BasicWizard;
042: import br.com.gfpshare.db.DataBaseManager;
043: import br.com.gfpshare.plugin.core.DynamicClassLoader;
044:
045: /**
046: *
047: * @author Igor Regis da Silva Simoes
048: * @since 21/10/2005
049: */
050: public abstract class MigradorDBWizard extends BasicWizard {
051: /**
052: *
053: * @param owner
054: * @param dbManager
055: */
056: public MigradorDBWizard(Frame owner) {
057: super (owner);
058: }
059:
060: /**
061: * Indica se o wizard será ou não executado.
062: * O default é não executar o Winzard. As subclasse que sobrescreverem este método por necessitarem de winzard
063: * devem chama-lo (super.precisaDeWizard) e depois retornar true, caso desejem que o script sql de migração seja
064: * executado.
065: * @return boolean
066: */
067: @Override
068: protected boolean precisaDeWizard() {
069: int versaoDB = Integer.parseInt(getClass().getName().substring(
070: "br.com.gfp.db.migration.MigrarDB".length()));
071: InputStream is = DynamicClassLoader.getClassLoader()
072: .getResourceAsStream(
073: "db/migration/db" + versaoDB + ".sql");
074:
075: BufferedReader reader = new LineNumberReader(
076: new InputStreamReader(is));
077: String ddl = "", temp = null;
078:
079: do {
080: try {
081: if ((temp = reader.readLine()) != null)
082: ddl += temp;
083: } catch (IOException e) {
084: throw new RuntimeException("Erro na migracao do DB de "
085: + (versaoDB - 1) + " para " + versaoDB, e);
086: }
087: } while (temp != null);
088:
089: //Se existirem ddl's para alterar a tabela TipoLancamento
090: if (ddl.indexOf("TipoLancamento") != -1) {
091: //Tipos de Lancamentos são internacionalizaveis, portanto devemos substituir
092: // o conteudo do ddl pelos valores no idioma em que está o arquivo.
093: Enumeration chaves = TipoDeLancamentosMessages
094: .getMessages().getProperties().getKeys();
095: while (chaves.hasMoreElements()) {
096: String chave = chaves.nextElement().toString();
097: ddl.replaceAll(chave, TipoDeLancamentosMessages
098: .getMessages().getString(chave));
099: }
100: }
101:
102: //Executamos o script ddl para mudança de layout do banco de dados
103: try {
104: DataBaseManager.getDataBaseManager().executarAtualizacao(
105: ddl, null);
106: } catch (SQLException e) {
107: if (e.getMessage().indexOf("already exists") != -1)
108: return true;
109: throw new RuntimeException("Erro na migracao do DB de "
110: + versaoDB + " para " + (versaoDB + 1), e);
111: }
112: return false;
113: }
114:
115: /**
116: * Sobrescrevemos o painel da superclasse
117: *
118: */
119: @Override
120: protected void adicionaPainelWelcome() {
121:
122: JPanel painelWelcome = new JPanel();
123: painelWelcome.setLayout(new GridBagLayout());
124: painelWelcome.setSize(421, 242);
125:
126: JTextArea jtaTitulo = new JTextArea();
127: jtaTitulo.setText(BasicWizardMessages.getMessages().getString(
128: "welcomeTitle"));
129: jtaTitulo.setFont(new java.awt.Font("Times New Roman",
130: java.awt.Font.BOLD | java.awt.Font.ITALIC, 18));
131: jtaTitulo.setEditable(false);
132: jtaTitulo.setOpaque(false);
133: GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
134: gridBagConstraints1.gridx = 0;
135: gridBagConstraints1.gridy = 0;
136: gridBagConstraints1.insets = new java.awt.Insets(00, 0, 20, 0);
137: gridBagConstraints1.anchor = java.awt.GridBagConstraints.NORTH;
138: gridBagConstraints1.fill = java.awt.GridBagConstraints.HORIZONTAL;
139: painelWelcome.add(jtaTitulo, gridBagConstraints1);
140:
141: JTextArea jtaIntro = new JTextArea();
142: jtaIntro.setText(BasicWizardMessages.getMessages().getString(
143: "intro"));
144: jtaIntro.setEditable(false);
145: jtaIntro.setOpaque(false);
146: GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
147: gridBagConstraints2.gridx = 0;
148: gridBagConstraints2.gridy = 1;
149: gridBagConstraints2.anchor = java.awt.GridBagConstraints.WEST;
150: painelWelcome.add(jtaIntro, gridBagConstraints2);
151:
152: JTextArea jtaAviso = new JTextArea();
153: jtaAviso.setText(BasicWizardMessages.getMessages().getString(
154: "aviso"));
155: jtaAviso.setFont(new java.awt.Font("Dialog", java.awt.Font.BOLD
156: | java.awt.Font.ITALIC, 12));
157: jtaAviso.setEditable(false);
158: jtaAviso.setOpaque(false);
159: GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
160: gridBagConstraints6.gridx = 0;
161: gridBagConstraints6.gridy = 3;
162: gridBagConstraints6.anchor = java.awt.GridBagConstraints.WEST;
163: gridBagConstraints6.insets = new java.awt.Insets(10, 0, 10, 0);
164: painelWelcome.add(jtaAviso, gridBagConstraints6);
165:
166: JTextArea jtaFinal = new JTextArea();
167: jtaFinal.setText(BasicWizardMessages.getMessages().getString(
168: "final"));
169: jtaFinal.setEditable(false);
170: jtaFinal.setOpaque(false);
171: GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
172: gridBagConstraints4.gridx = 0;
173: gridBagConstraints4.gridy = 5;
174: gridBagConstraints4.anchor = java.awt.GridBagConstraints.WEST;
175: painelWelcome.add(jtaFinal, gridBagConstraints4);
176:
177: addPage(painelWelcome);
178: }
179: }
|