/filesearch/SearchLocalFile/src/com/searchlocal/dao/HtmlDao.java

http://filesearch.googlecode.com/ · Java · 175 lines · 107 code · 11 blank · 57 comment · 4 complexity · 524261ec3ec34104841c98b7e8b270c1 MD5 · raw file

  1. /**
  2. * $RCSfile: HtmlDao.java
  3. * $Revision: 1.0
  4. * $Date: Jan 30, 2011
  5. *
  6. * Copyright (C) 2010 SlFile, Inc. All rights reserved.
  7. *
  8. * This software is the proprietary information of SlFile, Inc.
  9. * Use is subject to license terms.
  10. */
  11. package com.searchlocal.dao;
  12. import java.sql.Connection;
  13. import java.sql.PreparedStatement;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import java.sql.Timestamp;
  17. import java.util.HashMap;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Map;
  21. import com.searchlocal.bean.HtmlFileBean;
  22. import com.searchlocal.exception.DBException;
  23. import com.searchlocal.exception.LogicException;
  24. import com.searchlocal.util.CLogger;
  25. import com.searchlocal.util.SQLParameterUtil;
  26. import com.searchlocal.util.SqlUtil;
  27. import com.searchlocal.util.StringUtils;
  28. /**
  29. * Html??Dao
  30. *
  31. * <p>Title: Html??Dao</p>
  32. * <p>Description: </p>
  33. * <p>site: www.slfile.net</p>
  34. * @author changsong:qianjinfu@gmail.com
  35. * @version 1.0
  36. */
  37. public class HtmlDao extends BaseDao {
  38. /** ?? */
  39. private static CLogger logger = new CLogger(HtmlDao.class);
  40. /**
  41. * ???
  42. */
  43. public HtmlDao() {
  44. }
  45. /**
  46. * ??Html?????
  47. *
  48. * @param conn ?????
  49. * @param sql db??
  50. * @param elementList ??????
  51. * @throws DBException
  52. */
  53. public static void executeHtmlUpdateSQL(Connection conn, String sql, List<HtmlFileBean> elementList)
  54. throws DBException {
  55. HtmlFileBean element = null;
  56. PreparedStatement stmt;
  57. try {
  58. stmt = conn.prepareStatement(sql);
  59. for (Iterator<HtmlFileBean> iter = elementList.iterator(); iter.hasNext();) {
  60. element = (HtmlFileBean) iter.next();
  61. stmt.setString(1, element.getFilename());
  62. stmt.setString(2, element.getPath());
  63. stmt.setTimestamp(3, new Timestamp(element.getLastmodify()));
  64. stmt.setString(4, element.getContent());
  65. if (stmt != null) {
  66. stmt.executeUpdate();
  67. }
  68. }
  69. } catch (SQLException e) {
  70. String param = SQLParameterUtil.getBeanToString(element);
  71. logger.error("DB_E013", sql, param, e);
  72. throw new DBException("DB_E013", e);
  73. }
  74. closeConnection(null, stmt, null);
  75. }
  76. /**
  77. * ??Html?
  78. *
  79. * @param namesapce ?????
  80. * @throws DBException
  81. */
  82. public boolean createHtmltable(String namesapce) throws LogicException, DBException {
  83. Connection conn = BaseDao.getConn(namesapce);
  84. openTransaction(conn);
  85. boolean success = false;
  86. // ??SQL
  87. String presql = SqlUtil.getSqlbyId("createHtmlRecord");
  88. Map<String, String> paramMap = new HashMap<String, String>();
  89. paramMap.put("namespace", namesapce);
  90. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  91. try {
  92. success = execute(sql, conn);
  93. } catch (DBException e) {
  94. logger.error("DB_E014", sql, e);
  95. throw new DBException("DB_E014", e);
  96. }
  97. commit(conn);
  98. closeConnection(null, null, conn);
  99. return success;
  100. }
  101. /**
  102. * ??Html??
  103. *
  104. * @param beanList Html????
  105. * @param filepath ????
  106. * @param lastmodify ??????
  107. * @param filename ????
  108. * @param namespace ???
  109. * @throws DBException
  110. * @throws LogicException
  111. */
  112. public boolean insertHtmlRecord(List<HtmlFileBean> beanList, String filepath, long lastmodify,
  113. String filename, String namesapce) throws LogicException, DBException {
  114. Connection conn = BaseDao.getBaseConn(namesapce);
  115. HtmlFileBean element = null;
  116. String presql = SqlUtil.getSqlbyId("insertHtmlRecord");
  117. Map<String, String> paramMap = new HashMap<String, String>();
  118. paramMap.put("namespace", namesapce);
  119. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  120. try {
  121. conn.setReadOnly(false);
  122. openTransaction(conn);
  123. for (Iterator<HtmlFileBean> iter = beanList.iterator(); iter.hasNext();) {
  124. element = (HtmlFileBean) iter.next();
  125. element.setFilename(filename);
  126. element.setLastmodify(lastmodify);
  127. element.setPath(filepath);
  128. }
  129. executeHtmlUpdateSQL(conn, sql, beanList);
  130. } catch (Exception e) {
  131. String param = SQLParameterUtil.getBeanToString(element);
  132. logger.error("DB_E015", sql, param, e);
  133. throw new DBException("DB_E015", e);
  134. }
  135. commit(conn);
  136. closeConnection(null, null, conn);
  137. return true;
  138. }
  139. /**
  140. * ??Batch??
  141. *
  142. * @param datapath csv????
  143. * @param namespace ????
  144. * @throws DBException
  145. * @throws LogicException
  146. */
  147. public boolean execbatch(String datapath, String namesapce) throws DBException, LogicException {
  148. Connection conn = BaseDao.getBaseConn(namesapce);
  149. openTransaction(conn);
  150. Statement st = null;
  151. try {
  152. st = conn.createStatement();
  153. String sql = "Load Data InFile " + datapath
  154. + " Into Table t_html FIELDS TERMINATED BY ','";
  155. sql = StringUtils.editSQL(sql);
  156. st.execute(sql);
  157. } catch (SQLException e) {
  158. throw new DBException("DB_E027", e);
  159. }
  160. commit(conn);
  161. closeConnection(null, st, conn);
  162. return true;
  163. }
  164. }