KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > training > AcademicCourse


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

34
35 import java.util.Date JavaDoc;
36 import java.sql.SQLException JavaDoc;
37 import java.sql.PreparedStatement JavaDoc;
38 import java.sql.CallableStatement JavaDoc;
39 import java.sql.ResultSet JavaDoc;
40
41 import com.knowgate.jdc.JDCConnection;
42
43 import com.knowgate.debug.DebugFile;
44 import com.knowgate.dataobjs.DB;
45 import com.knowgate.dataobjs.DBPersist;
46 import com.knowgate.dataobjs.DBSubset;
47
48 import com.knowgate.misc.Gadgets;
49
50 /**
51  * Academic Course Instance
52  * @author Sergio Montoro Ten
53  * @version 2.2
54  */

55
56 public class AcademicCourse extends DBPersist {
57
58   public AcademicCourse() {
59     super(DB.k_academic_courses, "AcademicCourse");
60   }
61
62   public AcademicCourse(JDCConnection oConn, String JavaDoc sGuACourse)
63     throws SQLException JavaDoc {
64     super(DB.k_academic_courses, "AcademicCourse");
65     load(oConn, new Object JavaDoc[]{sGuACourse});
66   }
67
68   // ---------------------------------------------------------------------------
69

70   public boolean active() {
71     boolean bRetVal;
72     if (isNull(DB.bo_active))
73       bRetVal = false;
74     else
75       bRetVal = (getShort(DB.bo_active)==(short) 1);
76     return bRetVal;
77   } // active
78

79   // ---------------------------------------------------------------------------
80

81   public int countAlumni(JDCConnection oConn)
82     throws SQLException JavaDoc,IllegalStateException JavaDoc {
83
84     if (isNull(DB.gu_acourse))
85       throw new IllegalStateException JavaDoc("AcademicCourse.countBookings() gu_acourse not set");
86
87     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(*) FROM "+DB.k_x_course_alumni+" WHERE "+DB.gu_acourse+"=?");
88     oStmt.setString(1, getString(DB.gu_acourse));
89     ResultSet JavaDoc oRSet = oStmt.executeQuery();
90     oRSet.next();
91     int nAlumni = oRSet.getInt(1);
92     oRSet.close();
93     oStmt.close();
94     return nAlumni;
95   } // countAlumni
96

97   // ---------------------------------------------------------------------------
98

99   public int countBookings(JDCConnection oConn)
100     throws SQLException JavaDoc,IllegalStateException JavaDoc {
101
102     if (isNull(DB.gu_acourse))
103       throw new IllegalStateException JavaDoc("AcademicCourse.countBookings() gu_acourse not set");
104
105     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(*) FROM "+DB.k_x_course_bookings+" WHERE "+DB.gu_acourse+"=? AND "+DB.bo_canceled+"<>1");
106     oStmt.setString(1, getString(DB.gu_acourse));
107     ResultSet JavaDoc oRSet = oStmt.executeQuery();
108     oRSet.next();
109     int nBooks = oRSet.getInt(1);
110     oRSet.close();
111     oStmt.close();
112     return nBooks;
113   } // countBookings
114

115   // ---------------------------------------------------------------------------
116

117   public int countConfirmedBookings(JDCConnection oConn)
118     throws SQLException JavaDoc,IllegalStateException JavaDoc {
119
120     if (isNull(DB.gu_acourse))
121       throw new IllegalStateException JavaDoc("AcademicCourse.countBookings() gu_acourse not set");
122
123     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(*) FROM "+DB.k_x_course_bookings+" WHERE "+DB.gu_acourse+"=? AND "+DB.bo_canceled+"<>1 AND "+DB.bo_confirmed+"=1");
124     oStmt.setString(1, getString(DB.gu_acourse));
125     ResultSet JavaDoc oRSet = oStmt.executeQuery();
126     oRSet.next();
127     int nBooks = oRSet.getInt(1);
128     oRSet.close();
129     oStmt.close();
130     return nBooks;
131   } // countConfirmedBookings
132

133   // ---------------------------------------------------------------------------
134

135   public int countPaidBookings(JDCConnection oConn)
136     throws SQLException JavaDoc,IllegalStateException JavaDoc {
137
138     if (isNull(DB.gu_acourse))
139       throw new IllegalStateException JavaDoc("AcademicCourse.countBookings() gu_acourse not set");
140
141     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(*) FROM "+DB.k_x_course_bookings+" WHERE "+DB.gu_acourse+"=? AND "+DB.bo_canceled+"<>1 AND "+DB.bo_paid+"=1");
142     oStmt.setString(1, getString(DB.gu_acourse));
143     ResultSet JavaDoc oRSet = oStmt.executeQuery();
144     oRSet.next();
145     int nBooks = oRSet.getInt(1);
146     oRSet.close();
147     oStmt.close();
148     return nBooks;
149   } // countPaidBookings
150

151   // ---------------------------------------------------------------------------
152

153   public int countWaitingBookings(JDCConnection oConn)
154     throws SQLException JavaDoc,IllegalStateException JavaDoc {
155
156     if (isNull(DB.gu_acourse))
157       throw new IllegalStateException JavaDoc("AcademicCourse.countBookings() gu_acourse not set");
158
159     PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT COUNT(*) FROM "+DB.k_x_course_bookings+" WHERE "+DB.gu_acourse+"=? AND "+DB.bo_canceled+"<>1 AND "+DB.bo_waiting+"=1");
160     oStmt.setString(1, getString(DB.gu_acourse));
161     ResultSet JavaDoc oRSet = oStmt.executeQuery();
162     oRSet.next();
163     int nBooks = oRSet.getInt(1);
164     oRSet.close();
165     oStmt.close();
166     return nBooks;
167   } // countWaitingBookings
168

169   // ---------------------------------------------------------------------------
170

171   public int maxAlumni() {
172     if (isNull(DB.nu_max_alumni))
173       return 2147483647;
174     else
175       return getInt(DB.nu_max_alumni);
176   }
177
178   // ---------------------------------------------------------------------------
179

180   public boolean store(JDCConnection oConn) throws SQLException JavaDoc{
181     if (!AllVals.containsKey(DB.dt_modified))
182       AllVals.put(DB.dt_modified, new Date JavaDoc());
183     if (!AllVals.containsKey(DB.gu_acourse))
184       AllVals.put(DB.gu_acourse, Gadgets.generateUUID());
185     return super.store(oConn);
186   }
187
188   // ---------------------------------------------------------------------------
189

190   public boolean delete (JDCConnection oConn) throws SQLException JavaDoc {
191     return AcademicCourse.delete(oConn, getString(DB.gu_acourse));
192   }
193
194   // ---------------------------------------------------------------------------
195

196   public AcademicCourseAlumni[] getAlumni(JDCConnection oConn)
197     throws SQLException JavaDoc {
198     AcademicCourseAlumni[] aAlmni = null;
199     DBSubset oAlmni = new DBSubset(DB.k_x_course_alumni,
200                                    new AcademicCourseAlumni().getTable(oConn).getColumnsStr(),
201                                    DB.gu_acourse+"=? ORDER BY "+DB.gu_alumni,50);
202     int nBooks = oAlmni.load(oConn, new Object JavaDoc[]{get(DB.gu_acourse)});
203     if (nBooks>0) {
204       aAlmni = new AcademicCourseAlumni[nBooks];
205       for (int b=0; b<nBooks; b++) {
206         aAlmni[b] = new AcademicCourseAlumni();
207         aAlmni[b].putAll(oAlmni.getRowAsMap(b));
208       } // next
209
} // fi
210

211     return aAlmni;
212   } // getAlumni
213

214   // ---------------------------------------------------------------------------
215

216   public AcademicCourseBooking[] getAllBookings(JDCConnection oConn)
217     throws SQLException JavaDoc {
218     AcademicCourseBooking[] aBooks = null;
219     DBSubset oBooks = new DBSubset(DB.k_x_course_bookings,
220                                    new AcademicCourseBooking().getTable(oConn).getColumnsStr(),
221                                    DB.gu_acourse+"=? ORDER BY "+DB.dt_created,50);
222     int nBooks = oBooks.load(oConn, new Object JavaDoc[]{get(DB.gu_acourse)});
223     if (nBooks>0) {
224       aBooks = new AcademicCourseBooking[nBooks];
225       for (int b=0; b<nBooks; b++) {
226         aBooks[b] = new AcademicCourseBooking();
227         aBooks[b].putAll(oBooks.getRowAsMap(b));
228       } // next
229
} // fi
230
return aBooks;
231   } // getAllBookings
232

233   // ---------------------------------------------------------------------------
234

235   public AcademicCourseBooking[] getActiveBookings(JDCConnection oConn)
236     throws SQLException JavaDoc {
237     AcademicCourseBooking[] aBooks = null;
238     DBSubset oBooks = new DBSubset(DB.k_x_course_bookings,
239                                    new AcademicCourseBooking().getTable(oConn).getColumnsStr(),
240                                    DB.gu_acourse+"=? AND "+DB.bo_canceled+"<>1 "+
241                                    "ORDER BY "+DB.dt_created,50);
242     int nBooks = oBooks.load(oConn, new Object JavaDoc[]{get(DB.gu_acourse)});
243     if (nBooks>0) {
244       aBooks = new AcademicCourseBooking[nBooks];
245       for (int b=0; b<nBooks; b++) {
246         aBooks[b] = new AcademicCourseBooking();
247         aBooks[b].putAll(oBooks.getRowAsMap(b));
248       } // next
249
} // fi
250
return aBooks;
251   } // getActiveBookings
252

253   // ---------------------------------------------------------------------------
254

255   public Course getCourse(JDCConnection oConn)
256     throws SQLException JavaDoc,NullPointerException JavaDoc {
257     if (isNull(DB.gu_course)) throw new NullPointerException JavaDoc("AcademicCourse.getCourse() gu_course is null");
258     return new Course(oConn, getString(DB.gu_course));
259   } // getCourse
260

261   // ---------------------------------------------------------------------------
262

263   public Subject[] getSubjects(JDCConnection oConn)
264     throws SQLException JavaDoc,NullPointerException JavaDoc {
265     return getCourse(oConn).getSubjects(oConn);
266   }
267
268   // ---------------------------------------------------------------------------
269

270   public void convertConfirmedBookingsToAlumni(JDCConnection oConn)
271     throws SQLException JavaDoc {
272     AcademicCourseBooking[] aBooks = getActiveBookings(oConn);
273     if (null!=aBooks) {
274       for (int b=0; b<aBooks.length; b++) {
275         if (aBooks[b].confirmed()) {
276           aBooks[b].createAlumni(oConn);
277         }
278       } // next
279
} // fi
280
} // convertBookingsToAlumni
281

282   // ---------------------------------------------------------------------------
283

284   public static boolean delete (JDCConnection oConn, String JavaDoc sGuACourse) throws SQLException JavaDoc {
285     if (DebugFile.trace) {
286       DebugFile.writeln("Begin AcademicCourse.delete([JDCConnection],"+sGuACourse+")");
287       DebugFile.incIdent();
288     }
289     if (oConn.getDataBaseProduct()==JDCConnection.DBMS_POSTGRESQL) {
290       if (DebugFile.trace) {
291         DebugFile.writeln("Connection.prepareStatement(SELECT k_sp_del_acourse('"+sGuACourse+"'))");
292       }
293       PreparedStatement JavaDoc oStmt = oConn.prepareStatement("SELECT k_sp_del_acourse(?)");
294       oStmt.setString(1, sGuACourse);
295       oStmt.executeQuery();
296       oStmt.close();
297     }
298     else {
299       if (DebugFile.trace) {
300         DebugFile.writeln("Connection.prepareCall({call k_sp_del_acourse('"+sGuACourse+"')})");
301       }
302       CallableStatement JavaDoc oCall = oConn.prepareCall("{ call k_sp_del_acourse(?) }");
303       oCall.setString(1, sGuACourse);
304       oCall.execute();
305       oCall.close();
306     }
307     if (DebugFile.trace) {
308       DebugFile.decIdent();
309       DebugFile.writeln("End AcademicCourse.delete()");
310     }
311     return true;
312   }
313
314   // **********************************************************
315
// Public Constants
316

317   public static final short ClassId = 61;
318
319 }
320
Popular Tags