DecisionDAOImpl.java
上传用户:qing5858
上传日期:2015-10-27
资源大小:6056k
文件大小:5k
源码类别:

搜索引擎

开发平台:

Java

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