GitBucket
4.21.2
Toggle navigation
Snippets
Sign in
Files
Branches
1
Releases
Issues
2
Pull requests
Labels
Priorities
Milestones
Wiki
Forks
mark.george
/
marking
Browse code
Now have genuine weighted marks. Cleaned up schema SQL.
master
1 parent
824ec81
commit
59b522c052505f057c4c02cc849312d2eebbc663
Mark
authored
on 26 Aug 2014
Patch
Showing
2 changed files
src/dao/ResultDAO.java
src/schema.sql
Ignore Space
Show notes
View
src/dao/ResultDAO.java
package dao; import com.sun.rowset.CachedRowSetImpl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.rowset.CachedRowSet; import javax.swing.table.TableModel; import model.Criterion; import model.CriterionEditor; 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 (student, 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 student = ?")) { 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"); CachedRowSet crs = new CachedRowSetImpl(); crs.populate(rs); return new RowSetTableModel(crs); } catch(SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } return null; } private void clear() { criteria = new HashMap<>(); } }
package dao; import com.sun.rowset.CachedRowSetImpl; import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import java.util.logging.Level; import java.util.logging.Logger; import javax.sql.rowset.CachedRowSet; import javax.swing.table.TableModel; import model.Criterion; import model.CriterionEditor; 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 (student, 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 student = ?")) { 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(); PreparedStatement stmt = con.prepareStatement("select * from rawmarks")) { ResultSet rs = stmt.executeQuery(); CachedRowSet crs = new CachedRowSetImpl(); crs.populate(rs); return new RowSetTableModel(crs); } catch(SQLException ex) { logger.log(Level.SEVERE, ex.getLocalizedMessage(), ex); } return null; } private void clear() { criteria = new HashMap<>(); } }
Ignore Space
Show notes
View
src/schema.sql
CREATE TABLE ASSESSMENT (NAME VARCHAR PRIMARY KEY, BONUS_WEIGHT DOUBLE NOT NULL, OUTOF 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 (STUDENT VARCHAR PRIMARY KEY); CREATE TABLE RESULT (STUDENT VARCHAR NOT NULL, CRITERION VARCHAR NOT NULL, RESULT REAL NOT NULL, COMMENT VARCHAR, CONSTRAINT PK_RESULT PRIMARY KEY(STUDENT, CRITERION), CONSTRAINT FK_RESULT_STUDENT FOREIGN KEY(STUDENT) REFERENCES SUBMISSION(STUDENT), CONSTRAINT FK_RESULT_CRITERION FOREIGN KEY(CRITERION) REFERENCES CRITERION(CRITERION)); CREATE VIEW WEIGHTED_MARK AS SELECT STUDENT, SUM(WEIGHTED) * (SELECT OUTOF FROM ASSESSMENT) AS MARK FROM (SELECT STUDENT, RESULT, WEIGHT, MAX, (RESULT / MAX * (WEIGHT / (SELECT SUM(WEIGHT) FROM CRITERION))) AS WEIGHTED FROM RESULT INNER JOIN CRITERION ON RESULT.CRITERION = CRITERION.CRITERION) GROUP BY STUDENT; CREATE VIEW WEIGHTED_SPECIAL AS SELECT STUDENT, COALESCE(SUM(RESULT) * (SELECT BONUS_WEIGHT FROM ASSESSMENT), 0) AS SPECIAL FROM RESULT INNER JOIN CRITERION ON CRITERION.CRITERION = RESULT.CRITERION WHERE SPECIAL CREATE VIEW MARK AS SELECT WEIGHTED_MARK.STUDENT, CASEWHEN(MARK + SPECIAL <= (SELECT OUTOF FROM ASSESSMENT), MARK + SPECIAL, (SELECT OUTOF FROM ASSESSMENT)) AS MARK FROM WEIGHTED_MARK INNER JOIN WEIGHTED_SPECIAL ON WEIGHTED_MARK.STUDENT = WEIGHTED_SPECIAL.STUDENT 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 FEEDBACK AS SELECT STUDENT, CATEGORY.CATEGORY, COMMENT FROM CRITERION INNER JOIN RESULT ON RESULT.CRITERION = CRITERION.CRITERION INNER JOIN CATEGORY ON CRITERION.CATEGORY = CATEGORY.CATEGORY WHERE COMMENT <> '' ORDER BY CATEGORY.SORT_ORDER,CRITERION.SORT_ORDER; CREATE VIEW ALLMARKS("Student", "Weighted Mark") AS SELECT SUBMISSION.STUDENT, COALESCE(CAST(ROUND(MARK, 2) AS VARCHAR), '') FROM SUBMISSION LEFT JOIN MARK ON SUBMISSION.STUDENT = MARK.STUDENT
CREATE TABLE PUBLIC.CATEGORY( CATEGORY VARCHAR PRIMARY KEY, SORT_ORDER SMALLINT ); CREATE TABLE PUBLIC.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 PUBLIC.SUBMISSION( STUDENT VARCHAR PRIMARY KEY ); CREATE TABLE PUBLIC.RESULT( STUDENT VARCHAR NOT NULL, CRITERION VARCHAR NOT NULL, RESULT REAL NOT NULL, COMMENT VARCHAR, CONSTRAINT PK_RESULT PRIMARY KEY(STUDENT, CRITERION), CONSTRAINT FK_RESULT_STUDENT FOREIGN KEY(STUDENT) REFERENCES PUBLIC.SUBMISSION(STUDENT), CONSTRAINT FK_RESULT_CRITERION FOREIGN KEY(CRITERION) REFERENCES PUBLIC.CRITERION(CRITERION) ); CREATE VIEW RAWMARKS AS SELECT SUBMISSION.STUDENT as "Student" , SUM(RESULT) as "Raw Mark" FROM SUBMISSION LEFT OUTER JOIN RESULT ON SUBMISSION.STUDENT = RESULT.STUDENT GROUP BY SUBMISSION.STUDENT ORDER BY SUBMISSION.STUDENT
Show line notes below