GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
4
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
marking
Browse code
Fix the result dialog not appearing problem. Fix some schema issues.
master
1 parent
9679c57
commit
0e0125cc8712d3986a707d79b327ed38076283b1
Mark George
authored
on 23 Mar 2023
Patch
Showing
3 changed files
src/main/java/dao/ResultDAO.java
src/main/java/schema.sql
src/main/java/ui/MarkingFrame.java
Ignore Space
Show notes
View
src/main/java/dao/ResultDAO.java
package dao; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.rowset.CachedRowSet; import javax.sql.rowset.RowSetProvider; import javax.swing.table.TableModel; import model.Criterion; import model.CriterionEditor; import model.Student; import ui.CriterionPanel; /** * * @author mark */ public class ResultDAO { private static final Logger logger = Logger.getLogger(ResultDAO.class.getName()); private static Map<String, CriterionEditor> criteria = new HashMap<>(); public void addEditor(Criterion criterion, CriterionPanel criterionEditor) { criteria.put(criterion.getCriterion(), criterionEditor); } public Boolean isComplete() { for (CriterionEditor editor : criteria.values()) { if (!editor.isComplete()) { return Boolean.FALSE; } } return Boolean.TRUE; } public void save(String submissionId) { try { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("merge into result (submission_id, criterion, result, comment) values (?,?,?,?)")) { for (Map.Entry<String, CriterionEditor> entry : criteria.entrySet()) { CriterionEditor editor = entry.getValue(); if (!editor.isComplete()) { continue; } String criterion = entry.getKey(); Double result = editor.getResult(); String comment = editor.getComment(); stmt.setString(1, submissionId); stmt.setString(2, criterion); stmt.setDouble(3, result); stmt.setString(4, comment); stmt.addBatch(); } stmt.executeBatch(); clear(); } } catch (SQLException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); } } public void load(String submissionId) { try { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("select * from result where submission_id = ?")) { stmt.setString(1, submissionId); ResultSet rs = stmt.executeQuery(); while (rs.next()) { String criterion = rs.getString("criterion"); Double result = rs.getDouble("result"); String comment = rs.getString("comment"); CriterionEditor editor = criteria.get(criterion); editor.setResult(result); editor.setComment(comment); } } } catch (SQLException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); } } public TableModel getSubmissions() { try ( Connection con = JdbcConnection.getConnection(); Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery("select * from allmarks order by \"Student\""); CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet(); crs.populate(rs); return new RowSetTableModel(crs); } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } return null; } public Collection<Map<String, ?>> getResult(String studentId) { String sql = "SELECT * FROM report where student_id = ?"; Collection<Map<String, ?>> data = new ArrayList<>(); try ( Connection con = JdbcConnection.getConnection(); PreparedStatement s = con.prepareStatement(sql);) { s.setString(1, studentId); ResultSet rs = s.executeQuery(); while (rs.next()) { Map<String, Object> result = new HashMap<>(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnName(i); Object value = rs.getObject(columnName); if (value instanceof Number) { value = new BigDecimal(String.valueOf(value)); } result.put(columnName, value); } data.add(result); } return data; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); return null; } } public Student findStudent(String submissionId) { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("select * from student where submission_id = ?")) { stmt.setString(1, submissionId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { String id = rs.getString("student_id"); String username = rs.getString("username"); String firstName = rs.getString("first_name"); String lastName = rs.getString("last_name"); String email = rs.getString("email"); Boolean sendFeedback = rs.getBoolean("send_feedback"); Boolean emailSent = rs.getBoolean("email_sent"); Student s = new Student(id, submissionId, username, firstName, lastName, email, sendFeedback, emailSent); return s; } } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } return null; } private void clear() { criteria = new HashMap<>(); } public Collection<Student> getStudents() { List<Student> students = new ArrayList<>(); try ( Connection con = JdbcConnection.getConnection(); PreparedStatement studs = con.prepareStatement("select * from student where student_id in (select distinct student_id from report) order by username")) { ResultSet studsRs = studs.executeQuery(); while (studsRs.next()) { String id = studsRs.getString("student_id"); String submissionId = studsRs.getString("submission_id"); String firstName = studsRs.getString("first_name"); String lastName = studsRs.getString("last_name"); String username = studsRs.getString("username"); String email = studsRs.getString("email"); Boolean sendFeedback = studsRs.getBoolean("send_feedback"); Boolean emailSent = studsRs.getBoolean("email_sent"); Student s = new Student(id, submissionId, username, firstName, lastName, email, sendFeedback, emailSent); students.add(s); } } catch (SQLException ex) { throw new RuntimeException(ex); } return students; } public void mailSent(Student student) { try { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("update student set email_sent = true where student_id = ?");) { stmt.setString(1, student.getId()); stmt.executeUpdate(); con.commit(); } } catch (SQLException ex) { throw new RuntimeException(ex); } } }
package dao; import java.math.BigDecimal; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.ResultSetMetaData; import java.sql.SQLException; import java.sql.Statement; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.rowset.CachedRowSet; import javax.sql.rowset.RowSetProvider; import javax.swing.table.TableModel; import model.Criterion; import model.CriterionEditor; import model.Student; import ui.CriterionPanel; /** * * @author mark */ public class ResultDAO { private static final Logger logger = Logger.getLogger(ResultDAO.class.getName()); private static Map<String, CriterionEditor> criteria = new HashMap<>(); public void addEditor(Criterion criterion, CriterionPanel criterionEditor) { criteria.put(criterion.getCriterion(), criterionEditor); } public Boolean isComplete() { for (CriterionEditor editor : criteria.values()) { if (!editor.isComplete()) { return Boolean.FALSE; } } return Boolean.TRUE; } public void save(String student) { try { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("merge into result (submission_id, criterion, result, comment) values (?,?,?,?)")) { for (Map.Entry<String, CriterionEditor> entry : criteria.entrySet()) { CriterionEditor editor = entry.getValue(); if (!editor.isComplete()) { continue; } String criterion = entry.getKey(); Double result = editor.getResult(); String comment = editor.getComment(); stmt.setString(1, student); stmt.setString(2, criterion); stmt.setDouble(3, result); stmt.setString(4, comment); stmt.addBatch(); } stmt.executeBatch(); clear(); } } catch (SQLException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); } } public void load(String student) { try { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("select * from result where submission_id = ?")) { stmt.setString(1, student); ResultSet rs = stmt.executeQuery(); while (rs.next()) { String criterion = rs.getString("criterion"); Double result = rs.getDouble("result"); String comment = rs.getString("comment"); CriterionEditor editor = criteria.get(criterion); editor.setResult(result); editor.setComment(comment); } } } catch (SQLException e) { logger.log(Level.SEVERE, e.getLocalizedMessage(), e); } } public TableModel getSubmissions() { try ( Connection con = JdbcConnection.getConnection(); Statement stmt = con.createStatement()) { ResultSet rs = stmt.executeQuery("select * from allmarks order by \"Student\""); CachedRowSet crs = RowSetProvider.newFactory().createCachedRowSet(); crs.populate(rs); return new RowSetTableModel(crs); } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } return null; } public Collection<Map<String, ?>> getResult(String studentId) { String sql = "SELECT * FROM report where student_id = ?"; Collection<Map<String, ?>> data = new ArrayList<>(); try ( Connection con = JdbcConnection.getConnection(); PreparedStatement s = con.prepareStatement(sql);) { s.setString(1, studentId); ResultSet rs = s.executeQuery(); while (rs.next()) { Map<String, Object> result = new HashMap<>(); ResultSetMetaData metaData = rs.getMetaData(); int columnCount = metaData.getColumnCount(); for (int i = 1; i <= columnCount; i++) { String columnName = metaData.getColumnName(i); Object value = rs.getObject(columnName); if (value instanceof Number) { value = new BigDecimal(String.valueOf(value)); } result.put(columnName, value); } data.add(result); } return data; } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); return null; } } public Student findStudent(String submissionId) { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("select * from student where submission_id = ?")) { stmt.setString(1, submissionId); ResultSet rs = stmt.executeQuery(); if (rs.next()) { String id = rs.getString("student_id"); String username = rs.getString("username"); String firstName = rs.getString("first_name"); String lastName = rs.getString("last_name"); String email = rs.getString("email"); Boolean sendFeedback = rs.getBoolean("send_feedback"); Boolean emailSent = rs.getBoolean("email_sent"); Student s = new Student(id, submissionId, username, firstName, lastName, email, sendFeedback, emailSent); return s; } } catch (SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } return null; } private void clear() { criteria = new HashMap<>(); } public Collection<Student> getStudents() { List<Student> students = new ArrayList<>(); try ( Connection con = JdbcConnection.getConnection(); PreparedStatement studs = con.prepareStatement("select * from student where student_id in (select distinct student_id from report) order by username")) { ResultSet studsRs = studs.executeQuery(); while (studsRs.next()) { String id = studsRs.getString("student_id"); String submissionId = studsRs.getString("submission_id"); String firstName = studsRs.getString("first_name"); String lastName = studsRs.getString("last_name"); String username = studsRs.getString("username"); String email = studsRs.getString("email"); Boolean sendFeedback = studsRs.getBoolean("send_feedback"); Boolean emailSent = studsRs.getBoolean("email_sent"); Student s = new Student(id, submissionId, username, firstName, lastName, email, sendFeedback, emailSent); students.add(s); } } catch (SQLException ex) { throw new RuntimeException(ex); } return students; } public void mailSent(Student student) { try { try ( Connection con = JdbcConnection.getConnection(); PreparedStatement stmt = con.prepareStatement("update student set email_sent = true where student_id = ?");) { stmt.setString(1, student.getId()); stmt.executeUpdate(); con.commit(); } } catch (SQLException ex) { throw new RuntimeException(ex); } } }
Ignore Space
Show notes
View
src/main/java/schema.sql
CREATE TABLE assessment ( name VARCHAR PRIMARY KEY, out_of DOUBLE NOT NULL ); CREATE TABLE category ( category VARCHAR PRIMARY KEY, sort_order SMALLINT ); CREATE TABLE criterion ( criterion VARCHAR PRIMARY KEY, category VARCHAR NOT NULL, description VARCHAR NOT NULL, sort_order SMALLINT, weight REAL, min SMALLINT, max SMALLINT, special BOOLEAN, CONSTRAINT fk_criterion_category FOREIGN KEY(category) REFERENCES category(category) ); CREATE TABLE submission ( submission_id VARCHAR PRIMARY KEY ); CREATE TABLE student ( submission_id VARCHAR UNIQUE, student_id VARCHAR UNIQUE, username VARCHAR UNIQUE, first_name VARCHAR, last_name VARCHAR, email VARCHAR UNIQUE, send_feedback BOOLEAN, email_sent BOOLEAN ); CREATE TABLE result ( submission_id VARCHAR NOT NULL, criterion VARCHAR NOT NULL, result REAL NOT NULL, comment VARCHAR, CONSTRAINT pk_result PRIMARY KEY (submission_id, criterion), CONSTRAINT fk_result_student FOREIGN KEY (submission_id) REFERENCES submission(submission_id), CONSTRAINT fk_result_criterion FOREIGN KEY (criterion) REFERENCES criterion(criterion) ); CREATE VIEW weighted_mark AS SELECT submission_id, Sum(weighted) * (SELECT out_of FROM assessment) AS MARK FROM (SELECT submission_id, result, weight, max, ( result / max * ( weight / (SELECT Sum(weight) FROM criterion WHERE NOT special) ) ) AS WEIGHTED FROM result INNER JOIN criterion ON result.criterion = criterion.criterion WHERE NOT special) GROUP BY submission_id; -- special marks are multiplied by the criterion weight and added to the mark - use negative weight for penalties CREATE VIEW weighted_special AS SELECT submission_id, Sum(special) AS special from ( SELECT submission_id, ( result * weight ) AS special FROM result INNER JOIN criterion ON result.criterion = criterion.criterion WHERE special UNION ALL SELECT DISTINCT submission_id, 0 FROM result) group by submission_id -- version to use when bonus contributes to other assessment (maximum mark CAN be exceeded) --CREATE VIEW mark --AS -- SELECT weighted_mark.submission, -- Round(mark + special, 2) AS MARK -- FROM weighted_mark -- INNER JOIN weighted_special -- ON weighted_mark.submission = weighted_special.submission; -- version to use when bonus contributes only to this assessment (maximum mark can not be exceeded) CREATE VIEW mark AS SELECT weighted_mark.submission_id, Round( Casewhen( mark + special <= (SELECT out_of FROM assessment), mark + special, (SELECT out_of FROM assessment) ), 5) AS MARK FROM weighted_mark INNER JOIN weighted_special ON weighted_mark.submission_id = weighted_special.submission_id; CREATE VIEW marking_schedule AS SELECT criterion.category, description, weight, max AS OUTOF FROM criterion INNER JOIN category ON category.category = criterion.category ORDER BY category.sort_order, criterion.sort_order; CREATE VIEW allmarks("Student", "Weighted Mark", "Marking Complete", "Email Sent") AS SELECT submission.submission_id, COALESCE(Cast(Round(mark, 2) AS VARCHAR), ''), COALESCE(Cast(complete AS VARCHAR), ''), COALESCE(Cast(email_sent AS VARCHAR), '') FROM submission LEFT JOIN mark USING (submission_id) LEFT JOIN (SELECT submission_id, Count(criterion) = (SELECT Count(criterion) FROM criterion) AS complete FROM result GROUP BY submission_id) USING (submission_id) LEFT JOIN student USING (submission_id); -- view for showing relative weightings as percentages of total mark create view weighted_schedule as select cat.category, criterion, description, weight, special, round(weight / total_weight * 100, 2) as relative_weight, round(weight / total_weight * (select out_of from assessment), 2) as out_of from criterion crit, (select sum(weight) as total_weight from criterion where not special) inner join category cat on crit.category = cat.category order by cat.sort_order, crit.sort_order; create view weighted_categories as select category,round(sum(relative_weight),1) as weight FROM WEIGHTED_SCHEDULE where not special group by category; create view report as select a.name as assessment, a.out_of, s.student_id, s.first_name || ' ' || s.last_name as name, s.username, cat.category, cat.sort_order as cat_order, cr.description as criterion, cr.max as max, cr.sort_order as cr_order, r.result,r.comment, round(m.mark,3) as mark from assessment a join category cat inner join criterion cr on cat.category = cr.category inner join result r on r.criterion=cr.criterion inner join student s on r.submission_id =s.submission_id inner join mark m on r.submission_id = m.submission_id order by student_id, cat_order,cr_order -- use this if you want to produce useful category totals (and you don't care about weighting for individual criterion) --UPDATE criterion --SET weight = Cast(max AS DOUBLE) / (SELECT Count(*) -- FROM criterion);
CREATE TABLE assessment ( name VARCHAR PRIMARY KEY, out_of DOUBLE NOT NULL ); CREATE TABLE category ( category VARCHAR PRIMARY KEY, sort_order SMALLINT ); CREATE TABLE criterion ( criterion VARCHAR PRIMARY KEY, category VARCHAR NOT NULL, description VARCHAR NOT NULL, sort_order SMALLINT, weight REAL, min SMALLINT, max SMALLINT, special BOOLEAN, CONSTRAINT fk_criterion_category FOREIGN KEY(category) REFERENCES category(category) ); CREATE TABLE submission ( submission_id VARCHAR PRIMARY KEY ); CREATE TABLE student ( submission_id VARCHAR UNIQUE, student_id VARCHAR UNIQUE, username VARCHAR UNIQUE, first_name VARCHAR, last_name VARCHAR, email VARCHAR UNIQUE, send_feedback BOOLEAN, email_sent BOOLEAN ); CREATE TABLE result ( submission_id VARCHAR NOT NULL, criterion VARCHAR NOT NULL, result REAL NOT NULL, comment VARCHAR, CONSTRAINT pk_result PRIMARY KEY (submission_id, criterion), CONSTRAINT fk_result_student FOREIGN KEY (submission_id) REFERENCES submission(submission_id), CONSTRAINT fk_result_criterion FOREIGN KEY (criterion) REFERENCES criterion(criterion) ); CREATE VIEW weighted_mark AS SELECT submission_id, Sum(weighted) * (SELECT out_of FROM assessment) AS MARK FROM (SELECT submission_id, result, weight, max, ( result / max * ( weight / (SELECT Sum(weight) FROM criterion WHERE NOT special) ) ) AS WEIGHTED FROM result INNER JOIN criterion ON result.criterion = criterion.criterion WHERE NOT special) GROUP BY submission_id; CREATE VIEW weighted_special AS SELECT submission_id, Sum(special) AS special from ( SELECT submission_id, ( result / max * ( (SELECT out_of FROM assessment * weight) ) ) AS special FROM result INNER JOIN criterion ON result.criterion = criterion.criterion WHERE special UNION ALL SELECT DISTINCT submission_id, 0 FROM result) group by submission_id; -- version to use when bonus contributes to other assessment (maximum mark CAN be exceeded) --CREATE VIEW mark --AS -- SELECT weighted_mark.submission, -- Round(mark + special, 2) AS MARK -- FROM weighted_mark -- INNER JOIN weighted_special -- ON weighted_mark.submission = weighted_special.submission; -- version to use when bonus contributes only to this assessment (maximum mark can not be exceeded) CREATE VIEW mark AS SELECT weighted_mark.submission_id, Round(Casewhen(mark + special <= (SELECT out_of FROM assessment), mark + special, ( SELECT out_of FROM assessment)), 5) AS MARK FROM weighted_mark INNER JOIN weighted_special ON weighted_mark.submission_id = weighted_special.submission_id; CREATE VIEW marking_schedule AS SELECT criterion.category, description, weight, max AS OUTOF FROM criterion INNER JOIN category ON category.category = criterion.category ORDER BY category.sort_order, criterion.sort_order; CREATE VIEW allmarks("Student", "Weighted Mark", "Marking Complete", "Email Sent") AS SELECT submission.submission_id, COALESCE(Cast(Round(mark, 2) AS VARCHAR), ''), COALESCE(Cast(complete AS VARCHAR), ''), COALESCE(Cast(email_sent AS VARCHAR), '') FROM submission LEFT JOIN mark USING (submission_id) LEFT JOIN (SELECT submission_id, Count(criterion) = (SELECT Count(criterion) FROM criterion) AS complete FROM result GROUP BY submission_id) USING (submission_id) LEFT JOIN student USING (submission_id); -- view for showing relative weightings as percentages of total mark create view weighted_schedule as select cat.category, criterion, description, weight, special, round(weight / total_weight * 100, 2) as relative_weight, round(weight / total_weight * (select out_of from assessment), 2) as out_of from criterion crit, (select sum(weight) as total_weight from criterion where not special) inner join category cat on crit.category = cat.category order by cat.sort_order, crit.sort_order; create view weighted_categories as select category,round(sum(relative_weight),1) as weight FROM WEIGHTED_SCHEDULE where not special group by category; create view report as select a.name as assessment, a.out_of, s.student_id, s.first_name || ' ' || s.last_name as name, s.username, cat.category, cat.sort_order as cat_order, cr.description as criterion, cr.max as max, cr.sort_order as cr_order, r.result,r.comment, round(m.mark,3) as mark from assessment a join category cat inner join criterion cr on cat.category = cr.category inner join result r on r.criterion=cr.criterion inner join student s on r.submission_id =s.submission_id inner join mark m on r.submission_id = m.submission_id order by student_id, cat_order,cr_order -- use this if you want to produce useful category totals (and you don't care about weighting for individual criterion) --UPDATE criterion --SET weight = Cast(max AS DOUBLE) / (SELECT Count(*) -- FROM criterion);
Ignore Space
Show notes
View
src/main/java/ui/MarkingFrame.java
package ui; import dao.MarkingProperties; import dao.ResultDAO; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Collection; import java.util.List; import java.util.Map; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import model.Criterion; import model.Student; import report.FeedbackReportGenerator; import report.SendReport; /** * * @author mark */ public class MarkingFrame extends JDialog { private Student student; private final JPanel schedulePane; private final ResultDAO resultDao = new ResultDAO(); private final JLabel lblStudent; private final JLabel lblAssessment; private final SubmissionFrame parent; private final String assessment; private final JCheckBox cbExpandable; @SuppressWarnings("Convert2Lambda") public MarkingFrame(SubmissionFrame parent, String assessment) { this.setModal(true); this.parent = parent; this.assessment = assessment; // add scroll pane to the root JScrollPane scrollPane = new JScrollPane(); JPanel contentPane = new JPanel(); JPanel titlePane = new JPanel(); cbExpandable = new JCheckBox("Expand Text Areas?", false); scrollPane.setViewportView(contentPane); scrollPane.getVerticalScrollBar().setUnitIncrement(16); getContentPane().add(titlePane, java.awt.BorderLayout.NORTH); getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER); titlePane.setLayout(new BorderLayout()); lblStudent = new JLabel("Student"); Font newFont = lblStudent.getFont().deriveFont(Font.BOLD, 15); lblStudent.setFont(newFont); lblAssessment = new JLabel("Assessment"); lblAssessment.setFont(newFont); titlePane.add(BorderLayout.WEST, lblStudent); titlePane.add(BorderLayout.EAST, lblAssessment); // add other panes schedulePane = new JPanel(); JPanel buttonPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(schedulePane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); schedulePane.setLayout(new BoxLayout(schedulePane, BoxLayout.Y_AXIS)); // add buttons JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { save(); } }); // add buttons JButton btnCancel = new JButton("Cancel"); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { dispose(); } }); JButton btnMin = new JButton("Min"); btnMin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { min(); } }); JButton btnMax = new JButton("Max"); btnMax.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { max(); } }); JButton btnEmail = new JButton("Save & Email"); btnEmail.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { email(); } }); buttonPane.add(btnMin); buttonPane.add(btnMax); buttonPane.add(btnCancel); if (MarkingProperties.isEmailEnabled()) { buttonPane.add(btnEmail); } buttonPane.add(btnSave); buttonPane.add(cbExpandable); } public void addCategory(String category, List<Criterion> criteria) { schedulePane.add(new CategoryPanel(category, criteria)); } private void save() { if (!resultDao.isComplete()) { int res = JOptionPane.showConfirmDialog(MarkingFrame.this, "You haven't entered values for all criteria. Are you sure you want save?", "Incomplete", JOptionPane.OK_CANCEL_OPTION); if (JOptionPane.OK_OPTION == res) { resultDao.save(student.getSubmissionId()); parent.refresh(); dispose(); } } else { resultDao.save(student.getSubmissionId()); parent.refresh(); Collection<Map<String, ?>> result = resultDao.getResult(student.getId()); if (result.isEmpty()) { // marking is incomplete, so don't try to show a mark dispose(); return; } String mark = result.iterator().next().get("MARK").toString(); System.out.println("DISPLAYING MARK"); JOptionPane.showMessageDialog(this, "Mark is " + mark); dispose(); } } private void min() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; c.setZero(); } } } } } private void max() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; if (!c.getCriterion().isIsSpecial()) { c.setMax(); } else { c.setZero(); } } } } } } public void load(String submissionId) { student = new ResultDAO().findStudent(submissionId); lblStudent.setText(student.getFirstName() + " " + student.getLastName() + " (" + student.getId() + ")"); lblAssessment.setText(assessment); resultDao.load(submissionId); } public void display() { this.setPreferredSize(new Dimension(600, 980)); this.pack(); this.setVisible(true); } public void createReport() { try { new FeedbackReportGenerator(new ResultDAO().getResult(student.getId()), MarkingProperties.reportPath(), MarkingProperties.attachmentFilePrefix(), MarkingProperties.reportTitle(), MarkingProperties.reportFooter()).generate(student); } catch (Exception ex) { throw new RuntimeException(ex); } } public void emailReport() { String report = MarkingProperties.reportPath() + "/" + MarkingProperties.attachmentFilePrefix() + "-" + student.getId() + ".pdf"; new SendReport().sendReport(student, report); } @SuppressWarnings("Convert2Lambda") private void email() { save(); new Thread(new Runnable() { @Override public void run() { createReport(); emailReport(); } }).start(); } public boolean getExpandable() { return this.cbExpandable.isSelected(); } }
package ui; import dao.MarkingProperties; import dao.ResultDAO; import java.awt.BorderLayout; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.util.Collection; import java.util.List; import java.util.Map; import javax.swing.BoxLayout; import javax.swing.JButton; import javax.swing.JCheckBox; import javax.swing.JDialog; import javax.swing.JLabel; import javax.swing.JOptionPane; import javax.swing.JPanel; import javax.swing.JScrollPane; import model.Criterion; import model.Student; import report.FeedbackReportGenerator; import report.SendReport; /** * * @author mark */ public class MarkingFrame extends JDialog { private Student student; private String submissionKey; private final JPanel schedulePane; private final ResultDAO resultDao = new ResultDAO(); private final JLabel lblStudent; private final JLabel lblAssessment; private final SubmissionFrame parent; private final String assessment; private final JCheckBox cbExpandable; @SuppressWarnings("Convert2Lambda") public MarkingFrame(SubmissionFrame parent, String assessment) { this.setModal(true); this.parent = parent; this.assessment = assessment; // add scroll pane to the root JScrollPane scrollPane = new JScrollPane(); JPanel contentPane = new JPanel(); JPanel titlePane = new JPanel(); cbExpandable = new JCheckBox("Expand Text Areas?", false); scrollPane.setViewportView(contentPane); scrollPane.getVerticalScrollBar().setUnitIncrement(16); getContentPane().add(titlePane, java.awt.BorderLayout.NORTH); getContentPane().add(scrollPane, java.awt.BorderLayout.CENTER); titlePane.setLayout(new BorderLayout()); lblStudent = new JLabel("Student"); Font newFont = lblStudent.getFont().deriveFont(Font.BOLD, 15); lblStudent.setFont(newFont); lblAssessment = new JLabel("Assessment"); lblAssessment.setFont(newFont); titlePane.add(BorderLayout.WEST, lblStudent); titlePane.add(BorderLayout.EAST, lblAssessment); // add other panes schedulePane = new JPanel(); JPanel buttonPane = new JPanel(); contentPane.setLayout(new BorderLayout()); contentPane.add(schedulePane, BorderLayout.CENTER); contentPane.add(buttonPane, BorderLayout.SOUTH); schedulePane.setLayout(new BoxLayout(schedulePane, BoxLayout.Y_AXIS)); // add buttons JButton btnSave = new JButton("Save"); btnSave.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { save(); } }); // add buttons JButton btnCancel = new JButton("Cancel"); btnCancel.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { dispose(); } }); JButton btnMin = new JButton("Min"); btnMin.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { min(); } }); JButton btnMax = new JButton("Max"); btnMax.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { max(); } }); JButton btnEmail = new JButton("Save & Email"); btnEmail.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent ae) { email(); } }); buttonPane.add(btnMin); buttonPane.add(btnMax); buttonPane.add(btnCancel); if (MarkingProperties.isEmailEnabled()) { buttonPane.add(btnEmail); } buttonPane.add(btnSave); buttonPane.add(cbExpandable); } public void addCategory(String category, List<Criterion> criteria) { schedulePane.add(new CategoryPanel(category, criteria)); } private void save() { if (!resultDao.isComplete()) { int res = JOptionPane.showConfirmDialog(MarkingFrame.this, "You haven't entered values for all criteria. Are you sure you want save?", "Incomplete", JOptionPane.OK_CANCEL_OPTION); if (JOptionPane.OK_OPTION == res) { resultDao.save(student.getSubmissionId()); parent.refresh(); dispose(); } } else { resultDao.save(this.submissionKey); parent.refresh(); Collection<Map<String, ?>> result = resultDao.getResult(student.getSubmissionId()); if (result.isEmpty()) { // marking is incomplete, so don't try to show a mark dispose(); return; } String mark = result.iterator().next().get("MARK").toString(); JOptionPane.showMessageDialog(this, "Mark is " + mark); dispose(); } } private void min() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; c.setZero(); } } } } } private void max() { Component[] components = schedulePane.getComponents(); for (Component component : components) { if (component instanceof CategoryPanel) { CategoryPanel cp = (CategoryPanel) component; Component[] categories = cp.getComponents(); for (Component criterion : categories) { if (criterion instanceof CriterionPanel) { CriterionPanel c = (CriterionPanel) criterion; if (!c.getCriterion().isIsSpecial()) { c.setMax(); } else { c.setZero(); } } } } } } public void load(String id) { this.submissionKey = id; student = new ResultDAO().findStudent(id); lblStudent.setText(student.getFirstName() + " " + student.getLastName() + " (" + student.getId() + ")"); lblAssessment.setText(assessment); resultDao.load(this.submissionKey); } public void display() { this.setPreferredSize(new Dimension(600, 980)); this.pack(); this.setVisible(true); } public void createReport() { try { new FeedbackReportGenerator(new ResultDAO().getResult(student.getSubmissionId()), MarkingProperties.reportPath(), MarkingProperties.attachmentFilePrefix(), MarkingProperties.reportTitle(), MarkingProperties.reportFooter()).generate(student); } catch (Exception ex) { throw new RuntimeException(ex); } } public void emailReport() { String report = MarkingProperties.reportPath() + "/" + MarkingProperties.attachmentFilePrefix() + "-" + student.getId() + ".pdf"; new SendReport().sendReport(student, report); } @SuppressWarnings("Convert2Lambda") private void email() { save(); new Thread(new Runnable() { @Override public void run() { createReport(); emailReport(); } }).start(); } public boolean getExpandable() { return this.cbExpandable.isSelected(); } }
Show line notes below