KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > hipergate > BankAccount


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.hipergate;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37
38 import com.knowgate.debug.DebugFile;
39 import com.knowgate.jdc.JDCConnection;
40 import com.knowgate.dataobjs.DB;
41 import com.knowgate.dataobjs.DBBind;
42 import com.knowgate.dataobjs.DBPersist;
43
44 /**
45  * <p>Bank Account</p>
46  * @author Sergio Montoro Ten
47  * @version 3.0
48  */

49
50 public class BankAccount extends DBPersist {
51   public BankAccount() {
52     super(DB.k_bank_accounts, "BankAccount");
53   }
54
55   /**
56    * <p>Delete Bank Account</p>
57    * Associations of the bank account with companies and contacts are erased before deleting it.
58    * @param oConn JDBC Database Connection
59    * @throws SQLException
60    */

61   public boolean delete (JDCConnection oConn)
62     throws SQLException JavaDoc {
63     Statement JavaDoc oStmt;
64     boolean bRetVal;
65
66     if (DebugFile.trace) {
67       DebugFile.writeln("Begin BankAccount.delete([Connection])");
68       DebugFile.incIdent();
69     }
70
71     if (DBBind.exists(oConn, DB.k_x_company_bank, "U")) {
72       oStmt = oConn.createStatement();
73
74       if (DebugFile.trace)
75         DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_company_bank + " WHERE " + DB.nu_bank_acc + "='" + getStringNull(DB.nu_bank_acc, "") + "' AND " + DB.gu_workarea + "='" + getStringNull(DB.gu_workarea, "") + "')");
76
77       oStmt.executeUpdate("DELETE FROM " + DB.k_x_company_bank + " WHERE " + DB.nu_bank_acc + "='" + getStringNull(DB.nu_bank_acc, "") + "' AND " + DB.gu_workarea + "='" + getStringNull(DB.gu_workarea, "") + "'");
78       oStmt.close();
79     }
80
81     if (DBBind.exists(oConn, DB.k_x_contact_bank, "U")) {
82       oStmt = oConn.createStatement();
83
84       if (DebugFile.trace)
85         DebugFile.writeln("Statement.executeUpdate(DELETE FROM " + DB.k_x_contact_bank + " WHERE " + DB.nu_bank_acc + "='" + getStringNull(DB.nu_bank_acc, "") + "' AND " + DB.gu_workarea + "='" + getStringNull(DB.gu_workarea, "") + "')");
86
87       oStmt.executeUpdate("DELETE FROM " + DB.k_x_contact_bank + " WHERE " + DB.nu_bank_acc + "='" + getStringNull(DB.nu_bank_acc, "") + "' AND " + DB.gu_workarea + "='" + getStringNull(DB.gu_workarea, "") + "'");
88       oStmt.close();
89     }
90
91     bRetVal = super.delete(oConn);
92
93     if (DebugFile.trace) {
94       DebugFile.decIdent();
95       DebugFile.writeln("End BankAccount.delete() : " + String.valueOf(bRetVal));
96     }
97
98     return bRetVal;
99   } // delete
100

101   public String JavaDoc getString(String JavaDoc sKey) throws NullPointerException JavaDoc {
102     return AllVals.get(sKey).toString().trim();
103   }
104
105   public String JavaDoc getStringNull(String JavaDoc sKey, String JavaDoc sDefault) {
106     Object JavaDoc oVal;
107     if (AllVals.containsKey(sKey)) {
108       oVal = AllVals.get(sKey);
109       if (null==oVal)
110         return sDefault;
111       else
112         return oVal.toString().trim();
113     }
114     else
115       return sDefault;
116   }
117
118   /**
119    * Return 20 digits bank account formated as XXXX XXXX XX XXXXXXXXXX
120    * @return String
121    * @throws NullPointerException if nu_bank_acc is <b>null</b>
122    * @throws StringIndexOutOfBoundsException if nu_bank_acc length is not 20 characters
123    */

124   public String JavaDoc format20()
125     throws NullPointerException JavaDoc, StringIndexOutOfBoundsException JavaDoc {
126     if (isNull(DB.nu_bank_acc))
127       throw new NullPointerException JavaDoc("BankAccount.format20() Bank account may not be null");
128     else if (getString(DB.nu_bank_acc).length()!=20)
129       throw new StringIndexOutOfBoundsException JavaDoc("BankAccount.format20 can only be called on 20 digits bank accounts");
130     else {
131       String JavaDoc a = getString(DB.nu_bank_acc);
132       return a.substring(0,4)+" "+a.substring(4,8)+" "+a.substring(8,10)+" "+a.substring(10);
133     }
134   } // format20
135

136   /**
137    * Get control digits for 20 digits bank account
138    * @return String 2 digits control number
139    * @throws IllegalArgumentException if account length is not 20 characters
140    * @throws NullPointerException if account is <b>null</b>
141    * @since 3.0
142    */

143   public String JavaDoc getBankAccountDC()
144     throws IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
145     if (isNull(DB.nu_bank_acc))
146       throw new NullPointerException JavaDoc("BankAccount.getBankAccountDC() Bank account may not be null");
147     String JavaDoc sBankAcc = getString(DB.nu_bank_acc);
148     if (sBankAcc.length()!=20)
149       throw new IllegalArgumentException JavaDoc("BankAccount.getBankAccountDC() Bank account must be of 20 characters");
150     return BankAccount.getBankAccountDC(sBankAcc.substring(0,4),sBankAcc.substring(4,8), sBankAcc.substring(10));
151   } // getBankAccountDC()
152

153   /**
154    * Get control digit for a bank account number
155    * @param sAccountNumber String 10 digits bank account number
156    * @return int Control digit [0..9]
157    * @throws IllegalArgumentException if sAccountNumber length is not 10 characters
158    * @throws NullPointerException if sAccountNumber is <b>null</b>
159    * @since 3.0
160    */

161   public static int getBankAccountCtrl(String JavaDoc sAccountNumber)
162     throws IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
163     int ccc;
164     int suma = 0;
165     int contpesos = 10;
166     int[] mintpesos = new int[]{0, 6, 3, 7, 9, 10, 5, 8, 4, 2, 1};
167
168     if (sAccountNumber.length()!=10)
169       throw new IllegalArgumentException JavaDoc("BankAccount.getBankAccountCtrl() Bank account number must be 10 digits");
170
171     for (int d=0; d<10; d++) {
172       suma += (mintpesos[contpesos] * Integer.parseInt(sAccountNumber.substring(d,1)));
173       contpesos-=1;
174     } // next
175

176     ccc = 11 - (suma % 11);
177     if (ccc==10) ccc=1;
178     if (ccc==11) ccc=0;
179
180     return ccc;
181   } // getBankAccountCtrl
182

183   /**
184    * Get control digits for full bank account entity+office+number
185    * @param sEntity String 4 digits entity code
186    * @param sOffice String 4 digits office code
187    * @param sAccountNumber String 10 digits account number
188    * @return String 2 digits control number
189    * @since 3.0
190    */

191   public static String JavaDoc getBankAccountDC(String JavaDoc sEntity, String JavaDoc sOffice, String JavaDoc sAccountNumber)
192     throws IllegalArgumentException JavaDoc,NullPointerException JavaDoc {
193     if (sEntity.length()!=4)
194       throw new IllegalArgumentException JavaDoc("BankAccount.getBankAccountDC() Bank Entity code must be 4 digits");
195     if (sOffice.length()!=4)
196       throw new IllegalArgumentException JavaDoc("BankAccount.getBankAccountDC() Bank office code must be 4 digits");
197     return String.valueOf(getBankAccountCtrl("00" + sEntity + sOffice)) + String.valueOf(getBankAccountCtrl(sAccountNumber));
198   } // getBankAccountDC
199

200   // **********************************************************
201
// Public Constants
202

203   public static final short ClassId = 8;
204
205 }
206
Popular Tags