001: package com.knowgate.training;
002:
003: /*
004: Copyright (C) 2003-2005 Know Gate S.L. All rights reserved.
005: C/Oņa, 107 1š2 28050 Madrid (Spain)
006:
007: Redistribution and use in source and binary forms, with or without
008: modification, are permitted provided that the following conditions
009: are met:
010:
011: 1. Redistributions of source code must retain the above copyright
012: notice, this list of conditions and the following disclaimer.
013:
014: 2. The end-user documentation included with the redistribution,
015: if any, must include the following acknowledgment:
016: "This product includes software parts from hipergate
017: (http://www.hipergate.org/)."
018: Alternately, this acknowledgment may appear in the software itself,
019: if and wherever such third-party acknowledgments normally appear.
020:
021: 3. The name hipergate must not be used to endorse or promote products
022: derived from this software without prior written permission.
023: Products derived from this software may not be called hipergate,
024: nor may hipergate appear in their name, without prior written
025: permission.
026:
027: This library is distributed in the hope that it will be useful,
028: but WITHOUT ANY WARRANTY; without even the implied warranty of
029: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
030:
031: You should have received a copy of hipergate License with this code;
032: if not, visit http://www.hipergate.org or mail to info@hipergate.org
033: */
034:
035: import java.util.Date;
036: import java.sql.SQLException;
037: import java.sql.PreparedStatement;
038: import java.sql.CallableStatement;
039: import java.sql.ResultSet;
040:
041: import com.knowgate.jdc.JDCConnection;
042:
043: import com.knowgate.debug.DebugFile;
044: import com.knowgate.dataobjs.DB;
045: import com.knowgate.dataobjs.DBPersist;
046: import com.knowgate.dataobjs.DBSubset;
047:
048: import com.knowgate.misc.Gadgets;
049:
050: /**
051: * Academic Course Instance
052: * @author Sergio Montoro Ten
053: * @version 2.2
054: */
055:
056: public class AcademicCourse extends DBPersist {
057:
058: public AcademicCourse() {
059: super (DB.k_academic_courses, "AcademicCourse");
060: }
061:
062: public AcademicCourse(JDCConnection oConn, String sGuACourse)
063: throws SQLException {
064: super (DB.k_academic_courses, "AcademicCourse");
065: load(oConn, new Object[] { sGuACourse });
066: }
067:
068: // ---------------------------------------------------------------------------
069:
070: public boolean active() {
071: boolean bRetVal;
072: if (isNull(DB.bo_active))
073: bRetVal = false;
074: else
075: bRetVal = (getShort(DB.bo_active) == (short) 1);
076: return bRetVal;
077: } // active
078:
079: // ---------------------------------------------------------------------------
080:
081: public int countAlumni(JDCConnection oConn) throws SQLException,
082: IllegalStateException {
083:
084: if (isNull(DB.gu_acourse))
085: throw new IllegalStateException(
086: "AcademicCourse.countBookings() gu_acourse not set");
087:
088: PreparedStatement oStmt = oConn
089: .prepareStatement("SELECT COUNT(*) FROM "
090: + DB.k_x_course_alumni + " WHERE "
091: + DB.gu_acourse + "=?");
092: oStmt.setString(1, getString(DB.gu_acourse));
093: ResultSet oRSet = oStmt.executeQuery();
094: oRSet.next();
095: int nAlumni = oRSet.getInt(1);
096: oRSet.close();
097: oStmt.close();
098: return nAlumni;
099: } // countAlumni
100:
101: // ---------------------------------------------------------------------------
102:
103: public int countBookings(JDCConnection oConn) throws SQLException,
104: IllegalStateException {
105:
106: if (isNull(DB.gu_acourse))
107: throw new IllegalStateException(
108: "AcademicCourse.countBookings() gu_acourse not set");
109:
110: PreparedStatement oStmt = oConn
111: .prepareStatement("SELECT COUNT(*) FROM "
112: + DB.k_x_course_bookings + " WHERE "
113: + DB.gu_acourse + "=? AND " + DB.bo_canceled
114: + "<>1");
115: oStmt.setString(1, getString(DB.gu_acourse));
116: ResultSet oRSet = oStmt.executeQuery();
117: oRSet.next();
118: int nBooks = oRSet.getInt(1);
119: oRSet.close();
120: oStmt.close();
121: return nBooks;
122: } // countBookings
123:
124: // ---------------------------------------------------------------------------
125:
126: public int countConfirmedBookings(JDCConnection oConn)
127: throws SQLException, IllegalStateException {
128:
129: if (isNull(DB.gu_acourse))
130: throw new IllegalStateException(
131: "AcademicCourse.countBookings() gu_acourse not set");
132:
133: PreparedStatement oStmt = oConn
134: .prepareStatement("SELECT COUNT(*) FROM "
135: + DB.k_x_course_bookings + " WHERE "
136: + DB.gu_acourse + "=? AND " + DB.bo_canceled
137: + "<>1 AND " + DB.bo_confirmed + "=1");
138: oStmt.setString(1, getString(DB.gu_acourse));
139: ResultSet oRSet = oStmt.executeQuery();
140: oRSet.next();
141: int nBooks = oRSet.getInt(1);
142: oRSet.close();
143: oStmt.close();
144: return nBooks;
145: } // countConfirmedBookings
146:
147: // ---------------------------------------------------------------------------
148:
149: public int countPaidBookings(JDCConnection oConn)
150: throws SQLException, IllegalStateException {
151:
152: if (isNull(DB.gu_acourse))
153: throw new IllegalStateException(
154: "AcademicCourse.countBookings() gu_acourse not set");
155:
156: PreparedStatement oStmt = oConn
157: .prepareStatement("SELECT COUNT(*) FROM "
158: + DB.k_x_course_bookings + " WHERE "
159: + DB.gu_acourse + "=? AND " + DB.bo_canceled
160: + "<>1 AND " + DB.bo_paid + "=1");
161: oStmt.setString(1, getString(DB.gu_acourse));
162: ResultSet oRSet = oStmt.executeQuery();
163: oRSet.next();
164: int nBooks = oRSet.getInt(1);
165: oRSet.close();
166: oStmt.close();
167: return nBooks;
168: } // countPaidBookings
169:
170: // ---------------------------------------------------------------------------
171:
172: public int countWaitingBookings(JDCConnection oConn)
173: throws SQLException, IllegalStateException {
174:
175: if (isNull(DB.gu_acourse))
176: throw new IllegalStateException(
177: "AcademicCourse.countBookings() gu_acourse not set");
178:
179: PreparedStatement oStmt = oConn
180: .prepareStatement("SELECT COUNT(*) FROM "
181: + DB.k_x_course_bookings + " WHERE "
182: + DB.gu_acourse + "=? AND " + DB.bo_canceled
183: + "<>1 AND " + DB.bo_waiting + "=1");
184: oStmt.setString(1, getString(DB.gu_acourse));
185: ResultSet oRSet = oStmt.executeQuery();
186: oRSet.next();
187: int nBooks = oRSet.getInt(1);
188: oRSet.close();
189: oStmt.close();
190: return nBooks;
191: } // countWaitingBookings
192:
193: // ---------------------------------------------------------------------------
194:
195: public int maxAlumni() {
196: if (isNull(DB.nu_max_alumni))
197: return 2147483647;
198: else
199: return getInt(DB.nu_max_alumni);
200: }
201:
202: // ---------------------------------------------------------------------------
203:
204: public boolean store(JDCConnection oConn) throws SQLException {
205: if (!AllVals.containsKey(DB.dt_modified))
206: AllVals.put(DB.dt_modified, new Date());
207: if (!AllVals.containsKey(DB.gu_acourse))
208: AllVals.put(DB.gu_acourse, Gadgets.generateUUID());
209: return super .store(oConn);
210: }
211:
212: // ---------------------------------------------------------------------------
213:
214: public boolean delete(JDCConnection oConn) throws SQLException {
215: return AcademicCourse.delete(oConn, getString(DB.gu_acourse));
216: }
217:
218: // ---------------------------------------------------------------------------
219:
220: public AcademicCourseAlumni[] getAlumni(JDCConnection oConn)
221: throws SQLException {
222: AcademicCourseAlumni[] aAlmni = null;
223: DBSubset oAlmni = new DBSubset(DB.k_x_course_alumni,
224: new AcademicCourseAlumni().getTable(oConn)
225: .getColumnsStr(), DB.gu_acourse
226: + "=? ORDER BY " + DB.gu_alumni, 50);
227: int nBooks = oAlmni.load(oConn,
228: new Object[] { get(DB.gu_acourse) });
229: if (nBooks > 0) {
230: aAlmni = new AcademicCourseAlumni[nBooks];
231: for (int b = 0; b < nBooks; b++) {
232: aAlmni[b] = new AcademicCourseAlumni();
233: aAlmni[b].putAll(oAlmni.getRowAsMap(b));
234: } // next
235: } // fi
236:
237: return aAlmni;
238: } // getAlumni
239:
240: // ---------------------------------------------------------------------------
241:
242: public AcademicCourseBooking[] getAllBookings(JDCConnection oConn)
243: throws SQLException {
244: AcademicCourseBooking[] aBooks = null;
245: DBSubset oBooks = new DBSubset(DB.k_x_course_bookings,
246: new AcademicCourseBooking().getTable(oConn)
247: .getColumnsStr(), DB.gu_acourse
248: + "=? ORDER BY " + DB.dt_created, 50);
249: int nBooks = oBooks.load(oConn,
250: new Object[] { get(DB.gu_acourse) });
251: if (nBooks > 0) {
252: aBooks = new AcademicCourseBooking[nBooks];
253: for (int b = 0; b < nBooks; b++) {
254: aBooks[b] = new AcademicCourseBooking();
255: aBooks[b].putAll(oBooks.getRowAsMap(b));
256: } // next
257: } // fi
258: return aBooks;
259: } // getAllBookings
260:
261: // ---------------------------------------------------------------------------
262:
263: public AcademicCourseBooking[] getActiveBookings(JDCConnection oConn)
264: throws SQLException {
265: AcademicCourseBooking[] aBooks = null;
266: DBSubset oBooks = new DBSubset(DB.k_x_course_bookings,
267: new AcademicCourseBooking().getTable(oConn)
268: .getColumnsStr(), DB.gu_acourse + "=? AND "
269: + DB.bo_canceled + "<>1 " + "ORDER BY "
270: + DB.dt_created, 50);
271: int nBooks = oBooks.load(oConn,
272: new Object[] { get(DB.gu_acourse) });
273: if (nBooks > 0) {
274: aBooks = new AcademicCourseBooking[nBooks];
275: for (int b = 0; b < nBooks; b++) {
276: aBooks[b] = new AcademicCourseBooking();
277: aBooks[b].putAll(oBooks.getRowAsMap(b));
278: } // next
279: } // fi
280: return aBooks;
281: } // getActiveBookings
282:
283: // ---------------------------------------------------------------------------
284:
285: public Course getCourse(JDCConnection oConn) throws SQLException,
286: NullPointerException {
287: if (isNull(DB.gu_course))
288: throw new NullPointerException(
289: "AcademicCourse.getCourse() gu_course is null");
290: return new Course(oConn, getString(DB.gu_course));
291: } // getCourse
292:
293: // ---------------------------------------------------------------------------
294:
295: public Subject[] getSubjects(JDCConnection oConn)
296: throws SQLException, NullPointerException {
297: return getCourse(oConn).getSubjects(oConn);
298: }
299:
300: // ---------------------------------------------------------------------------
301:
302: public void convertConfirmedBookingsToAlumni(JDCConnection oConn)
303: throws SQLException {
304: AcademicCourseBooking[] aBooks = getActiveBookings(oConn);
305: if (null != aBooks) {
306: for (int b = 0; b < aBooks.length; b++) {
307: if (aBooks[b].confirmed()) {
308: aBooks[b].createAlumni(oConn);
309: }
310: } // next
311: } // fi
312: } // convertBookingsToAlumni
313:
314: // ---------------------------------------------------------------------------
315:
316: public static boolean delete(JDCConnection oConn, String sGuACourse)
317: throws SQLException {
318: if (DebugFile.trace) {
319: DebugFile
320: .writeln("Begin AcademicCourse.delete([JDCConnection],"
321: + sGuACourse + ")");
322: DebugFile.incIdent();
323: }
324: if (oConn.getDataBaseProduct() == JDCConnection.DBMS_POSTGRESQL) {
325: if (DebugFile.trace) {
326: DebugFile
327: .writeln("Connection.prepareStatement(SELECT k_sp_del_acourse('"
328: + sGuACourse + "'))");
329: }
330: PreparedStatement oStmt = oConn
331: .prepareStatement("SELECT k_sp_del_acourse(?)");
332: oStmt.setString(1, sGuACourse);
333: oStmt.executeQuery();
334: oStmt.close();
335: } else {
336: if (DebugFile.trace) {
337: DebugFile
338: .writeln("Connection.prepareCall({call k_sp_del_acourse('"
339: + sGuACourse + "')})");
340: }
341: CallableStatement oCall = oConn
342: .prepareCall("{ call k_sp_del_acourse(?) }");
343: oCall.setString(1, sGuACourse);
344: oCall.execute();
345: oCall.close();
346: }
347: if (DebugFile.trace) {
348: DebugFile.decIdent();
349: DebugFile.writeln("End AcademicCourse.delete()");
350: }
351: return true;
352: }
353:
354: // **********************************************************
355: // Public Constants
356:
357: public static final short ClassId = 61;
358:
359: }
|