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

http://filesearch.googlecode.com/ · Java · 358 lines · 227 code · 23 blank · 108 comment · 14 complexity · be2ef778ec6b1aa1f82880432888f382 MD5 · raw file

  1. /**
  2. * $RCSfile: FileDao.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.ResultSet;
  15. import java.sql.SQLException;
  16. import java.sql.Statement;
  17. import java.sql.Timestamp;
  18. import java.util.ArrayList;
  19. import java.util.HashMap;
  20. import java.util.Iterator;
  21. import java.util.List;
  22. import java.util.Map;
  23. import com.searchlocal.exception.DBException;
  24. import com.searchlocal.exception.LogicException;
  25. import com.searchlocal.param.FileParam;
  26. import com.searchlocal.util.CLogger;
  27. import com.searchlocal.util.SQLParameterUtil;
  28. import com.searchlocal.util.SqlUtil;
  29. import com.searchlocal.util.StringUtils;
  30. /**
  31. * ????Dao
  32. *
  33. * <p>Title: ????Dao</p>
  34. * <p>Description: </p>
  35. * <p>site: www.slfile.net</p>
  36. * @author changsong:qianjinfu@gmail.com
  37. * @version 1.0
  38. */
  39. public class FileDao extends BaseDao {
  40. /** ?? */
  41. private static CLogger logger = new CLogger(FileDao.class);
  42. /**
  43. * ???
  44. */
  45. public FileDao() {
  46. }
  47. /**
  48. * ?????????
  49. *
  50. * @param conn db??
  51. * @param sql db??
  52. * @param elementList ??????
  53. * @throws DBException
  54. */
  55. public static void executeFileUpdateSQL(Connection conn, String sql,
  56. List<FileParam> elementList) throws DBException {
  57. FileParam element = null;
  58. PreparedStatement stmt;
  59. try {
  60. stmt = conn.prepareStatement(sql);
  61. conn.setReadOnly(false);
  62. for (Iterator<FileParam> iter = elementList.iterator(); iter.hasNext();) {
  63. element = (FileParam) iter.next();
  64. if (null != element) {
  65. stmt.setString(1, element.getPath());
  66. stmt.setTimestamp(2, new Timestamp(element.getLastModify()));
  67. stmt.setString(3, element.getError());
  68. }
  69. if (stmt != null) {
  70. stmt.executeUpdate();
  71. }
  72. }
  73. } catch (SQLException e) {
  74. // String param = SQLParameterUtil.getBeanToString(element);
  75. // logger.error("DB_E022", sql, param, e);
  76. throw new DBException("DB_E022", e);
  77. }
  78. closeConnection(null, stmt, null);
  79. }
  80. /**
  81. * ??File??
  82. *
  83. * @param namespace ???
  84. * @param path ????
  85. * @throws DBException
  86. * @throws LogicException
  87. */
  88. public boolean deleteFileRecord(String namespace, String path) throws DBException,
  89. LogicException {
  90. Connection conn = BaseDao.getConn(namespace);
  91. openTransaction(conn);
  92. boolean success = false;
  93. // ??SQL
  94. String sql = SqlUtil.getsql(namespace, "deleteFileRecord");
  95. logger.debug("sql:" + sql);
  96. try {
  97. PreparedStatement stmt = conn.prepareStatement(sql);
  98. stmt.setString(1, path);
  99. success = stmt.execute();
  100. } catch (Exception e) {
  101. logger.error("DB_E023", sql, e);
  102. throw new DBException("DB_E023", e);
  103. }
  104. commit(conn);
  105. closeConnection(null, null, conn);
  106. return success;
  107. }
  108. /**
  109. * ??File??
  110. *
  111. * @param namespace ???
  112. * @param paths ??????
  113. * @throws DBException
  114. * @throws LogicException
  115. */
  116. public boolean deleteFileRecords(String namespace, List<String> paths) throws DBException,
  117. LogicException {
  118. Connection conn = BaseDao.getConn(namespace);
  119. openTransaction(conn);
  120. boolean success = false;
  121. // ??SQL
  122. String sql = SqlUtil.getsql(namespace, "deleteFileRecord");
  123. logger.debug("sql:" + sql);
  124. String path;
  125. try {
  126. for (Iterator<String> iter = paths.iterator(); iter.hasNext();) {
  127. path = (String) iter.next();
  128. if (null != path) {
  129. PreparedStatement stmt = conn.prepareStatement(sql);
  130. stmt.setString(1, path);
  131. success = stmt.execute();
  132. }
  133. }
  134. } catch (Exception e) {
  135. logger.error("DB_E023", sql, e);
  136. throw new DBException("DB_E023", e);
  137. }
  138. commit(conn);
  139. closeConnection(null, null, conn);
  140. return success;
  141. }
  142. /**
  143. * ???????????????????
  144. *
  145. * @param namespace ???
  146. * @param table ?
  147. * @param path ??
  148. * @throws DBException
  149. * @throws LogicException
  150. */
  151. public boolean deleteRecordByPath(String namespace, String table, String path)
  152. throws DBException, LogicException {
  153. Connection conn = BaseDao.getConn(namespace);
  154. openTransaction(conn);
  155. boolean success = false;
  156. // ??SQL
  157. String presql = SqlUtil.getSqlbyId("deleteRecordByPath");
  158. Map<String, String> paramMap = new HashMap<String, String>();
  159. paramMap.put("namespace", namespace);
  160. paramMap.put("table", table);
  161. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  162. logger.debug("sql:" + sql);
  163. PreparedStatement stmt;
  164. try {
  165. conn.setReadOnly(false);
  166. stmt = conn.prepareStatement(sql);
  167. stmt.setString(1, path);
  168. success = stmt.execute();
  169. } catch (SQLException e) {
  170. logger.error("DB_E023", sql, e);
  171. throw new DBException("DB_E023", e);
  172. }
  173. commit(conn);
  174. closeConnection(null, null, conn);
  175. return success;
  176. }
  177. /**
  178. * ??File?
  179. *
  180. * @param namespace ???
  181. * @throws DBException
  182. * @throws LogicException
  183. */
  184. public boolean createFiletable(String namesapce) throws DBException,
  185. LogicException {
  186. Connection conn = BaseDao.getConn(namesapce);
  187. openTransaction(conn);
  188. boolean success = false;
  189. // ??SQL
  190. String presql = SqlUtil.getSqlbyId("createFileRecord");
  191. Map<String, String> paramMap = new HashMap<String, String>();
  192. paramMap.put("namespace", namesapce);
  193. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  194. try {
  195. success = execute(sql, conn);
  196. } catch (DBException e) {
  197. logger.error("DB_E023", sql, e);
  198. throw new DBException("DB_E023", e);
  199. }
  200. commit(conn);
  201. closeConnection(null, null, conn);
  202. return success;
  203. }
  204. /**
  205. * ??File??
  206. *
  207. * @param namespace ???
  208. * @throws DBException
  209. * @throws LogicException
  210. */
  211. public List<FileParam> getFileRecord(String namespace) throws LogicException,
  212. DBException {
  213. Connection conn = BaseDao.getConn(namespace);
  214. // SQL??
  215. String presql = SqlUtil.getSqlbyId("selectFileRecord");
  216. Map<String, String> paramMap = new HashMap<String, String>();
  217. paramMap.put("namespace", namespace);
  218. List<FileParam> returnList = new ArrayList<FileParam>();
  219. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  220. try {
  221. setReadOnly(conn);
  222. Statement stmt = conn.createStatement();
  223. ResultSet rs = stmt.executeQuery(sql);
  224. while (rs.next()) {
  225. FileParam param = new FileParam();
  226. param.setPath(rs.getString("path"));
  227. param.setLastModify(rs.getTimestamp("lastmodify").getTime());
  228. param.setError(rs.getString("error"));
  229. returnList.add(param);
  230. }
  231. } catch (SQLException e) {
  232. throw new DBException("DB_E024", e);
  233. }
  234. closeConnection(null, null, conn);
  235. return returnList;
  236. }
  237. /**
  238. * ??File??
  239. *
  240. * @param filebeanList ??????
  241. * @param namespace ????
  242. * @throws DBException
  243. * @throws LogicException
  244. */
  245. public boolean insertFileRecord(List<FileParam> beanList, String namespace)
  246. throws LogicException, DBException {
  247. Connection conn = BaseDao.getConn(namespace);
  248. openTransaction(conn);
  249. // SQL??
  250. String presql = SqlUtil.getSqlbyId("insertFileRecord");
  251. Map<String, String> paramMap = new HashMap<String, String>();
  252. paramMap.put("namespace", namespace);
  253. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  254. try {
  255. conn.setReadOnly(false);
  256. executeFileUpdateSQL(conn, sql, beanList);
  257. } catch (DBException e) {
  258. // String param = SQLParameterUtil.getBeanToString(element);
  259. // logger.error("DB_E022", sql, param, e);
  260. throw new DBException("DB_E024", e);
  261. } catch (SQLException e) {
  262. // TODO Auto-generated catch block
  263. e.printStackTrace();
  264. }
  265. commit(conn);
  266. closeConnection(null, null, conn);
  267. return true;
  268. }
  269. /**
  270. * ??File??
  271. *
  272. * @param filebeanList ????
  273. * @param namespace ????
  274. * @throws DBException
  275. * @throws LogicException
  276. */
  277. public boolean updateFileRecord(List<FileParam> beanList, String namespace)
  278. throws LogicException, DBException {
  279. Connection conn = BaseDao.getConn(namespace);
  280. openTransaction(conn);
  281. // SQL??
  282. String presql = SqlUtil.getSqlbyId("updateFileRecord");
  283. PreparedStatement stmt;
  284. FileParam element;
  285. try {
  286. for (Iterator<FileParam> iter = beanList.iterator(); iter.hasNext();) {
  287. element = (FileParam) iter.next();
  288. Map<String, String> paramMap = new HashMap<String, String>();
  289. paramMap.put("namespace", namespace);
  290. String sql = SQLParameterUtil.convertSQL(presql, paramMap);
  291. stmt = conn.prepareStatement(sql);
  292. if (null != element) {
  293. stmt.setTimestamp(1, new Timestamp(element.getLastModify()));
  294. stmt.setString(2, element.getError());
  295. stmt.setString(3, element.getPath());
  296. }
  297. if (stmt != null) {
  298. stmt.executeUpdate();
  299. }
  300. }
  301. } catch (SQLException e) {
  302. // String param = SQLParameterUtil.getBeanToString(element);
  303. // logger.error("DB_E022", sql, param, e);
  304. throw new DBException("DB_E024", e);
  305. }
  306. commit(conn);
  307. closeConnection(null, null, conn);
  308. return true;
  309. }
  310. /**
  311. * ??Batch??
  312. *
  313. * @param datapath csv????
  314. * @param namespace ????
  315. * @throws DBException
  316. * @throws LogicException
  317. */
  318. public boolean execbatch(String datapath, String namesapce) throws DBException, LogicException {
  319. Connection conn = BaseDao.getConn(namesapce);
  320. openTransaction(conn);
  321. Statement st = null;
  322. try {
  323. st = conn.createStatement();
  324. String sql = "Load Data InFile " + datapath
  325. + " Into Table t_file FIELDS TERMINATED BY ','";
  326. sql = StringUtils.editSQL(sql);
  327. st.execute(sql);
  328. } catch (SQLException e) {
  329. throw new DBException("DB_E027", e);
  330. }
  331. commit(conn);
  332. closeConnection(null, st, conn);
  333. return true;
  334. }
  335. }