/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
- /**
- * $RCSfile: FileDao.java
- * $Revision: 1.0
- * $Date: Jan 30, 2011
- *
- * Copyright (C) 2010 SlFile, Inc. All rights reserved.
- *
- * This software is the proprietary information of SlFile, Inc.
- * Use is subject to license terms.
- */
- package com.searchlocal.dao;
-
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Map;
-
- import com.searchlocal.exception.DBException;
- import com.searchlocal.exception.LogicException;
- import com.searchlocal.param.FileParam;
- import com.searchlocal.util.CLogger;
- import com.searchlocal.util.SQLParameterUtil;
- import com.searchlocal.util.SqlUtil;
- import com.searchlocal.util.StringUtils;
-
- /**
- * ????Dao
- *
- * <p>Title: ????Dao</p>
- * <p>Description: </p>
- * <p>site: www.slfile.net</p>
- * @author changsong:qianjinfu@gmail.com
- * @version 1.0
- */
- public class FileDao extends BaseDao {
-
- /** ?? */
- private static CLogger logger = new CLogger(FileDao.class);
-
- /**
- * ???
- */
- public FileDao() {
- }
-
- /**
- * ?????????
- *
- * @param conn db??
- * @param sql db??
- * @param elementList ??????
- * @throws DBException
- */
- public static void executeFileUpdateSQL(Connection conn, String sql,
- List<FileParam> elementList) throws DBException {
- FileParam element = null;
- PreparedStatement stmt;
- try {
- stmt = conn.prepareStatement(sql);
- conn.setReadOnly(false);
- for (Iterator<FileParam> iter = elementList.iterator(); iter.hasNext();) {
- element = (FileParam) iter.next();
- if (null != element) {
- stmt.setString(1, element.getPath());
- stmt.setTimestamp(2, new Timestamp(element.getLastModify()));
- stmt.setString(3, element.getError());
- }
- if (stmt != null) {
- stmt.executeUpdate();
- }
- }
- } catch (SQLException e) {
- // String param = SQLParameterUtil.getBeanToString(element);
- // logger.error("DB_E022", sql, param, e);
- throw new DBException("DB_E022", e);
- }
- closeConnection(null, stmt, null);
- }
-
- /**
- * ??File??
- *
- * @param namespace ???
- * @param path ????
- * @throws DBException
- * @throws LogicException
- */
- public boolean deleteFileRecord(String namespace, String path) throws DBException,
- LogicException {
- Connection conn = BaseDao.getConn(namespace);
- openTransaction(conn);
- boolean success = false;
- // ??SQL
- String sql = SqlUtil.getsql(namespace, "deleteFileRecord");
- logger.debug("sql:" + sql);
- try {
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setString(1, path);
- success = stmt.execute();
- } catch (Exception e) {
- logger.error("DB_E023", sql, e);
- throw new DBException("DB_E023", e);
- }
- commit(conn);
- closeConnection(null, null, conn);
- return success;
- }
-
- /**
- * ??File??
- *
- * @param namespace ???
- * @param paths ??????
- * @throws DBException
- * @throws LogicException
- */
- public boolean deleteFileRecords(String namespace, List<String> paths) throws DBException,
- LogicException {
- Connection conn = BaseDao.getConn(namespace);
- openTransaction(conn);
- boolean success = false;
- // ??SQL
- String sql = SqlUtil.getsql(namespace, "deleteFileRecord");
- logger.debug("sql:" + sql);
- String path;
- try {
- for (Iterator<String> iter = paths.iterator(); iter.hasNext();) {
- path = (String) iter.next();
- if (null != path) {
- PreparedStatement stmt = conn.prepareStatement(sql);
- stmt.setString(1, path);
- success = stmt.execute();
- }
- }
- } catch (Exception e) {
- logger.error("DB_E023", sql, e);
- throw new DBException("DB_E023", e);
- }
- commit(conn);
- closeConnection(null, null, conn);
- return success;
- }
-
-
- /**
- * ???????????????????
- *
- * @param namespace ???
- * @param table ?
- * @param path ??
- * @throws DBException
- * @throws LogicException
- */
- public boolean deleteRecordByPath(String namespace, String table, String path)
- throws DBException, LogicException {
- Connection conn = BaseDao.getConn(namespace);
- openTransaction(conn);
- boolean success = false;
- // ??SQL
- String presql = SqlUtil.getSqlbyId("deleteRecordByPath");
- Map<String, String> paramMap = new HashMap<String, String>();
- paramMap.put("namespace", namespace);
- paramMap.put("table", table);
-
- String sql = SQLParameterUtil.convertSQL(presql, paramMap);
- logger.debug("sql:" + sql);
-
- PreparedStatement stmt;
- try {
- conn.setReadOnly(false);
- stmt = conn.prepareStatement(sql);
- stmt.setString(1, path);
- success = stmt.execute();
- } catch (SQLException e) {
- logger.error("DB_E023", sql, e);
- throw new DBException("DB_E023", e);
- }
- commit(conn);
- closeConnection(null, null, conn);
- return success;
- }
-
- /**
- * ??File?
- *
- * @param namespace ???
- * @throws DBException
- * @throws LogicException
- */
- public boolean createFiletable(String namesapce) throws DBException,
- LogicException {
- Connection conn = BaseDao.getConn(namesapce);
- openTransaction(conn);
- boolean success = false;
- // ??SQL
- String presql = SqlUtil.getSqlbyId("createFileRecord");
- Map<String, String> paramMap = new HashMap<String, String>();
- paramMap.put("namespace", namesapce);
- String sql = SQLParameterUtil.convertSQL(presql, paramMap);
- try {
- success = execute(sql, conn);
- } catch (DBException e) {
- logger.error("DB_E023", sql, e);
- throw new DBException("DB_E023", e);
- }
- commit(conn);
- closeConnection(null, null, conn);
- return success;
- }
-
- /**
- * ??File??
- *
- * @param namespace ???
- * @throws DBException
- * @throws LogicException
- */
- public List<FileParam> getFileRecord(String namespace) throws LogicException,
- DBException {
- Connection conn = BaseDao.getConn(namespace);
- // SQL??
- String presql = SqlUtil.getSqlbyId("selectFileRecord");
- Map<String, String> paramMap = new HashMap<String, String>();
- paramMap.put("namespace", namespace);
-
- List<FileParam> returnList = new ArrayList<FileParam>();
- String sql = SQLParameterUtil.convertSQL(presql, paramMap);
- try {
- setReadOnly(conn);
- Statement stmt = conn.createStatement();
- ResultSet rs = stmt.executeQuery(sql);
-
- while (rs.next()) {
- FileParam param = new FileParam();
- param.setPath(rs.getString("path"));
- param.setLastModify(rs.getTimestamp("lastmodify").getTime());
- param.setError(rs.getString("error"));
- returnList.add(param);
- }
- } catch (SQLException e) {
- throw new DBException("DB_E024", e);
- }
- closeConnection(null, null, conn);
- return returnList;
- }
-
- /**
- * ??File??
- *
- * @param filebeanList ??????
- * @param namespace ????
- * @throws DBException
- * @throws LogicException
- */
- public boolean insertFileRecord(List<FileParam> beanList, String namespace)
- throws LogicException, DBException {
- Connection conn = BaseDao.getConn(namespace);
- openTransaction(conn);
-
- // SQL??
- String presql = SqlUtil.getSqlbyId("insertFileRecord");
- Map<String, String> paramMap = new HashMap<String, String>();
- paramMap.put("namespace", namespace);
-
- String sql = SQLParameterUtil.convertSQL(presql, paramMap);
- try {
- conn.setReadOnly(false);
- executeFileUpdateSQL(conn, sql, beanList);
- } catch (DBException e) {
- // String param = SQLParameterUtil.getBeanToString(element);
- // logger.error("DB_E022", sql, param, e);
- throw new DBException("DB_E024", e);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- commit(conn);
- closeConnection(null, null, conn);
- return true;
- }
-
- /**
- * ??File??
- *
- * @param filebeanList ????
- * @param namespace ????
- * @throws DBException
- * @throws LogicException
- */
- public boolean updateFileRecord(List<FileParam> beanList, String namespace)
- throws LogicException, DBException {
-
- Connection conn = BaseDao.getConn(namespace);
- openTransaction(conn);
- // SQL??
- String presql = SqlUtil.getSqlbyId("updateFileRecord");
- PreparedStatement stmt;
- FileParam element;
- try {
- for (Iterator<FileParam> iter = beanList.iterator(); iter.hasNext();) {
- element = (FileParam) iter.next();
- Map<String, String> paramMap = new HashMap<String, String>();
- paramMap.put("namespace", namespace);
- String sql = SQLParameterUtil.convertSQL(presql, paramMap);
- stmt = conn.prepareStatement(sql);
-
- if (null != element) {
- stmt.setTimestamp(1, new Timestamp(element.getLastModify()));
- stmt.setString(2, element.getError());
- stmt.setString(3, element.getPath());
- }
- if (stmt != null) {
- stmt.executeUpdate();
- }
- }
- } catch (SQLException e) {
- // String param = SQLParameterUtil.getBeanToString(element);
- // logger.error("DB_E022", sql, param, e);
- throw new DBException("DB_E024", e);
- }
- commit(conn);
- closeConnection(null, null, conn);
- return true;
- }
-
- /**
- * ??Batch??
- *
- * @param datapath csv????
- * @param namespace ????
- * @throws DBException
- * @throws LogicException
- */
- public boolean execbatch(String datapath, String namesapce) throws DBException, LogicException {
- Connection conn = BaseDao.getConn(namesapce);
- openTransaction(conn);
- Statement st = null;
- try {
- st = conn.createStatement();
- String sql = "Load Data InFile " + datapath
- + " Into Table t_file FIELDS TERMINATED BY ','";
- sql = StringUtils.editSQL(sql);
- st.execute(sql);
- } catch (SQLException e) {
- throw new DBException("DB_E027", e);
- }
- commit(conn);
- closeConnection(null, st, conn);
- return true;
- }
- }