1 package net.javacoding.jspider.core.storage.jdbc; 2 3 import net.javacoding.jspider.core.storage.DecisionDAO; 4 import net.javacoding.jspider.core.storage.spi.DecisionDAOSPI; 5 import net.javacoding.jspider.core.storage.spi.StorageSPI; 6 import net.javacoding.jspider.core.logging.LogFactory; 7 import net.javacoding.jspider.core.logging.Log; 8 import net.javacoding.jspider.core.model.*; 9 import net.javacoding.jspider.api.model.*; 10 11 import java.sql.*; 12 13 16 class DecisionDAOImpl implements DecisionDAOSPI { 17 18 public static final int SUBJECT_SPIDER = 1; 19 public static final int SUBJECT_PARSE = 2; 20 21 public static final String ATTRIBUTE_ID = "id"; 22 public static final String ATTRIBUTE_SUBJECT = "subject"; 23 public static final String ATTRIBUTE_TYPE = "type"; 24 public static final String ATTRIBUTE_COMMENT = "comment"; 25 public static final String ATTRIBUTE_DECISION = "decision"; 26 public static final String ATTRIBUTE_RULE = "rule"; 27 28 protected Log log; 29 30 protected DBUtil dbUtil; 31 protected StorageSPI storage; 32 33 public DecisionDAOImpl ( StorageSPI storage, DBUtil dbUtil ) { 34 this.log = LogFactory.getLog(DecisionDAO.class); 35 this.dbUtil = dbUtil; 36 this.storage = storage; 37 } 38 39 public void saveSpiderDecision(ResourceInternal resource, DecisionInternal decision) { 40 saveDecision(SUBJECT_SPIDER,resource, decision); 41 } 42 43 public void saveParseDecision(ResourceInternal resource, DecisionInternal decision) { 44 saveDecision(SUBJECT_PARSE,resource, decision); 45 } 46 47 protected void saveDecision ( int subject, ResourceInternal resource, DecisionInternal decision ) { 48 PreparedStatement ps = null; 49 PreparedStatement ps2 = null; 50 try { 51 Connection connection = dbUtil.getConnection(); 52 ps = connection.prepareStatement("insert into jspider_decision ( resource, subject, type, comment ) values (?,?,?,?)"); 53 ps.setInt(1, resource.getId()); 54 ps.setInt(2, (subject)); 55 ps.setInt(3, (decision.getDecision())); 56 ps.setString(4, (decision.getComment())); 57 ps.executeUpdate(); 58 59 DecisionStep[] steps = decision.getSteps(); 60 for (int i = 0; i < steps.length; i++) { 61 DecisionStep step = steps[i]; 62 ps2 = connection.prepareStatement("insert into jspider_decision_step ( resource, subject, sequence, type, rule, decision, comment ) values (?,?,?,?,?,?,?)"); 63 ps2.setInt(1, resource.getId()); 64 ps2.setInt(2, subject); 65 ps2.setInt(3, i); 66 ps2.setInt(4, (step.getRuleType())) ; 67 ps2.setString(5, (step.getRule())); 68 ps2.setInt(6, (step.getDecision())); 69 ps2.setString(7, (step.getComment())); 70 ps2.executeUpdate(); 71 } 72 } catch (SQLException e) { 73 log.error("SQLException", e); 74 } finally{ 75 dbUtil.safeClose(ps, log); 76 dbUtil.safeClose(ps2, log); 77 } 78 } 79 80 public DecisionInternal findSpiderDecision(ResourceInternal resource) { 81 return findDecision ( SUBJECT_SPIDER, resource); 82 } 83 84 public DecisionInternal findParseDecision(ResourceInternal resource) { 85 return findDecision ( SUBJECT_PARSE, resource); 86 } 87 88 protected DecisionInternal findDecision ( int subject, ResourceInternal resource ) { 89 DecisionInternal decision = null; 90 PreparedStatement ps = null; 91 PreparedStatement ps2 = null; 92 ResultSet rs = null; 93 ResultSet rs2 = null; 94 try { 95 Connection connection = dbUtil.getConnection(); 96 ps = connection.prepareStatement("select * from jspider_decision where resource=? and subject=?"); 97 ps.setInt(1, resource.getId()); 98 ps.setInt(2, subject); 99 rs = ps.executeQuery(); 100 if ( rs.next() ) { 101 int type = rs.getInt(ATTRIBUTE_TYPE); 102 String comment = rs.getString(ATTRIBUTE_COMMENT); 103 decision = new DecisionInternal ( type, comment ); 104 105 ps2 = connection.prepareStatement("select * from jspider_decision_step where resource=? and subject=? order by sequence"); 106 ps2.setInt(1, resource.getId()); 107 ps2.setInt(2, subject); 108 rs2 = ps2.executeQuery(); 109 while ( rs2.next ( ) ) { 110 String rule = rs2.getString(ATTRIBUTE_RULE); 111 int stepType = rs2.getInt(ATTRIBUTE_TYPE); 112 int stepDecision = rs2.getInt(ATTRIBUTE_DECISION); 113 String stepComment = rs2.getString(ATTRIBUTE_COMMENT); 114 decision.addStep(rule,stepType, stepDecision, stepComment); 115 } 116 } 117 } catch (SQLException e) { 118 log.error("SQLException", e); 119 } finally{ 120 dbUtil.safeClose(rs, log); 121 dbUtil.safeClose(ps, log); 122 dbUtil.safeClose(rs2, log); 123 dbUtil.safeClose(ps2, log); 124 } 125 return decision; 126 } 127 128 } 129 | Popular Tags |