/** * Method that creates a ClassAttendance object * * @author Ognjen Mišiæ * @param classDescription argument describing the class * @param date of the class * @param duration duration of the class * @return an object * @throws ParseException */ public static ClassAttendance createClass(String classDescription, String date, double duration) throws ParseException { ClassAttendance ca = new ClassAttendance(); if (classDescription.length() > 2 && classDescription.length() < 45) { ca.setClassDescription(classDescription); } else { setErrorMessage("Class description invalid."); } // we reverse validation cause the point is to plan classes ahead and // enter them to table if (!isValidDate(date)) { ca.setDate(date); } else { setErrorMessage("Can't plan classes in past."); } if (duration > 0) { ca.setDuration((int) duration); } else { setErrorMessage("Invalid duraton."); } // this checks if there is anything in errormessage so it returns a null // object if there is an error if (getErrorMessage() != null && !getErrorMessage().isEmpty()) { return null; } else { return ca; } }
/** * Method that returns object of class attendance * * @author Ognjen Mišiæ * @param classID we identify entry by its classID * @return Object ClassAttendance * @throws SQLException */ public static ClassAttendance returnClassAttendance(int classID) throws SQLException { ClassAttendance ca = new ClassAttendance(); try { conn = connectToDb(); stmnt = createReadOnlyStatement(conn); rs = stmnt.executeQuery(SELECT_ALL_FROM_CLASSES_ATTENDANCE); while (rs.next()) { if (rs.getInt("class_id") == classID) { ca.setClassDescription(rs.getString("class_description")); ca.setDate(rs.getString("date")); ca.setDuration(rs.getInt("duration")); ca.setClassID(rs.getInt("class_id")); setErrorMessage(null); break; } else { setErrorMessage("Could not find selected class id"); } } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { rs.close(); } closeStatement(stmnt); closeConnection(conn); } // this checks if there is anything in errormessage so it returns a null // object if there is an error if (getErrorMessage() != null && !getErrorMessage().isEmpty()) { return null; } else { return ca; } }
/** * Method that takes a passed class attendance object and adds it to the table in db * * @author Ognjen Mišiæ * @param ca a new entry in to be added to the table */ public static void addClass(ClassAttendance ca) throws SQLException { if (ca != null) { try { conn = connectToDb(); stmnt = createUpdateableStatement(conn); String sql = "INSERT INTO classes_attendance (class_description, date, duration) VALUES('" + ca.getClassDescription() + "','" + ca.getDate() + "','" + ca.getDuration() + "');"; stmnt.executeUpdate(sql); } catch (SQLException e) { e.printStackTrace(); } finally { closeStatement(stmnt); closeConnection(conn); } } }
/** * Method that edits a class from a database * * @author Ognjen Mišiæ * @param ca Class attendance we want to edit * @param classID we identify the class attendance by class id parameter * @throws ParseException * @throws SQLException */ public static void editClass(ClassAttendance ca, int classID) throws ParseException, SQLException { try { conn = connectToDb(); stmnt = createReadOnlyStatement(conn); rs = stmnt.executeQuery(SELECT_ALL_FROM_CLASSES_ATTENDANCE); String sql = "UPDATE classes_attendance SET class_description='" + ca.getClassDescription() + "', date='" + ca.getDate() + "', duration='" + ca.getDuration() + "' WHERE class_id='" + classID + "';"; while (rs.next()) { if (rs.getInt("class_id") == ca.getClassID()) { Statement stmnt2 = createUpdateableStatement(conn); if (ca.getClassDescription().length() < 2) { setErrorMessage("Invalid class description"); break; } else if (!isValidDate(ca.getDate())) { setErrorMessage("Invalid date."); break; } else if (ca.getDuration() < 1) { setErrorMessage("Invalid class duration"); break; } else { setErrorMessage(null); stmnt2.executeUpdate(sql); break; } } } } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { rs.close(); } closeStatement(stmnt); closeConnection(conn); } }