📄 dbloadvisitor.java
字号:
/* * DBLoadVisitor.java * * Created on 2003年11月6日, 下午12:23 */package romulus;import java.sql.*;/** * The Visitor is used to load the full structure of the Romulus Node. * @author Romulus * @version 1.0 */public class DBLoadVisitor implements Visitor { private Connection con = null; /** Creates new DBLoadVisitor */ public DBLoadVisitor(Connection con) { this.con = con; } /** Visit the Content. */ public void VisitContent(Content cont) throws SQLException, RomulusException{ //prepare the basic structure int c_type = 0; //prepare the select sql int c_s_ident = RomulusToolSet.getSIdent(con, "Content", cont.getIdent()); int c_i_s_ident = 0; String type_sql = "select con_i_s_ident, con_type from contentlist where con_s_ident =?"; String textcon_sql = "select ident, text from textcontent where s_ident =?"; //content item type PreparedStatement pre = con.prepareStatement(type_sql); pre.setInt(1, c_s_ident); ResultSet res = pre.executeQuery(); while(res.next()){ c_i_s_ident = res.getInt(1); c_type = res.getInt(2); if(c_type == ContentItem.Int_Type_TextContent || c_type == ContentItem.Int_Type_EmTextContent){ //text content only PreparedStatement pre_text = con.prepareStatement(textcon_sql); pre_text.setInt(1, c_i_s_ident); ResultSet res_text = pre_text.executeQuery(); if(res_text.next()){ TextContent textcon = new TextContent(res_text.getString(1), res_text.getString(2), c_type == ContentItem.Int_Type_EmTextContent?true:false); textcon.Accept(this); cont.Add(textcon); } } else{ throw new RomulusException(RomulusException.TypeError); } } res.close(); pre.close(); } /** Visit the Feedback. */ public void VisitFeedback(Feedback fb) throws SQLException, RomulusException{ //prepare the basic structure Content cont = null; //prepare the select sql int f_s_ident = RomulusToolSet.getSIdent(con, "Feedback", fb.getIdent()); int con_s_ident = 0; String con_sql = "select ident, label from content where s_ident in " +"(select con_s_ident from feedback where s_ident = ?)"; //get content PreparedStatement pre = con.prepareStatement(con_sql); pre.setInt(1, f_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ cont = new Content(res.getString(1), res.getString(2)); cont.Accept(this); fb.setContent(cont); } res.close(); pre.close(); } /** Visit the Objective. */ public void VisitObjective(Objective obj) throws SQLException, RomulusException{ //prepare the basic structure Content cont = null; //prepare the select sql int o_s_ident = RomulusToolSet.getSIdent(con, "Objective", obj.getIdent()); int con_s_ident = 0; String con_sql = "select ident, label from content where s_ident in " +"(select con_s_ident from objective where s_ident = ?)"; //get content PreparedStatement pre = con.prepareStatement(con_sql); pre.setInt(1, o_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ cont = new Content(res.getString(1), res.getString(2)); cont.Accept(this); obj.setContent(cont); } res.close(); pre.close(); } /** Visit the Question. */ public void VisitQuestion(Question que) throws SQLException, RomulusException{ //prepare the basic structure Objective obj = null; Feedback fb = null; //prepare the select sql int q_s_ident = RomulusToolSet.getSIdent(con, "Question", que.getIdent()); int qitem_s_ident = 0; int q_type = QuestionItem.CHOICE_TYPE; String obj_sql = "select ident, can_view from objective where s_ident in " +"(select o_s_ident from objectlist where q_s_ident =?)"; String fb_sql = "select ident, can_view, title from feedback where s_ident in " +"(select f_s_ident from feedbacklist where q_s_ident =?)"; //objective PreparedStatement pre = con.prepareStatement(obj_sql); pre.setInt(1, q_s_ident); ResultSet res = pre.executeQuery(); while(res.next()){ obj = new Objective(res.getString(1), res.getInt(2)); obj.Accept(this); que.addObjective(obj); } res.close(); pre.close(); //feedback pre =con.prepareStatement(fb_sql); pre.setInt(1, q_s_ident); res = pre.executeQuery(); while(res.next()){ fb = new Feedback(res.getString(1), res.getInt(2), res.getString(3)); fb.Accept(this); que.addFeedback(fb); } res.close(); pre.close(); //get item type String type_sql = "select i_s_ident, type from itemlist where q_s_ident =?"; pre = con.prepareStatement(type_sql); pre.setInt(1, q_s_ident); res = pre.executeQuery(); if(res.next()){ qitem_s_ident = res.getInt(1); q_type = res.getInt(2); if(q_type == QuestionItem.FIB_TYPE){ //fib String fib_sql = "select ident, maxchars, correctanswer from fib where s_ident = ? "; PreparedStatement pre_qitem = con.prepareStatement(fib_sql); pre_qitem.setInt(1, qitem_s_ident); ResultSet res_qitem = pre_qitem.executeQuery(); if(res_qitem.next()){ FIB fib = new FIB(res_qitem.getString(1), res_qitem.getInt(2), res_qitem.getString(3)); fib.Accept(this); que.setQuestionItem(fib); } } else if(q_type == QuestionItem.CHOICE_TYPE){ //choice String choice_sql = "select ident, rcardinality, shuffle from choice where s_ident = ? "; PreparedStatement pre_qitem = con.prepareStatement(choice_sql); pre_qitem.setInt(1, qitem_s_ident); ResultSet res_qitem = pre_qitem.executeQuery(); if(res_qitem.next()){ Choice ch = new Choice(res_qitem.getString(1), res_qitem.getInt(2), (res_qitem.getInt(3) == 0)?false:true); ch.Accept(this); que.setQuestionItem(ch); } } else{ throw new RomulusException(RomulusException.TypeError); } } res.close(); pre.close(); } /** Visit the QuestionItem. */ public void VisitQuestionItem(QuestionItem qitem) throws SQLException, RomulusException{ if(qitem instanceof FIB){ int fib_s_ident = RomulusToolSet.getSIdent(con, "FIB", ((FIB)qitem).getIdent()); String con_sql = "select ident, label from content where s_ident in " +"(select con_s_ident from FIB where s_ident = ?)"; //get content PreparedStatement pre = con.prepareStatement(con_sql); pre.setInt(1, fib_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ Content cont = new Content(res.getString(1), res.getString(2)); cont.Accept(this); ((FIB)qitem).setContent(cont); } res.close(); pre.close(); } else if(qitem instanceof Choice){ int choice_s_ident = RomulusToolSet.getSIdent(con, "choice", ((Choice)qitem).getIdent()); String con_sql = "select ident, label from content where s_ident in " +"(select con_s_ident from choice where s_ident = ?)"; //get content PreparedStatement pre = con.prepareStatement(con_sql); pre.setInt(1, choice_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ Content cont = new Content(res.getString(1), res.getString(2)); cont.Accept(this); ((Choice)qitem).setContent(cont); } //get choiceitem String citem_sql = "select ident, label, iscorrect from choiceitem where s_ident in " +"(select ci_s_ident from choiceitemlist where c_s_ident = ?)"; pre = con.prepareStatement(citem_sql); pre.setInt(1, choice_s_ident); res = pre.executeQuery(); ChoiceItem citem = null; while(res.next()){ citem = new ChoiceItem(res.getString(1), res.getString(2), res.getInt(3)==1?true:false); citem.Accept(this); ((Choice)qitem).addChoiceItem(citem); } res.close(); pre.close(); } else{ throw new RomulusException(RomulusException.TypeError); } } /** Visit Test. */ public void VisitTest(Test test) throws SQLException, RomulusException{ //prepare the select param int t_s_ident = RomulusToolSet.getSIdent(con, "Test", test.getIdent()); String sql = "select ident, title, timing from question where s_ident in " +"(select q_s_ident from questionlist where t_s_ident =?)"; //search the question and add to test PreparedStatement pre = con.prepareStatement(sql); pre.setInt(1, t_s_ident); ResultSet res = pre.executeQuery(); Question que = null; while(res.next()){ que = new Question(res.getString(1), res.getInt(3), res.getString(2)); que.Accept(this); test.add(que); } res.close(); pre.close(); } /** Visit the ChoiceItem. */ public void VisitChoiceItem(ChoiceItem citem) throws SQLException, RomulusException { int ci_s_ident = RomulusToolSet.getSIdent(con, "choiceitem", citem.getIdent()); String con_sql = "select ident, label from content where s_ident in " +"(select con_s_ident from choiceitem where s_ident = ?)"; //get content PreparedStatement pre = con.prepareStatement(con_sql); pre.setInt(1, ci_s_ident); ResultSet res = pre.executeQuery(); if(res.next()){ Content cont = new Content(res.getString(1), res.getString(2)); cont.Accept(this); citem.setContent(cont); } res.close(); pre.close(); } /**Visit the ContentItem.*/ public void VisitContentItem(ContentItem citem) throws SQLException, RomulusException{ //leaf node, no more content }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -