1 package com.knowgate.training; 2 3 34 35 import java.util.Date ; 36 import java.sql.SQLException ; 37 import java.sql.PreparedStatement ; 38 import java.sql.CallableStatement ; 39 import java.sql.ResultSet ; 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 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 sGuACourse) 63 throws SQLException { 64 super(DB.k_academic_courses, "AcademicCourse"); 65 load(oConn, new Object []{sGuACourse}); 66 } 67 68 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 } 79 81 public int countAlumni(JDCConnection oConn) 82 throws SQLException ,IllegalStateException { 83 84 if (isNull(DB.gu_acourse)) 85 throw new IllegalStateException ("AcademicCourse.countBookings() gu_acourse not set"); 86 87 PreparedStatement 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 oRSet = oStmt.executeQuery(); 90 oRSet.next(); 91 int nAlumni = oRSet.getInt(1); 92 oRSet.close(); 93 oStmt.close(); 94 return nAlumni; 95 } 97 99 public int countBookings(JDCConnection oConn) 100 throws SQLException ,IllegalStateException { 101 102 if (isNull(DB.gu_acourse)) 103 throw new IllegalStateException ("AcademicCourse.countBookings() gu_acourse not set"); 104 105 PreparedStatement 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 oRSet = oStmt.executeQuery(); 108 oRSet.next(); 109 int nBooks = oRSet.getInt(1); 110 oRSet.close(); 111 oStmt.close(); 112 return nBooks; 113 } 115 117 public int countConfirmedBookings(JDCConnection oConn) 118 throws SQLException ,IllegalStateException { 119 120 if (isNull(DB.gu_acourse)) 121 throw new IllegalStateException ("AcademicCourse.countBookings() gu_acourse not set"); 122 123 PreparedStatement 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 oRSet = oStmt.executeQuery(); 126 oRSet.next(); 127 int nBooks = oRSet.getInt(1); 128 oRSet.close(); 129 oStmt.close(); 130 return nBooks; 131 } 133 135 public int countPaidBookings(JDCConnection oConn) 136 throws SQLException ,IllegalStateException { 137 138 if (isNull(DB.gu_acourse)) 139 throw new IllegalStateException ("AcademicCourse.countBookings() gu_acourse not set"); 140 141 PreparedStatement 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 oRSet = oStmt.executeQuery(); 144 oRSet.next(); 145 int nBooks = oRSet.getInt(1); 146 oRSet.close(); 147 oStmt.close(); 148 return nBooks; 149 } 151 153 public int countWaitingBookings(JDCConnection oConn) 154 throws SQLException ,IllegalStateException { 155 156 if (isNull(DB.gu_acourse)) 157 throw new IllegalStateException ("AcademicCourse.countBookings() gu_acourse not set"); 158 159 PreparedStatement 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 oRSet = oStmt.executeQuery(); 162 oRSet.next(); 163 int nBooks = oRSet.getInt(1); 164 oRSet.close(); 165 oStmt.close(); 166 return nBooks; 167 } 169 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 180 public boolean store(JDCConnection oConn) throws SQLException { 181 if (!AllVals.containsKey(DB.dt_modified)) 182 AllVals.put(DB.dt_modified, new Date ()); 183 if (!AllVals.containsKey(DB.gu_acourse)) 184 AllVals.put(DB.gu_acourse, Gadgets.generateUUID()); 185 return super.store(oConn); 186 } 187 188 190 public boolean delete (JDCConnection oConn) throws SQLException { 191 return AcademicCourse.delete(oConn, getString(DB.gu_acourse)); 192 } 193 194 196 public AcademicCourseAlumni[] getAlumni(JDCConnection oConn) 197 throws SQLException { 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 []{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 } } 211 return aAlmni; 212 } 214 216 public AcademicCourseBooking[] getAllBookings(JDCConnection oConn) 217 throws SQLException { 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 []{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 } } return aBooks; 231 } 233 235 public AcademicCourseBooking[] getActiveBookings(JDCConnection oConn) 236 throws SQLException { 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 []{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 } } return aBooks; 251 } 253 255 public Course getCourse(JDCConnection oConn) 256 throws SQLException ,NullPointerException { 257 if (isNull(DB.gu_course)) throw new NullPointerException ("AcademicCourse.getCourse() gu_course is null"); 258 return new Course(oConn, getString(DB.gu_course)); 259 } 261 263 public Subject[] getSubjects(JDCConnection oConn) 264 throws SQLException ,NullPointerException { 265 return getCourse(oConn).getSubjects(oConn); 266 } 267 268 270 public void convertConfirmedBookingsToAlumni(JDCConnection oConn) 271 throws SQLException { 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 } } } 282 284 public static boolean delete (JDCConnection oConn, String sGuACourse) throws SQLException { 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 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 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 317 public static final short ClassId = 61; 318 319 } 320
| Popular Tags
|