001: /*
002: * Created on 26/01/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.parser;
024:
025: import java.io.File;
026: import java.io.FileNotFoundException;
027: import java.io.IOException;
028: import java.sql.SQLException;
029: import java.util.ArrayList;
030:
031: import br.com.gfp.dao.AccountImportMappingDAO;
032: import br.com.gfp.dao.GFPController;
033: import br.com.gfp.data.AccountImportMapping;
034: import br.com.gfp.data.PendingTransaction;
035: import br.com.gfp.ofc.tools.ImportUtils;
036: import br.com.gfp.ofc.tools.OFCConversor;
037: import br.com.gfp.ofc.windows.SelectAccountDialog;
038: import br.com.gfp.util.SimpleLog;
039:
040: public class OFC2TransactionConversor {
041: protected static final String TEMP_FILE_SUFIX = ".temp";
042:
043: /**
044: * Parser do XML OFC
045: */
046: protected OFCParser parser;
047:
048: protected boolean transactionCompositionFinished;
049:
050: public OFC2TransactionConversor() {
051: }
052:
053: public OFC2TransactionConversor(String filePath)
054: throws OFCImportException {
055: try {
056: corrigirArquivo(filePath);
057: } catch (IOException e1) {
058: // TODO Auto-generated catch block
059: e1.printStackTrace();
060: }
061:
062: parser = new OFCParser(filePath + TEMP_FILE_SUFIX);
063: new File(filePath + TEMP_FILE_SUFIX).delete();
064: }
065:
066: protected void corrigirArquivo(String filePath)
067: throws FileNotFoundException, IOException {
068: new OFCConversor().execute(new File(filePath), new File(
069: filePath + TEMP_FILE_SUFIX));
070: }
071:
072: public ArrayList<PendingTransaction> getLancamentos()
073: throws MapeamentoToContaException {
074: if (transactionCompositionFinished)
075: return parser.getTransactions();
076: return finishTransactionComposition();
077: }
078:
079: /**
080: * Here we finish to fill all transaction fields like
081: * like account and account type for that transactions and
082: * the type of each transaction.
083: *
084: * If this method was already executed for these transactions
085: * it will only return the transactions assembled.
086: * @return ArrayList<LancamentoPendente>
087: * @throws MapeamentoToContaException
088: */
089: protected ArrayList<PendingTransaction> finishTransactionComposition()
090: throws MapeamentoToContaException {
091: defineAccountForTransactions(getAccountForTransactions());
092: ImportUtils.defineTypeForTransactions(parser.getTransactions());
093: transactionCompositionFinished = true;
094: return parser.getTransactions();
095: }
096:
097: /**
098: * This method define for what account the parsed transactions will be
099: * registered on database
100: * @throws MapeamentoToContaException
101: * @throws SQLException
102: */
103: protected AccountImportMapping getAccountForTransactions()
104: throws MapeamentoToContaException {
105: AccountImportMappingDAO controller = new AccountImportMappingDAO();
106:
107: AccountImportMapping mapeamentoConta = new AccountImportMapping();
108: mapeamentoConta.setStringAMapear(parser.getConta().toString());
109: try {
110: return controller.getBy(mapeamentoConta);
111: } catch (SQLException e) {
112: SelectAccountDialog dialog = new SelectAccountDialog(
113: mapeamentoConta.getStringAMapear());
114: dialog.setVisible(true);
115: mapeamentoConta.setTipoContaComQueMapear(Integer
116: .parseInt(dialog.getTipoConta()));
117: mapeamentoConta.setContaComQueMapear(dialog.getConta());
118: if (mapeamentoConta.getTipoContaComQueMapear() == null
119: || mapeamentoConta.getContaComQueMapear() == null)
120: throw new MapeamentoToContaException();
121: try {
122: controller.adicionarNovo(mapeamentoConta);
123: } catch (SQLException e1) {
124: ((SimpleLog) GFPController.getGFPController()
125: .getContexto().get(GFPController.LOG))
126: .log("Erro ao registrar mapeamento de contas");
127: ((SimpleLog) GFPController.getGFPController()
128: .getContexto().get(GFPController.LOG)).log(e
129: .getLocalizedMessage());
130: ((SimpleLog) GFPController.getGFPController()
131: .getContexto().get(GFPController.LOG)).log(e);
132: }
133: return mapeamentoConta;
134: }
135: }
136:
137: protected void defineAccountForTransactions(
138: AccountImportMapping mapContaImportacao) {
139: for (PendingTransaction lancamentoPendente : parser
140: .getTransactions()) {
141: lancamentoPendente.setTipoConta(""
142: + mapContaImportacao.getTipoContaComQueMapear());
143: lancamentoPendente.setConta(mapContaImportacao
144: .getContaComQueMapear());
145: }
146: }
147:
148: public static void main(String[] args) {
149: // new ConversorOFC2Lancamento("I:\\GFProject\\4353008.ofc");
150: try {
151: new OFC2TransactionConversor(
152: "/home/igor/Desktop/extrato.ofc");
153: } catch (OFCImportException e) {
154: // TODO Auto-generated catch block
155: e.printStackTrace();
156: }
157: }
158: }
|