001: /*
002: * Created on 23/03/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.Toolkit;
026: import java.awt.datatransfer.Clipboard;
027: import java.awt.datatransfer.DataFlavor;
028: import java.awt.datatransfer.Transferable;
029: import java.awt.datatransfer.UnsupportedFlavorException;
030: import java.awt.event.ActionEvent;
031: import java.awt.event.ActionListener;
032: import java.io.IOException;
033: import java.util.ArrayList;
034: import java.util.Calendar;
035: import java.util.Date;
036: import java.util.StringTokenizer;
037:
038: import javax.swing.JFrame;
039: import javax.swing.JOptionPane;
040:
041: import br.com.gfp.dao.GFPController;
042: import br.com.gfp.data.PendingTransaction;
043: import br.com.gfp.internationalization.ActionsMessages;
044: import br.com.gfp.ofc.tools.ImportUtils;
045: import br.com.gfp.ofc.windows.SelectAccountDialog;
046: import br.com.gfp.util.SimpleLog;
047: import br.com.gfp.windows.ConfirmImportedTransactionsWindow;
048:
049: public class ImportInMemoryCreditCardBBAction implements ActionListener {
050: private static ImportInMemoryCreditCardBBAction action;
051:
052: private Descricao descricao = null;
053:
054: public void actionPerformed(ActionEvent e) {
055: new Thread() {
056: @Override
057: public void run() {
058: StringTokenizer text = new StringTokenizer(
059: getClipboardContents(), "\n");
060: ArrayList<PendingTransaction> transactions = parseTransactions(text);
061: defineAccount(transactions);
062: ImportUtils.defineTypeForTransactions(transactions);
063: ImportUtils.conciliate(transactions);
064: GFPController.getGFPController().create(
065: ConfirmImportedTransactionsWindow.class);
066: }
067: }.start();
068: }
069:
070: public static void main(String[] args) {
071: new ImportInMemoryCreditCardBBAction().actionPerformed(null);
072: }
073:
074: private void defineAccount(
075: ArrayList<PendingTransaction> transactions) {
076: SelectAccountDialog dialog = new SelectAccountDialog("");
077: dialog.setVisible(true);
078:
079: for (PendingTransaction transaction : transactions) {
080: transaction.setConta(dialog.getConta());
081: transaction.setTipoConta(dialog.getTipoConta());
082: }
083: }
084:
085: private ArrayList<PendingTransaction> parseTransactions(
086: StringTokenizer text) {
087: ArrayList<PendingTransaction> result = new ArrayList<PendingTransaction>();
088: while (text.hasMoreTokens()) {
089: String temp = text.nextElement().toString();
090: result.add(parseTransaction(temp));
091: if (descricao != null && descricao.getParcelasFuturas() > 0)
092: for (int i = 0; i < descricao.getParcelasFuturas(); i++) {
093: Calendar day = Calendar.getInstance();
094: day.setTime(parseDay(temp.substring(6, 12)));
095: day.add(Calendar.MONTH, 1);
096: temp = temp
097: .replace(
098: temp.substring(6, 12).trim(),
099: ajustaZero(""
100: + day
101: .get(Calendar.DAY_OF_MONTH))
102: + "."
103: + ajustaZero(""
104: + (day
105: .get(Calendar.MONTH) + 1)));
106: PendingTransaction transaction = parseTransaction(temp);
107: transaction.setDia(day.getTime());
108: result.add(transaction);
109: }
110: }
111: return result;
112: }
113:
114: private String ajustaZero(String dataField) {
115: if (dataField.trim().length() == 1)
116: return "0" + dataField;
117: return dataField;
118: }
119:
120: private PendingTransaction parseTransaction(String temp) {
121: PendingTransaction pendingTransaction = new PendingTransaction();
122: pendingTransaction
123: .setDia(parseDay(temp.substring(6, 12).trim()));
124: descricao = parseDescricao(temp.substring(12, 39).trim());
125: pendingTransaction.setDescricao(descricao.getText());
126: pendingTransaction.setValor(parseValue(temp.substring(39)
127: .trim()));
128: return pendingTransaction;
129: }
130:
131: private Descricao parseDescricao(String string) {
132: Descricao retorno = new Descricao();
133: if (string.toUpperCase().indexOf("PARC") != -1) {
134: String temp = string.substring(string.toUpperCase()
135: .indexOf("PARC")
136: + "PARC".length());
137: retorno.setParcelasFuturas(Integer.parseInt(temp.substring(
138: 4).trim())
139: - Integer.parseInt(temp.substring(1, 4).trim()));
140: retorno.setText(string.substring(0,
141: string.toUpperCase().indexOf("PARC")).trim());
142: } else
143: retorno.setText(string.substring(0, string.length() - 3)
144: .trim());
145: return retorno;
146: }
147:
148: private Double parseValue(String string) {
149: String temp = string.substring(0, string.lastIndexOf(",") + 3);
150: temp = temp.replace('.', 'p').replace(',', 'v');
151: temp = temp.replace("p", "").replace('v', '.');
152: double value = Double.parseDouble(temp);
153: if (!string.endsWith("-"))
154: value *= -1;
155: return value;
156: }
157:
158: private Date parseDay(String str) {
159: Calendar dia = Calendar.getInstance();
160: dia.set(Calendar.DAY_OF_MONTH, Integer.parseInt(str.substring(
161: 0, 2)));
162: dia.set(Calendar.MONTH,
163: Integer.parseInt(str.substring(3, 5)) - 1);
164: return dia.getTime();
165: }
166:
167: /**
168: * Here we get the clipboard contents if it is of text type
169: * @return
170: */
171: public String getClipboardContents() {
172: String result = "";
173: Clipboard clipboard = Toolkit.getDefaultToolkit()
174: .getSystemClipboard();
175: // the Object param of getContents is not currently used
176: Transferable contents = clipboard.getContents(null);
177: boolean hasTransferableText = (contents != null)
178: && contents
179: .isDataFlavorSupported(DataFlavor.stringFlavor);
180: if (hasTransferableText) {
181: try {
182: result = (String) contents
183: .getTransferData(DataFlavor.stringFlavor);
184: } catch (UnsupportedFlavorException e) {
185: // highly unlikely since we are using a standard DataFlavor
186: ((SimpleLog) GFPController.getGFPController()
187: .getContexto().get(GFPController.LOG))
188: .log("Error when importing transactions from ClipBoard!");
189: ((SimpleLog) GFPController.getGFPController()
190: .getContexto().get(GFPController.LOG)).log(e
191: .getLocalizedMessage());
192: ((SimpleLog) GFPController.getGFPController()
193: .getContexto().get(GFPController.LOG)).log(e);
194: } catch (IOException e) {
195: ((SimpleLog) GFPController.getGFPController()
196: .getContexto().get(GFPController.LOG))
197: .log("Error when importing transactions from ClipBoard!");
198: ((SimpleLog) GFPController.getGFPController()
199: .getContexto().get(GFPController.LOG)).log(e
200: .getLocalizedMessage());
201: ((SimpleLog) GFPController.getGFPController()
202: .getContexto().get(GFPController.LOG)).log(e);
203: JOptionPane.showMessageDialog((JFrame) GFPController
204: .getGFPController().getContexto().get(
205: GFPController.FRAME_PRINCIPAL),
206: ActionsMessages.getMessages().getString(
207: "ErroImportacaoClipboard"));
208: }
209: }
210: return result;
211: }
212:
213: public static ImportInMemoryCreditCardBBAction getAction() {
214: return action;
215: }
216:
217: public static void initializeAction() {
218: if (action == null)
219: action = new ImportInMemoryCreditCardBBAction();
220: }
221: }
|