001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.billing;
034:
035: import java.sql.SQLException;
036: import java.sql.Statement;
037: import java.sql.CallableStatement;
038: import java.sql.ResultSet;
039: import java.sql.Types;
040:
041: import com.knowgate.jdc.JDCConnection;
042: import com.knowgate.dataobjs.DB;
043: import com.knowgate.dataobjs.DBBind;
044: import com.knowgate.dataobjs.DBPersist;
045:
046: import com.knowgate.misc.Gadgets;
047:
048: /**
049: * <p>Billing Accounts Support for Application Service Providers</p>
050: * @author Sergio Montoro Ten
051: * @version 2.2
052: */
053:
054: public class Account extends DBPersist {
055: public Account() {
056: super (DB.k_accounts, "Account");
057: }
058:
059: // ----------------------------------------------------------
060:
061: public boolean store(JDCConnection oConn) throws SQLException {
062: java.sql.Timestamp dtNow = new java.sql.Timestamp(DBBind
063: .getTime());
064: java.math.BigDecimal d;
065:
066: if (!AllVals.containsKey(DB.id_account)) {
067: String sNextAccount = Gadgets.leftPad(String.valueOf(DBBind
068: .nextVal(oConn, "seq_" + DB.k_accounts)), '0', 10);
069: put(DB.id_account, sNextAccount);
070: }
071:
072: replace(DB.dt_modified, dtNow);
073:
074: return super .store(oConn);
075: } // store
076:
077: // **********************************************************
078: // Static Methods
079:
080: /**
081: * <p>Get User Account Type</p>
082: * In the standard version there are 3 account types:<br>
083: * 'C' for Corporate Accounts, those having its own domain and a variable number or Users.<br>
084: * 'P' for Professional Accounts, those sharing a domain and having a single User.<br>
085: * 'S' for System Accounts, those having special priviledges for system administration.
086: * @param oConn Database Connection
087: * @param sUserId User Unique Identifier (k_users table primary key)
088: * @return User Account Type
089: * @throws SQLException
090: */
091: public static String getUserAccountType(JDCConnection oConn,
092: String sUserId) throws SQLException {
093: Statement oStmt;
094: ResultSet oRSet;
095: CallableStatement oCall;
096: String sTp;
097:
098: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
099: oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
100: ResultSet.CONCUR_READ_ONLY);
101: oRSet = oStmt.executeQuery("SELECT k_get_account_tp ('"
102: + sUserId + "')");
103: oRSet.next();
104: sTp = oRSet.getString(1);
105: oRSet.close();
106: oStmt.close();
107: } else {
108: oCall = oConn
109: .prepareCall("{ call k_get_account_tp (?,?) }");
110:
111: oCall.setString(1, sUserId);
112: oCall.registerOutParameter(2, Types.CHAR);
113:
114: oCall.execute();
115:
116: if (JDCConnection.DBMS_ORACLE == oConn.getDataBaseProduct())
117: sTp = oCall.getString(2);
118: else
119: sTp = oCall.getString(2);
120:
121: if (sTp != null)
122: sTp = sTp.trim();
123:
124: oCall.close();
125: }
126: return sTp;
127: } // getUserAccountType()
128:
129: // ----------------------------------------------------------
130:
131: /**
132: * <p>Get User Account Status.</p>
133: * @param oConn Database Connection
134: * @param sAccId Account Identifier
135: * @return <b>true</b> if account is valid and active, <b>false</b> otherwise.
136: * @throws SQLException
137: */
138: public static boolean checkStatus(JDCConnection oConn, String sAccId)
139: throws SQLException {
140: Statement oStmt;
141: ResultSet oRSet;
142: CallableStatement oCall;
143: short iActive;
144:
145: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
146: oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
147: ResultSet.CONCUR_READ_ONLY);
148: oRSet = oStmt.executeQuery("SELECT k_check_account ('"
149: + sAccId + "')");
150: oRSet.next();
151: iActive = oRSet.getShort(1);
152: oRSet.close();
153: oStmt.close();
154: } else {
155: oCall = oConn.prepareCall("{ call k_check_account (?,?) }");
156: oCall.setString(1, sAccId);
157: oCall.registerOutParameter(2, Types.SMALLINT);
158: oCall.execute();
159: iActive = oCall.getShort(2);
160: oCall.close();
161: }
162:
163: return ((short) 0 != iActive);
164:
165: } // checkStatus
166:
167: // ----------------------------------------------------------
168:
169: /**
170: * <p>Get number of days left until account automatically expires.<p>
171: * @param oConn Database Connection
172: * @param sAccId Account Identifier
173: * @return Number of days left for expiration.
174: * @throws SQLException
175: */
176: public static int daysLeft(JDCConnection oConn, String sAccId)
177: throws SQLException {
178: Statement oStmt;
179: ResultSet oRSet;
180: CallableStatement oCall;
181: int iDaysLeft;
182:
183: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
184: oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
185: ResultSet.CONCUR_READ_ONLY);
186: oRSet = oStmt
187: .executeQuery("SELECT k_get_account_days_left ('"
188: + sAccId + "')");
189: oRSet.next();
190: iDaysLeft = oRSet.getInt(1);
191: oRSet.close();
192: oStmt.close();
193: } else {
194: oCall = oConn
195: .prepareCall("{ call k_get_account_days_left (?,?) }");
196: oCall.setString(1, sAccId);
197: oCall.registerOutParameter(2, Types.INTEGER);
198: oCall.execute();
199: iDaysLeft = oCall.getInt(2);
200: oCall.close();
201: }
202:
203: return iDaysLeft;
204: } // daysLeft
205:
206: // ----------------------------------------------------------
207:
208: /**
209: * <p>Find out whether or not and Account is in Trial Mode.</p>
210: * @param oConn Database Connection
211: * @param sAccId Account Identifier
212: * @return <b>true</b> if account is in trial mode, <b>false</b> otherwise.
213: * @throws SQLException
214: */
215: public static boolean isTrial(JDCConnection oConn, String sAccId)
216: throws SQLException {
217: Statement oStmt;
218: ResultSet oRSet;
219: CallableStatement oCall;
220: short iTrial;
221:
222: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
223: oStmt = oConn.createStatement(ResultSet.TYPE_FORWARD_ONLY,
224: ResultSet.CONCUR_READ_ONLY);
225: oRSet = oStmt.executeQuery("SELECT k_get_account_trial ('"
226: + sAccId + "')");
227: oRSet.next();
228: iTrial = oRSet.getShort(1);
229: oRSet.close();
230: oStmt.close();
231: } else {
232: oCall = oConn
233: .prepareCall("{ call k_get_account_trial (?,?) }");
234: oCall.setString(1, sAccId);
235: oCall.registerOutParameter(2, Types.SMALLINT);
236: oCall.execute();
237: iTrial = oCall.getShort(2);
238: oCall.close();
239: }
240:
241: return ((short) 0 != iTrial);
242: } // isTrial
243:
244: // **********************************************************
245: // Variables Privadas
246:
247: public static final short ClassId = 6;
248:
249: } // Account
|