KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > billing > Account


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.billing;
34
35 import java.sql.SQLException JavaDoc;
36 import java.sql.Statement JavaDoc;
37 import java.sql.CallableStatement JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.Types JavaDoc;
40
41 import com.knowgate.jdc.JDCConnection;
42 import com.knowgate.dataobjs.DB;
43 import com.knowgate.dataobjs.DBBind;
44 import com.knowgate.dataobjs.DBPersist;
45
46 import com.knowgate.misc.Gadgets;
47
48 /**
49  * <p>Billing Accounts Support for Application Service Providers</p>
50  * @author Sergio Montoro Ten
51  * @version 2.2
52  */

53
54 public class Account extends DBPersist {
55   public Account() {
56     super(DB.k_accounts, "Account");
57   }
58
59   // ----------------------------------------------------------
60

61   public boolean store(JDCConnection oConn) throws SQLException JavaDoc {
62     java.sql.Timestamp JavaDoc dtNow = new java.sql.Timestamp JavaDoc(DBBind.getTime());
63     java.math.BigDecimal JavaDoc d;
64
65     if (!AllVals.containsKey(DB.id_account)) {
66       String JavaDoc sNextAccount = Gadgets.leftPad(String.valueOf(DBBind.nextVal(oConn, "seq_" + DB.k_accounts)), '0', 10);
67       put (DB.id_account, sNextAccount);
68     }
69
70     replace(DB.dt_modified, dtNow);
71
72     return super.store(oConn);
73   } // store
74

75   // **********************************************************
76
// Static Methods
77

78   /**
79    * <p>Get User Account Type</p>
80    * In the standard version there are 3 account types:<br>
81    * 'C' for Corporate Accounts, those having its own domain and a variable number or Users.<br>
82    * 'P' for Professional Accounts, those sharing a domain and having a single User.<br>
83    * 'S' for System Accounts, those having special priviledges for system administration.
84    * @param oConn Database Connection
85    * @param sUserId User Unique Identifier (k_users table primary key)
86    * @return User Account Type
87    * @throws SQLException
88    */

89   public static String JavaDoc getUserAccountType(JDCConnection oConn, String JavaDoc sUserId) throws SQLException JavaDoc {
90     Statement JavaDoc oStmt;
91     ResultSet JavaDoc oRSet;
92     CallableStatement JavaDoc oCall;
93     String JavaDoc sTp;
94
95     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
96       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
97       oRSet = oStmt.executeQuery("SELECT k_get_account_tp ('" + sUserId + "')");
98       oRSet.next();
99       sTp = oRSet.getString(1);
100       oRSet.close();
101       oStmt.close();
102     }
103     else {
104       oCall = oConn.prepareCall("{ call k_get_account_tp (?,?) }");
105
106       oCall.setString(1, sUserId);
107       oCall.registerOutParameter(2, Types.CHAR);
108
109       oCall.execute();
110
111       if (JDCConnection.DBMS_ORACLE==oConn.getDataBaseProduct())
112         sTp = oCall.getString(2);
113       else
114         sTp = oCall.getString(2);
115
116       if (sTp!=null) sTp = sTp.trim();
117
118       oCall.close();
119     }
120     return sTp;
121   } // getUserAccountType()
122

123   // ----------------------------------------------------------
124

125   /**
126    * <p>Get User Account Status.</p>
127    * @param oConn Database Connection
128    * @param sAccId Account Identifier
129    * @return <b>true</b> if account is valid and active, <b>false</b> otherwise.
130    * @throws SQLException
131    */

132   public static boolean checkStatus (JDCConnection oConn, String JavaDoc sAccId) throws SQLException JavaDoc {
133     Statement JavaDoc oStmt;
134     ResultSet JavaDoc oRSet;
135     CallableStatement JavaDoc oCall;
136     short iActive;
137
138     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
139       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
140       oRSet = oStmt.executeQuery("SELECT k_check_account ('" + sAccId + "')");
141       oRSet.next();
142       iActive = oRSet.getShort(1);
143       oRSet.close();
144       oStmt.close();
145     }
146     else {
147       oCall = oConn.prepareCall("{ call k_check_account (?,?) }");
148       oCall.setString(1, sAccId);
149       oCall.registerOutParameter(2, Types.SMALLINT);
150       oCall.execute();
151       iActive = oCall.getShort(2);
152       oCall.close();
153     }
154
155     return ((short)0!=iActive);
156
157   } // checkStatus
158

159   // ----------------------------------------------------------
160

161   /**
162    * <p>Get number of days left until account automatically expires.<p>
163    * @param oConn Database Connection
164    * @param sAccId Account Identifier
165    * @return Number of days left for expiration.
166    * @throws SQLException
167    */

168   public static int daysLeft (JDCConnection oConn, String JavaDoc sAccId) throws SQLException JavaDoc {
169     Statement JavaDoc oStmt;
170     ResultSet JavaDoc oRSet;
171     CallableStatement JavaDoc oCall;
172     int iDaysLeft;
173
174     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
175       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
176       oRSet = oStmt.executeQuery("SELECT k_get_account_days_left ('" + sAccId + "')");
177       oRSet.next();
178       iDaysLeft = oRSet.getInt(1);
179       oRSet.close();
180       oStmt.close();
181     }
182     else {
183       oCall = oConn.prepareCall("{ call k_get_account_days_left (?,?) }");
184       oCall.setString(1, sAccId);
185       oCall.registerOutParameter(2, Types.INTEGER);
186       oCall.execute();
187       iDaysLeft = oCall.getInt(2);
188       oCall.close();
189     }
190
191     return iDaysLeft;
192   } // daysLeft
193

194   // ----------------------------------------------------------
195

196   /**
197    * <p>Find out whether or not and Account is in Trial Mode.</p>
198    * @param oConn Database Connection
199    * @param sAccId Account Identifier
200    * @return <b>true</b> if account is in trial mode, <b>false</b> otherwise.
201    * @throws SQLException
202    */

203   public static boolean isTrial (JDCConnection oConn, String JavaDoc sAccId) throws SQLException JavaDoc {
204     Statement JavaDoc oStmt;
205     ResultSet JavaDoc oRSet;
206     CallableStatement JavaDoc oCall;
207     short iTrial;
208
209     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
210       oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
211       oRSet = oStmt.executeQuery("SELECT k_get_account_trial ('" + sAccId + "')");
212       oRSet.next();
213       iTrial = oRSet.getShort(1);
214       oRSet.close();
215       oStmt.close();
216     }
217     else {
218       oCall = oConn.prepareCall("{ call k_get_account_trial (?,?) }");
219       oCall.setString(1, sAccId);
220       oCall.registerOutParameter(2, Types.SMALLINT);
221       oCall.execute();
222       iTrial = oCall.getShort(2);
223       oCall.close();
224     }
225
226     return ((short)0!=iTrial);
227   } // isTrial
228

229   // **********************************************************
230
// Variables Privadas
231

232   public static final short ClassId = 6;
233
234 } // Account
Popular Tags