001: /*
002: * Created on 05/04/2007
003: *
004: * Swing Components - visit http://sf.net/projects/gfd
005: *
006: * Copyright (C) 2007 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.ofc.actions;
024:
025: import java.awt.event.ActionEvent;
026: import java.awt.event.ActionListener;
027: import java.sql.SQLException;
028: import java.util.ArrayList;
029:
030: import javax.swing.JFrame;
031: import javax.swing.JOptionPane;
032:
033: import br.com.gfp.csv.parser.CSVParser;
034: import br.com.gfp.dao.GFPController;
035: import br.com.gfp.dao.PrescriptedCSVImportDAO;
036: import br.com.gfp.data.PendingTransaction;
037: import br.com.gfp.data.PrescriptedCSVImport;
038: import br.com.gfp.ofc.tools.ImportUtils;
039: import br.com.gfp.ofc.windows.CSVImportWizard;
040: import br.com.gfp.ofc.windows.SelectAccountDialog;
041: import br.com.gfp.util.SimpleLog;
042: import br.com.gfp.windows.ConfirmImportedTransactionsWindow;
043:
044: public class ImportCSVDataAction implements ActionListener {
045: private static ImportCSVDataAction action;
046:
047: public void actionPerformed(final ActionEvent e) {
048: new Thread() {
049: @Override
050: public void run() {
051: CSVParser csvParser = null;
052: if (e.getActionCommand().equals("wizard")) {
053: CSVImportWizard csvImpWizard = new CSVImportWizard(
054: (JFrame) GFPController.getGFPController()
055: .getContexto()
056: .get(GFPController.FRAME_PRINCIPAL));
057: csvImpWizard.setVisible(true);
058: csvParser = new CSVParser(csvImpWizard
059: .getConfiguration());
060: } else {
061: PrescriptedCSVImport prescriptedCSVImport = new PrescriptedCSVImport();
062: prescriptedCSVImport.setId(Integer.parseInt(e
063: .getActionCommand()));
064: try {
065: prescriptedCSVImport = new PrescriptedCSVImportDAO()
066: .getBy(prescriptedCSVImport);
067: } catch (SQLException e1) {
068: ((SimpleLog) GFPController.getGFPController()
069: .getContexto().get(GFPController.LOG))
070: .log("Error on load prescripted csv import from data base: "
071: + e.getActionCommand());
072: ((SimpleLog) GFPController.getGFPController()
073: .getContexto().get(GFPController.LOG))
074: .log(e1.getLocalizedMessage());
075: ((SimpleLog) GFPController.getGFPController()
076: .getContexto().get(GFPController.LOG))
077: .log(e1);
078: JOptionPane
079: .showMessageDialog(
080: (JFrame) GFPController
081: .getGFPController()
082: .getContexto()
083: .get(
084: GFPController.FRAME_PRINCIPAL),
085: "Erro ao carregar configuração salva anteriormente, wizard será iniciado");
086: CSVImportWizard csvImpWizard = new CSVImportWizard(
087: (JFrame) GFPController
088: .getGFPController()
089: .getContexto()
090: .get(
091: GFPController.FRAME_PRINCIPAL));
092: prescriptedCSVImport = csvImpWizard
093: .getConfiguration();
094: }
095: csvParser = new CSVParser(prescriptedCSVImport);
096: }
097: defineAccount(csvParser);
098: ImportUtils.defineTypeForTransactions(csvParser
099: .getTransactions());
100: ImportUtils.conciliate(csvParser.getTransactions());
101: GFPController.getGFPController().create(
102: ConfirmImportedTransactionsWindow.class);
103: }
104: }.start();
105: }
106:
107: private void defineAccount(CSVParser parser) {
108: int account = -1, accountType = -1;
109: ArrayList<PendingTransaction> transactions = parser
110: .getTransactions();
111:
112: if ((account = parser.getPrescriptedCSVImport().getAccount()) == -1
113: || (accountType = parser.getPrescriptedCSVImport()
114: .getAccountType()) == -1) {
115: SelectAccountDialog dialog = new SelectAccountDialog("");
116: dialog.setVisible(true);
117: account = dialog.getConta();
118: accountType = Integer.parseInt(dialog.getTipoConta());
119: }
120:
121: for (PendingTransaction transaction : transactions) {
122: transaction.setConta(account);
123: transaction.setTipoConta("" + accountType);
124: }
125: }
126:
127: public static ImportCSVDataAction getAction() {
128: return action;
129: }
130:
131: public static void initializeAction() {
132: if (action == null)
133: action = new ImportCSVDataAction();
134: }
135:
136: }
|